作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是面向对象设计的新手,并且仍在学习它的过程中。
我为火车预订系统设计了一个类(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);
}
最佳答案
在前一种方法中,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
,
Station
和
TrainTicket
类应该公开公共(public)访问器(getter 和 setter)和访问其数据的方法。由于您没有说明是否使用数据库来存储预订信息(如预订的门票等),因此您需要将预订信息保存在
ReservationSystem
中。类本身,即应该有一个像
Map
这样的数据结构存储已预订的门票(
Ticket.Id
最有可能是键)、已取消的门票等。调用
bookTicket()
会将票添加到
Map
和
cancelTicket()
从
Map
中删除票证.
ReservationSystem
还需要维护与每个实例关联的有效火车和车站的列表。与系统的所有交互都是通过
ReservationSystem
的实例进行的。类(class)。
关于java - 为火车预订系统设计一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9273357/
我从我的用户那里得到反馈,“有时”我的游戏应用程序有一个错误,那就是飞船变得完全无法控制。对此进行调查后,看起来 CoreMotion 报告的态度漂移得非常快(不到一秒),而且它是突然发生的。您最多可
我需要在 map 中的两点之间绘制折线。但首先我必须加载一张只有铁路轨道路线的 map 。 Google Maps API 上可以仅显示铁路轨道。如果是这样的API,否则是否有任何可以使用的替代API
我知道这是一个非常开放的问题,但有谁知道我如何确定用户是否乘坐公共(public)交通工具?理想情况下,我会运行一个后台服务,这样只有乘坐公共(public)交通工具的订阅者才能收到我的提醒。 最佳答
我是一名优秀的程序员,十分优秀!