forked from samuelstacey/Financial-Tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIncomes.java
More file actions
342 lines (319 loc) · 14.4 KB
/
Incomes.java
File metadata and controls
342 lines (319 loc) · 14.4 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
public class Incomes {
/**
* Presents users with option and calls correct method based on input
*/
public void mainMenu(){
boolean running = true;
while (running) {
System.out.println("Please select an option by using the character in brackets:\n" +
"1. Add new income\n" +
"2. View all incomes\n" +
"3. View total monthly income\n" +
"4. Edit an income\n" +
"5. Remove income\n" +
"6. Return to main menu");
try {
String input = App.userIn.readLine();
switch (input) {
case "1":
addIncome();
break;
case "2":
viewIncomes();
break;
case "3":
System.out.println(totalIncome());
break;
case "4":
editIncome();
break;
case "5":
System.out.println("Please enter the income you would like to remove (exactly as it appears)");
removeIncome();
break;
case "6":
running = false;
System.out.println("Exiting to Main Menu...\n\n");
break;
default:
System.out.println("Not an option. Choose again");
}
} catch(IOException e) {
e.printStackTrace();
System.out.println("Unable to take input from console");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("Error with DB");
}
}
}
/**
* will sum all incomes
* @return sum of all income streams
*/
public float totalIncome() { //Loop through and add all incomes then print per month with formatting of string
ResultSet rs = RetrieveAndStore.readAllRecords("tblIncomes");
float incomeTotal = 0;
try {
while (rs.next()) //Loop through the resultset to sum total income
{
incomeTotal += rs.getFloat("MonthlySalary");
}
} catch (SQLException e) {
e.printStackTrace();
}
return incomeTotal;
}
private void viewIncomes() {
ResultSet rs = RetrieveAndStore.readAllRecords("tblIncomes");
try {
while (rs.next()) //Loop through the resultset
{
//Store each income record to print
int id = rs.getInt("IncomeID");
double monthlyIncome = rs.getDouble("MonthlySalary");
String name = rs.getString("IncomeName");
// print the results
System.out.println(String.format("\n%d: %s\n£%.2f\n", id,
name, monthlyIncome));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* Takes an input on the frequency of the payment and subsequent inputs to calculate monthly income
* Will add income stream to the properties file
* @throws IOException
*
*/
private void addIncome() throws IOException, SQLException {
// Name
boolean cont = false;
System.out.println("First, set a name for this category?");
String name = App.userIn.readLine();
while(!cont){
if(RetrieveAndStore.exists("tblIncomes","IncomeName",name)){
System.out.println("A pool with that name already exists, try another:");
name = App.userIn.readLine();
} else {
cont = true;
}
}
double customPay;
double monthlySalary = 0;
boolean validInput = false; //used to verify they choose a frequency option
System.out.println("How frequently do you get paid?\n" +
"(H)ourly\n" +
"(D)aily\n" +
"(W)eekly\n" +
"(M)onthly\n" +
"(Y)early");
String frequency = App.userIn.readLine(); //take input from console
while(!validInput) {
validInput=true;
switch (frequency) {
case "h":
case "H": //if input is hour
System.out.println("How many hours a week?");
String amountString = App.userIn.readLine();
double hoursPerWeek;
if(!Validation.isDouble(amountString) || (hoursPerWeek = Double.parseDouble(amountString)) <= 0) {
System.out.println("Invalid amount given\n\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("How much do you get per hour?");
amountString = App.userIn.readLine();
if(!Validation.isDouble(amountString) || Double.parseDouble(amountString) <= 0) {
System.out.println("Invalid amount given\n\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;
}
}
customPay = Float.parseFloat(amountString);
monthlySalary = ((customPay*hoursPerWeek*52)/12); //work out hours worked per week then convert to monthly
break;
case "d":
case "D":
System.out.println("How much do you get per day?");
amountString = App.userIn.readLine();
if(!Validation.isDouble(amountString) || Double.parseDouble(amountString) <= 0) {
System.out.println("Invalid amount given\n\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;
}
}
customPay = Float.parseFloat(amountString);
monthlySalary = (customPay*5*52)/12; //5 days per week then same as weekly pay
break;
case "w":
case "W":
System.out.println("How much do you get per week?");
amountString = App.userIn.readLine();
if(!Validation.isDouble(amountString) || (customPay = Double.parseDouble(amountString)) <= 0) {
System.out.println("Invalid amount given\n\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;
}
}
monthlySalary = (customPay*52)/12; //52 weeks in a year, but / by 12 for per month salary
break;
case "m":
case "M":
System.out.println("How much do you get per month?");
amountString = App.userIn.readLine();
if(!Validation.isDouble(amountString) || (monthlySalary = Double.parseDouble(amountString)) <= 0) {
System.out.println("Invalid amount given\n\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;
}
}
break;
case "y":
case "Y":
System.out.println("How much do you get per year?");
amountString = App.userIn.readLine();
if(!Validation.isDouble(amountString) || (customPay = Double.parseDouble(amountString)) <= 0) {
System.out.println("Invalid amount given\n\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;
}
}
monthlySalary = customPay/12;
break;
default:
validInput = false;
System.out.println("Not a valid Input, try again:");
frequency = App.userIn.readLine();
}
}
//Add the record to the database
RetrieveAndStore.sqlExecute("INSERT INTO tblIncomes (IncomeID, IncomeName, MonthlySalary) VALUES (" +
(RetrieveAndStore.maxID("tblIncomes", "IncomeID") + 1) + ", '" +
name + "', " + monthlySalary + ")");
RetrieveAndStore.rowNumberUpdater("tblIncomes", "IncomeID");
RetrieveAndStore.rowNumberUpdater("tblIncomes", "IncomeID");
}
private void editIncome() throws SQLException, IOException {
ResultSet rs = RetrieveAndStore.readAllRecords("tblIncomes");
while(rs.next()){
System.out.println(String.format("\n%d: %s\n£%.2f\n", rs.getInt("IncomeID"),
rs.getString("IncomeName"), rs.getDouble("MonthlySalary")));
}
System.out.println("Please enter the ID of the income 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("tblIncomes","IncomeID"),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("tblIncomes","IncomeName",newValueString)){
System.out.println("An income 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","tblIncomes",columnName,
newValueString, "IncomeID", 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","tblIncomes",columnName,
newAmount, "IncomeID", id));
}
}
System.out.println("Successfully updated field!");
break;
}
}
/**
* will remove an income from the properties folder
* @throws IOException
*/
private void removeIncome() throws IOException {
viewIncomes(); //Print all budgets to the console
System.out.println("Please enter a record number to delete");
String input = App.userIn.readLine();
if (!Validation.isInteger(input)) {
return;
}
if (!Validation.isRangeValid(1, RetrieveAndStore.maxID("tblIncomes", "IncomeID"), Integer.parseInt(input))) {
return;
}
RetrieveAndStore.sqlExecute("DELETE FROM tblIncomes WHERE IncomeID = '" + input + "'"); //Call method to execute deletion
System.out.format("Record %s deleted successfully\n", input); //Tell the user record has been removed
RetrieveAndStore.rowNumberUpdater("tblIncomes","IncomeID");
}
}
/* TODO
comments
*/