如何从我的“活动”中获取价格因素来计算我的门票价格

我有一个 Event 类,它是由 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;



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


RISEBY
浏览 102回答 1
1回答

智慧大石

看起来您从未将事件分配给票证构造函数中的票证属性,因此为空值并且无法返回价格因素。尝试分配它并看看是否有效
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java