为列车预约系统设计一个类

bkhjykvo  于 2022-09-18  发布在  Java
关注(0)|答案(2)|浏览(177)

我是面向对象设计的新手,还在学习过程中。我为火车预订系统设计了一个类,但我不确定如何建模主预订类。
这就是我的设计外观:

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);
}

我想知道主保留类应该如何访问其他类的方法和成员。
提前谢谢。

uqdfh47h

uqdfh47h1#

在前一种方法中,ReservationSystem类是abstract,即它们不能被示例化,但应该被子类化以访问方法。如果您只有一种预订系统,那么没有必要为它创建一个抽象类。除此之外,我看不出您为ReservationSystem类指定的2个备选方案之间有任何区别。
谈到第二个问题,
我想知道主保留类应该如何访问其他类的方法和成员
TrainStationTrainTicketclasses应公开公共访问器(getter和setter)和访问其数据的方法。由于您尚未说明是否使用数据库来存储预订信息(如预订的机票等),因此您需要将预订信息保存在ReservationSystem类本身中,即应使用Map这样的数据结构来存储预订的机票(Ticket.Id将是最有可能的键)、取消的机票等。对bookTicket()的调用会将票据添加到Map,而cancelTicket()将票据从Map中删除。
此外,ReservationSystem还需要维护与每个示例相关的有效列车和车站列表。与系统的所有交互都是通过ReservationSystem类的示例进行的。

368yc8dk

368yc8dk2#

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);
            }
        });
    }

相关问题