gpt4 book ai didi

java - 带密码和简单订单的菜单

转载 作者:行者123 更新时间:2023-12-02 04:16:56 25 4
gpt4 key购买 nike

创建一个在下订单之前需要密码的程序。成功输入密码后,客户应该会看到一条欢迎消息。然后是菜单。客户应该能够看到菜单和子菜单中的选项。订单(含价格)应在客户退出计划之前完成。

在我下面的代码中,它仍然要求输入密码,我不知道下一步该怎么做。另外,订单应该被统计并出现在客户退出之前的最后部分。请引导我完成这个!

欢迎来到 Favor Stuff(派对赠品)

请从可用菜单中选择。

A.万宝路灯

1. Soft Pack 70.00

2. Flip Top 80.00

B.万宝路红

1. Soft Pack 70.00

2. Flip Top 80.00

C.登喜路

1. Soft Pack 60.00

2. Flip Top 70.00

D.菲利普

1. Soft Pack 70.00

2. Flip Top 80.00





import java.util.Scanner;
import java.io.*;
public class MenuORDER {
public static void main(String[] args) {
int attempts = 0;
String Order;

Scanner keyboard = new Scanner(System.in);
String password = null;
String CORRECT_PWORD = "1234";

do {
System.out.println("Enter Password: ");
password = keyboard.next();
if (!password.equals(CORRECT_PWORD)) {
System.out.println("Password is incorrect!");
attempts++;
}
} while (!password.equals(CORRECT_PWORD) && attempts <3);


if (password.equals(CORRECT_PWORD)) {

System.out.println("Welcome to Favor Stuff (Party Giveaways)!");
do {
System.out.println("Please select from the available options below! ");
System.out.println("A. Marlboro Lights");
System.out.println("B. Marlboro Red");
System.out.println("C. Dunhill");
System.out.println("D. Phillip");

System.out.print("Sellect your brand: ");
System.out.println("");
String Price = keyboard.nextLine();
String Price1 = "";
String Price2 = "";

switch (Price) {
case "A": Price1 = "Soft Pack 70.00";
break;
case "B": Price2 = "Flip Top 80.00";
break;
case "C": Price1 = "Soft Pack 70.00";
break;
case "D": Price2 = "Flip Top 80.00";
break;
case "E": Price1 = "Soft Pack 70.00";
break;
case "F": Price2 = "Flip Top 80.00";
break;
case "G": Price1 = "Soft Pack 70.00";
break;
case "H": Price2 = "Flip Top 80.00";
break;
case "I": Price1 = "Soft Pack 70.00";
break;
case "J": Price2 = "Flip Top 80.00";
break;
default:
System.out.println("Invalid Input");
break;
}
System.out.println("Thank you! Your order is " + brand + Price);

}while (!password.equals(CORRECT_PWORD));
}while (!password.equals(CORRECT_PWORD) && attempts <3);
}
}

最佳答案

首先分解您的要求,首先您需要验证用户......

int attempts = 0;
Scanner keyboard = new Scanner(System.in);
String password = null;
String CORRECT_PWORD = "1234";
String Options = null;
do {
System.out.print("Enter Pin Code: ");
password = keyboard.next();
if (!password.equals(CORRECT_PWORD)) {
System.out.println("Password is incorrect!");
attempts++;
}
} while (!password.equals(CORRECT_PWORD) && attempts < 3);

他们要么输入正确的密码,要么失败。根据结果​​,您需要决定下一步该做什么...

if (password.equals(CORRECT_PWORD)) {
System.out.println("Welcome to Favor Stuff (Party Giveaways)!");
// Only valid user options should appear here...
} else {
System.out.println("Sorry but you can no longer continue!:");
}

// Program is allowed to terminate naturally

所以,在“有效用户”部分,您想显示主菜单...

if (password.equals(CORRECT_PWORD)) {
System.out.println("Welcome to Favor Stuff (Party Giveaways)!");
String option = null;
do {
System.out.println("Please select from the available options below! ");
System.out.println("A. Truffled Boxes");
System.out.println("B. Candy Jars");
System.out.println("C. Jellies");
System.out.println("D. Cupcakes");
System.out.println("E. Bride and Groom");
System.out.println("X. Exit");
option = keyboard.nextLine();
if (option.equals("A")) {
String subOption = null;
do {
System.out.println("1. Truffled Boxes with two (2) truffles and personalized thank you tags");
System.out.println("2. Truffled Boxes with four (4) truffles and personalized thank you tags");
System.out.println("3. Truffled Boxes with six (6) truffles and personalized thank you tags");
System.out.println("0. Return");
subOption = keyboard.nextLine();
if (subOption.equals("1")) {

} else if (subOption.equals("2")) {
} else if (subOption.equals("3")) {
} else if (subOption.equals("0")) {
// Do nothing, but don't want to display
// invalid entry message
} else {
System.out.println("Invalid entry!");
}
} while (!subOption.equalsIgnoreCase("0"));
} else if (option.equals("B")) {
// Candy jars...
} else if (option.equals("C")) {
// Jellies...
} else if (option.equals("D")) {
// Candy Cupcakes...
} else if (option.equals("E")) {
// Bride groom...
} else if (option.equalsIgnoreCase("x")) {
// Do nothing, but don't want to display
// invalid entry message
} else {
System.out.println("Invalid entry!");
}
} while (!option.equalsIgnoreCase("x"));
}

