2011-08-27 67 views
-1

我的程序不會找到我創建的票證類。似乎不能解決我做錯了什麼?謝謝導入票證類問題(java)

package javacw; 

/* 
* 
* @Author Christopher Kempster; 
* 
*/ 
import java.awt.event.*; 
import java.awt.*; 
import javax.swing.*; 


public class Main extends JFrame implements ActionListener { 


    public JButton buyButton, viewSeatsButton, clearSeatsButton; 
    String[] showTimes = {"1pm", "3pm", "5pm", "7pm", "9pm"}; 
    String[] ageCategories = {"Adult", "Child", "OAP"}; 
    String[] seatingSections = {"Left", "Middle", "Right"}; 
    public JComboBox times, categories, sections; 
    public JTextField numberOfTickets; 
    public Ticket ticket; 
    public int price; 


    public static void main(String args[]) { 

     Main dashboard = new Main(); 

     dashboard.setVisible(true); 



    } 

    public void actionPerformed(ActionEvent e) { 
     Object item = times.getSelectedItem(); 
     String stringTimes = (String)item; 

     Object item1 = categories.getSelectedItem(); 
     String stringCategories = (String)item1; 

     Object item2 = sections.getSelectedItem(); 
     String stringSections = (String)item2; 

     String text = numberOfTickets.getText(); 

     int ii = Integer.parseInt(text); 

     if(e.getSource() == buyButton) { 
      buyTickets(stringTimes, stringSections, ii, stringCategories); 
     } 
     else if(e.getSource() == viewSeatsButton) { 
      showSeats(stringTimes, stringSections); 
     } 
     else if(e.getSource() == clearSeatsButton) { 
      clearSection(stringTimes, stringSections); 
     } 

    } 

    public Main() { 
     ticket = new Ticket(); 
     JPanel panel = new JPanel(); 



     FlowLayout flow = new FlowLayout(); 

     panel.setLayout(flow); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     setSize(300, 500); 

     buyButton = new JButton("Buy tickets"); 
     viewSeatsButton = new JButton("View seats"); 
     clearSeatsButton = new JButton("Clear seats"); 

     times = new JComboBox(showTimes); 
     categories = new JComboBox(ageCategories); 
     sections = new JComboBox(seatingSections); 

     JLabel label = new JLabel("Number of tickets:"); 

     numberOfTickets = new JTextField(5); 

     buyButton.addActionListener(this); 
     viewSeatsButton.addActionListener(this); 
     clearSeatsButton.addActionListener(this); 

     panel.add(times); 
     panel.add(categories); 
     panel.add(sections); 

     panel.add(label); 

     panel.add(numberOfTickets); 

     panel.add(buyButton); 
     panel.add(viewSeatsButton); 
     panel.add(clearSeatsButton); 



     Container cp = getContentPane(); 
     cp.add(panel, BorderLayout.CENTER); 
     cp.setBackground(Color.red); 

    } 

    public void showSeats(String a, String b) { 
     JOptionPane.showMessageDialog(this, "There are " + ticket.seatsLeft(a, b) + " seats left in the " + b + " at " + a + "."); 
    } 

    public void clearSection(String a, String b) { 
     ticket.clearSeats(a, b); 
     JOptionPane.showMessageDialog(this, "There are now " + ticket.seatsLeft(a, b) + " tickets left for this section."); 
    } 

    public void buyTickets(String a, String b, int i, String c) { 
     if(c.equals("Adult")) { 
      price = 5; 
     } 
     else if(c.equals("Child")) { 
      price = 2; 
     } 
     else if(c.equals("OAP")) { 
      price = 2; 
     } 

     price = price * i; 

     if(ticket.buyTix(a, b, i)) { 

      JOptionPane.showMessageDialog(this, "Tickets have been bought!\nThere are " + ticket.seatsLeft(a, b) + " seats left in this section.\nPrice of tickets bought: £" + price + "."); 

     } 
     else { 

      JOptionPane.showMessageDialog(this, "Sorry, there are just " + ticket.seatsLeft(a, b) + " tickets left for this section."); 

     } 
    } 

} 


Ticket.java 

package javacw; 
/* 
* 
* @Author Christopher Kempster; 
* 
*/ 

public class Ticket { 

    int c, seatsLeft; 
    public int[] seating = {12, 40, 12, 12, 40, 12, 12, 40, 12, 12, 40, 12, 12, 40, 12}; 
    public int[] seating2 = {12, 40, 12, 12, 40, 12, 12, 40, 12, 12, 40, 12, 12, 40, 12}; 
    public Ticket() { 

    } 

    public void clearSeats(String a, String b) { 
     seating[convertArrayPointer(a, b)] = seating2[convertArrayPointer(a, b)]; 
    } 

    public boolean buyTix(String a, String b, int i) { 

     if(seatsLeft(a, b) >= i) { 
      seating[convertArrayPointer(a, b)] -= i; 
      return true; 
     } 
     else { 
      return false; 
     } 

    } 

    public int seatsLeft(String a, String b) { 

     seatsLeft = seating[convertArrayPointer(a, b)]; 

     return seatsLeft; 
    } 

    public int convertArrayPointer(String a, String b) { 

     if(a.equals("1pm") && b.equals("Left")) { 
      c = 0; 
     } 
     else if(a.equals("1pm") && b.equals("Middle")) { 
      c = 1; 
     } 
     else if(a.equals("1pm") && b.equals("Right")) { 
      c = 2; 
     } 

     else if(a.equals("3pm") && b.equals("Left")) { 
      c = 3; 
     } 
     else if(a.equals("3pm") && b.equals("Middle")) { 
      c = 4; 
     } 
     else if(a.equals("3pm") && b.equals("Right")) { 
      c = 5; 
     } 

     else if(a.equals("5pm") && b.equals("Left")) { 
      c = 6; 
     } 
     else if(a.equals("5pm") && b.equals("Middle")) { 
      c = 7; 
     } 
     else if(a.equals("5pm") && b.equals("Right")) { 
      c = 8; 
     } 

     else if(a.equals("7pm") && b.equals("Left")) { 
      c = 9; 
     } 
     else if(a.equals("7pm") && b.equals("Middle")) { 
      c = 10; 
     } 
     else if(a.equals("7pm") && b.equals("Right")) { 
      c = 11; 
     } 

     else if(a.equals("9pm") && b.equals("Left")) { 
      c = 12; 
     } 
     else if(a.equals("9pm") && b.equals("Middle")) { 
      c = 13; 
     } 
     else if(a.equals("9pm") && b.equals("Right")) { 
      c = 14; 
     } 
     return c; 
    } 

} 
+1

不要發佈您的代碼,發佈您的錯誤消息。 – bdares

+0

事實上,我認爲應該發佈 – Trefex

+0

他應該發佈**必要的**部分代碼,以顯示它是如何完成的,而不是整個程序。 –

回答

2

這個編譯正確,當我嘗試在Eclipse下。

該問題不是來自您的代碼。我認爲它來自您的環境設置。源路徑不正確或您的Ticket.java未位於正確的目錄中(這裏應該是[src path]/javacw)。