Поэтому я должен составить программу, в которой пользователь будет вводить, сколько из каждого блюда они заказывали на завтрак, обед и ужин. Затем я должен отобразить сумму, которую они заказали за каждый прием пищи, а также общую стоимость каждого, налоги и т. Д. Поэтому я создал сетку в JFrame для отображения результатов, но у меня продолжают появляться ошибки с некоторыми из моих Jlabels, их числами являются; 8, 9, 10, 13, 14, 15, 18, 19, 35, 40, 45. Могу ли я использовать что-либо кроме JLabel для отображения результатов? Спасибо
import java.awt.*;
import java.util.*;
import javax.swing.JOptionPane;
import javax.swing.*;
public class MealCost extends JFrame
{
// Constants
static Scanner console = new Scanner (System.in);
static final double Sales_Tax = .10;
static final double Breakfast = 5.50;
static final double DiscountB = .10;
static final double Lunch = 9.50;
static final double DiscountL = .15;
static final double Dinner = 16.50;
static final double DiscountD = .08;
public static void main(String[] args)
{
// Variables and their known values
int breakfast = 0;
int lunch = 0;
int dinner = 0;
double tax = 0;
double BreakfastCost = 0;
double Breakfast_D = 0;
double LunchCost = 0;
double Lunch_D = 0;
double DinnerCost = 0;
double Dinner_D = 0;
double total_b = 0;
double total_l = 0;
double total_d = 0;
double total = 0;
double total_due = 0;
char Input;
String InputStr;
String outputMsg;
// This program will display the total cost and discount for each meal of the day.
// Output Message
outputMsg = "This program will prompt you for the total number of meals ordered for each meal time. \n"
+ "Breakfast cost $5.50 per meal, but if 10 or more are ordered there is a 10% discount. \n"
+ "Lunch cost $9.50 per meal, but if 15 or more are ordered there is a 15% discount. \n"
+ "Dinner cost $16.50 per meal, but if 8 or more are ordered there is a 12% discount. \n";
JOptionPane.showMessageDialog(null,outputMsg,"My Favorite Catering Service",
JOptionPane.INFORMATION_MESSAGE);
// User Input
// Ask user if any meals where ordered.
InputStr = JOptionPane.showInputDialog ("Were any meals ordered today (Y)es or (N)o : ");
while ((InputStr.length() > 1) || (InputStr.length() == 0 ))
{
InputStr = JOptionPane.showInputDialog ("Only enter a single characater (Y) or (N) : ");
}
Input = InputStr.charAt(0);
switch (Input)
{
case 'Y':
case 'y':
// Meals for breakfast ordered
InputStr = JOptionPane.showInputDialog ("Enter the number of Breakfast ordered : ");
breakfast = Integer.parseInt(InputStr);
// Meals for lunch ordered
InputStr = JOptionPane.showInputDialog ("Enter the number of Lunch ordered : ");
lunch = Integer.parseInt(InputStr);
// Meals for dinner ordered
InputStr = JOptionPane.showInputDialog ("Enter the number of Dinner ordered : ");
dinner = Integer.parseInt(InputStr);
// Breakfast discount
if (breakfast >= 10)
Breakfast_D = Breakfast * breakfast * DiscountB;
total_b = Breakfast * breakfast - Breakfast_D;
// Lunch discount
if (lunch >= 15)
Lunch_D = Lunch * lunch * DiscountL;
total_l = Lunch * lunch - Lunch_D;
// Dinner discount
if (dinner >= 8)
Dinner_D = Dinner * dinner * DiscountD;
total_d = Dinner * dinner - Dinner_D;
break;
case 'N':
case 'n':
// Output Message
outputMsg = "Thank you for placing your order with us.";
JOptionPane.showMessageDialog(null,outputMsg ,"My Favorite Catering Service",
JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
break;
default:
// Calculations
tax = (total_b + total_l + total_d) * Sales_Tax;
total = total_b + total_l + total_d;
total_due = tax + total;
// output message
outputMsg = Input + " was an invalid response. \n Thank you for ording with us";
JOptionPane.showMessageDialog(null,outputMsg ,"My Favorite Catering Service",
JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
// Results page
JFrame frame = new JFrame("My Favorite Catering Service");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,400);
frame.setLocationRelativeTo(null);
JPanel panel = new JPanel ();
panel.setLayout (new GridLayout(9,5,5,10));
JLabel label1 = new JLabel ("Meal");
JLabel label2 = new JLabel ("Quantity");
JLabel label3 = new JLabel ("Cost");
JLabel label4 = new JLabel ("Discount");
JLabel label5 = new JLabel ("Cost After Discount");
JLabel label6 = new JLabel ("Breakfast");
JLabel label7 = new JLabel ("", breakfast);
JLabel label8 = new JLabel ("$%.2f", Breakfast * breakfast);
JLabel label9 = new JLabel ("$%.2f", Breakfast_D);
JLabel label10 = new JLabel ("$%.2f", total_b);
JLabel label11 = new JLabel ("Lunch");
JLabel label12 = new JLabel ("", lunch);
JLabel label13 = new JLabel ("$%.2f", Lunch * lunch);
JLabel label14 = new JLabel ("$%.2f", Lunch_D);
JLabel label15 = new JLabel ("$%.2f", total_l);
JLabel label16 = new JLabel ("Dinner");
JLabel label17 = new JLabel ("", dinner);
JLabel label18 = new JLabel ("$%.2f", Dinner * dinner);
JLabel label19 = new JLabel ("$%.2f", total_d);
JLabel label20 = new JLabel ("");
JLabel label21 = new JLabel ("");
JLabel label22 = new JLabel ("");
JLabel label23 = new JLabel ("");
JLabel label24 = new JLabel ("");
JLabel label25 = new JLabel ("");
JLabel label26 = new JLabel ("");
JLabel label27 = new JLabel ("");
JLabel label28 = new JLabel ("");
JLabel label29 = new JLabel ("");
JLabel label30 = new JLabel ("");
JLabel label31 = new JLabel ("Total");
JLabel label32 = new JLabel ("");
JLabel label33 = new JLabel ("");
JLabel label34 = new JLabel ("");
JLabel label35 = new JLabel ("$%.2f", total);
JLabel label36 = new JLabel ("Sales Tax");
JLabel label37 = new JLabel ("");
JLabel label38 = new JLabel ("");
JLabel label39 = new JLabel ("");
JLabel label40 = new JLabel ("$%.2f", tax);
JLabel label41 = new JLabel ("Total Due");
JLabel label42 = new JLabel ("");
JLabel label43 = new JLabel ("");
JLabel label44 = new JLabel ("");
JLabel label45 = new JLabel ("$%.2f", total_due);
panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
panel.add(label1);
panel.add(label2);
panel.add(label3);
panel.add(label4);
panel.add(label5);
panel.add(label6);
panel.add(label7);
panel.add(label8);
panel.add(label9);
panel.add(label10);
panel.add(label11);
panel.add(label12);
panel.add(label13);
panel.add(label14);
panel.add(label15);
panel.add(label16);
panel.add(label17);
panel.add(label18);
panel.add(label19);
panel.add(label20);
panel.add(label21);
panel.add(label22);
panel.add(label23);
panel.add(label24);
panel.add(label25);
panel.add(label26);
panel.add(label27);
panel.add(label28);
panel.add(label29);
panel.add(label30);
panel.add(label31);
panel.add(label32);
panel.add(label33);
panel.add(label34);
panel.add(label35);
panel.add(label36);
panel.add(label37);
panel.add(label38);
panel.add(label39);
panel.add(label40);
panel.add(label41);
panel.add(label42);
panel.add(label43);
panel.add(label44);
panel.add(label45);
frame.add(panel);
}
}