简单的订票java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18214233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:22:39  来源:igfitidea点击:

Simple ticket reservation java

java

提问by Abisheak

import java.io.*;
import java.util.*;

public class Ticket_reserve {

  private static int counter=100;
  List<String> BookingList=new ArrayList<String>();
  ArrayList<Integer> AgeList=new ArrayList<Integer>();

  public void reservation(){
    System.out.println("Enter the tickets needed:");
    Scanner tkts=new Scanner(System.in);
    int tickets=tkts.nextInt();
    if(tickets<=counter){
      System.out.println("Name and age please");
      System.out.println("age:");
      Scanner age=new Scanner(System.in);
      int Age=age.nextInt();
      if(Age<18){
        System.out.println("You're under 18.Booking cancelled");
      }else{
        for(int i=0;i<tickets;i++){
          System.out.println("Name:");
          Scanner nom=new Scanner(System.in);
          String name=nom.nextLine();
          BookingList.add(name);
          AgeList.add(Age);
          counter--;
        }
      }
    }else{
      System.out.println(tickets+"tickets  unavailable");
    }
    System.out.println("Names: "+BookingList+","+"Age:"+AgeList);
  }


  public static void main(String[] args) {
    Ticket_reserve t1=new Ticket_reserve();
    t1.reservation();
  }
}

This is my code and it works perfectly. The only problem I have is I need to check the age of each person and then book the ticket for that person (if they are above 18); else cancel it. I couldn't get a better idea, so I put it inside ifchecking for ticket availability and now I can only get the age of one person. I need to iterate through each person and print their age. Should I use a whileloop instead?

这是我的代码,它完美地工作。我唯一的问题是我需要检查每个人的年龄,然后为那个人订票(如果他们超过 18 岁);否则取消它。我想不出更好的主意,所以我把它放在if检查票的可用性中,现在我只能得到一个人的年龄。我需要遍历每个人并打印他们的年龄。我应该使用while循环吗?

Thank you.

谢谢你。

采纳答案by ihsan kocak

But you are taking just 1 person?You should use a loop for taking more than 1 person.Second, you should have a person class for OOP. Let us come to your question: After taking the properties of people, you should create a Person with these information.And add it to Person list(no need to have age list).Once you take people into a list, you can iterate on the list:

但是你只带 1 人?你应该使用一个循环来带超过 1 人。第二,你应该有一个面向 OOP 的人类。让我们来回答你的问题:在取了人的属性之后,你应该用这些信息创建一个人。并将其添加到人列表中(不需要有年龄列表)。一旦你把人放到列表中,你就可以迭代列表:

List<Person> people=new ArrayList<Person>();
//take  people info from console and add it to the list:
Person person=new Person(age,name,etc);
people.add(person);
for(Person p:people){
    if(p.getAge<18){//say something
     }
    else{//say something
    }
}

回答by Louis Fellows

Probably best to use a for loop.

可能最好使用 for 循环。

i.e.

IE

for (each ticket) {
    Ask for age
}
...etc...

回答by JNL

This is more with designing the class.

这更多与设计课程有关。

Create a private boolean instance for age, say private boolean isAdult;

为 age创建一个私有布尔实例,比如private boolean isAdult;

Initialize it in the constructorfor that instance of the class, and then you can just use a getterto see if the age is valid.

在该类实例的构造函数中初始化它,然后您可以使用 getter来查看年龄是否有效。

If yes, move on with other calculation;
else just return with an error message.

Hope this helps.

希望这可以帮助。

回答by Mukesh Kumar Singh

You are using very basic way to develop your code.

您正在使用非常基本的方式来开发您的代码。

You are not using the best feature of oops. "class"

您没有使用 oops 的最佳功能。“班级

  1. The most important entity in your program is passenger details.

  2. So better you create a class passengerwith name and ageas variables and methods passenger(String name,int age)and getDetails().

  3. why have you created scanner object that many times.Once created scanner object it can be used in all cases .
  4. Use only one List or LinkedList, i suggest to store the passenger objects.and then iterate through it during printing.

  5. You can make the code work any waybut you cant work with that code

  1. 您的计划中最重要的实体是乘客详细信息。

  2. 因此,更好地创建一个类乘客姓名和年龄为变量和方法乘客(字符串名称,诠释岁)getDetails()

  3. 为什么要多次创建扫描仪对象。一旦创建了扫描仪对象,它就可以在所有情况下使用。
  4. 仅使用一个 List 或 LinkedList,我建议存储乘客对象。然后在打印过程中对其进行迭代。

  5. 您可以使代码以任何方式工作,但不能使用该代码