Someone was graciously kind enough to help me with part 1 of this, I\'m now need
ID: 3779293 • Letter: S
Question
Someone was graciously kind enough to help me with part 1 of this, I'm now need to implement part 2 into this. I have the code for part 1 just part 2 is giving me trouble. Any help would be appreciated! Thanks in advance! Also I bolded part 2 below under the code.
A set of classes is used to handle the different ticket types for a theater event. All tickets have a unique serial number that is assigned when the ticket is constructed and a price. There are many types of tickets as described in the following table:
Ticket Type
Description
Ticket
This is an abstract class representing all tickets
FixedPriceTicket
This is an abstract class representing tickets that are always the same price. The constructor accepts the price as the parameter.
FreeTicket
These tickets are free. (Thus this class is a subclass of FixedPriceTicket)
WalkupTicket
These tickets are purchased on the day of the event for $50. (Thus this class is a subclass of FixedPriceTicket)
AdvanceTicket
Tickets purchased ten or more days in advance cost $30.Tickets purchased fewer than ten days in advance cost $40.
StudentAdvanceTicket
These are AdvanceTickets that cost half of what an AdvanceTicket would normally cost.
Design a class hierarchy that encompasses the above classes. Implement the Ticket abstract class. This class will store a serial number as its private data. Provide an abstract method to get the price of the ticket called getPrice, provide a method that returns the serial number called getSerialNumber, and provide an implementation of toString() that prints the serial number and price information. The Ticket class must provide a constructor that generates the serial number. To do so, use the following strategy: maintain a static ArrayList<Integer> representing previously assigned serial numbers. Repeatedly generate a new serial number using a random number generator until you obtain a serial number not already assigned.
Implement the FixedPriceTicket class. The constructor accepts a price. The class is abstract but you can and should implement the method that returns the price information.
Implement the WalkupTicket class and the ComplementaryTicket class.
Implement the AdvanceTicket class. Provide a constructor that takes a parameter indicating the number of days in advance that the ticket is being purchased. Recall that the number of days of advanced purchase affects the ticket price.
Implement the StudentAdvanceTicket class. Provide a constructor that takes a parameter indicating the number of days in advance that the ticket is being purchased. The toString method should include a notation that this is a student ticket. This ticket costs half of an AdvanceTicket. If the pricing scheme forAdvanceTicket changes, the StudentAdvanceTicket price should be computed correctly with no code modification to the StudentAdvanceTicket class.
Write a class TicketOrder that stores a collection of Tickets. TicketOrder should provide methods add, toString, and totalPrice.
Write a class Event that models any event that requires tickets for admission. An Event has a title, a date, the fixed ticket price for this event, a maximum number of tickets that can be sold for the event, and a list of the allowable ticket types for this event
//Main.java
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Random generator = new Random(); //Used in switch case to randomly generate a type of Ticket.
int typeOfTicket;
TicketOrder order1;
Scanner sc = new Scanner(System.in);
String input;
System.out.println("Usage: Enter number of random tickets to generate. To end the program, type in "exit".");
input = sc.nextLine();
while (!input.equals("exit")) {
order1 = new TicketOrder();
int num;
try {
num = Integer.parseInt(input);
} catch (Exception e) {
System.out.println(" Please provide a valid number");
input = sc.nextLine();
continue;
}
for (int i = 0; i < num; i++) {
//Generate up to 7 different types of tickets and add them to our TicketOrder object.
//StudentAdvanceTicket and AdvanceTicket will have
//a chance to create a ticket with num of days <= 10 or > 10
typeOfTicket = generator.nextInt(7);
switch (typeOfTicket) {
case 0:
order1.add(new FixedPriceTicket(generator.nextInt(51) + 50));
System.out.println("Generating FixedPriceTicket with price between 50 and 100");
break;
case 1:
order1.add(new AdvanceTicket(generator.nextInt(11)));
System.out.println("Generating AdvanceTicket with number of days between 0 and 10");
break;
case 2:
order1.add(new AdvanceTicket(generator.nextInt(90) + 11));
System.out.println("Generating AdvanceTicket with number of days between 11 and 100");
break;
case 3:
order1.add(new ComplementaryTicket());
System.out.println("Generating ComplementaryTicket");
break;
case 4:
order1.add(new StudentAdvanceTicket(generator.nextInt(11))); //Number of days between 0 and 10
System.out.println("Generating StudentAdvanceTicket with number of days between 0 and 10");
break;
case 5:
order1.add(new StudentAdvanceTicket(generator.nextInt(90) + 11)); // between 11 and 100
System.out.println("Generating StudentAdvanceTicket with number of days between 11 and 100");
break;
case 6:
order1.add(new WalkupTicket());
System.out.println("Generating WalkupTicket");
break;
}
}
System.out.println(" Generated " + num + " tickets.");
System.out.println(order1.toString());
System.out.println(" Enter number of tickets to purchase or enter "exit" to quit");
input = sc.nextLine();
}
System.exit(0);
}
}
==============================================================================
//AdvanceTicket.java
public class AdvanceTicket extends FixedPriceTicket {
public AdvanceTicket(int numOfDays) {
super(40);
if (numOfDays > 10) {
price = 30.0;
}
}
}
=======================================================================
//ComplementaryTicket.java
public final class ComplementaryTicket extends FixedPriceTicket {
/**
* Ticket with a price of 0
*/
public ComplementaryTicket() {
super(0);
}
}
=========================================================================
//FixedPriceTicket.java
public class FixedPriceTicket extends Ticket{
/**
* Ticket with a fixed price.
* @param price Price of ticket.
*/
public FixedPriceTicket(Number price) {
super(price.doubleValue());
}
public Double getPrice() {
return price;
}
}
=============================================================================
//StudentAdvanceTicket.java
public final class StudentAdvanceTicket extends AdvanceTicket {
/**
* StudentAdvanceTicket is half the price of an AdvanceTicket
* @param numOfDays Number of days in advance that the ticket is purchased
*/
public StudentAdvanceTicket(int numOfDays) {
super(numOfDays);
price = getPrice() / 2;
}
@Override
public String toString() {
return super.toString() + " (Student)";
}
}
=============================================================================
// Ticket.java
import java.util.ArrayList;
import java.util.Random;
public abstract class Ticket {
public static ArrayList<Integer> serialNumberList = new ArrayList<Integer>();
private static Random generator = new Random();
private Integer serialNumber;
protected Double price;
public Ticket() {
//convert nanoseconds since 1970 into a number between 1 and the Max value of an integer.
int seed = ((int)(System.currentTimeMillis() % Integer.MAX_VALUE) + 1);
//Convert the serial number into a value between 1 and 1000.
serialNumber = (generator.nextInt(seed) % 1000)+ 1;
while (serialNumberList.contains(serialNumber)) {
serialNumber = (generator.nextInt(seed) % 500) + 1;
}
serialNumberList.add(serialNumber);
}
/**
* Constructor for Ticket that takes in a ticketPrice
* @param ticketPrice Price of ticket
*/
public Ticket(Number ticketPrice) {
this();
price = ticketPrice.doubleValue();
}
/**
* Returns the serial Number
* @return serialNumber
*/
public final Integer getSerialNumber() {
return serialNumber;
}
public abstract Double getPrice();
/**
* Returns a string representation of a Ticket
* @return String
*/
@Override
public String toString() {
return "Serial Number: " + serialNumber + " Price: $" + price;
}
}
========================================================================
//TicketOrder.java
import java.util.HashMap;
public class TicketOrder {
/**
* HashMap that keeps track of tickets already stored.
*/
private HashMap<Integer, Ticket> storedTickets = new HashMap<Integer, Ticket>();
/** Default Constructor */
public TicketOrder() {
}
//the add method can accept one or multiple Ticket objects as a parameter
//It will also only add tickets with serial numbers that are not already stored.
/**
* Method to add Tickets to the HashMap in TicketOrder.
* This method can take one or more arguments of type Ticket.
*
* @param tickets Any subclass of Ticket
*/
public void add(Ticket... tickets) {
for (Ticket ticks : tickets) {
if (!storedTickets.containsKey(ticks.getSerialNumber()))
storedTickets.put(ticks.getSerialNumber(), ticks);
}
}
/**
*
* @return the total price of all Tickets stored.
*/
public Double totalPrice() {
Double total = 0.0;
for (Ticket tic : storedTickets.values()) {
total += tic.getPrice();
}
return total;
}
@Override
public String toString() {
if (storedTickets.isEmpty()) {
return "No stored tickets in TicketOrder ";
}
String output = "";
for (Ticket tic : storedTickets.values()) {
output += tic.toString() + " ";
}
output += "Total Price: " + totalPrice() + " ";
return output;
}
}
=====================================================================
// WalkupTicket.java
public final class WalkupTicket extends FixedPriceTicket {
public WalkupTicket() {
super(50);
}
}
=====================================================================
Part 2:
For this project you will be implementing a simulation of a ticket purchasing system. Your client wants to know if they should implement a customer loyalty program so that customers with higher status get faster service when purchasing tickets. The client is interested in comparing the average wait time, the total sales amount, and the number of customers who got tired of waiting and decided to leave without making a purchase. (Customers waiting to purchase tickets leave after 10 minutes if they haven’t been served, so the simulation will need to included and report this activity.) To make the comparison, you will need to model the existing system first and gather statistics on it. Then, implement the changes requested by your client and repeat the simulation. Finally, you will submit a report to your client that explains the results of your efforts.
a. Your client did not like the fixed prices on the WalkupTicket and AdvanceTicket classes that you created. Refactor these classes so that the price of the ticket is full price for a WalkupTicket and there is a 10% discount for AdvanceTicket. StudentAdvanceTicket should remain as specified previously.
b. Create the driver class called TicketSimTester. This class will read in a file containing the current list of events and store the events in an indexed, searchable data structure. It will initialize and execute the simulation for an amount of time (represented as minutes) that will be specified as the first command line argument to the program. A second command line argument is the name of the event list file.
c. Implement the Customer class. Customer objects will be generated as part of the simulation. The Customer fields include a sequential customer number, randomly generated arrival time, a randomly generated transaction time, a randomly generated event selection, a randomly generated number of tickets to purchase and the ticket type, and a status (platinum, gold, silver, or none). For the first simulation, all customers will have a status of “none” and in the second run of the simulation you will randomly set customer status based on the following percentages: platinum – 30%; gold – 40%; silver – 20%; none – 10%. For ease in execution of the simulation, a third command line argument to the TicketSimTester class will be a boolean that, if true, assigns status values to Customers in the simulation.
d. Run the program with a simulated time of 600 minutes (10 hours) for each case, collecting the required data for your report to the client. You may want to repeat these runs, to collect as much data as possible
Ticket Type
Description
Ticket
This is an abstract class representing all tickets
FixedPriceTicket
This is an abstract class representing tickets that are always the same price. The constructor accepts the price as the parameter.
FreeTicket
These tickets are free. (Thus this class is a subclass of FixedPriceTicket)
WalkupTicket
These tickets are purchased on the day of the event for $50. (Thus this class is a subclass of FixedPriceTicket)
AdvanceTicket
Tickets purchased ten or more days in advance cost $30.Tickets purchased fewer than ten days in advance cost $40.
StudentAdvanceTicket
These are AdvanceTickets that cost half of what an AdvanceTicket would normally cost.
Explanation / Answer
//TicketSimTester.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Random;
import java.util.Scanner;
public class TicketSimTester {
public static HashMap<Integer,Event> eventList = new HashMap<Integer,Event>(); //holds the list of events
public static HashMap<Integer,customer> customers= new HashMap<Integer,customer>(); //holds the list of customers
public static void main(String args[]){
String fileName;//="c:/eclipse/event.txt";
String eventName;
int amtTime;
BufferedReader br;
Event e;
customer c;
try{
amtTime=Integer.parseInt(args[0]);
System.out.println("Enter event list file name:");
fileName=args[1];
br = new BufferedReader(new FileReader(fileName)); //reading the events information from the file(specified in commandLine argument
Scanner sc = new Scanner(System.in);
String input;
do{
eventName=br.readLine();
e=new Event(eventName,0,amtTime);
eventList.put(e.getEventNumber(),e); //adding events to event list
System.out.println("Enter Price For the Event:");
input = sc.nextLine();
e.setPrice(Double.parseDouble(input)); //initialising the price per each event.
e.addTickets(); //getting the tickets for each event.
}while(eventName!=null);
System.out.println("How many customers:");
input = sc.nextLine();
//getting customers information
for(int i=0;i<Integer.parseInt(input);i++){
System.out.println("Enter Customer "+i+1+ "Name:");
input = sc.nextLine();
c=new customer(input);
//adding the customer to the customers list
if(!customers.containsKey(c.getCustomerNumber()))
customers.put(c.getCustomerNumber(),c);
if(args[2]=="1"){
int seed = ((int)(System.currentTimeMillis() % Integer.MAX_VALUE) + 1);
for(customer cs:customers.values()){
//setting the customer status based on the command line arguement passed .
cs.setStatus(((new Random().nextInt(seed)) % 4)+1);
cs.setEvent(eventList.get(c.getEventSelect())); //assigning an event to the customer
}
}
}
}catch(Exception er){
System.out.println(er);
}
}
//displaying the customers report.
public void displayReort(){
System.out.println("Customer Details :");
System.out.println("CustomerName AverageWaitingTime Total Sales:");
for(customer cs:customers.values())
System.out.println(cs.getCustName()+cs.averageWait()+cs.getAmtPurchaged());
System.out.println("Total Waiting:"+averageWaiting()+" TotalSales: "+totalSales() +" Customers Tired Waiting: "+tiredWaiting());
}
//computing average waiting time for all customers.
public int averageWaiting(){
int wt=0;
for(customer c:customers.values())
wt=wt+c.averageWait();
return wt/customers.size();
}
//computing total sales
public double totalSales(){
double ts=0;
for(customer c:customers.values())
ts=ts+c.getAmtPurchaged();
return ts=0;
}
//computing tired in waiting for all customers
public int tiredWaiting(){
int tw=0;
for(customer c:customers.values())
if(c.averageWait()>10)
tw=tw+1;
return tw;
}
}
//Event.java
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class Event {
public static ArrayList<Integer> eventNumberList = new ArrayList<Integer>();
protected TicketOrder order;
protected String title;
protected int ticketType;
// 0- FixedPriceTicket,1-AdvanceTicket(1-10),2-AdvanceTicket(11-100),3-ComplementaryTicket,
// 4-StudentAdvanceTicket(1-10),5-StudentAdvanceTicket(11-100),6-WalkupTicket
protected int noOfTickets;
protected int eventNumber;
protected int amtTime;
protected double price;
public Event(String t,int et,int at) {
int seed = ((int)(System.currentTimeMillis() % Integer.MAX_VALUE) + 1);
//Convert the serial number into a value between 1 and 1000.
eventNumber = (new Random().nextInt(seed) % 1000)+ 1;
while (eventNumberList.contains(eventNumber)) {
eventNumber = (new Random().nextInt(seed) % 500) + 1;
}
noOfTickets=(new Random().nextInt(seed) % 1000)+1;
ticketType=(new Random().nextInt(seed) % 7)+1;
title=t;
amtTime=at;
eventNumberList.add(eventNumber);
}
//getting title of the event
public String geTitle() {
return title;
}
//setting titleof the event
public void setTitle(String eventName) {
this.title = eventName;
}
//getting number of the event
public final int getEventNumber() {
return eventNumber;
}
//getting type of the event
public int getTicketType() {
return ticketType;
}
//setting type of the event
public void setEventType(int ticketType) {
this.ticketType = ticketType;
}
//getting amount time of the event
public int getAmtTime() {
return amtTime;
}
//setting amount time of the event
public void setAmtTime(int amtTime) {
this.amtTime = amtTime;
}
//creating tickets for each event.
public void addTickets(){
Random generator = new Random(); //Used in switch case to randomly generate a type of Ticket.
Scanner sc = new Scanner(System.in);
String input;
System.out.println("Enter number of random tickets to generate for customer: type in "exit".");
input = sc.nextLine();
int j=Integer.parseInt(input);
while (!input.equals("exit")) {
order= new TicketOrder();
//here we generate tickets based on the given number of tickets in the console.
for (int i = 0; i < j; i++) {
// Generate up to 7 different types of tickets and add them to our TicketOrder object.
// StudentAdvanceTicket and AdvanceTicket will have a chance to create a ticket with num of days <= 10 or > 10
ticketType = generator.nextInt(7);
switch (ticketType) {
case 0:
order.add(new FixedPriceTicket(generator.nextInt(51) + 50));
System.out.println("Generating FixedPriceTicket with price between 50 and 100");
break;
case 1:
order.add(new AdvanceTicket(generator.nextInt(11)));
System.out.println("Generating AdvanceTicket with number of days between 0 and 10");
break;
case 2:
order.add(new AdvanceTicket(generator.nextInt(90) + 11));
System.out.println("Generating AdvanceTicket with number of days between 11 and 100");
break;
case 3:
order.add(new ComplementaryTicket());
System.out.println("Generating ComplementaryTicket");
break;
case 4:
order.add(new StudentAdvanceTicket(generator.nextInt(11))); //Number of days between 0 and 10
System.out.println("Generating StudentAdvanceTicket with number of days between 0 and 10");
break;
case 5:
order.add(new StudentAdvanceTicket(generator.nextInt(90) + 11)); // between 11 and 100
System.out.println("Generating StudentAdvanceTicket with number of days between 11 and 100");
break;
case 6:
order.add(new WalkupTicket(generator.nextInt(51)+50));
System.out.println("Generating WalkupTicket");
break;
}
}
System.out.println(" Generated " + noOfTickets+ " tickets.");
System.out.println(order.toString());
System.out.println(" Enter number of tickets to purchase or enter "exit" to quit");
input = sc.nextLine();
}
//System.exit(0);
}
public int getNoOfTickets() {
return noOfTickets;
}
public void setNoOfTickets(int noOfTickets) {
this.noOfTickets = noOfTickets;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getTitle() {
return title;
}
public void setTicketType(int ticketType) {
this.ticketType = ticketType;
}
}
//customer.java
import java.util.ArrayList;
import java.util.Random;;
public class customer {
public static ArrayList<Integer> customerNumberList = new ArrayList<Integer>();
public static Event event;
private static Random generator = new Random();
protected int arrvTime;
protected int transTime;
protected int eventSelect;
protected Integer status=0;//0-none 1-paltinum 2-gold 3-silver
protected Integer custNumber;
protected Double amtPurchaged;
protected String custName;
public customer(String cName) {
// convert nanoseconds since 1970 into a number between 1 and the Max value of an integer.
int seed = ((int)(System.currentTimeMillis() % Integer.MAX_VALUE) + 1);
//Convert the serial number into a value between 1 and 1000.
custNumber = (generator.nextInt(seed) % 1000)+ 1;
while (customerNumberList.contains(custNumber)) {
custNumber = (generator.nextInt(seed) % 500) + 1;
}
customerNumberList.add(custNumber);
arrvTime = (generator.nextInt(seed) % 3600)+1;
transTime=(generator.nextInt(seed) % 3600)+1;
eventSelect=(generator.nextInt(seed) % 1000)+1;
status=0;
custName=cName;
}
/**
* Returns the customer Number
* @return custNumber
*/
public final Integer getCustomerNumber() {
return custNumber;
}
//setting status of customer.
public void setStatus(int s) {
status =s;
}
//setting status of customer.
public Integer getStatus(int s) {
return status;
}
@Override
public String toString() {
return "Customer Number: " + custNumber + " Amount Purchaged: $" +amtPurchaged;
}
//getting ArrivalTime of customer
public int getArrvTime() {
return arrvTime;
}
//setting ArrivalTime of customer
public void setArrvTime(int aTime) {
arrvTime = aTime;
}
//getting eventselection of customer
public int getEventSelect() {
return eventSelect;
}
//setting eventselection of customer
public void setEventSelect(int eventSelect) {
this.eventSelect = eventSelect;
}
//getting status of customer
public Integer getStatus() {
return status;
}
//setting status of customer
public void setStatus(Integer status) {
this.status = status;
}
//getting transaction time of customer.
public int getTransTime() {
return transTime;
}
//setting transaction time of customer.
public void setTransTime(int tTime) {
transTime = tTime;
}
//getting purchaged amount of customer.
public Double getAmtPurchaged() {
amtPurchaged=amtPurchaged+event.order.totalPrice();
return amtPurchaged;
}
//setting purchaged amount of customer.
public void setAmtPurchaged(Double amtPurchaged) {
this.amtPurchaged = amtPurchaged;
}
//computing purchaged amount based on discount
public double computeDiscount(){
double r=0.0;
if(status==1)
r=amtPurchaged-amtPurchaged *(30/100);
else if(status==2)
r=amtPurchaged-amtPurchaged*(40/100);
else if(status==3)
r=amtPurchaged-amtPurchaged*(20/100);
else
r=amtPurchaged-amtPurchaged*(10/100);
return r;
}
//comupting average waiting time
public int averageWait(){
return (transTime-arrvTime);
}
//getting the customer name
public String getCustName() {
return custName;
}
//setting the customer name
public void setCustName(String custName) {
this.custName = custName;
}
//getting the event
public static Event getEvent() {
return event;
}
//setting the event
public static void setEvent(Event event) {
customer.event = event;
}
}
// WalkupTicket.java
public final class WalkupTicket extends FixedPriceTicket {
public WalkupTicket(double price){
super(price);
}
}
//AdvanceTicket.java
public class AdvanceTicket extends FixedPriceTicket {
public AdvanceTicket(int numOfDays) {
super((36));
if (numOfDays > 10) {
price = 27.0;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.