首页 > 解决方案 > 我应该在哪里实例化我的“票”以防止它们都变成相同的价格?

问题描述

我有一个销售活动门票的程序,尤其是“戏剧”。门票以 10.0 的基值开始,并乘以 priceFactor。我的价格因素工作正常,但门票最终都取走了最终门票价格的价值。

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

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]

标签: java

解决方案


DerMolly 将 pricefactor 带入 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;

    }

推荐阅读