gpt4 book ai didi

java - 继承和关联建模,怎么做?

转载 作者:行者123 更新时间:2023-12-01 04:38:03 27 4
gpt4 key购买 nike

我想在 Java 中模拟以下情况,但我陷入困境:

enter image description here

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

我计划像下面的测试代码一样调用我的类,但我不知道如何对上述情况进行建模:

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拼写错误,这只是主程序的草稿

最佳答案

我认为你可以稍微不同地处理这个问题

客户应该能够进行预订,并且应该保留他们已进行的预订的引用

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())
}
}

但“预订”实际上是客户根据交通方式时间表进行的操作

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

public Transport getTransport()
{
return transport;
}
}

那么交通工具可以是飞机或公共(public)汽车,无论哪种方式都可以容纳乘客

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);
}
}

你会使用它

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);
}

关于java - 继承和关联建模,怎么做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17033259/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com