forked from jcrouser/CSC120-A8
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHouse.java
More file actions
189 lines (173 loc) · 5.45 KB
/
Copy pathHouse.java
File metadata and controls
189 lines (173 loc) · 5.45 KB
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
/* This is a stub for the House class */
import java.util.ArrayList;
import javax.management.RuntimeErrorException;
import java.lang.Math;
import java.util.Scanner;
/**
* The house class that extends the building class and adds the attributes residents and hasDiningRoom.
*/
public class House extends Building {
private ArrayList<String> residents;
private boolean hasDiningRoom;
private boolean hasElevator;
/**
* The constructor for the house class that allows user to input following params
* @param name
* @param address
* @param nFloors
* @param hasDiningRoom
* @param hasElevator
*/
public House(String name, String address, int nFloors, boolean hasDiningRoom, boolean hasElevator) {
super(name, address, nFloors);
this.residents = new ArrayList<String>();
this.hasDiningRoom = hasDiningRoom;
this.hasElevator = hasElevator;
}
/**
* The constructor for the house class that allows user to input following params
* @param name
* @param address
* @param nFloors
*/
public House(String name, String address, int nFloors) {
super(name,address,nFloors);
this.residents = new ArrayList<String>();
this.hasDiningRoom = true;
this.hasElevator = false;
}
/**
* The constructor for the house class with default all params
*/
public House() {
super();
this.residents = new ArrayList<String>();
this.hasDiningRoom = true;
this.hasElevator = false;
}
/**
* Getter for hasDiningRoom
* @return boolean for if the house has a dining room
*/
public boolean hasDiningRoom() {
return this.hasDiningRoom;
}
/**
* Getter for hasElevator
* @return boolean for if the house has elevator
*/
public boolean hasElevator() {
return this.hasElevator;
}
/**
* Getter for nResidents
* @return the size of the residents ArrayList (Number of residents)
*/
private int nResidents() {
return this.residents.size();
}
/**
* Adds new resident to ArrayList residents
* @param name name of new resident
*/
public void moveIn(String name) {//Make a way to move in many ppl at once
if (isResident(name) == false) {
this.residents.add(name);
}
else {
System.out.println(name + " already lives here!");
}
}
/**
* Adds multiple new residents to ArrayList residents at once
* @param nPeople number of people being moved in
*/
public void moveIn(int nPeople) {
int roundsCounter = 0;
Scanner myObj = new Scanner(System.in);
ArrayList <String> names = new ArrayList<String>();
while (nPeople > roundsCounter) {
System.out.println("Give me a name to move in:");
String name = myObj.nextLine();
names.add(name);
if (isResident(name) == false) {
this.residents.add(name);
}
else {
System.out.println(name + " already lives here!");
}
roundsCounter += 1;
}
myObj.close();
}
/**
* Removes resident from ArrayList resident
* @param name name of resident
* @return name of removed resident
*/
public String moveOut(String name) {
if (isResident(name) == true) {
this.residents.remove(name);
return name;
}
else {
System.out.println(name + " does not live here!");
return name;
}
}
/**
* Determines if someone is a resident of the house
* @param person name of potential resident of the house
* @return boolean for if the person lives in the house
*/
private boolean isResident(String person) {
if (this.residents.contains(person)) {
return true;
}
else {
return false;
}
}
/**
* Allows the user to move floors based on presence or lack of elevator an floor picked
* @param floorNum floor number to move to
*/
public void goToFloor(int floorNum) { //Modify this or override in child class b/c teleporting doesn't work
if (this.activeFloor == -1) {
throw new RuntimeException("You are not inside this Building. Must call enter() before navigating between floors.");
}
if (floorNum < 1 || floorNum > this.nFloors) {
throw new RuntimeException("Invalid floor number. Valid range for this Building is 1-" + this.nFloors +".");
}
if (this.hasElevator() == false && Math.abs(this.activeFloor - floorNum) > 1) {
throw new RuntimeException("There is no elevator! You can only use the goUp() or goDown methods to change by more than 1 floor!");
}
System.out.println("You are now on floor #" + floorNum + " of " + this.name);
this.activeFloor = floorNum;
}
/**
* Prints available methods for this class
*/
public void showOptions() {
System.out.println("Available options at " + this.name + ":\n + enter() \n + exit() \n + goUp() \n + goDown()\n + goToFloor(n) \n + moveIn() \n + moveOut()");
}
/**
* Tester for the house class methods.
* @param args
*/
public static void main(String[] args) {
House Albright = new House("Albright", "Bedford Terrace", 4, true, false);
Albright.moveIn("Maura");
Albright.moveIn("Nina");
Albright.moveIn("Nina");
Albright.moveOut("Joe");
Albright.moveOut("Maura");
System.out.println("T/F is a resident: " + Albright.isResident("Maura"));
System.out.println("T/F is a resident: " + Albright.isResident("Sara"));
System.out.println("Number of residents: " + Albright.nResidents());
Albright.showOptions();
House Default = new House();
System.out.println(Default.getAddress());
Albright.moveIn(2);
}
}