2013-04-11 96 views
-3

如何计算购买的杂货总价?目前,我只能计算其中一个杂货店的数量。计算总价

下面是我所使用的类:

import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 
import java.text.DecimalFormat; 

/** 
    Long Distance Calls 
*/ 

public class GroceryMain extends JFrame 
{ 
    private RatePanel ratePanel;  // A panel for rates 
    private QuantityPanel quantityPanel; // A panel for minutes 
    private JPanel buttonPanel;   // A panel for the buttons 
    private JButton calcButton;   // Calculates everything 
    private JButton exitButton;   // Exits the application 

    /** 
     Constructor 
    */ 

    public GroceryMain() 
    { 
     // Display a title. 
     setTitle("Grocery Shop"); 

     // Specify what happens when the close button is clicked. 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     // Create a RatePanel object. 
     ratePanel = new RatePanel(); 

     // Create a MinutesPanel object. 
     quantityPanel = new QuantityPanel(); 

     // Build the panel that contains the buttons. 
     buildButtonPanel(); 

     // Add the panels to the content pane. 
     add(ratePanel, BorderLayout.WEST); 
     add(quantityPanel, BorderLayout.EAST); 
     add(buttonPanel, BorderLayout.SOUTH); 

     // Pack and display the window. 
     pack(); 
     setVisible(true); 
    } 

    /** 
     The buildButtonPanel method creates a panel containing 
     buttons. 
    */ 

    private void buildButtonPanel() 
    { 
     // Create a button to calculate the charges. 
     calcButton = new JButton("Calculate Charges"); 

     // Add an action listener to the button. 
     calcButton.addActionListener(new CalcButtonListener()); 

     // Create a button to exit the application. 
     exitButton = new JButton("Exit"); 

     // Add an action listener to the button. 
     exitButton.addActionListener(new ExitButtonListener()); 

     // Put the buttons in their own panel. 
     buttonPanel = new JPanel(); 
     buttonPanel.add(calcButton); 
     buttonPanel.add(exitButton); 
    } 

    /** 
     CalcButtonListener is an action listener class for the 
     calcButton component. 
    */ 

    private class CalcButtonListener implements ActionListener 
    { 
     /** 
     actionPerformed method 
     @param e An ActionEvent object. 
     */ 

     public void actionPerformed(ActionEvent e) 
     { 
     double rate;   // Applicable rate 
     double totalCharges; // Total charges 

     // Create a DecimalFormat object to format output. 
     DecimalFormat dollar = new DecimalFormat("#,##0.00"); 

     // Get the applicable rate. 
     rate = ratePanel.getRate(); 

     // Get the total charges 
     totalCharges = quantityPanel.getCharges(rate); 

     // Display the message. 
     JOptionPane.showMessageDialog(null, "Total Charges: £" + 
              dollar.format(totalCharges)); 
     } 
    } // End of inner class 

    /** 
     ExitButtonListener is an action listener class for the 
     exitButton component. 
    */ 

    private class ExitButtonListener implements ActionListener 
    { 
     /** 
     actionPerformed method 
     @param e An ActionEvent object. 
     */ 

     public void actionPerformed(ActionEvent e) 
     { 
     System.exit(0); 
     } 
    } // End of inner class 

    /** 
     The main method creates an instance of the LongDistance 
     class, causing it to display its window. 
    */ 

    public static void main(String[] args) 
    { 
     GroceryMain ld = new GroceryMain(); 
    } 
} 


    import java.awt.*; 
import javax.swing.*; 

/** 
    MinutesPanel class 
    Long Distance Calls 
*/ 

public class QuantityPanel extends JPanel 
{ 
    private JTextField quantity; // To get minutes 
    private JTextField quantity2; // To get minutes 
    private JTextField baked_beans_JT;  // JTextField box for baked_beans 
    private JTextField Cornflakes_JT;  // JTextField box for cornflakes 
    private JTextField Sugar_JT; // JTextField box for sugar box 
    private JTextField Tea_Bags_JT; // JTextField box for tea bag 
    private JTextField Instant_Coffee_JT; // JTextField box for Instant_Coffee_Box 
    private JTextField Bread_JT;  // JTextField box for bread box 
    private JTextField Sausage_JT; // JTextField box for sausage box 
    private JTextField egg_JT; // JTextField box for egg box 
    private JTextField milk_JT; // JTextField box for milk 
    private JTextField potatoes_JT; // JTextField box for potatoes 

    /** 
     Constructor 
    */ 

