您如何计算所购杂货的总价?目前,我只计算其中一个杂货店的数量。
以下是我使用的课程:
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;
}
}
我希望能够在用户选择多个复选框及其数量时计算杂货的总价格。
答案 0 :(得分:4)
尝试if - if而不是if-else if。
if - else如果满足条件,则执行适当的语句并且不评估其余条件。
if -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;
}
答案 1 :(得分: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
方法,您可以对其进行调整并保存一些复制粘贴。
但实际上,请查看数组或集合。如果没有他们,你将面临一个令人沮丧的未来。