我是面向对象设计的新手,还在学习过程中。我为火车预订系统设计了一个类,但我不确定如何建模主预订类。
这就是我的设计外观:
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
等)的对象。?
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);
}
我想知道主保留类应该如何访问其他类的方法和成员。
提前谢谢。
2条答案
按热度按时间uqdfh47h1#
在前一种方法中,
ReservationSystem
类是abstract,即它们不能被示例化,但应该被子类化以访问方法。如果您只有一种预订系统,那么没有必要为它创建一个抽象类。除此之外,我看不出您为ReservationSystem
类指定的2个备选方案之间有任何区别。谈到第二个问题,
我想知道主保留类应该如何访问其他类的方法和成员
Train
、Station
和TrainTicket
classes应公开公共访问器(getter和setter)和访问其数据的方法。由于您尚未说明是否使用数据库来存储预订信息(如预订的机票等),因此您需要将预订信息保存在ReservationSystem
类本身中,即应使用Map
这样的数据结构来存储预订的机票(Ticket.Id
将是最有可能的键)、取消的机票等。对bookTicket()
的调用会将票据添加到Map
,而cancelTicket()
将票据从Map
中删除。此外,
ReservationSystem
还需要维护与每个示例相关的有效列车和车站列表。与系统的所有交互都是通过ReservationSystem
类的示例进行的。368yc8dk2#