【问题标题】:Designing a class for train reservation system为火车预订系统设计一个类
【发布时间】:2012-02-14 07:46:41
【问题描述】:

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

这就是我的设计的样子:

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
}


我的问题:我的预订舱位应该像这样吗? 如果是这样,该类中的方法将如何访问TrainTicketPaymentDetails等其他类的对象?

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 类应该如何访问其他类的方法和成员。

提前致谢。

【问题讨论】:

标签: java oop


【解决方案1】:

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

关于第二个问题,

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

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

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

【讨论】:

    【解决方案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);
                }
            });
        }
    

    【讨论】:

    • 没有解释的代码转储不是很有帮助,尤其是当问题是关于设计的时候。
    • 另请注意,问题与 GUI 设计无关。 GUI 类不应污染域对象。
    猜你喜欢
    • 2020-07-29
    • 2012-03-27
    • 2018-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    • 1970-01-01
    • 2020-10-17
    相关资源
    最近更新 更多