-
Notifications
You must be signed in to change notification settings - Fork 271
Expand file tree
/
Copy pathBuilding.java
More file actions
114 lines (95 loc) · 3.91 KB
/
Building.java
File metadata and controls
114 lines (95 loc) · 3.91 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
public class Building {
protected String name;
protected String address;
protected int nFloors;
protected int activeFloor = -1; // Default value indicating we are not inside this building
/* Default constructor */
public Building() {
this("<Name Unknown>", "<Address Unknown>", 1);
}
/* Overloaded constructor with address only */
public Building(String address) {
this(); // Call default constructor
this.address = address; // Override address
}
/* Overloaded constructor with name, address */
public Building(String name, String address) {
this(name, address, 1); // Call full constructor with hard-coded # floors
}
/* Full constructor */
public Building(String name, String address, int nFloors) {
if (name != null) { this.name = name; }
if (address != null) { this.address = address; }
if (nFloors < 1) {
throw new RuntimeException("Cannot construct a building with fewer than 1 floor.");
}
this.nFloors = nFloors;
}
/* Accessors */
public String getName() {
return this.name;
}
public String getAddress() {
return this.address;
}
public int getFloors() {
return this.nFloors;
}
/* Navigation methods */
public Building enter() {
if (activeFloor != -1) {
throw new RuntimeException("You are already inside this Building.");
}
this.activeFloor = 1;
System.out.println("You are now inside " + this.name + " on the ground floor.");
return this; // Return a pointer to the current building
}
public Building exit() {
if (this.activeFloor == -1) {
throw new RuntimeException("You are not inside this Building. Must call enter() before exit().");
}
if (this.activeFloor > 1) {
throw new RuntimeException("You have fallen out a window from floor #" +this.activeFloor + "!");
}
System.out.println("You have left " + this.name + ".");
this.activeFloor = -1; // We're leaving the building, so we no longer have a valid active floor
return null; // We're outside now, so the building is null
}
public void goToFloor(int floorNum) {
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 +".");
}
System.out.println("You are now on floor #" + floorNum + " of " + this.name);
this.activeFloor = floorNum;
}
public void goUp() {
this.goToFloor(this.activeFloor + 1);
}
public void goDown() {
this.goToFloor(this.activeFloor - 1);
}
public void showOptions() {
System.out.println("Available options at " + this.name + ":\n + enter() \n + exit() \n + goUp() \n + goDown()\n + goToFloor(n)");
}
public String toString() {
return this.name + " is a " + this.nFloors + "-story building located at " + this.address + ".";
}
public static void main(String[] args) {
System.out.println("------------------------------------");
System.out.println("Test of Building constructor/methods");
System.out.println("------------------------------------");
Building fordHall = new Building("Ford Hall", "100 Green Street Northampton, MA 01063", 4);
System.out.println(fordHall);
fordHall.showOptions();
System.out.println("-----------------------------------");
System.out.println("Demonstrating enter/exit/navigation");
System.out.println("-----------------------------------");
fordHall.enter();
fordHall.goUp();
fordHall.goDown();
fordHall.exit();
}
}