我是面向对象设计的新手,仍然在学习它。 我为火车预订系统设计了一个课程,但我不确定如何为主要预订课程建模。
这就是我的设计外观:
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);
}
我想知道主要的Reservation类应该如何访问其他类的方法和成员。
提前致谢。
答案 0 :(得分:0)
在前一种方法中,ReservationSystem
类是abstract,即它们不能被实例化,但应该被子类化以访问方法。如果您只有一种预订系统,那么就没有必要为它创建一个抽象类。除此之外,我没有看到您为ReservationSystem
类指定的2替代方案之间存在任何差异。
来到第二个问题,
我想知道主要的Reservation类应该如何访问其他类的方法和成员
Train
,Station
和TrainTicket
类应公开访问其数据的公共访问者(getter和setter)和方法。由于您尚未表明您是否使用数据库存储预订信息(如预订门票等),您需要在ReservationSystem
类本身中保留预订信息,即应该有一个类似于数据结构的数据结构Map
用于存储预订的门票(Ticket.Id
将是最可能的关键字),取消门票等等。对bookTicket()
的调用会将票证添加到Map
并cancelTicket()
从Map
中删除票证。
此外,ReservationSystem
还需要维护有效列车列表以及与每个实例关联的站点。与系统的所有交互都是通过ReservationSystem
类的实例进行的。
答案 1 :(得分:-2)
import java.util.*;
import javax.swing.JOptionPane;
/*
@Author 12CSE54
@ Date 29.10.14
*/
public class reservation extends javax.swing.JFrame {
Scanner s=new Scanner(System.in);
List arr1=new ArrayList();
Set<Integer> arr= new HashSet<Integer>();
static String aa,bb,cc;
static int ag;
static int pp;
/** Creates new form reservation */
public reservation() {
initComponents();
}
public reservation(String na,int ag,String from,String to)
{
aa=na;
bb=from;
cc=to;
pp=ag;
}
int var,c=0;
int b;
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
b=Integer.parseInt(JOptionPane.showInputDialog(this,"Enter the seat number"));
Iterator<Integer> itr=arr.iterator();
while(itr.hasNext())
{
var=itr.next();
if(b==var)
{
JOptionPane.showMessageDialog(this,"ticket already reserved");
c=0;
break;
}
else
{
c=1;
}
}
if(c==1)
{
JOptionPane.showMessageDialog(this,"Ticket Available Ticket Booked for you");
}
arr.add(b);
JOptionPane.showMessageDialog(this,"booked sucessfully");
}
int[] a=new int[100];
Iterator itr=arr1.iterator();
int flag1=-1,f=0;
int b1;
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
b1=Integer.parseInt(JOptionPane.showInputDialog(this,"Enter the seat number allocated to you"));
Iterator<Integer> itr=arr.iterator();
while(itr.hasNext())
{
var=itr.next();
if(b1==var)
{
f=1;
arr.remove(flag1);
break;
}
else
{
f=0;
}
flag1++;
}
if(f==1)
{
JOptionPane.showMessageDialog(this,"Tickets cancelled");
}
else if(f==0)
{
JOptionPane.showMessageDialog(this,"Tickets is not already reserved");
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
passengersdetails p=new passengersdetails();
p.setVisible(true);
arr.add(1);
arr.add(2);
arr.add(10);
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
jFrame1.setSize(600, 600);
jFrame1.setVisible(true);
jTextField1.setText(String.valueOf(aa));
jTextField2.setText(String.valueOf(pp));
jTextField3.setText(String.valueOf(bb));
jTextField4.setText(String.valueOf(cc));
jTextField5.setText(String.valueOf(b));
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
list i=new list();
i.setVisible(true);
}
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new reservation().setVisible(true);
}
});
}