导入Ticket类问题(java)

时间:2011-08-27 15:05:25

标签: 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 个答案:

答案 0 :(得分:2)

当我在Eclipse下尝试时,这会正确编译。

问题不是来自您的代码。我认为它来自您的环境设置。源路径不正确或您的Ticket.java未位于正确的目录中(此处应为[src path]/javacw)。