我被要求建立一个系统,我将有一个菜单与一堆功能,但主要的一点是,它是关于安排doctor 和护士预约.首先,我想,因为我们可能有很多不同的人(生病的正常人,想要预约,doctor 和护士),我应该创建一个"Person"类,并能够创建一组具有一组参数的人.在我go 找它的路上,我发现自己有一个错误,说我的数组是空的,即使用户应该插入信息.
这是我到目前为止在菜单课上学到的东西.我现在正在try 完成的部分只是菜单上的数字1,这意味着添加一个人,我想稍后将其存储在内存中,并对选项2进行编程,即显示存储在内存中的人.
import java.text.BreakIterator;
import java.util.Scanner;
public class Menu {
Person people[] = new Person[9];
public Menu(){
}
void showMenu(){
Scanner r = new Scanner(System.in);
int option;
System.out.println("==== Managing medical appointments ====");
System.out.println("1 - Add a person");
System.out.println("2 - Show people");
System.out.println("2 - Book an appointment");
System.out.println("3 - De-select an appointment");
System.out.println("4 - Show all appointments");
System.out.println("5 - Show appointments by date");
System.out.println("6 - Average enquiry time");
System.out.println("7 - Average time of enquiries by date");
System.out.println("8 - Save queries");
System.out.println("9 - Read queries");
System.out.println("0 - Exit");
System.out.println("Choose the desired option: ");
option = r.nextInt();
switch(option){
case 1: addPerson();
break;
case 2: showPeople();
break;
}
}
public void showPeople(){
for (int i =0 ; i< people.length ; i++){
System.out.println(people[i]);
}
}
public void addPerson() {
Scanner r = new Scanner(System.in);
int nId;
String name = "nothing";
int nTel;
System.out.println("Enter ID number: ");
nId = r.nextInt();
System.out.println("Enter the person's name: ");
name = r.nextLine();
System.out.println("Enter phone number: ");
nTel = r.nextInt();
people[0].setPerson(nId,name,nTel);
}
}
这是我的主要内容.我还有一大堆其他的课程,比如建立,这将延伸到医院或健康中心,预约,等等.在这种情况下,在Main上,我所做的就是创建一个对象的菜单类型,并调用我创建的showMenu函数.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Menu menu = new Menu();
menu.showMenu();
}
}
当我运行我的Main时收到的错误:
==== Medical appointment management ====
1 - Add a person
2 - Show people
2 - Book an appointment
3 - Cancel an appointment
4 - Show all appointments
5 - Show appointments by date
6 - Average appointment time
7 - Average appointment time by date
8 - Save appointments
9 - Read appointments
0 - Exit
Choose the desired option:
1
Enter the identification number:
12313123
Enter the person's name:
Enter the phone number:
9712312
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Person.setPerson(int, String, int)" because "this.persons[0]" is null
at Menu.addPerson(Menu.java:49)
at Menu.showMenu(Menu.java:27)
at Main.main(Main.java:6)
Process finished with exit code 1
如你所见,它显示了菜单,我 Select 选项1,这是Add Person,然后他开始询问我在类中包括的参数,但首先,由于某种原因,它直接跳过了名字,然后他给我那个错误,告诉我我的数组为空.
编辑:按照建议翻译了程序,希望它更容易.谢谢!