Luke_i
Luke_i

Reputation: 163

Java Classes Inheritance

I need some assistance in quite a sticky situation. My program is working fine as it is but I was told to change how a class is working. What I have is a Booking Class which extends a Customer Class to get the Customer details. Now I was told that the Booking Class must have a property of type Customer instead of extending the Class. Then in my program I load the Customer as a whole object instead of reading line by line and inserting them into the text boxes. Anyone can help me through this?

public class Booking implements Serializable{
    
    private String flighttime;
    private String flightlocation;
    private String flightfee;
    private boolean car;
    private boolean insurance;
    private Customer customer;

    /**
     * @return the flighttime
     */
    public String getFlighttime() {
        return flighttime;
    }

    /**
     * @param flighttime the flighttime to set
     */
    public void setFlighttime(String flighttime) {
        this.flighttime = flighttime;
    }

    /**
     * @return the flightlocation
     */
    public String getFlightlocation() {
        return flightlocation;
    }

    /**
     * @param flightlocation the flightlocation to set
     */
    public void setFlightlocation(String flightlocation) {
        this.flightlocation = flightlocation;
    }

    /**
     * @return the flightfee
     */
    public String getFlightfee() {
        return flightfee;
    }

    /**
     * @param flightfee the flightfee to set
     */
    public void setFlightfee(String flightfee) {
        this.flightfee = flightfee;
    }

    /**
     * @return the car
     */
    public boolean getCar() {
        return isCar();
    }

    /**
     * @param car the car to set
     */
    public void setCar(boolean car) {
        this.car = car;
    }

    /**
     * @return the car
     */
    public boolean isCar() {
        return car;
    }

    /**
     * @return the insurance
     */
    public boolean isInsurance() {
        return insurance;
    }

    /**
     * @param insurance the insurance to set
     */
    public void setInsurance(boolean insurance) {
        this.insurance = insurance;
    }
    
    public boolean getInsurance() {
        return isInsurance();
    }

    /**
     * @return the customer
     */
    public Customer getCustomer() {
        return customer;
    }

    /**
     * @param customer the customer to set
     */
    public void setCustomer(Customer customer) {
        this.customer = customer;
    }
    
}

Upvotes: 1

Views: 199

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

Then in my program I load the Customer as a whole object instead of reading line by line and inserting them into the text boxes.

  1. Change Booking so that it does not extend Customer since it does not satisfy the "is-a" relationship.
  2. You will still need to get your Customer data and create a Customer object the same way as previously,
  3. but once created, then you could perhaps put your Customer into a Customer collection such as an ArrayList,
  4. and even store this collection in a database or file,
  5. and all the Customers could be perhaps then displayed in your GUI as a JComboBox if you need to select one Customer from many.
  6. Since now that Booking does not extend Customer, you would add a Customer instance into your Booking object the same as any other type of field: through either a constructor parameter public Booking(Customer customer, ...<other params>) or via a setter method, public void setCustomer(Customer customer) {...}, or both.

Upvotes: 2

Related Questions