gpt4 book ai didi

java - 如何从我的 "event"中获取价格因子来计算我的门票价格

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:43:35 25 4
gpt4 key购买 nike

我有一个事件类,由 Concert、Play 和 Meeting 实现。我在单独的票类中出售给相应的“事件”。在 Ticket 中,我需要 getPrice() ,它从 10 开始,并且需要在预订事件(具有不同的价格因素)时乘以价格因素。

我试过简单地调用一个静态 PRICE,它是门票的基本价格,并调用了 event.getPriceFactor(),它返回 null,我显然可以在 Ticket 中实例化这个事件。


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);
}

public double getPriceFactor() {
return priceFactor;
}
------------------------------------------------------------------------------
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 {
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;
}

我期望 Ticket.PRICE (10.) * event.getPriceFactor() (1.0) 返回 10,但它返回 null。

最佳答案

看起来您从未将事件分配给票证构造函数中的票证属性,因此它是一个空值并且无法返回价格因素。尝试分配它,看看是否可行

关于java - 如何从我的 "event"中获取价格因子来计算我的门票价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58033422/

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