NetBeans中的简单Java票务系统

Cap*_*tag 3 java bufferedreader java.util.scanner

我们需要为netbeans的本地电影院完成一个简单的票务系统,而我为两个问题感到困惑。

问题1作为输出的一部分,一旦您选择了票证类型+数量,就需要有一个输出“您正在购买Y数量的X张票证”

问题2是长者票需要花费$ 32.50,我似乎无法找到一种允许使用小数进行计算的解决方法。我进行了调试,似乎将数字更改为整数,然后该整数将无法正确计算。救命!

package ticketingsystem;

import java.io.*;
public class ticketingsystem
{
     public static void main(String []args) throws Exception
     {
         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
         String order,again;
         int quantity,price1=0,price2=0, price3=0,loop1=0,quantity1,quantity2=0;

         System.out.println("  ");  

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

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

         do{
            System.out.println(" ");
            System.out.print("Enter your option: ");
            order=br.readLine();
            if (order.equalsIgnoreCase("1")) {
                price1=18;
            } else if (order.equalsIgnoreCase("2")) {
                price1=36;
            }   
            else if (order.equalsIgnoreCase("3")) {
                price1= (int) 32.5;
            }   

            System.out.print("Enter the number of tickets: ");
            quantity1= Integer.parseInt(br.readLine());
            quantity2=quantity1+quantity2;

            price2=price1*quantity2;   
            System.out.println("You are purchasing " int (order=br) " tickets at" (quantity1);

            System.out.print("Do you wish to continue?  (Y/N) : ");
            again=br.readLine();
            if (again.equalsIgnoreCase("y"))
                loop1=loop1+1;
            else loop1=loop1-100;
      } while (loop1==1);    

     System.out.println(" ");
     System.out.println("Total Price           : "+price2);   

 }
}
Run Code Online (Sandbox Code Playgroud)

Ste*_*oom 5

您好建议进行以下更改。

首先将price2重命名为totalPrice,并将其更改为double:

double totalPrice;

我将为TicketType创建一个枚举类,您还可以在其中分配price1值:

enum TicketType {
  child(18), adult(36), senior(32.5);

  TicketType(double price) {
    this.price = price;
  }

  private double price;

  public double getPrice() {
    return price;
  }
}
Run Code Online (Sandbox Code Playgroud)

您现在可以将主要方法更改为此:

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

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String order, again;
    int quantity = 0;
    double totalPrice;
    TicketType ticketType; // add the enum to the method

    System.out.println("  ");

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

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

    do {
      System.out.println(" ");
      System.out.print("Enter your option: ");
      order = br.readLine();

      //use a switch case instead of the if else pattern
      switch (order.toLowerCase()) {
        case "1":
          ticketType = TicketType.child;
          break;

        case "3":
          ticketType = TicketType.senior;
          break;

        default:
          ticketType = TicketType.adult;
          break;
      }

      System.out.print("Enter the number of tickets: ");
      quantity = Integer.parseInt(br.readLine());

      totalPrice += ticketType.getPrice() * quantity;
      // %s will be replaced with the string value of ticketType, %.2f means it will be replaced with a decimal, round to decimals 
      System.out.printf("You are purchasing %s tickets at %.2f \n", ticketType, ticketType.getPrice());

      System.out.print("Do you wish to continue?  (Y/N) : ");
      again = br.readLine(); 
    }
    while (again.equalsIgnoreCase("y"));

    System.out.println(" ");
    System.out.printf("Total Price           : $%.2f \n", totalPrice);
  }
Run Code Online (Sandbox Code Playgroud)

您可以使用System.out.printf()来格式化打印到控制台的消息