1

我想在Java中建模以下情况,但我被卡住了:

在此处输入图像描述

特别是与客户、预订、航班预订和巴士预订相关的部分。我想要一组 Customer 对象,每个对象都有自己的预订列表(可以是航班预订或巴士预订)。

我打算像下面的测试代码一样调用我的类,但我不知道如何模拟上述情况:

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)

现在我被困在这里,如何将 fl1 和 bus1 分配给第一个客户(cli1)?这样我就知道客户 1 进行了航班预订 (fl1) 和巴士预订 (bus1)。我问这个是因为我想稍后迭代我所有的客户,看看每个客户都做了哪些预订。

谢谢,请不要考虑java拼写错误,这只是主程序的草稿

4

1 回答 1

0

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);
}
于 2013-06-10T23:20:10.377 回答