    public QuantityPanel() 
    { 
     // Create a label prompting the user and a text field. 
     //JLabel minutesMsg = new JLabel("Quantity:"); 
     quantity = new JTextField(5); 

     quantity2 = new JTextField(5); 
     baked_beans_JT = new JTextField(5); 
     Cornflakes_JT = new JTextField(5); 
     Sugar_JT = new JTextField(5); 
     Tea_Bags_JT = new JTextField(5); 
     Instant_Coffee_JT = new JTextField(5); 
     Bread_JT = new JTextField(5); 
     Sausage_JT = new JTextField(5); 
     egg_JT = new JTextField(5); 
     milk_JT = new JTextField(5); 
     potatoes_JT = new JTextField(5); 


     //initialize text field to 0 
     baked_beans_JT.setText("0"); 
     Cornflakes_JT.setText("0"); 
     Sugar_JT.setText("0"); 
     Tea_Bags_JT.setText("0"); 
     Instant_Coffee_JT.setText("0"); 
     Bread_JT.setText("0"); 
     Sausage_JT.setText("0"); 
     egg_JT.setText("0"); 
     milk_JT.setText("0"); 
     potatoes_JT.setText("0"); 


     // Create a GridLayout manager. 
     setLayout(new GridLayout(15, 1)); 

     // Create a border. 
     setBorder(BorderFactory.createTitledBorder("QTY")); 

     // Add the labels and text fields to this panel. 
     //add(minutesMsg); 
     //add(quantity); 
    // add(quantity2); 
     add(baked_beans_JT); 
     add(Cornflakes_JT); 
     add(Sugar_JT); 
     add(Tea_Bags_JT); 
     add(Instant_Coffee_JT); 
     add(Bread_JT); 
     add(Sausage_JT); 
     add(egg_JT); 
     add(milk_JT); 
     add(potatoes_JT); 

    } 

    /** 
     The getCharges method uses the specified rate to calculate 
     the charges for the number of minutes entered. 
     @param rate The per-minute rate. 
     @return The charges for the number of minutes used. 
    */ 

    public double getCharges(double rate) 
    { 
     double charges = Double.parseDouble(baked_beans_JT.getText()) * rate; 

     return charges; 
    } 

} 


    import java.awt.*; 
import javax.swing.*; 
import java.text.DecimalFormat; 

/** 
    RatePanel class 
    Long Distance Calls 
*/ 

public class RatePanel extends JPanel 
{ 
    // Named constants for rates 
    private final double BAKED_BEANS= 0.35; 
    private final double CORNFLAKES = 1.75; 
    private final double SUGAR = 0.75; 
    private final double TEA_BAGS = 1.15; 
    private final double INSTANT_COFFEE = 2.50; 
    private final double BREAD = 1.25; 
    private final double SAUSAGES = 1.30; 
    private final double EGGS = 0.75; 
    private final double MILK = 0.65; 
    private final double POTATOES = 2.00; 

    private JCheckBox bakedbeans; // Radio button for daytime rate 
    private JCheckBox cornflakes; // Radio button for evening rate 
    private JCheckBox sugar; // Radio button for off peak rate 
    private JCheckBox teabags; // Radio button for off peak rate 
    private JCheckBox instantcoffee; // Radio button for off peak rate 
    private JCheckBox bread; // Radio button for off peak rate 
    private JCheckBox sausages; // Radio button for off peak rate 
    private JCheckBox eggs; // Radio button for off peak rate 
    private JCheckBox milk; // Radio button for off peak rate 
    private JCheckBox potatoes; // Radio button for off peak rate 

    //private ButtonGroup bg;  // Radio button group 

    /** 
     Constructor 
    */ 

