我有一个Event类,该类由Concert,Play和Meeting实现。我在单独的票务类中出售给相应的“活动”。在Ticket中,我需要getPrice(),该价格从10开始,并且在预订事件(具有不同价格因素)时需要乘以价格因素。
我试图简单地调用一个静态PRICE(它是票证的基本价格),然后调用event.getPriceFactor(),它返回null,显然我可以在票证中实例化此事件。
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。
答案 0 :(得分:1)
似乎您从未将事件分配给票证构造函数中的票证属性,因此它是一个空值,无法返回价格因素。尝试分配它,看看是否可行