-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHousingManagement.java
161 lines (145 loc) · 5.48 KB
/
HousingManagement.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
import java.util.SortedSet; // The Collection interface that we will implement
import java.util.TreeSet; // The specific collection type that will be used
import java.util.Scanner; // The following imports are for stream handler classes that are related to the reading and writing of text streams from/to the csv file.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.*;
/**
* HousingManagement models the information used by the Lambeth Walk Housing Association to maintain their housing register
*
* @author Christopher Arnold
* @version 04/05/2022
*/
public class HousingManagement
{
private SortedSet<Property> properties;
/**
* Constructor for objects of class HousingManagement, it will create a TreeSet of properties
*/
public HousingManagement()
{
properties = new TreeSet<>();
}
/**
* Method to clear the housing register
*/
public void clear()
{
properties.clear();
}
/**
* Method to populate the housing register with five different example member objects
*/
public void populate()
{
properties.add(new Property("THE GLEBE", "tenant", 3, false));
properties.add(new Property("CROMWELL HOUSE", "tenant", 3, false));
properties.add(new Property("WARWICK HOUSE", "leaseholder", 5, false));
properties.add(new Property("1 CLOISTER ROAD", "leaseholder", 2, true));
properties.add(new Property("2 CLOISTER ROAD", "freeholder", 2, true));
}
/**
* Method to write the housing register to a csv file
* @param fileName name of the csv file
*/
public void writeCSVFile(String fileName) {
try (FileWriter writer = new FileWriter(fileName)) {
for (Property p : properties) {
writer.write(p.toString());
writer.write("\n");
}
} catch (IOException ex) {
System.out.println("Failed to Write File\n");
ex.printStackTrace();
}
}
/**
* Method to read a csv file and add the properties to the collection
* @param fileName name of the csv file
*/
public void readCSVFile(String fileName) {
try (BufferedReader reader = Files.newBufferedReader(Paths.get(fileName))) {
String line = reader.readLine();
while (line != null) {
try (Scanner lineScanner = new Scanner(line)) {
lineScanner.useDelimiter(",");
String aName = lineScanner.next();
String aType = lineScanner.next();
int aSize = Integer.parseInt(lineScanner.next());
boolean hasAGarage = lineScanner.nextBoolean();
properties.add(new Property(aName, aType, aSize, hasAGarage));
}
line = reader.readLine();
}
} catch (IOException ex) {
System.out.println("Failed to Read File\n");
ex.printStackTrace();
}
}
/**
* Method to add a new property to the collection
* @param aProperty new property object to be added to the collection
*/
public void addMember(Property aProperty)
{
if (properties.contains(aProperty)) {
System.out.println("The " + aProperty.getType() + " residing at the " + aProperty.getSize()
+ " bedroom sized property " + aProperty.getName()
+ " is already on the housing register.");
}
properties.add(aProperty);
}
/**
* Method to remove an existing property from the collection
* @param aProperty property object to be removed from the collection
* @return true if the property in question has been rmoved from the collection, otherwise false
*/
public boolean removeMember(Property aProperty)
{
if (!(properties.contains(aProperty))) {
return false;
}
return properties.remove(aProperty);
}
/**
* Method to update the garage value of a specified property in the housing register
* @param aProperty property object to be updated
* @param withGarage new garage value
*/
public void updateMember(Property aProperty, boolean withGarage)
{
if (properties.contains(aProperty)) {
aProperty.setGarage(withGarage);
} else {
System.out.println("The property " + aProperty.getName() + " could not be found. ");
}
}
/**
* Returns a subset of the original housing register, the subset is filtered by the property size in the parameter
* @param bedSize filter attibute used to create a new subset
* @return sub_set a subset of the properties TreeSet containing properties with the same size
* as specified in the parameter
*/
public TreeSet<Property> selectMembers(int bedSize)
{
TreeSet<Property> sub_set = new TreeSet<Property>();
for (Property p : properties) {
if(p.getSize() == bedSize) {
sub_set.add(p);
}
}
return sub_set;
}
/**
* Prints a each of the properties in the TreeSet, one property per line with one final blank line
*/
public void printMembers()
{
for (Property p : properties) {
System.out.printf("%-25s %-11s %-2d %b\n", p.getName(), p.getType(), p.getSize(), p.hasGarage());
}
}
}