-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREC.java
179 lines (153 loc) · 5.7 KB
/
REC.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
import java.sql.SQLException;
import java.util.ArrayList;
public class REC {
private final TextIO textView;
private final CommandParser parser;
private boolean isDone;
public REC() {
this.textView = new TextIO();
parser = new CommandParser();
DataSource.getInstance();
isDone = false;
}
void start() {
while (!isDone) {
String userCommand = textView.prompt("Please enter a command");
String command = parser.parseCommand(userCommand);
switch (command) {
case "help":
helpCommand();
break;
case "quit":
quitCommand();
break;
case "rpt":
reportCommand(userCommand);
break;
case "trade":
tradeCommand(userCommand);
break;
default:
textView.display("\nInvalid command. list commands by typing \"help\"\n");
}
}
}
void helpCommand() {
textView.display("\nhelp");
textView.display("quit");
textView.display("rpt [listing <min> <max>], [summary]");
textView.display("trade <offer1> <offer2>\n");
}
void quitCommand() {
isDone = true;
}
void reportCommand(String userCommand) {
String[] args = parser.parseArgs(userCommand);
switch (args[0]) {
case "listing":
if (args.length == 3) {
String min = args[1];
String max = args[2];
listing(min, max);
}
break;
case "summary":
summary();
break;
default:
textView.display("Invalid command. list commands by typing \"help\"");
}
}
void tradeCommand(String userCommand) {
String[] args = parser.parseArgs(userCommand);
if (args.length == 2) {
int offer1 = Integer.parseInt(args[0]);
int offer2 = Integer.parseInt(args[1]);
try {
DataSource.getInstance().executeTrade(offer1, offer2);
} catch (SQLException e) {
textView.display("Error occurred while attempting to set auto commit to true " + e);
}
}
isDone = true;
}
void listing(String min, String max) {
try {
String query = "SELECT Listing_ID, Num_Bedrooms, Num_Baths, City, State, Price FROM PROPERTIES WHERE PRICE BETWEEN "
+ min + " AND " + max + " ORDER BY PRICE";
String[][] dataset = DataSource.getInstance().executeQuery(query);
int[] columnWidths = textView.calculateColumnWidth(dataset);
String report = textView.formatReport(dataset, 10, columnWidths);
textView.display(report);
isDone = true;
} catch (SQLException e) {
textView.display("Error " + e);
} finally {
DataSource.getInstance().close();
}
}
void summary() {
try {
String query = "SELECT State, Count(*) AS Count, MIN(PRICE) AS Low, MAX(PRICE) AS High, ROUND(AVG(PRICE)) AS Average FROM active_properties GROUP BY STATE ORDER BY STATE";
ArrayList<DAO> daos = DataSource.getInstance().executeDAO(query);
String[][] dataset = datasetFromDao(daos);
int[] columnWidths = textView.calculateColumnWidth(dataset);
String report = textView.formatReport(dataset, 10, columnWidths);
textView.display(report);
isDone = true;
} catch (SQLException e) {
textView.display("Error getting data: " + e.getLocalizedMessage());
} finally {
DataSource.getInstance().close();
}
}
private String[] regionSummary(ArrayList<DAO> daos) {
String count = String.valueOf(totalCount(daos));
String min = String.valueOf(minPrice(daos));
String max = String.valueOf(maxPrice(daos));
String[] regionSummary = new String[] { "", count, min, max, "" };
return regionSummary;
}
private int totalCount(ArrayList<DAO> daos) {
int totalCount = 0;
for (int i = 0; i < daos.size(); i++) {
totalCount += daos.get(i).getCount();
}
return totalCount;
}
private int minPrice(ArrayList<DAO> daos) {
int min = Integer.MAX_VALUE;
for (int i = 0; i < daos.size(); i++) {
if (daos.get(i).getMin() < min) {
min = daos.get(i).getMin();
}
}
return min;
}
private int maxPrice(ArrayList<DAO> daos) {
int max = Integer.MIN_VALUE;
for (int i = 0; i < daos.size(); i++) {
if (daos.get(i).getMax() > max) {
max = daos.get(i).getMax();
}
}
return max;
}
private String[][] datasetFromDao(ArrayList<DAO> daos) {
String[] columns = new String[] { "State", "Count", "Low", "High", "Average" };
String[][] dataset = new String[daos.size() + 2][];
dataset[0] = columns;
for (int i = 0; i < daos.size(); i++) {
DAO dao = daos.get(i);
String state = dao.getState();
String count = String.valueOf(dao.getCount());
String min = String.valueOf(dao.getMin());
String max = String.valueOf(dao.getMax());
String avg = String.valueOf(dao.getAvg());
String[] rowResult = new String[] { state, count, min, max, avg };
dataset[i + 1] = rowResult;
}
dataset[daos.size() + 1] = regionSummary(daos);
return dataset;
}
}