-
Notifications
You must be signed in to change notification settings - Fork 0
/
RentACat.java
261 lines (230 loc) · 6.91 KB
/
RentACat.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import java.util.ArrayList;
import java.util.Scanner;
public class RentACat {
public ArrayList<Cat> _cats = new ArrayList<Cat>();
/**
* Return a cat.
* This should call the .returnCat() method on the passed-in
* Cat object.
* If the cat has *not* been rented out, then this method
* should return false. If the cat was already rented out,
* this method should return true.
* @param Cat the cat to return
* @return boolean false if cat was not rented out, true otherwise
*/
public boolean returnCat(Cat c) {
if(c == null) {
return false; // cat does not exist
}
if(!c.getRented()) {
return false;
}
c.returnCat();
return true;
}
/**
* Rent a cat.
* This should call the .rentCat() method on the passed-in
* Cat object.
* If the cat has *not* been rented out, then this method
* should return true. If the cat was already rented out,
* this method should return false.
* @param Cat the cat to rent
* @return boolean false if cat was rented out, true otherwise
*/
public boolean rentCat(Cat c) {
if(c == null) {
return false; // cat does not exist
}
if(c.getRented()) {
return false;
}
c.rentCat();
return true;
}
/**
* Given a list of cats, create a String list using the .toString()
* method of each NON-RENTED Cat object in the list. That is,
* it should only add cats who are available to be rented.
* These cats should be separated by "\n" characters (line feeds).
* Example:
* ID 1. Jennyanydots
* ID 2. Old Deuteronomy
* ID 3. Mistoffelees
* @param ArrayList<Cat> list of cats to print (filtering out rented ones)
* @return "\n"-delimited list of rentable cats
*/
public String listCats(ArrayList<Cat> catList) {
StringBuilder rentableList = new StringBuilder("");
for(Cat c: catList) {
if(!c.getRented()) {
rentableList.append(c.toString());
rentableList.append("\n");
}
}
if(rentableList.length() > 0){
rentableList.deleteCharAt(rentableList.length() - 1); // remove last new line
}
return rentableList.toString();
}
/**
* Given a list of cats and id, return true if a cat exists in the list
* or false if no cat with that ID number exists in the list.
* If list is null or contains 0 elements, should always
* return false.
* @param int id ID of cat to search for
* @param ArrayList<Cat> list of cats
* @return true if cat exists in list, false otherwise
*/
public boolean catExists(int id, ArrayList<Cat> catList) {
if(getCat(id, catList) == null){
return false;
}
return true;
}
/**
* Given a list of cats and id, return true if a cat exists in the list
* and is available for rent; otherwise return false.
* If list is null or contains 0 elements, should always
* return false.
* @param int id ID of cat to search for
* @param ArrayList<Cat> list of cats
* @return true if cat available for rent, false otherwise
*/
public boolean catAvailable(int id, ArrayList<Cat> catList) {
// null / zero-element check
if (catList == null || catList.size() == 0) {
return false;
}
Cat c = getCat(id, catList);
if (c == null) {
// No cat of this ID exists, thus it is not available
return false;
} else {
if (c.getRented()) {
// This cat exists, but has already been rented
return false;
}
}
// If cat exists and is not rented, then the cat
// is available to rent
return true;
}
/**
* Given a list of cats and an id, return a reference to
* the specified cat if a cat with that ID exists.
* Return null if no cat of that ID exists in the list.
* @param int id ID of cat to search for
* @param ArrayList<Cat> catList - list of cats to search
* @return Cat searched for if exists, null otherwise
*/
public Cat getCat(int id, ArrayList<Cat> catList) {
// null / zero-element check
if (catList == null || catList.size() == 0) {
return null;
}
// Loop through every cat in the cat list
for (Cat c : catList) {
// If we found a cat whose id matches the id
// of the argument, then we have a match and
// can thus return a reference to that cat
if (c.getId() == id) {
return c;
}
}
// If we get all the way through the list and did
// not find a cat whose ID matches the passed-in
// ID, then the cat is not in the list
return null;
}
/**
* Main method
* @param args - IGNORED, kept for compatibility
*/
public static void main(String[] args) {
RentACat rc = new RentACat();
rc._cats.add(new Cat(1, "Jennyanydots"));
rc._cats.add(new Cat(2, "Old Deuteronomy"));
rc._cats.add(new Cat(3, "Mistoffelees"));
// Create a local copy for this method
ArrayList<Cat> cats = rc._cats;
Scanner sc = new Scanner(System.in);
boolean validCat = false;
int option;
boolean keepGoing = true;
while (keepGoing) {
validCat = false;
System.out.print("Option [1,2,3,4] >");
try {
option = sc.nextInt();
switch (option) {
case 1:
System.out.println("Cats for Rent");
System.out.println(rc.listCats(cats));
break;
case 2:
validCat = false;
int catIdToRent;
while (!validCat) {
System.out.print("Rent which cat? > ");
try {
catIdToRent = sc.nextInt();
Cat c = rc.getCat(catIdToRent, cats);
if (c == null) {
throw new NumberFormatException();
} else if (c.getRented()) {
System.out.println("Sorry, " + c.getName() + " is not here!");
validCat = false; //modified by shb
} else {
rc.rentCat(c);
System.out.println(c.getName() + " has been rented.");
validCat = true;
}
} catch (Exception nfex) {
System.err.println("Invalid cat ID.");
}
}
break;
case 3:
validCat = false;
int catIdToReturn;
while (!validCat) {
System.out.print("Return which cat? > ");
try {
catIdToReturn = sc.nextInt();
Cat c = rc.getCat(catIdToReturn, cats);
if (c == null) {
throw new NumberFormatException();
} else if (!c.getRented()) {
System.out.println(c.getName() + " is already here!");
validCat = true;
} else {
rc.returnCat(c);
System.out.println("Welcome back," + c.getName() + "!");
validCat = true;
}
} catch (Exception nfex) {
System.err.println("Invalid cat ID.");
sc.nextInt();
}
}
break;
case 4:
keepGoing = false;
break;
default:
throw new NumberFormatException();
}
} catch (Exception nfex) {
System.err.println("Please enter 1, 2, 3 or 4");
System.err.println("1. See list of cats for rent");
System.err.println("2. Rent a cat to a customer");
System.err.println("3. Return a cat");
System.err.println("4. Quit");
// Clear out the non-int in the scanner
sc.next();
}
}
System.out.println("Closing up shop for the day!");
}
}