该主循环负责主菜单中的所有选项。每个子部分也将有它自己的输入循环。如果您对方法做了任何工作,我会将所有这些分解为它自己的方法,以使生活更轻松。

要跟踪订单,有许多可能的解决方案,例如,您可以创建一个 Product 类,其中包含有关每个产品的信息(名称、价格),然后创建一个 Order 类,其中包含有关已订购的 Product 和数量的信息。这可能是首选方式。但更简单、更黑客的方法是定义一系列键......

public static final String TRUFFLE_BOXES_2 = "Truffle Boxes with (2) truffles and personalized thank you tags";
public static final String TRUFFLE_BOXES_4 = "Truffle Boxes with (4) truffles and personalized thank you tags";
public static final String TRUFFLE_BOXES_6 = "Truffle Boxes with (6) truffles and personalized thank you tags";

public static final String CANDY_MARSHMELLOW = "Marshmallow";
public static final String CANDY_CHOCOLATE = "Chocolate";
public static final String CANDY_COOKIES = "Cookies";

public static final String JELLIES_JELLY_BEANS = "Jelly Beans";

public static final String CUP_CAKES_REGULAR = "Regular cupcake box";
public static final String CUP_CAKES_REGULAR_BULK = "Regular cupcake box by bulk";

public static final String BRIDE_AND_GROOM_BOX = "Bride and groom box (per pair)";

这些表示对 Map 执行查找的快速且简单的键

然后,您可以定义每个产品/ key 的价格

Map<String, Double> prices = new HashMap<>(10);
prices.put(TRUFFLE_BOXES_2, 45.00);
prices.put(TRUFFLE_BOXES_4, 50.00);
prices.put(TRUFFLE_BOXES_6, 60.00);

prices.put(CANDY_MARSHMELLOW, 35.00);
prices.put(CANDY_CHOCOLATE, 50.00);
prices.put(CANDY_COOKIES, 45.00);

prices.put(JELLIES_JELLY_BEANS, 45.00);

prices.put(CUP_CAKES_REGULAR, 60.00);
prices.put(CUP_CAKES_REGULAR_BULK, 40.00);

prices.put(BRIDE_AND_GROOM_BOX, 70.00);

然后您可以使用诸如...之类的方式跟踪订单

Map<String, Integer> order = new HashMap<>(25);
order.put(CANDY_CHOCOLATE, 10);
order.put(CUP_CAKES_REGULAR, 5);
order.put(CANDY_COOKIES, 12);

并在需要时提供统计

double total = 0;
for (Map.Entry<String, Integer> entry : order.entrySet()) {

int quanity = entry.getValue();
double price = prices.get(entry.getKey());

total += quanity * price;

System.out.println(String.format(
"%-20s x %4d @ %7s = %10s",
entry.getKey(),
quanity,
NumberFormat.getCurrencyInstance().format(price),
NumberFormat.getCurrencyInstance().format(quanity * price)));

}

System.out.printf("%-20s %4s %7s ==========%n", "", "", "");
System.out.printf("%-20s %4s %7s %10s%n", "", "", "", NumberFormat.getCurrencyInstance().format(total));

其中打印

Chocolate            x   10 @  $50.00 =    $500.00
Cookies x 12 @ $45.00 = $540.00
Regular cupcake box x 5 @ $60.00 = $300.00
==========
$1,340.00

I want to change the options into Cigar brands, and use switch so it would be better... but add the prices at the end just like what you did with the previous one.. I'm confused

我又要把你搞糊涂了...

基本上,这是您的整个(更新)计划、项目、价格、菜单、计数。

它利用商品、订单、价格和组的 map 来生成整个程序。

