2012-02-14 4 views
1

저는 Object Oriented Design을 처음 사용하면서도 여전히 학습 과정에 있습니다. 전철 예약 시스템에 대한 수업을 설계했지만 주 예약 클래스를 모델링하는 방법에 대해서는 잘 모릅니다.기차 예약 시스템 용 클래스 설계

이 내 디자인 모습입니다 :

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


내가 방법을 알고 싶어요해야 기본 예약 클래스 접근 방법과 다른 클래스의 멤버.

미리 감사드립니다.

+3

이것은 C++가 아닙니다. – Pubby

+2

은 http://codereview.stackexchange.com/에 더 적합합니다. – Sikorski

답변

0

이전의 접근 방식에서 ReservationSystem 클래스는 abstract입니다. 즉, 인스턴스화 할 수 없지만 메소드에 액세스하려면 서브 클래 싱해야합니다. 한 가지 종류의 예약 시스템 만 있다면, 그것에 대한 추상 클래스를 작성할 필요가 없습니다. 그 외에는 ReservationSystem 클래스에 대해 지정한 2 가지 대안간에 차이점이 없습니다. 두 번째 질문에 오는

,

내가 주 예약 클래스 액세스가 다른 클래스

Train, StationTrainTicket 클래스

의 방법과 회원들이 대중에 노출해야하는 방법을 알고 싶어요 접근 자 (getter 및 setter) 및 데이터에 액세스하는 메소드 데이터베이스를 사용하여 예약 정보 (예 : 예약 된 티켓 등)를 저장할지 여부를 지정하지 않았으므로 ReservationSystem 클래스에 예약 정보를 유지해야합니다. 즉, Map과 같은 데이터 구조가 있어야합니다. 예약 티켓 ( Ticket.Id)이 가장 가능성있는 열쇠 일 것입니다. bookTicket()을 호출하면 Map에 티켓이 추가되고 cancelTicket()Map에서 티켓을 제거합니다.

또한 ReservationSystem은 유효한 기차 목록과 각 인스턴스와 연결된 스테이션을 유지해야합니다. 시스템과의 모든 상호 작용은 ReservationSystem 클래스의 인스턴스를 통해 이루어집니다.

-1
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); 
      } 
     }); 
    } 
+0

설명이없는 코드 덤프는별로 도움이되지 않습니다. 특히 질문이 디자인과 관련이있는 경우에는 그렇지 않습니다. – Robert

+0

또한이 질문은 GUI 디자인이 아니라는 점에 유의하십시오. GUI 클래스는 도메인 객체를 오염 시켜서는 안됩니다. – Robert