Each ticket has a unique ticket number that is assigned when the ticket is constructed. For all ticket classes, the toString method returns a string containing the information for that ticket with additional information if needed. Three additional classes are used to represent the different types of the tickets and are described in the table below.
Interpreting the above UML All tickets have a ticket number and a price. The default ticket number is 000 and the default price is 50.00. The class Ticket is specified as shown in the following declaration as interpretation of the UML diagram shown above
public class Ticket {
private int ticketNumber;
private double price;
public Ticket(int ticketNumber, double price) {
this.ticketNumber = ticketNumber;
this.price = price;
}
public Ticket() {
this.ticketNumber = 000;
this.price = 50.00;
}
public void setPrice(double price) {
this.price = price;
}
public void setTicketNumber(int ticketNumber) {
this.ticketNumber = ticketNumber;
}
public int getTicketNumber() {
return ticketNumber;
}
public double getPrice() {
return price;
}
@Override
public String toString() {
return "Ticket{" + "ticketNumber=" + ticketNumber + ", price=" + price + '}';
}
}
Using the class hierarchy and specifications given above, the complete class declarations for WalkIn, EarlyPurchase and StudentEarlyPurchase classes are shown below:
/**
*
* @author maanderson
*/
public class WalkIn extends Ticket{
public WalkIn(int ticketNumber, double price) {
super(ticketNumber, price);
}
@Override
public double getPrice(){
return super.getPrice();
}
@Override
public String toString() {
return "WalkIn{" + super.toString() + '}';
}
}
/**
*
* @author maanderson
*/
public class EarlyPurchase extends Ticket {
private int daysAdvance;
public EarlyPurchase(int daysAdvance, int ticketNumber, double price) {
super(ticketNumber, price);
this.daysAdvance = daysAdvance;
}
public EarlyPurchase(int daysAdvance) {
this.daysAdvance = daysAdvance;
}
@Override
public double getPrice() {
double price = 0;
if (this.daysAdvance >= 10) {
price = super.getPrice() - 0.10;
} else if (this.daysAdvance < 10 && this.daysAdvance > 0) {
price = 40;
}
return price;
}
@Override
public String toString() {
return "EarlyPurchase{" + "daysAdvance=" + daysAdvance + super.toString() + '}';
}
}
/**
*
* @author maanderson
*/
public class StudentEarlyPurchase extends EarlyPurchase {
public StudentEarlyPurchase(int daysAdvance, int ticketNumber, double price) {
super(daysAdvance, ticketNumber, price);
}
public StudentEarlyPurchase(int daysAdvance) {
super(daysAdvance);
}
@Override
public double getPrice() {
return super.getPrice() - 0.15;
}
@Override
public String toString() {
return "StudentEarlyPurchase{" + super.toString() + "Student ID Required!" + '}';
}
}