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

时间:2019-05-17 10:43:13

标签: java netbeans

我必须完成目前正在做的工作,并且要进行一些更改,因此我坚持希望在提交之前遇到的最后一个问题

我需要为电影院创建一个简单的票务系统,并且已经能够(在其他经验丰富的开发人员的帮助下)创建一个工作系统。但是,票务系统需要提示用户进行确认以继续该方法,并显示当前的总费用。确认必须是用户输入数字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 个答案:

答案 0 :(得分:1)

如果您希望用户必须按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