我需要帮助,因为我试图让数量有效

时间:2015-03-08 12:46:46

标签: jframe double joptionpane

有人可以帮我解决有关数量的问题,因为它不会显示在我的帐单(JTextArea)中,而且我已经解决了我的打印编写问题

以下代码如下:     ` 所以,我需要每个人在数量问题上的帮助

    package gb.dhaneBonz.ChezDanielle.Main;
    //Chez Danielle Bistro System
    //importations and the heading of this System.
    import java.awt.*;
    import java.awt.event.*;
    import java.io.FileWriter;
    import java.io.PrintWriter;
    import javax.swing.*;
    import javax.swing.event.*;
    //The menuChoicesItems and menuChoicesPrices have worked together 
    //with the bill(JList) in order to display it
    public class ChezDanielle extends JFrame implements ActionListener
        {
        static String[] menuChoicesItems =
            {"Andouillette (pork offal sausage) 120.00",
            "Pot au feu (Lyonnais pot roast) 80.75",
            "Reinsdyrsteik (Reindeer roast) 250.30",
            "Bordeaux Superieur Wine 250.00",
            "Crispy Pata 380.00",
            "Cabernet Sauvignon Wine 180.40",
            "Pain Perdu(French Toast)65.00",
            "French Fries(1 bucket) 55.50",
            "Chicharon 34.00",
            "Coffee(Cappuccino) 40.90",
            "Latte Coffee 35.00",
            "Iced Tea(Bottomless) 40.00",
            "Leche Flan 55.75",
            "Maja Blanca 250.00",
            "Bistek Tagalog 87.00",
            "Buko Pie 67.00",
            "Coq au Vin(Chicken with red wine soup) 180.00",
            "Ice Cream Box 230,00",
            "Baileys Whisky 380.00",
            "Durian Shake(Bottomless) 60.00",
            "Creme Brulee 230.00",};

        static double[] menuChoicesPrices = {120.00, 80.75, 250.30, 250.00, 380.00,
            180.40, 65.00, 55.50, 34.00,
            40.00, 35.00, 40.00, 55.75, 250.00, 87.00, 67.00, 180.00, 230.00, 380.00, 60.00, 230.00};
        private JList menuChoices;
        private JTextArea bill;
        private Container pane;
        public ChezDanielle()
        {
            super("ChezDanielle Bistro Simplice");
            setResizable(false);
            setIconImage(Toolkit.getDefaultToolkit().getImage(ChezDanielle.class.getResource("/Images/Icon.png")));
    //Colours and Layouts of this programme.
            pane = getContentPane();
            pane.setBackground(new Color(255, 20, 147));
            pane.setLayout(new BorderLayout(5, 5));
    //Label placement is on the NORTH and its parameters below.
            JLabel menuChoicesJLabel = new JLabel("Le Menu");
            menuChoicesJLabel.setForeground(Color.WHITE);
            menuChoicesJLabel.setHorizontalAlignment(SwingConstants.LEFT);
            pane.add(menuChoicesJLabel,BorderLayout.NORTH);
            menuChoicesJLabel.setFont(new Font("Segoe UI", Font.BOLD, 20));
    //Menu is on the WEST and its parameters below.
            menuChoices = new JList(menuChoicesItems);
            menuChoices.setForeground(Color.BLACK);
            menuChoices.setBackground(Color.WHITE);
            JScrollPane scrollPane = new JScrollPane (menuChoices);
            pane.add(scrollPane,BorderLayout.WEST);
            menuChoices.setFont(new Font("Segoe UI", Font.BOLD, 14));           
    //Receipt area is on the EAST and its parameters below.
            bill = new JTextArea();
            pane.add(bill,BorderLayout.EAST);
            bill.setFont(new Font("Segoe UI", Font.PLAIN, 14));
    //Button is on the SOUTH and its parameters below in order to
    //make it work.
            JButton button = new JButton("Please Select and Order");
            button.setFont(new Font("Segoe UI", Font.PLAIN, 11));
            setSize(747, 484);
            pane.add(button,BorderLayout.SOUTH);
            button.addActionListener(this);
            setSize(747, 484);
            setVisible(true);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
    //Method for quantity
        private void quantity()
        {
  final double[] quantity = {0,1,2,3,10,20};
                Double.parseDouble(JOptionPane.showInputDialog(null,"Enter Quantity: "+quantity));
        }
    //Method codes to display the order and the total cost.
        private void displayBill()
        {
            int[] listArray = menuChoices.getSelectedIndices();
            double salesTax = 0.065;
            double quantity = 0;
            double tax;
            double subTotal = 0;
            double total;
    //Receipt area's extra parameters.
        bill.setEditable(false);
        bill.setText("");
    //Total and initialisation of the programme.
        for (int index = 0; index < listArray.length; index++)
            subTotal = subTotal
            + menuChoicesPrices,quantity[listArray[index]];
        tax = salesTax * subTotal;
        total = subTotal + tax;
    //This Displays the costs of the order.
        bill.append("---------------------Chez Danielle--------------------------\n");
        bill.append("---------------------Our Branches:--------------------------\n");
        bill.append("------Paris Davao London Edinburgh Oslo Marseille-----------\n");
        bill.append("----Website: www.chezdanielle.co.uk | Phone No: 440-9087----\n\n");
        for (int index = 0; index < listArray.length; index++)
        {
            bill.append(menuChoicesItems,quantity[listArray[index]]+ "\n");
        }
        bill.append("\n");
        bill.append("SUB TOTAL\t\tPHP "
                + String.format("%.2f", subTotal) + "\n");
        bill.append("TAX \t\tPHP "
                + String.format("%.2f", tax) + "\n");
        bill.append("TOTAL \t\tPHP "
                + String.format("%.2f", total) + "\n\n");
        bill.append("Bonjour, Merci - Have a Nice Day\n\n");
    //Resets the receipt array in order to pave way for the new order(Loop).
        menuChoices.clearSelection();
        repaint();
        }
        public void actionPerformed(ActionEvent event)
        {   
            if (event.getActionCommand().equals("Please Select and Order"))         
                quantity();
                outFile();
                displayBill();
        }
        public static void main(String[] args) 
        {
                ChezDanielle alc = new ChezDanielle();      
        }
    //Method for the TextFile   
        private void outFile()
        {
            try{
                PrintWriter printW=new PrintWriter(new FileWriter("Receipt.txt",true));
                printW.println(""+bill.getText());
                printW.close();
            JOptionPane.showMessageDialog(null, new JTextArea("Thank You for Choosing Chez Danielle!"));
            }catch (Exception e) {  
            JOptionPane.showInternalMessageDialog(null, e.getMessage());
            }
        }
    }

关于如何使其发挥作用的任何想法?

1 个答案:

答案 0 :(得分:0)

它已经修好了,已经没事了

package gb.dhaneBonz.ChezDanielle.Main;
//Chez Danielle Bistro System
//importations and the heading of this System.
import java.awt.*;
import java.awt.event.*;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.util.Arrays;
import javax.swing.*;
import javax.swing.event.*;
//The menuChoicesItems and menuChoicesPrices have worked together due to array methods.
//with the bill(JList) in order to display it
public class ChezDanielle extends JFrame implements ActionListener
    {
    static String[] menuChoicesItems =
        {"Andouillette (pork offal sausage) 120.00",
        "Pot au feu (Lyonnais pot roast) 80.75",
        "Reinsdyrsteik (Reindeer roast) 250.30",
        "Bordeaux Superieur Wine 250.00",
        "Crispy Pata 380.00",
        "Cabernet Sauvignon Wine 180.40",
        "Pain Perdu(French Toast)65.00",
        "French Fries(1 bucket) 55.50",
        "Chicharon 34.00",
        "Coffee(Cappuccino) 40.90",
        "Latte Coffee 35.00",
        "Iced Tea(Bottomless) 40.00",
        "Leche Flan 55.75",
        "Maja Blanca 250.00",
        "Bistek Tagalog 87.00",
        "Buko Pie 67.00",
        "Coq au Vin(Chicken with red wine soup) 180.00",
        "Ice Cream Box 230,00",
        "Baileys Whisky 380.00",
        "Durian Shake(Bottomless) 60.00",
        "Creme Brulee 230.00",};

    static double[] menuChoicesPrices = {120.00, 80.75, 250.30, 250.00, 380.00,
        180.40, 65.00, 55.50, 34.00,
        40.90, 35.00, 40.00, 55.75, 250.00, 87.00, 67.00, 180.00, 230.00, 380.00, 60.00, 230.00};
    private JList menuChoices;
    private JTextArea bill;
    private Container pane;
    public ChezDanielle()
    {
        super("ChezDanielle Bistro Simplice");
        setResizable(false);
        setIconImage(Toolkit.getDefaultToolkit().getImage(ChezDanielle.class.getResource("/Images/Icon.png")));
//Colours and Layouts of this programme.
        pane = getContentPane();
        pane.setBackground(new Color(255, 20, 147));
        pane.setLayout(new BorderLayout(5, 5));
// the Label placement is on the NORTH and its parameters below.
        JLabel menuChoicesJLabel = new JLabel("Le Menu");
        menuChoicesJLabel.setForeground(Color.WHITE);
        menuChoicesJLabel.setHorizontalAlignment(SwingConstants.LEFT);
        pane.add(menuChoicesJLabel,BorderLayout.NORTH);
        menuChoicesJLabel.setFont(new Font("Segoe UI", Font.BOLD, 20));
// the menuChoicesItems is on the WEST and its parameters below.
        menuChoices = new JList(menuChoicesItems);
        menuChoices.setForeground(Color.BLACK);
        menuChoices.setBackground(Color.WHITE);
        JScrollPane scrollPane = new JScrollPane (menuChoices);
        pane.add(scrollPane,BorderLayout.WEST);
        menuChoices.setFont(new Font("Segoe UI", Font.BOLD, 14));           
//I've placed the receipt area is on the EAST and its parameters below.
        bill = new JTextArea();
        pane.add(bill,BorderLayout.EAST);
        bill.setFont(new Font("Segoe UI", Font.PLAIN, 14));
//Button is on the SOUTH and its parameters below in order to
//make it work.
        JButton button = new JButton("Please Select and Order");
        button.setFont(new Font("Segoe UI", Font.PLAIN, 11));
        setSize(747, 484);
        pane.add(button,BorderLayout.SOUTH);
        button.addActionListener(this);
        setSize(747, 484);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
//Method codes for quantity 
    private void quantity()
    {

    }
//Method codes to display the order and the total cost.
    private void displayBill()
    {
        JOptionPane.showMessageDialog(null, new JTextArea("Thank You for Choosing Chez Danielle!"));
        int[] quantity = new int[0];
        int choiceLength = menuChoices.getSelectedIndices().length;
        for(int i = 0; i < choiceLength; i++){
//          JOptionPane.showMessageDialog(null, menuChoices.getSelectedValues());
            int qty = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Quantity of  "+menuChoices.getSelectedValues()[i]));
            quantity = Arrays.copyOf(quantity, quantity.length + 1);
            quantity[quantity.length - 1] = qty; 
        }   
        int[] listArray = menuChoices.getSelectedIndices();

        double salesTax = 0.065;
        //int quantity ;
        double tax;
        double subTotal = 0;
        double total;
        double itemTotal;
//Receipt area's extra parameters.
    bill.setEditable(false);
    bill.setText("");
//Total and initialisation of the programme.
    for (int index = 0; index < listArray.length; index++)
    {
        itemTotal = menuChoicesPrices[listArray[index]] * quantity[index];
        subTotal = subTotal + itemTotal;
    }   
    tax = salesTax * subTotal;
    total = subTotal + tax ;
//This Displays the costs of the order.
    bill.append("---------------------Chez Danielle--------------------------\n");
    bill.append("---------------------Our Branches:--------------------------\n");
    bill.append("------Paris Davao London Edinburgh Oslo Marseille-----------\n");
    bill.append("----Website: www.chezdanielle.co.uk | Phone No: 440-9087----\n\n");
    for (int index = 0; index < listArray.length; index++)
    {
        bill.append(menuChoicesItems[listArray[index]]+ " x "+ quantity[index]+" /Order"+"\n");
    }
    bill.append("\n");
    bill.append("SUB TOTAL\t\tPHP "
            + String.format("%.2f", subTotal) + "\n");
    bill.append("TAX \t\tPHP "
            + String.format("%.2f", tax) + "\n");
    bill.append("TOTAL \t\tPHP "
            + String.format("%.2f", total) + "\n\n");
    bill.append("Bonjour, Merci - Have a Nice Day\n\n");
//Resets the receipt array in order to pave way for the new order(Loop).
    menuChoices.clearSelection();
    repaint();
    }
    public void actionPerformed(ActionEvent event)
    {   
        if (event.getActionCommand().equals("Please Select and Order"))         
            quantity();
            outFile();
            displayBill();
    }
    public static void main(String[] args) 
    {
            ChezDanielle alc = new ChezDanielle();      
    }
//Method for the TextFile   
    private void outFile()
    {
        try{
            PrintWriter printW=new PrintWriter(new FileWriter("Receipt.txt",true));
            printW.println(""+bill.getText());
            printW.close();

        }catch (Exception e) {  
        JOptionPane.showInternalMessageDialog(null, e.getMessage());
        }
    }
}