gpt4 book ai didi

java - 我应该在哪里实例化我的 "Ticket"以防止它们全部变成相同的价格?

转载 作者:行者123 更新时间:2023-12-02 01:19:18 26 4
gpt4 key购买 nike

我有一个出售 Activity 门票的程序,特别是“戏剧”门票。门票的起始价格为 10.0,并乘以价格系数。我的价格因素正常工作,但门票最终都采用最终门票价格的值。

我尝试在 Event 类和 Play 类的 addTicket 方法 (stackoverflowerror) 以及 Ticket 构造函数中实例化 Ticket 对象(Ticket tick = new Ticket(this);...因为它需要 Event 的参数),但没有成功。我注意到,在不添加任何实例化的情况下,我的票证对象序列号仍然会增加,所以我不确定为什么如果*它正在创建一个新的票证对象,那么我的所有票证最终都会以相同的价格。

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]

最佳答案

DerMolly 的将价格因子引入 Ticket 类的替代方案是仅在创建时计算价格,而不是在每个 getPrice 上计算:

    public Ticket(Event event) throws NoSpaceException, UnsupportedOperationException { 

this.event = event;
event.addTicket(this);
this.price = Ticket.PRICE * event.getPriceFactor();
this.serialNumber = computeSerialNumber();

}

public double getPrice() {

return price;

}

关于java - 我应该在哪里实例化我的 "Ticket"以防止它们全部变成相同的价格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58038116/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com