-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMadisonBuffetDriver.java
197 lines (185 loc) · 6.85 KB
/
MadisonBuffetDriver.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// --== CS400 File Header Information ==--
// Name: Youlin Qu
// Email: [email protected]
// Team: NC
// TA: Daniel
// Lecturer: Florian Heimerl
// Notes to Grader: <optional extra notes>
import java.io.File;
import java.io.FileNotFoundException;
import java.util.NoSuchElementException;
import java.util.Scanner;
/**
* Description: This is a class that intended to use MadisonBuffet.java and give users a
* way to interact with the program with certain commands.
*
*/
public class MadisonBuffetDriver
{
private MadisonBuffet madisonBuffet; //private MadisonBuffet use to organize and save the data
public MadisonBuffetDriver()
{
madisonBuffet = new MadisonBuffet();
}
/**
* processing command, if command is
* a : add restaurant with its name and ratings
* af: add bunch of restaurant in file
* r : remove restaurant with name
* s : show all the restaurant/ show restaurant by a specific name.
* b : back to menu
* q : quit
* @param command a string should be in lowercase and trimmed
*/
public void processingCommand(String command)
{
Scanner sc = new Scanner(command);
String [] commands = new String[3];
int index=0;
while(sc.hasNext())
{
if(index == 3)
{
System.out.println("There can be most three commands");
return;
}
commands[index++] = sc.next();
}
try
{
switch (commands[0].toLowerCase())
{
case "a":
try
{
madisonBuffet.put(commands[1],Double.parseDouble(commands[2]));
}
catch(NumberFormatException | NullPointerException npe)
{
System.out.println("Please type with the following way: <a> <name> <a number>");
}
break;
case "af":
try
{
addFile(commands[1]);
}
catch(NullPointerException npe)
{
System.out.println("Please enter a file name");
}
break;
case "r":
try
{
madisonBuffet.remove(commands[1]);
}
catch(NoSuchElementException nse)
{
System.out.println("There appears to be no such restaurant named: "+ commands[1]);
}
break;
case "s":
if(commands[1] == null) System.out.println(madisonBuffet.showAll());
else
{
try
{
System.out.println(madisonBuffet.showName(commands[1]));
}
catch(NoSuchElementException nse)
{
System.out.println("There appears to be no such restaurant named: "+ commands[1]);
}
}
break;
case "sr":
System.out.println(madisonBuffet.rankRating());
break;
case "b":
case "q":
System.out.println("Thank you for using the program!");
break;
default:
System.out.println("Warning - Command not recognized: " + command);
break;
}
}
catch(Exception e)
{
System.out.println("Unexpected exception happens: ");
e.printStackTrace();
System.exit(0);
}
}
/**
* private helper to implement the addFile functionality
* @param fileName name of the file
*/
private void addFile(String fileName)
{
try
{
Scanner sc = new Scanner(new File(fileName));
Scanner sc2;
while(sc.hasNext())
{
sc2 = new Scanner(sc.nextLine());
String name = sc2.next();
if(name.equals("")) break;
double rating = Double.parseDouble(sc2.next());
madisonBuffet.put(name,rating);
}
System.out.println("file " + fileName + " added");
}
catch(FileNotFoundException fnf)
{
System.out.println("File doesn't exist");
}
catch(NoSuchElementException | NumberFormatException e)
{
e.printStackTrace();
System.out.println("Your file contents are not in the right format");
}
}
/**
* run method to start the program and keeps it running until user enter 'q';
* @param sc Scanner object
*/
public void run(Scanner sc)
{
addFile("madisonBuffet.txt");
String command = "None";
while(!command.equals("q"))
{
displayMenu();
command = sc.nextLine().trim();
processingCommand(command);
}
}
/**
* private helper method to display the menu
*/
private void displayMenu()
{
System.out.println("" +
"**********************************************************************************************************\n" +
"* Welcome to Madison Buffet, here are the commands you can do:\n" +
"* \"a <name> <rating>\" adding a restaurant with its name int string and rating in double\n" +
"* THE RATING SHOULD BE A DOUBLE WITH ONE DECIMAL PLACE FROM 0.0 TO 5.0\n" +
"* \"af <name>\" adding a bunch of restaurants in file, each line in file contents must be <name, rating>\n" +
"* \"r <name>\" removing a restaurant with its name in string\n" +
"* \"s\" show all the restaurant in the program\n" +
"* \"s <name>\" show a specific restaurant\n" +
"* \"sr\" show all restaurants by rank\n" +
"* \"b\" back to this menu\n" +
"* \"q\" quit the program\n" +
"* WARNING: YOU HAVE TO ENTER THE CORRECT TYPE, AND THE COMMANDS ARE NOT CASE SENSITIVE!\n" +
"* AND THE NAME OF THE RESTAURANT SHOULD NOT CONTAIN SPACE!\n" +
"**********************************************************************************************************");
}
public static void main(String [] args)
{
new MadisonBuffetDriver().run(new Scanner(System.in));
}
}