У меня есть программа, которая продает билеты на мероприятия и, в частности, «Игры».Билеты начинаются с базового значения 10,0 и умножаются на PriceFactor.У меня правильно работает ценовой фактор, но все билеты в итоге принимают значение окончательной цены билета.
Я пытался создать экземпляр объекта Ticket (Ticket tick = new Ticket (this); ... потому чтоон принимает параметр Event) в классе Event и методах addTicket класса Play (stackoverflowerror) и в конструкторе Ticket, но безуспешно.Что я заметил, так это то, что без добавления каких-либо экземпляров серийные номера моих билетов по-прежнему увеличиваются, поэтому я не уверен, почему все мои билеты имеют одинаковую цену, если * создается новый объект Ticket.
public class Play extends Event {
/**
* Creates a Play object with the description and price factor.
*
* @param description the description of the play
* @param priceFactor the price factor for the play
*/
public Play(String description, double priceFactor) {
super(description, priceFactor);
}
/**
* Creates a play with the given description and a price factor of 1.0.
*
* @param description the description of the play
*/
public Play(String description) {
this(description, 1.0);
}
/**
* Adds a ticket to the list of tickets sold for this Play object. It also
* adjusts the price factor.
*
* @param ticket the Ticket object to be added
* @return true iff the Ticket object could be added.
* @throws NoSpaceException
* @throws UnsupportedOperationException
*/
@Override
public boolean addTicket(Ticket ticket) throws UnsupportedOperationException, NoSpaceException {
double i = this.getPriceFactor();
if (this.getTickets().size() < 3) {
super.addTicket(ticket);
}
else if (this.getTickets().size() == 3) {
super.setPriceFactor(i * 1.2);
i = super.getPriceFactor();
super.addTicket(ticket);
}
else if (this.getTickets().size() == 4) {
super.setPriceFactor(i * 1.2);
super.addTicket(ticket);
}
return true;
}
/**
* Returns a String representation.
*
*/
@Override
public String toString() {
return "Play" + " " + super.getEventId() + " " + super.getDescription() + " " + super.getPriceFactor();
}
}
-----------------------------------------------------------------------------
public abstract class Event {
private String description;
protected int ticketsSold;
private int eventId;
private double priceFactor;
private static int counter = 1;
private static final int CAPACITY = 5;
private ObservableList<Ticket> tickets = FXCollections.observableArrayList();
/**
* Stores the description and price factor and assigns a unique id to the event.
* The constructor also allocates the array tickets.
*
* @param description a description of this Play
* @param priceFactor the price factor for this Play
*
*/
public Event(String description, double priceFactor) {
this.description = description;
this.priceFactor = priceFactor;
this.eventId = computeSerialNumber();
}
/**
* Receives the description and stores that and a price factor of 1.0. Besides,
* it assigns a unique id to the event. The constructor also allocates the array
* tickets.
*
* @param description a description of this Play
*
*/
public Event(String description) {
this(description, 1.0);
}
/**
* Returns the unique id of the play
*
* @return id of the play
*
*/
public int getEventId() {
return eventId;
}
/**
* Returns the tickets list
*
* @return the tickets list
*/
public ObservableList<Ticket> getTickets() {
return tickets;
}
/**
* Sets the price factor for the event.
*
* @param priceFactor the new price factor
*/
public void setPriceFactor(double priceFactor) {
this.priceFactor = priceFactor;
}
/**
* Computes and returns the total proceeds for this event.
*
* @return total proceeds
*/
public double getProceeds() {
double sum = 0;
for (Ticket t : tickets) {
sum += t.getPrice();
}
return sum;
}
/**
* Compares this Play with object. Follows the semantics of the equals method in
* Object.
*
*/
@Override
public boolean equals(Object object) {
if (this == object)
return true;
else
return false;
}
public int hashcode() {
return this.eventId;
}
/**
* Returns the description of the Play object
*
* @return description
*/
public String getDescription() {
return description;
}
/**
* Returns the price factor
*
* @return price factor
*/
public double getPriceFactor() {
return priceFactor;
}
/**
* Setter for description
*
* @param description the new description
*/
public void setDescription(String description) {
this.description = description;
}
/**
* Returns a unique serial number. This is a helper method.
*
* @return serial number
*/
private int computeSerialNumber() {
int i = counter;
counter++;
return i;
}
/**
* Adds a ticket to the list of tickets sold for this Play object.
*
* @param ticket the Ticket object to be added
* @return true iff the Ticket object could be added.
* @throws NoSpaceException
* @throws UnsupportedOperationException
*/
public boolean addTicket(Ticket ticket) throws UnsupportedOperationException, NoSpaceException {
if (tickets.size() == CAPACITY)
return false;
else
tickets.add(ticket);
ticketsSold++;
return true;
}
/**
* Returns a String representation of this Event object
*/
@Override
public String toString() {
return description + " " + eventId;
}
}
--------------------------------------------------------------------------------
public class Ticket {
private static int counter = 1;
private int serialNumber;
private double price;
private static double PRICE = 10.0;
private Event event;
/**
* Creates a ticket for an event. An exception is thrown if there is no space.
*
* @param event the event for which the ticket is being created.
* @throws NoSpaceException
*/
public Ticket(Event event) throws NoSpaceException, UnsupportedOperationException {
this.event = event;
event.addTicket(this);
this.serialNumber = computeSerialNumber();
}
/**
* Returns the price of the ticket
*
* @return ticket price
*/
public double getPrice() {
price = Ticket.PRICE * event.getPriceFactor();
return price;
}
/**
* Generates a String representation of the Ticket.
*/
@Override
public String toString() {
return "Ticket serialNumber = " + serialNumber + ", " + "price =" + this.getPrice();
}
/*
* Creates a serial number for the ticket.
*/
private static int computeSerialNumber() {
int i = counter;
counter++;
return i;
}
}
public class Test {
public static void main(String[] args) throws UnsupportedOperationException, NoSpaceException {
double a,b,c,d,e;
Play p = new Play("p1", 1.0);
Ticket tick = new Ticket(p);
Ticket tick2 = new Ticket(p);
Ticket tick3 = new Ticket(p);
Ticket tick4 = new Ticket(p);
Ticket tick5 = new Ticket(p);
p.addTicket(tick);
p.addTicket(tick2);
p.addTicket(tick3);
p.addTicket(tick4);
p.addTicket(tick5);
a = tick.getPrice();
b = tick2.getPrice();
c = tick3.getPrice();
d = tick4.getPrice();
e = tick5.getPrice();
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(e);
}
}
Ожидаемые результаты для билетов в порядке [10,0, 10,0, 10,0, 12,0, 14,399]
Фактические результаты [14,399, 14,399, 14,399, 14,399, 14,399]