gpt4 book ai didi

java - 在餐厅 OOP 程序中添加菜单小计

转载 作者:太空宇宙 更新时间:2023-11-04 12:49:32 25 4
gpt4 key购买 nike

好吧,所以我有点陷入这个问题,你需要获取小计并添加它们,但每次我输入它们时,我最终都会得到最后输入的值。

输入示例:1-早餐然后1-59.002-否(返回早餐菜单)3-35.00(应该是 59 + 35)然后退出,然后当我输入现金并找零时,我总是得到 35.00 输入。

package finalsProject;

import java.text.DecimalFormat;
import javax.swing.*;
/* Program Name: Menu Bar
* Program Version: 1.2b
* Programmed By: Lance Cyril L. Luchico
* Modified By: Ivan Aldwin A. Cristobal
*
* This Program enables the user to select from a wide variety
* of food choices using the provided menus in the program.
* The program is created using OOP, and is divided into 4 methods.
* First-Breakfast, Second-Lunch, Third Dinner, and fourth, The Checkout.
*
* This Program has its parts interchanged from two menu programs
* for a better functionality.
*
*/

public class Menu {

static float sub , vat = 0.12f , total , pay , change ;
static int meal , choice , order , add, addorders, comporders ;
static DecimalFormat df=new DecimalFormat("#.00");

public static void showMenu(){


{
meal = Integer.parseInt(JOptionPane.showInputDialog(null,"Mcdonalds"
+ "\nChoose your Meal"
+ "\n[1] Breakfast"
+ "\n[2] Lunch"
+ "\n[3] Dinner"));

if (meal == 1)
Breakfast(meal);
else if (meal == 2)
Lunch(meal);
else if (meal == 3)
Dinner(meal);
}
}
public static void Breakfast(int meal){
choice = Integer.parseInt(JOptionPane.showInputDialog(null,"Breakfast Choices"
+ "\n[1] Hamburger Mcdo \t59.00"
+ "\n[2] Breakfast Deluxe \t99.00"
+ "\n[3] Chicken Muffin \t35.00"
+ "\n[4] Egg McMuffin \t49.00"
+ "\n[5] Apple Pie \t39.00"));

order = Integer.parseInt(JOptionPane.showInputDialog(null,"How many orders?"));


if (choice == 1){
sub = 59 * order;
}
else if (choice == 2){
sub = 99 * order;
}
else if (choice == 3){
sub = 35 * order;
}
else if (choice == 4){
sub = 49 * order;
}
else if (choice == 5){
sub = 39 * order;
}

addorders = Integer.parseInt(JOptionPane.showInputDialog("Any Additional Orders?:"
+ "\n[1]Yes"
+ "\n[2]No"
+ "\nChoice: "));
if(addorders == 1){
showMenu();
}
else if(addorders == 2){
comporders = Integer.parseInt(JOptionPane.showInputDialog("Orders Complete? "
+ "\n[1]Yes"
+ "\n[2]No"));

if(comporders == 1)
Checkout();
if(comporders == 2)
Breakfast(meal);
}
}
public static void Lunch(int meal){
choice = Integer.parseInt(JOptionPane.showInputDialog(null,"Lunch Choices"
+ "\n[1] Chicken McNuggets \t59.00"
+ "\n[2] Double McSpicy \t59.00"
+ "\n[3] French Fries \t99.00"
+ "\n[4] McWings \t79.00"
+ "\n[5] Coke \t29.00"));

order = Integer.parseInt(JOptionPane.showInputDialog(null,"How many orders?"));


if (choice == 1){
sub = 59 * order;
}
else if (choice == 2){
sub = 59 * order;
}
else if (choice == 3){
sub = 99 * order;
}
else if (choice == 4){
sub = 79 * order;
}
else if (choice == 5){
sub = 35 * order;
}
addorders = Integer.parseInt(JOptionPane.showInputDialog("Any Additional Orders?:"
+ "\n[1]Yes"
+ "\n[2]No"
+ "\nChoice: "));
if(addorders == 1){
showMenu();
}
else if(addorders == 2){
comporders = Integer.parseInt(JOptionPane.showInputDialog("Orders Complete? "
+ "\n[1]Yes"
+ "\n[2]No"));

if(comporders == 1)
Checkout();
if(comporders == 2)
Lunch(meal);
}
}
public static void Dinner(int meal){
choice = Integer.parseInt(JOptionPane.showInputDialog("Dinner Choices"
+ "\n[1] Mocha Frappe \t59.00"
+ "\n[2] Garden Side Salad \t29.00"
+ "\n[3] McDouble \t89.00"
+ "\n[4] McSpicy \t99.00"
+ "\n[5] CheeseBurger \t35.00"));

order = Integer.parseInt(JOptionPane.showInputDialog("How many orders?"));

if (choice == 1){
sub = 59 * order;
}
else if (choice == 2){
sub = 29 * order;
}
else if (choice == 3){
sub = 89 * order;
}
else if (choice == 4){
sub = 99 * order;
}
else if (choice == 5){
sub = 35 * order;
}

addorders = Integer.parseInt(JOptionPane.showInputDialog("Any Additional Orders?:"
+ "\n[1]Yes"
+ "\n[2]No"
+ "\nChoice: "));
if(addorders == 1){
showMenu();
}
else if(addorders == 2){
comporders = Integer.parseInt(JOptionPane.showInputDialog("Orders Complete? "
+ "\n[1]Yes"
+ "\n[2]No"));

if(comporders == 1)
Checkout();
if(comporders == 2)
Dinner(meal);
}
}

public static void Checkout(){
pay = Integer.parseInt(JOptionPane.showInputDialog("Enter your Money"));

vat = sub * vat;
total = sub + total;
change = pay - total;

if (pay > total){
JOptionPane.showMessageDialog(null,"Subtotal : " + df.format(sub)
+ "\nVAT : " + df.format(vat)
+ "\nTotal : " + df.format(total)
+ "\nCash : " + df.format(pay)
+ "\nChange : " + df.format(change));
}
else if (pay < total){
JOptionPane.showMessageDialog(null,"Unable to Continue Transaction, Insufficient Funds");
}
}
}

最佳答案

您正在为 sub 分配值,而不是添加它们,而不是执行

sub = ...

使用

sub += ...

这会将价格添加到小计中

附带说明,使所有方法静态化本质上违背了 OOP 的目的。

关于java - 在餐厅 OOP 程序中添加菜单小计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35965284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com