-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMyView.java
88 lines (71 loc) · 2.45 KB
/
MyView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package view;
import java.util.Scanner;
import controller.MyController;
import model.Person;
public class MyView
{
private static final int ADD_A_PERSON = 1;
private static final int DISPLAY_ALL_PEOPLE = 2;
private static final int EXIT = 3;
private int menuChoice;
private MyController theController;
//This is the default constructor. We have added some of our own code in here
//to create an instance of the controller which this view class will use.
public MyView()
{
this.theController = new MyController();
}
public void startUserInterface()
{
Scanner keyboard = new Scanner(System.in);
displayMenu();
menuChoice = keyboard.nextInt();
//Keep looping while the user has not chosen to exit.
while(menuChoice != EXIT)
{
switch(menuChoice)
{
case ADD_A_PERSON : {
//Do whatever needs to be done to add a person
String name = "";
int age = 0;
System.out.println("Please enter person's name");
name = keyboard.next();
System.out.println("Please enter person's age ");
age = keyboard.nextInt();
//The user interface has gathered the name and age. We now
//send them to the controller for "processing".
theController.addPerson(name, age);
break;
}
case DISPLAY_ALL_PEOPLE : {
//Do whatever needs to be done to lookup a person
//If the user wants to display all people we ask the controller to give us
//all the people which it has stored in its array of People.
Person[] thePeople = theController.displayAllPeople();
int noOfPeopleInArray = theController.getNoOfPeopleInArray();
//We use noOfPeopleInArray here instead of thePeople.length because we
//only want to visit slots in the array which have a Person object in them.
//We don't want to visit empty slots later in the array which contain null in each.
for(int i=0; i<noOfPeopleInArray; i++)
{
Person currentPerson = thePeople[i];
System.out.println(currentPerson.getName()+ " "+currentPerson.getAge());
}
break;
}
}
displayMenu();
menuChoice = keyboard.nextInt();
}
System.out.println("Goodbye!");
}
public void displayMenu()
{
System.out.println();
System.out.println("1. Add a Person ");
System.out.println("2. Display All People in system ");
System.out.println("3. Exit");
System.out.println();
}
}