-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRecurringOutgoings.java
More file actions
214 lines (200 loc) · 8.63 KB
/
RecurringOutgoings.java
File metadata and controls
214 lines (200 loc) · 8.63 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
public class RecurringOutgoings {
/**
* will ask for which option on
* takes input from main menu and calls correct method
* @throws IOException
*/
public void mainMenu() throws IOException {
boolean running = true;
try {
while (running) {
System.out.println("Please select an option by using the character in brackets:\n" +
"1. Add new regular payment (Monthly)\n" +
"2. View all outgoings\n" +
"3. Your total outgoings\n" +
"4. Edit an outgoing\n" +
"5. Remove outgoing\n" +
"6. Return to main menu");
String input = App.userIn.readLine();
switch (input) {
case "1":
addOutgoing();
break;
case "2":
viewOutgoings();
break;
case "3":
System.out.println("Your total outgoings:\n£" + totalOutgoings());
break;
case "4":
editOutgoing();
break;
case "5":
removeOutgoing();
System.out.println("Successfully removed outgoing");
break;
case "6":
running = false;
System.out.println("Exiting to Main Menu...\n\n");
break;
default:
System.out.println("Not an option, try again.");
break;
}
}
} catch (SQLException e) {
e.printStackTrace();
System.out.println("Error with DB");
}
}
/**
* will sum all outgoings
* @return sum of all outgoings
*/
public float totalOutgoings() {
ResultSet rs = RetrieveAndStore.readAllRecords("tblOutgoings");
float total = 0;
try {
while (rs.next()) // Loop through the resultset
{
total += rs.getInt("OutgoingAmount"); //Sum total of all outgoings
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return total;
}
private void viewOutgoings() {
ResultSet rs = RetrieveAndStore.readAllRecords("tblOutgoings");
try {
while (rs.next()) // Loop through the resultset
{
// Store each outgoing record to print
int id = rs.getInt("OutgoingID");
String name = rs.getString("OutgoingName");
double amount = rs.getDouble("OutgoingAmount");
// print the results
System.out.format("\n%s: %s\n£%.2f\n", id, name, amount);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* will add an outgoing to the outgoings in properties file
* @throws IOException
* @throws NumberFormatException
*/
private void addOutgoing() throws NumberFormatException, IOException {
System.out.println("How much will you be paying per month?");
String amountString = App.userIn.readLine();
double amount;
if(!Validation.isDouble(amountString) || (amount = Double.parseDouble(amountString)) <= 0) {
System.out.println("Invalid amount given\n");
return;
}
if (amountString.contains(".")) {
int decimalPos = amountString.indexOf('.');
if ((amountString.length() - 1) - decimalPos > 2) {
System.out.println("Invalid number, too precise\n\n");
return;
}
}
System.out.println("What is the name of this payment?");
String name = App.userIn.readLine();
RetrieveAndStore.sqlExecute("INSERT INTO tblOutgoings (OutgoingID, OutgoingName, OutgoingAmount) VALUES" +
" (" + (RetrieveAndStore.maxID("tblOutgoings", "OutgoingID") + 1) + ", '" + name + "', "
+ amount + ")");
System.out.println("Successfully added new outgoing\n");
RetrieveAndStore.rowNumberUpdater("tblOutgoings","OutgoingID");
}
private void editOutgoing() throws SQLException, IOException {
ResultSet rs = RetrieveAndStore.readAllRecords("tblOutgoings");
while(rs.next()){
System.out.println(String.format("\n%d: %s\n£%.2f\n", rs.getInt("OutgoingID"),
rs.getString("OutgoingName"), rs.getDouble("OutgoingAmount")));
}
System.out.println("Please enter the ID of the outgoing you wish to update");
String idString = App.userIn.readLine();
if(!(Validation.isInteger(idString))) return;
int id = Integer.parseInt(idString);
if(!Validation.isRangeValid(1,RetrieveAndStore.maxID("tblOutgoings","OutgoingID"),id)) return;
System.out.println("Great, and which field would you like to update?");
ResultSetMetaData rsmd = rs.getMetaData();
for (int i = 2; i <= rsmd.getColumnCount(); i++) { // Loop through all columns and print column name
System.out.println(String.format("%d: %s", i-1, rsmd.getColumnName(i)));
}
// They choose one of the columns printed above
String columnIntString = App.userIn.readLine();
if (!Validation.isInteger(columnIntString)) return;
int columnInt = Integer.parseInt(columnIntString)+1;
if (!Validation.isRangeValid(2, rsmd.getColumnCount(), columnInt)) return;
String columnName = rsmd.getColumnName(columnInt);
System.out.println("Please input a new value for " + columnName);
String newValueString = App.userIn.readLine();
boolean cont = false;
switch (columnInt){
case 2:
// Name
while(!cont){
if(RetrieveAndStore.exists("tblOutgoings","OutgoingName",newValueString)){
System.out.println("An outgoing with that name already exists, try another:");
newValueString = App.userIn.readLine();
} else {
cont = true;
}
}
RetrieveAndStore.sqlExecute(String.format("UPDATE %s SET %s = '%s' WHERE %s = %d","tblOutgoings",columnName,
newValueString, "OutgoingID", id));
System.out.println("Successfully updated field!");
break;
case 3:
double newAmount;
while(!cont){
if(!(Validation.isDouble(newValueString)) || Double.parseDouble(newValueString) <= 0){
System.out.println("Invalid amount, try another:");
newValueString = App.userIn.readLine();
} else {
if(newValueString.contains(".")){
int decimalPos = newValueString.indexOf('.');
if ((newValueString.length() - 1) - decimalPos > 2) {
System.out.println("Invalid number, too precise, try again:");
newValueString = App.userIn.readLine();
continue;
}
}
newAmount = Double.parseDouble(newValueString);
cont = true;
RetrieveAndStore.sqlExecute(String.format("UPDATE %s SET %s = %.2f WHERE %s = %d","tblOutgoings",columnName,
newAmount, "OutgoingID", id));
}
}
System.out.println("Successfully updated field!");
break;
default:
System.out.println("Not an option");
break;
}
}
private void removeOutgoing() throws IOException {
viewOutgoings();
System.out.println("Please type the record number of the record you would like to delete");
String value = App.userIn.readLine();
if (!Validation.isInteger(value)) {
return;
}
if (!Validation.isRangeValid(1, RetrieveAndStore.maxID("tblOutgoings", "OutgoingID"), Integer.parseInt(value))) {
return;
}
RetrieveAndStore.sqlExecute("DELETE FROM tblOutgoings WHERE OutgoingID = " + value); // Call method to execute
// deletion
System.out.format("Record %s deleted successfully\n", value); // Tell the user record has been removed
RetrieveAndStore.rowNumberUpdater("tblOutgoings","OutgoingID");
}
}