Last active
September 12, 2020 23:02
-
-
Save Naorvi/7cc7f09c0e4d3cad9254abf711918054 to your computer and use it in GitHub Desktop.
Ticket project using Inheritance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*AdvanceTicket.java subclass of Ticket | |
* March 24, 2020 | |
*/ | |
public class AdvanceTicket extends Ticket { | |
//additional property | |
protected int daysInAdvance; | |
//parameter constructor | |
public AdvanceTicket(int num, int days) | |
{ | |
super.number=num; | |
daysInAdvance=days; | |
setPrice(); | |
} | |
//IDE/JAVA added a default constructor automatically | |
public AdvanceTicket() { | |
} | |
//override SetPrice | |
public void setPrice() | |
{ | |
if(daysInAdvance>=10){ | |
super.price=30; | |
} | |
if(daysInAdvance<10 && daysInAdvance>=1){ | |
super.price=40; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*StudentAdvanceTicket.java subclass of AdvanceTicket | |
* March 24, 2020 | |
*/ | |
import java.text.DecimalFormat; | |
public class StudentAdvanceTicket extends AdvanceTicket{ | |
//For formatting currency | |
DecimalFormat currencyF = new DecimalFormat("#.00"); | |
//parameter constructor | |
public StudentAdvanceTicket(int num, int days) | |
{ | |
super.daysInAdvance=days; | |
super.number=num; | |
setPrice(); | |
} | |
//override setPrice() | |
public void setPrice() | |
{ | |
if(super.daysInAdvance>=10){ | |
super.price=15; | |
} | |
if(super.daysInAdvance<10 && super.daysInAdvance>=1){ | |
super.price=20; | |
} | |
} | |
//override toString() | |
public String toString() | |
{ | |
return "Ticket number: " + number + ", Price: $" + currencyF.format(price)+ " (ID required)"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*Ticket.java superclass | |
* March 24, 2020 | |
*/ | |
import java.text.DecimalFormat; | |
public abstract class Ticket { | |
//properties | |
protected int number; | |
protected double price; | |
//For formatting currency | |
DecimalFormat currencyF = new DecimalFormat("#.00"); | |
//parameter constructor | |
public Ticket(int num) | |
{ | |
number=num; | |
price=0.0; | |
} | |
//IDE/JAVA added a default constructor automatically | |
protected Ticket() { | |
} | |
//abstract setPrice Method | |
public abstract void setPrice(); | |
//toString method | |
public String toString() | |
{ | |
return "Ticket number: " + number + ", Price: $" + currencyF.format(price); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*TicketMain.java Driver program to test the Ticket hierarchy. | |
* March 24, 2020 | |
* Description: This program demonstrates a hierarchy of classes to | |
* represent different types of tickets to a campus event. | |
*/ | |
public class TicketMain { | |
public static void main(String[] args) { | |
//array of Tickets, size 3 | |
Ticket[] tickets =new Ticket[3]; | |
//assign three different types of Tickets to the array | |
tickets[0]=new WalkUpTicket(17); | |
tickets[1]=new AdvanceTicket(24,10); | |
tickets[2]=new StudentAdvanceTicket(30,9); | |
//use polymorphism and a for loop to display the three tickets | |
for (int x=0;x<tickets.length;x++){ | |
System.out.println(tickets[x]); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*WalkUpTicket.java subclass of Ticket | |
* March 24, 2020 | |
*/ | |
public class WalkUpTicket extends Ticket{ | |
//parameter constructor | |
public WalkUpTicket(int num) | |
{ | |
super.number=num; | |
setPrice(); | |
} | |
//override setPrice() | |
public void setPrice() | |
{ | |
super.price=50; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment