gpt4 book ai didi

java - 需要向简单的票务系统添加用户确认和运行总计

转载 作者:行者123 更新时间:2023-12-02 09:53:00 24 4
gpt4 key购买 nike

我必须完成当前的作业,我一直在对其进行一些更改,但我坚持要解决提交前的最后一个问题

我需要为电影院创建一个简单的票务系统,并且我已经能够(在其他经验丰富的开发人员的帮助下)创建一个工作系统。然而,票务系统需要提示用户确认以继续该方法,并显示当前的总成本。确认需要用户输入数字1,否则输出错误信息。

我尝试使用 again = br.readLine(); 输出 1 来确认购买,我还尝试导入 java.util.Scanner 类然后输入并使用 int 创建错误消息,但它继续显示错误。

 package ticketingsystem;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ticketingsystem {

enum TicketType {
// I chose to use the enum class for the ticket prices, //
// as it made it much easier to use a switch statement, instead of multiple if statements. //
CHILD(18), ADULT(36), SENIOR(32.5);
//Enum classes are needed to be used in upper case, otherwise the program will crash//
//as I discovered//
TicketType(double price) {
this.price = price;
}

private double price;

public double getPrice() {
return price;
}
}

public static void main(String[] args) throws IOException {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//As the program needed the ability to run in an indefinite loop constantly adding //
//calculations, I chose to use buffered reader, as it buffered the method each loop//
//continuing to add a total number.//

String type, again;
int quantity = 0;
// This is where the new calculation starts, it is set at 0 before being calculated and
//added to the total price.//
double totalPrice = 0;
TicketType ticketType;
//We link the TicketType calculations to the enum class, hence//
// TicketType ticketType.//

System.out.println("Welcome to the cinemas!");

System.out.println("MAIN MENU\n");
System.out.println("Cinema has the following ticketing options\n");
System.out.println("1 = Child (4-5 yrs)");
System.out.println("2 = Adult (18+ yrs)");
System.out.println("3 = Senior (60+ yrs)");

do {
//Our loop calculation method starts here//
System.out.print("\nEnter your option: ");

type = br.readLine();

switch (type.toLowerCase()) {
case "1":
ticketType = TicketType.CHILD;
break;

case "2":
ticketType = TicketType.ADULT;
break;

default:
ticketType = TicketType.SENIOR;
break;
}

System.out.print("Enter total No of tickets: ");

quantity = Integer.parseInt(br.readLine());

totalPrice += ticketType.getPrice() * quantity;
//totalPrice is the ticketType cost (hence the += operator), times//
//the quantity.//
System.out.printf("--> You are purchasing %s - %s Ticket(s) at $%s\n", quantity, ticketType, ticketType.getPrice());
System.out.println("Press 1 to confirm purchase");

//This is where we confirm the purchase//
// This is where the current total cost needs to be output //
System.out.print("\nDo you wish to continue? (Y/N) : ");

again = br.readLine();

} while (again.equalsIgnoreCase("y"));
//This is where the calculation method ends (as we are using a do/while loop).//
// The while statement means that if we type "y", the loop will begin again with a buffer.//
//If we type N, the loop will end, and the program will continue running past the loop.//
System.out.printf("\n==> Total Price : $%s \n", totalPrice);
}
}

最佳答案

如果您希望用户必须按 1 才能接受购买,则只需在询问该问题后输入一个简单的 if...else 即可。像这样的东西:

String confirmation = br.readLine();
if (confirmation.equals("1")) {
totalPrice += ticketType.getPrice() * quantity;
System.out.println("Current total is: " + totalPrice);
} else {
System.out.println("You did not press 1 so ticket purchase cancelled");
System.out.println("Current cost is still: " + totalPrice);
}

这样一来,只有当他们按下 1 时才会更新总数。

Example

关于java - 需要向简单的票务系统添加用户确认和运行总计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56184813/

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