How to get the price factor from my "event" to calculate the price of my tickets

Michael Harrison :

I have an Event class the is implemented by Concert, Play and Meeting. I am in a separate Ticket class for sale to a respective "event." In Ticket I need to getPrice() which starts at 10 and needs to be multiplied by a price factor when an event (that have different price factors) is booked.

I have tried simply calling a static PRICE that is the base price for a ticket and called event.getPriceFactor() which returns null and I obviously can instantiate this event in 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;
    }

I am expecting Ticket.PRICE (10.) * event.getPriceFactor() (1.0) to return 10, but it returns null.

Grimmpier :

it looks like you never assign the event to the ticket attribute in your ticket constructor, therefore being a null val and unable to return a price factor. try assigning it and see if that works

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=164524&siteId=1