-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPersonManager.java
104 lines (89 loc) · 3.53 KB
/
PersonManager.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import java.util.Scanner;
import java.util.InputMismatchException;
public class PersonManager {
private static PersonDataManager pdm= new PersonDataManager();
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
System.out.println("Welcome... Starting in few moments...");
System.out.println("MENU IS HERE! :");
int i = 0;
while (i != -1) {
System.out.println("(I)- Import from File" + '\n' + "(A)- Add Person" + '\n' + "(R)- Remove Person" + '\n'
+ "(G)- Get Information" + '\n' + "(P)- Print Table" + '\n' + "(S)- Save to File" + '\n'
+ "(Q)- Quit");
System.out.println("Please select option: ");
String input = stdin.nextLine();
if (input.toUpperCase().equals("Q")) {
System.out.println("Sorry to see you go! Have a good day :)");
i = -1;
} else if (input.toUpperCase().equals("I")) {
System.out.println("Please enter a location: ");
String loc = stdin.nextLine();
PersonDataManager.buildFromFile(loc);
System.out.println("Loading...." + '\n' + "Person data loaded successfully!");
} else if (input.toUpperCase().equals("P")) {
pdm.printTable();
} else if (input.toUpperCase().equals("A")) {
try {
System.out.println("Please enter the name of the person: ");
String name = stdin.next();
if(name.matches("-?\\d+")) { // checking if the input is string
throw new IllegalArgumentException();
}
System.out.println("Please enter the age of the person: ");
int age= stdin.nextInt();
System.out.println("Please enter the gender: ");
String gender = stdin.next();
if (!gender.toUpperCase().equals("M") && !gender.toUpperCase().equals("F")) {
throw new IllegalArgumentException();
}
System.out.println("Please enter the height of the person: ");
double height = stdin.nextDouble();
System.out.println("Please enter the weight of the person: ");
double weight = stdin.nextDouble();
HW1_Person newPerson = new HW1_Person(name, age, gender, height, weight);
pdm.addPerson(newPerson);
System.out.println(newPerson.getName() + " added succesfully");
} catch (InputMismatchException ex) {
System.out.println("Input mismatch !");
}catch(IllegalArgumentException ex) {
System.out.println("Input Mismatch !");
}
catch (PersonAlreadyExistsException io) {
System.out.println("Person already exists");
}
stdin.nextLine();
} else if (input.toUpperCase().equals("R")) {
System.out.println("Enter the name of the person: ");
String name = stdin.nextLine();
try {
pdm.removePerson(name);
System.out.println(name + " has been removed.");
} catch (PersonDoesNotExistsException pe) {
System.out.println(name + " does not exist!");
}
} else if (input.toUpperCase().equals("G")) {
System.out.println("Enter the name of the person: ");
String name = stdin.nextLine();
try {
pdm.getPerson(name);
} catch (PersonDoesNotExistsException pe) {
System.out.println(name + " does not exist");
}
} else if (input.toUpperCase().equals("S")) {
System.out.println("Please select the name for the file: ");
String name = stdin.nextLine();
try {
pdm.SaveToFile(name);
System.out.println("A file named " + name + " has been created !");
} catch (Exception io) {
System.out.println("");
}
}
else {
System.out.println("Please select valid option !");
}
}
stdin.close();
}
}