gpt4 book ai didi

java - 为火车预订系统设计一个类

转载 作者:行者123 更新时间:2023-12-01 06:24:44 25 4
gpt4 key购买 nike

我是面向对象设计的新手,并且仍在学习它的过程中。
我为火车预订系统设计了一个类(class),但我不确定如何对主要预订类(class)进行建模。

这就是我的设计的外观:

public class Station {
private int stationID;
private String stationName;

// Getters and Setters
}

public class Train {
private int trainID;
private String trainName;
private Map<Station, Double> trainStationsWithFares;

public Train(int ID, String trainName, Station[] stations) {
// Initialize ID and name and create a hashmap with all stations and
// zero fare initially for all stations.
....
trainStationsWithFares = new HashMap<Station, Double>();
for(Station s : stations) trainStationsWithFares.put(s, new Double(0.0));
}

// Getters and Setters (including methods to add new stations with fares and
// update fares of existing stations
}

public class Passenger {
private String Name;
private int id;
private int age;
private static final enum { Male, Female } gender;
}

public class TicketDetails {
private Train t;
private Station from;
private Station to;
private Passenger passenger;

// Getters and Setters
}

public class TrainTicket {
private int ID;
private TicketDetails ticketDetails;
private Double fare;

public TrainTicket(TicketDetails ticketDetails)
throws InvalidTrainException, InvalidFromStationException,
InvalidToStationException {
...
calculateFare();
}

// Calculates fare based on Train and from and to Stations and taxes, etc.
private void calculateFare() {
this.fare = ...
}
}

// Assuming card payment only for online reservation system for simplicity.
// Design can be modified and enhanced suitably.
public class PaymentDetails {
private String cardNumber;
private String cardExpirationMonth;
private String cardExpirationYear;
private String cardCVV;

// Getters and Setters
}

我的 问题 : 我的预订类应该是这样的吗?
如果是这样,该类中的方法将如何访问其他类的对象,例如 TrainTicket , PaymentDetails , ETC。?
public abstract class ReservationSystem {

// Purchase train ticket by providing ticket details and payment details.
public abstract TrainTicket purchaseTicket(TicketDetails ticketDetails,
PaymentDetails paymentDetails);

// Cancel existing train ticket by providing ticket reference and payment details
// for cancellation charges.
public abstract boolean cancelTicket(TrainTicket tt, PaymentDetails details);

// Modify existing train ticket by providing existing ticket reference,new ticket
// details and payment details for any additional charge or modify charges.
public abstract TrainTicket modifyTicket(TrainTicket existingTicket,
TicketDetails newTicketDetails, PaymentDetails paymentDetails) {
}
}

或者它应该看起来像这样:
public class ReservationSystem{
private Map<trainId, Train> trains;
private Ticketdetails ticketdetails;
private PaymentDetails paymentDetails;
private TrainTicket tt;

public vector<Train> getTrain(TicketDetails ticketdetails);
public TrainTicket purchaseTicket(TicketDetails ticketDetails,
PaymentDetails paymentDetails);
public boolean cancelTicket(TrainTicket tt, PaymentDetails details);
public TrainTicket modifyTicket(TrainTicket existingTicket,
TicketDetails newTicketDetails, PaymentDetails paymentDetails);
}

我想知道主 Reservation 类应该如何访问其他类的方法和成员。

提前致谢。

最佳答案

在前一种方法中,ReservationSystem类(class)是 abstract ,即它们不能被实例化,但应该被子类化以访问方法。如果您只有一种预订系统,那么为它创建抽象类是没有意义的。除此之外,我看不出您为 ReservationSystem 指定的 2 个备选方案之间有任何区别。类(class)。

来到第二个问题,

I want to know how should the main Reservation class access the methods and members of other classes


Train , StationTrainTicket类应该公开公共(public)访问器(getter 和 setter)和访问其数据的方法。由于您没有说明是否使用数据库来存储预订信息(如预订的门票等),因此您需要将预订信息保存在 ReservationSystem 中。类本身,即应该有一个像 Map 这样的数据结构存储已预订的门票( Ticket.Id 最有可能是键)、已取消的门票等。调用 bookTicket()会将票添加到 MapcancelTicket()Map 中删除票证.

此外, ReservationSystem还需要维护与每个实例关联的有效火车和车站的列表。与系统的所有交互都是通过 ReservationSystem 的实例进行的。类(class)。

关于java - 为火车预订系统设计一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9273357/

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