“数据”都是可变的,但是当您创建/添加/删除产品时,菜单和订购系统的工作方式不需要更改

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class TestShop {

public static final String MALBORO_LIGHTS = "Malboro Lights";
public static final String MALBORO_LIGHTS_SOFT = "Marlboro Lights, Soft Pack";
public static final String MALBORO_LIGHTS_FLIP = "Marlboro Lights, Flip Pack";

public static final String MALBORO_RED = "Malboro Reds";
public static final String MALBORO_RED_SOFT = "Marlboro Reds, Soft Pack";
public static final String MALBORO_RED_FLIP = "Marlboro Reds, Flip Pack";

public static final String DUNHILL = "Dunhill";
public static final String DUNHILL_SOFT = "Dunhill, Soft Pack";
public static final String DUNHILL_FLIP = "Dunhill, Flip Pack";

public static final String PHILLIP = "Phillip";
public static final String PHILLIP_SOFT = "Phillip, Soft Pack";
public static final String PHILLIP_FLIP = "Phillip, Flip Pack";

public static void main(String[] args) {
new TestShop();
}

public TestShop() {
Scanner keyboard = new Scanner(System.in);

/**************************************************************************/
/* This part is dynamic, change this when you want new items, prices or */
/* groups */
/**************************************************************************/

// A list of items that have been ordered
Map<String, Integer> orders = new HashMap<>(25);

// A list of items keyed to prices
Map<String, Double> prices = new HashMap<>(25);
prices.put(MALBORO_LIGHTS_SOFT, 70d);
prices.put(MALBORO_LIGHTS_FLIP, 80d);
prices.put(MALBORO_RED_SOFT, 70d);
prices.put(MALBORO_RED_FLIP, 80d);
prices.put(DUNHILL_SOFT, 60d);
prices.put(DUNHILL_FLIP, 70d);
prices.put(PHILLIP_SOFT, 70d);
prices.put(PHILLIP_FLIP, 80d);

// Mapping items to groups...
Map<String, List<String>> groups = new HashMap<>(25);
groups.put(MALBORO_LIGHTS, new ArrayList<>(Arrays.asList(new String[]{MALBORO_LIGHTS_SOFT, MALBORO_LIGHTS_FLIP})));
groups.put(MALBORO_RED, new ArrayList<>(Arrays.asList(new String[]{MALBORO_RED_SOFT, MALBORO_RED_FLIP})));
groups.put(DUNHILL, new ArrayList<>(Arrays.asList(new String[]{DUNHILL_SOFT, DUNHILL_FLIP})));
groups.put(PHILLIP, new ArrayList<>(Arrays.asList(new String[]{PHILLIP_SOFT, PHILLIP_FLIP})));

/**************************************************************************/

/**************************************************************************/
/* The rest of this is pretty static and is driven by the data from above */
/* This means, you don't need change anything below here, when the stuff */
/* changes */
/**************************************************************************/

boolean done = false;
do {
System.out.println("Welcome to my shop");
List<String> keys = new ArrayList<>(groups.keySet());
for (int index = 0; index < keys.size(); index++) {
System.out.println("[" + (index + 1) + "] " + keys.get(index));
}
System.out.println("[0] Exit");
String input = keyboard.nextLine();
try {
int selectedIndex = Integer.parseInt(input);
if (selectedIndex == 0) {
done = true;
} else if (selectedIndex > 0 && selectedIndex <= keys.size()) {
String key = keys.get(selectedIndex - 1);

List<String> items = groups.get(key);
boolean subDone = false;
do {
System.out.println("Items for " + key + "....");
for (int index = 0; index < items.size(); index++) {
System.out.println(" [" + (index + 1) + "] " + items.get(index));
}
System.out.println(" [0] Return");
input = keyboard.nextLine();
try {
int index = Integer.parseInt(input);
if (index > 0 && index <= items.size()) {
index--; // The items in the list are 0 indexed
String item = items.get(index);
Integer quanity = orders.get(item);
if (quanity == null) {
quanity = 1;
} else {
quanity++;
}
orders.put(item, quanity);
} else if (index == 0) {
subDone = true;
} else {
System.out.println("Invalid selection, please try again");
}
} catch (NumberFormatException exp) {
System.out.println("!! " + input + " is not a valid selection");
}
} while (!subDone);
} else {
System.out.println("Invalid selection, please try again");
}
} catch (NumberFormatException exp) {
System.out.println("!! " + input + " is not a valid selection");
}

} while (!done);

double total = 0;
for (Map.Entry<String, Integer> entry : orders.entrySet()) {

int quanity = entry.getValue();
double price = prices.get(entry.getKey());

total += quanity * price;

System.out.println(String.format(
"%-20s x %4d @ %7s = %10s",
entry.getKey(),
quanity,
NumberFormat.getCurrencyInstance().format(price),
NumberFormat.getCurrencyInstance().format(quanity * price)));

}

System.out.printf("%-20s %4s %7s ==========%n", "", "", "");
System.out.printf("%-20s %4s %7s %10s%n", "", "", "", NumberFormat.getCurrencyInstance().format(total));
}

}

关于java - 带密码和简单订单的菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33206299/

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