    public RatePanel() 
    { 
     // Create a DecimalFormat object. 
     DecimalFormat dollar = new DecimalFormat("#,##0.00"); 

     // Create the check boxes. 
     bakedbeans = new JCheckBox("Bakedbeans (£" + 
         dollar.format(BAKED_BEANS) + " per packet)"); 
     cornflakes = new JCheckBox("Cornflakes (£" + 
         dollar.format(CORNFLAKES) + " per packet)"); 
     sugar = new JCheckBox("Sugar (£" + 
         dollar.format(SUGAR) + " per packet)"); 

     teabags = new JCheckBox("Teabags (£" + 
       dollar.format(TEA_BAGS) + " per item)"); 
     instantcoffee = new JCheckBox("Instantcoffee (£" + 
       dollar.format(INSTANT_COFFEE) + " per packet)"); 
     bread = new JCheckBox("Bread (£" + 
       dollar.format(BREAD) + " per packet)"); 
     sausages = new JCheckBox("Sausages (£" + 
       dollar.format(SAUSAGES) + " per packet)"); 
     eggs = new JCheckBox("Eggs (£" + 
       dollar.format(EGGS) + " per packet)"); 
     milk = new JCheckBox("Milk (£" + 
       dollar.format(MILK) + " per packet)"); 
     potatoes = new JCheckBox("Potatoes (£" + 
       dollar.format(POTATOES) + " per packet)"); 

     // Create a GridLayout manager. 
     setLayout(new GridLayout(15, 12)); 

     // Create a border. 
     setBorder(BorderFactory.createTitledBorder("Select a Category")); 

    // Add the check boxes to this panel. 
     add(bakedbeans); 
     add(cornflakes); 
     add(sugar); 
     add(teabags); 
     add(instantcoffee); 
     add(bread); 
     add(sausages); 
     add(eggs); 
     add(milk); 
     add(potatoes); 
    } 

    /** 
     The getRate method returns the rate for the selected 
     rate category. 
     @return One of the constants DAYTIME_RATE, EVENING_RATE, or 
       OFF_PEAK_RATE. 
    */ 

    public double getRate() 
    { 
     double rate = 0.0; 

     if (bakedbeans.isSelected()) 
     rate += BAKED_BEANS; 
     if (cornflakes.isSelected()) 
     rate = CORNFLAKES; 
     else if (sugar.isSelected()) 
     rate += SUGAR; 
     else if (teabags.isSelected()) 
      rate += TEA_BAGS; 
     else if (instantcoffee.isSelected()) 
      rate += INSTANT_COFFEE; 
     else if (bread.isSelected()) 
      rate += BREAD; 
     else if (sausages.isSelected()) 
      rate += SAUSAGES; 
     else if (eggs.isSelected()) 
      rate += EGGS; 
     else if (milk.isSelected()) 
      rate += MILK; 
     else if (potatoes.isSelected()) 
      rate += POTATOES; 

     return rate; 
    } 
} 

我希望能够计算杂货的总价格,当用户选择的复选框,其数量多..

+3

请仅发布相关部分。 (例如,对于这个问题,我们并不需要知道关于按钮的所有信息) – Maroun 2013-04-11 15:36:22

回答

4

试试if if if if if else else if。

if - else如果满足条件,则执行相应的语句,并且不评估其余条件。

如果-if将评估每个条件。

public double getRate() 
{ 
    double rate = 0.0; 

    if (bakedbeans.isSelected()) 
    rate += BAKED_BEANS; 
    if (cornflakes.isSelected()) 
    rate += CORNFLAKES; 
    if (sugar.isSelected()) 
    rate += SUGAR; 
    if (teabags.isSelected()) 
     rate += TEA_BAGS; 
    if (instantcoffee.isSelected()) 
     rate += INSTANT_COFFEE; 
    if (bread.isSelected()) 
     rate += BREAD; 
    if (sausages.isSelected()) 
     rate += SAUSAGES; 
    if (eggs.isSelected()) 
     rate += EGGS; 
    if (milk.isSelected()) 
     rate += MILK; 
    if (potatoes.isSelected()) 
     rate += POTATOES; 

    return rate; 
} 
+0

Bug alert:'rate = CORNFLAKES;'。但这并不能解决任何问题 - 您现在将所有选定商品的价格乘以您想要的烘焙豆的数量。 – 2013-04-11 15:48:40

+0

@JacobRaihle谢谢。我不明白你的意思。 – 2013-04-11 15:56:22

+0

看我的回答:) – 2013-04-11 16:30:30

0

据我所知,getCharges是用来返回所选项目的总成本。当前的实现是:

public double getCharges(double rate) 
{ 
    double charges = Double.parseDouble(baked_beans_JT.getText()) * rate; 

    return charges; 
} 

不幸的是,你不能用烘豆的选择的号码来预测还有什么顾客是买:)

有几种方法,这可能是固定的,我害怕他们都需要修改代码中的其他地方。我会建议看看数组的使用。如果您想避免这种情况,你必须做同样的事情到这一点:

double charges = 0; 
charges += Integer.parseInt(baked_beans_JT.getText()) * BAKED_BEANS; 
charges += Integer.parseInt(cornflakes_JT.getText()) * CORNFLAKES; 
... 

由于将不再被需要的getRate方法,你能适应这一点,节省一些的复制粘贴。

但是,真的,看看数组或集合。没有他们,你将面临令人沮丧的未来。