简体   繁体   中英

inheritance and association modelling, how to do it?

I want to model the following situation in Java, but I am stuck:

在此处输入图片说明

Specifically the part related to Customer, Booking, Flight Booking and Bus Booking. I would like to have an array of Customer objects with each one having their own Booking list (that can be a flight book or a bus book).

I was planning to call my classes like in the following test code, but I do not have an idea how I can model the aforementioned situation:

public class Test
       public static void main(String argv[])
              Customer customerList      //this will hold an array of objects of clients
              Client cli1=new Client("smith","jhonson","2341")
              Client cli2=new Client("tor", "jhon","1234")
              customerList.addClient(cli1)
              customerList.addClient(cli2)
              FlightBook fl1=new FlightBook("Canada","12/July")
              BusBook bus1=new BusBook("Montreal","15/July")
              Booking bookList       //holds an array of flight and bus bookings
              bookList.addBook(fl1)
              bookList.addBook(bus1)

now I am stuck here, how I can assign the fl1 and bus1 to the first customer (cli1)? So that I know that the client 1 has make a flight booking (fl1) and a bus booking (bus1). I ask this because I would like to iterate later over all my clients and see which bookings each of one has made.

Thanks and please do not consider the java typos, it is only a draft of the main program

I think you could approach this slightly differently

a customer should be able to make bookings and should hold a refference to the bookings they have made

class Customer
{
    private String fname;
    private String lname;
    private String address;
    private Set<Schedule> bookings;

    public Booking makeBooking(Schedule booking)
    {
        booking.getTransport().addPassenger(this);

        bookings.add(booking);

        // account.debit(booking.price());
    }

    public void cancelBooking(Schedule booking)
    {
        booking.getTransport().removePassenger(this);

        bookings.remove(booking);

        // account.credit(booking.getPrice())
    }
}

But a 'booking' is actually what the customer makes from a schedule of Transportation modes

class Schedule
{
    private Transport transport;
    private String departsFrom;
    private String arrivesAt;
    private Date departureDate;
    private Date arrivalDate;

    public Transport getTransport()
    {
        return transport;
    }
}

Then a Transport could be a plane or a bus either way it should hold passengers

abstract class Transport
{
    private int id;

    private Set<Customer> passengers;

    public void addPassenger(Customer passenger)
    {
        if (!isFull())
        {
            passengers.add(passenger);
        }
    }

    public void removePassenger(Customer passenger)
    {
        passengers.remove(passenger);
    }
}

and you would use it

public static void main(String[] args)
{
    // take a mode of transport
    Transport plane = new Plane();

    // put it in a schedule
    Schedule schedule = new Schedule(plane, "London", "New York", "2013-01-01", "2013-01-02");

    // let customers make a booking against the schedule
    Customer jo = new Customer("Jo", "Smith");

    Customer al = new Customer("Al", "Bar");

    jo.makeBooking(schedule);

    al.makeBooking(schedule);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM