-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathReportGenerator.java
More file actions
199 lines (175 loc) · 9.36 KB
/
ReportGenerator.java
File metadata and controls
199 lines (175 loc) · 9.36 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
190
191
192
193
194
195
196
197
198
199
import java.sql.*;
public class ReportGenerator {
public static void showPopularDroneReport(String dbUrl) {
// From Checkpoint 04 - 4 - m
String sql = "SELECT Drone.DroneSerialNumber, sum(Customer.Distance) " +
"FROM Customer, Drone, Rentals " +
"WHERE Customer.UserID = Rentals.UserID " +
"AND Rentals.DroneSerialNumber = Drone.DroneSerialNumber " +
"GROUP BY Drone.DroneSerialNumber " +
"ORDER BY sum(Customer.Distance) DESC";
// Set up
System.out.println("\n--- Popular Drone Report (Total Distance) ---");
System.out.printf("| %-20s | %-18s |%n", "Drone Serial", "Total Distance");
System.out.println("|----------------------|--------------------|");
// Printing
try (Connection conn = DriverManager.getConnection(dbUrl);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
String serial = rs.getString(1);
double totalDist = rs.getDouble(2);
System.out.printf("| %-20s | %-18.0f |%n", serial, totalDist);
}
} catch (SQLException e) {
System.out.println("Error running report: " + e.getMessage());
}
System.out.println("|----------------------|--------------------|");
}
public static void showFrequentRenterReport(String dbUrl) {
// From Checkpoint 03
String sql = "SELECT UserID, Count(RentalNumber) " +
"FROM Rentals " +
"GROUP BY UserID " +
"HAVING Count(RentalNumber) = (" +
" SELECT Max(count) FROM (" +
" SELECT Count(RentalNumber) as count FROM Rentals GROUP BY UserID" +
" ))";
// Set up
System.out.println("\n---- Member with Most Rentals ----");
System.out.printf("| %-15s | %-15s |%n", "User ID", "Items Rented");
System.out.println("|-----------------|-----------------|");
// Print
try (Connection conn = DriverManager.getConnection(dbUrl);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
String userId = rs.getString(1);
int count = rs.getInt(2);
System.out.printf("| %-15s | %-15d |%n", userId, count);
}
} catch (SQLException e) {
System.out.println("Error running report: " + e.getMessage());
}
System.out.println("|-----------------|-----------------|");
}
public static void showEquipmentByTypeAndMaxYear(String dbUrl, String type, int maxYear) {
// Revised From Checkpoint 03
String sql = "SELECT Type, Description, Year " +
"FROM Equipment " +
"WHERE Type = ? AND Year <= ?";
System.out.println("\n--- Equipment Report (" + type + " before " + maxYear + ") ---");
System.out.printf("| %-15s | %-30s | %-6s |%n", "Type", "Description", "Year");
System.out.println("|-----------------|--------------------------------|--------|");
try (Connection conn = DriverManager.getConnection(dbUrl);
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, type);
pstmt.setInt(2, maxYear);
try (ResultSet rs = pstmt.executeQuery()) {
boolean equip = false;
while (rs.next()) {
String resType = rs.getString("Type");
String desc = rs.getString("Description");
int year = rs.getInt("Year");
System.out.printf("| %-15s | %-30s | %-6d |%n", resType, desc, year);
equip = true;
}
if (!equip) {
System.out.printf("| No %-12s equipment found before %-4d |%n", type, maxYear);
}
}
} catch (SQLException e) {
System.out.println("Error running report: " + e.getMessage());
}
System.out.println("|-----------------|--------------------------------|--------|\n");
}
public static void rentingCheckouts(String dbURL, String name){
String sql = "SELECT E.EquipmentSerialNumber, E.Model, R.RentalCheckOuts " +
"FROM Equipment E Join Rentals R On E.EquipmentSerialNumber = R.EquipmentSerialNumber Join Customer C On R.UserID = C.UserID " +
"WHERE C.Name = ? ";
System.out.println("\n--- Renting Checkout Report for " + name + " ---");
System.out.printf("| %-30s | %-20s | %-20s |%n", "EquipmentSerialNumber", "Model", "RentalCheckOuts");
System.out.println("|--------------------------------|----------------------|----------------------|");
try (Connection conn = DriverManager.getConnection(dbURL);
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, name);
try (ResultSet rs = pstmt.executeQuery()) {
boolean equip = false;
while (rs.next()) {
String EquipmentSerialNumber = rs.getString("EquipmentSerialNumber");
String Model = rs.getString("Model");
String RentalCheckOuts = rs.getString("RentalCheckOuts");
System.out.printf("| %-30s | %-20s | %-20s |%n", EquipmentSerialNumber, Model, RentalCheckOuts);
equip = true;
}
if (!equip) {
System.out.println("| No popular items found |");
}
}
} catch (SQLException e) {
System.out.println("Error running report: " + e.getMessage());
}
System.out.println("|------------------------------------------------------------------------------|\n");
}
public static void popularItem(String dbURL){
String sql = "SELECT e.equipmentSerialNumber, e.description, e.model, count(*) AS timesRented " +
"FROM equipment e JOIN rentals r ON e.equipmentSerialNumber = r.equipmentSerialNumber " +
"GROUP BY e.equipmentSerialNumber, e.description, e.model " +
"ORDER BY timesRented DESC ";
System.out.println("\n--- Popular Item Report ---");
System.out.printf("| %-20s | %-30s | %-20s | %-15s |%n", "Equipment Serial", "Description", "Model", "Times Rented");
System.out.println("|----------------------|--------------------------------|----------------------|-----------------|");
try (Connection conn = DriverManager.getConnection(dbURL);
PreparedStatement pstmt = conn.prepareStatement(sql)) {
try (ResultSet rs = pstmt.executeQuery()) {
boolean equip = false;
while (rs.next()) {
String serial = rs.getString("equipmentSerialNumber");
String description = rs.getString("description");
String model = rs.getString("model");
int timesRented = rs.getInt("timesRented");
System.out.printf("| %-20s | %-30s | %-20s | %-15d |%n", serial, description, model, timesRented);
equip = true;
}
if (!equip) {
System.out.println("| No popular items found |");
}
}
} catch (SQLException e) {
System.out.println("Error running report: " + e.getMessage());
}
System.out.println("|------------------------------------------------------------------------------------------------|");
}
public static void popularManufacturer(String dbURL){
String sql = "SELECT distinct m.name " +
"FROM manufacturer m join equipment e on m.manufacturerid = e.manufacturerid join rentals r on e.equipmentserialnumber = r.equipmentserialnumber " +
"WHERE r.userID IN ( " +
" SELECT userID " +
" FROM rentals " +
" GROUP BY userID " +
" HAVING COUNT(*) > " +
"(SELECT avg(total) " +
"FROM (SELECT count(*) as total FROM rentals GROUP BY userID) " +
"))";
System.out.println("\n--- Popular Manufacturer Report ---");
System.out.printf("| %-30s |%n", "Name");
System.out.println("|--------------------------------|");
try (Connection conn = DriverManager.getConnection(dbURL);
PreparedStatement pstmt = conn.prepareStatement(sql)) {
try (ResultSet rs = pstmt.executeQuery()) {
boolean equip = false;
while (rs.next()) {
String name = rs.getString("name");
System.out.printf("| %-30s |%n", name);
equip = true;
}
if (!equip) {
System.out.println("| No popular manufacturers found |");
}
}
} catch (SQLException e) {
System.out.println("Error running report: " + e.getMessage());
}
System.out.println("|--------------------------------|\n");
}
}