Consider the task of representing types of tickets to campus events. Each ticket
ID: 3667049 • Letter: C
Question
Consider the task of representing types of tickets to campus events. Each ticket has a unique number and a price. There are three types of tickets: walk-up tickets, advance tickets, and student advance tickets. Figure 9.10 in the text illustrates the types.
Walk-up tickets are purchased the day of the event and cost $50.
Advance tickets purchased 10 or more days before the event cost $30, and advanced tickets purchased fewer than 10 days before the event cost $40.
Student advance tickets are sold at half price of normal advance tickets: when they are purchased 10 or more days early they cost $15, and when they are purchased fewer than 10 days early they cost $20.
Implement a class called Ticket that will serve as the superclass for all three types of tickets. Define all common operations in this class, and specify all differing operations in such a way that every subclass must implement them. No such a way that every subclass must implement them. No actual objects of type Ticket will be created; each actual ticket will be an object of a subclass type. Define the following operations: (a) The ability to construct a ticket by number. (b) The ability to ask for a ticket's price. © The ability to println a ticket object as a String. An example String might be "Number: 17, Price: 50.00".
Implement a class called WalkupTicket to represent a walk-up event ticket. Walk-up tickets are also constructed by number, and they have a price of $50.
Implement a class called AdvanceTicket to represent tickets purchased in advance. An advance ticket is constructed with a ticket number and with the number of days in advance the ticket was purchased. Advance tickets purchased 10 or more days before an event cast $30, and advance tickets purchased fewer than 10 days before the event cost $40.
Implement a class called StudentAdvanceTicket to represent tickets purchased in advance by students. A student advance ticket is constructed with a ticket number and with the number of days in advance that the ticket was purchased. Student advance tickets purchased 10 or more days before an event cost $15, and student advance tickets purchased fewer than 10 days before an event cost $20 (half the normal advance ticket). When a student advance ticket is printed, the String should mention that the student must show a student ID (for example "Number: 17, Price: 50.00 (ID required)").
After the classes stated in the problem above are written and if the following
BuyTicket.java file is executed, you should get following output:
public class BuyTicket {
public static void main(String[] args){
WalkupTicket wt1=new WalkupTicket(12);
System.out.println(wt1);
AdvanceTicket at1=new AdvanceTicket (13,12);
System.out.println(at1);
AdvanceTicket at2 = new AdvanceTicket(14,2);
System.out.println(at2);
StudentAdvanceTicket sat1= new StudentAdvanceTicket(15,20);
System.out.println(sat1);
StudentAdvanceTicket sat2= new StudentAdvanceTicket(16,4);
System.out.println(sat2);
}
}
Output: Number: 12, Price: 50.0
Number: 13, Price: 30.0
Number: 14, Price: 40.0
Number: 15, Price: 15.0 (ID required)
Number: 16, Price: 20.0 (ID required)
Explanation / Answer
// Ticket is abstract class because
//If we have to force subclass/child class to implement member functions of Ticket class,
//Ticket class has to be abstract because of abstract methods declared in it
// More over No actual objects of type Ticket will be created
abstract class Ticket {
String ticketLabel; // member variable to print ticket label
protected int tNum; // variable for ticket number
protected double price; // Variable for ticket price
Ticket(int num){
tNum = num;
price = 50;
// Prepare the Ticket label
ticketLabel = "Number: "+tNum+", Price: "+price+".";
updatePrice();
}
public double getPrice()
{
updatePrice();
return price;
}
// toString method of Object class to be overridden
// to print proper label instead of printing Ticket@5456456.... ie classname@....
// The ability to println a ticket object as a String.
@Override
public String toString()
{
updateLabel();
return ticketLabel;
}
// Two abstract methods are delacred below to force subclasses to update Ticket label and price
// as price and label may change based on ticket type
abstract void updateLabel();
abstract void updatePrice();
}
class WalkupTicket extends Ticket{
//The ability to construct a ticket by number.
WalkupTicket(int num){
super(num);
}
// WalkupTicket is not much different from Ticket class
//so not much of need to write code in updatePrice &updateLabel
void updatePrice(){
// price = 50.00;
}
void updateLabel(){
// Prepare the Ticket label
// ticketLabel = "Number: "+tNum+", Price: "+price+".";
}
}
class AdvanceTicket extends Ticket{
int advDays;
AdvanceTicket(int num,int advD){
super(num);
advDays = advD;
updatePrice();
}
public double getPrice(){
return price;
}
void updatePrice(){
if(advDays >= 10){
price = 30.00;
}else{
price = 40.00;
}
}
void updateLabel(){
// Prepare the Ticket label
ticketLabel = "Number: "+tNum+", Price: "+price+".";
}
}
class StudentAdvanceTicket extends Ticket{
int advDays;
StudentAdvanceTicket(int num,int advD){
super(num);
advDays = advD;
updatePrice();
}
public double getPrice(){
return price;
}
void updatePrice(){
if(advDays >= 10){
price = 15.00;
}else{
price = 20.00;
}
}
void updateLabel(){
// Prepare the Ticket label
ticketLabel = "Number: "+tNum+", Price: "+price+"(ID required).";
}
}
public class BuyTicket {
public static void main(String[] args){
WalkupTicket wt1=new WalkupTicket(12);
System.out.println(wt1);
AdvanceTicket at1=new AdvanceTicket (13,12);
System.out.println(at1);
AdvanceTicket at2 = new AdvanceTicket(14,2);
System.out.println(at2);
StudentAdvanceTicket sat1= new StudentAdvanceTicket(15,20);
System.out.println(sat1);
StudentAdvanceTicket sat2= new StudentAdvanceTicket(16,4);
System.out.println(sat2);
}
}
OUTPUT: