-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBudgetTime.java
More file actions
280 lines (256 loc) · 8.71 KB
/
BudgetTime.java
File metadata and controls
280 lines (256 loc) · 8.71 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
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* Class to take Budget input and store it
*
* @author Sam
*/
public class BudgetTime {
/**
* Set recurring budget over a time period
*
* @throws IOException Handled in main along with other IO exceptions to reduce
* error handling code
*/
private void addBudget() throws IOException {
int numberOfDays = timeInput(); // Gets time input from user and converts to days
if (numberOfDays == 0) {
return; // Quit if time input failed
}
System.out.println("Please enter a budget (£) for the selected time period");
String amountString = App.userIn.readLine();
double budgetAmount;
if(!Validation.isDouble(amountString) || (budgetAmount = 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;
}
}
// Write SQL statement here then pass to method
// Allow for option to select category
System.out.println("Do you wish to set this budget for an existent category? (Y)es/(N)o");
String option = App.userIn.readLine();
String category = "Unknown";
if(option.toLowerCase().equals("y")){
displayCategories();
System.out.println("Enter the name of the category: ");
category = App.userIn.readLine();
while (!exists(category)){
System.out.println("This category does not exist, please enter an existent one: ");
category = App.userIn.readLine();
}
}
RetrieveAndStore.sqlExecute("INSERT INTO tblBudget (BudgetAmount, NumberOfDays, Category) VALUES ("
+ budgetAmount + ", " + numberOfDays + ", '" + category + "')");
System.out.println("Success a budget has been set for £" + budgetAmount + " every " + numberOfDays +
" days in " + category + "!");
RetrieveAndStore.rowNumberUpdater("tblBudget","BudgetID");
}
/**
* List all categories existent to let the user choose
*/
private void displayCategories(){
ResultSet rs = RetrieveAndStore.readAllRecords("tblCategory");
try{
System.out.println("Please choose a category from the following: ");
while (rs.next()){
System.out.println(rs.getString("CategoryName"));
}
}catch (SQLException e){
e.printStackTrace();
}
}
/**
* Checks if the category typed by the user exists
* @param categoryName String representing category name
* @return true if category exists
* false otherwise
*/
private boolean exists(String categoryName){
ResultSet rs = RetrieveAndStore.readAllRecords("tblCategory");
try{
while(rs.next()){
if(rs.getString("CategoryName").equals(categoryName)) return true;
}
return false;
} catch (SQLException e){
e.printStackTrace();
return false;
}
}
/**
* Print the budgets from the database to the user
*/
private void printBudgets() {
ResultSet rs = RetrieveAndStore.readAllRecords("tblBudget");
try {
while (rs.next()) // Loop through the resultset
{
// Store each budget record to print
int id = rs.getInt("BudgetID");
int budget = rs.getInt("BudgetAmount");
int days = rs.getInt("NumberOfDays");
String category = rs.getString("Category");
// print the results
System.out.format("%s. Budget amount = £%s, Number of days = %s, Category = %s\n", id, budget, days, category);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Delete a stored budget
*
* @throws IOException Handled in main along with other IO exceptions to reduce
* error handling code
*/
private void deleteBudget() throws IOException {
printBudgets(); // 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("tblBudget", "BudgetID"), Integer.parseInt(input))) {
return;
}
RetrieveAndStore.sqlExecute("DELETE FROM tblBudget WHERE BudgetID = '" + input + "'"); // Call method to execute
// deletion
System.out.format("Record %s deleted successfully\n", input); // Tell the user record has been removed
}
/**
* Amend a stored budget
*
* @throws IOException Handled in main along with other IO exceptions to reduce
* error handling code
*/
private void amendBudget() throws IOException {
printBudgets(); // Print all budgets to the console
System.out.println("Please enter a record number to amend");
String input = App.userIn.readLine();
if (!Validation.isInteger(input)) { // Recordnumber input validated
return;
}
int recordNumber = Integer.parseInt(input);
if (!Validation.isRangeValid(1, RetrieveAndStore.maxID("tblBudget", "BudgetID"), recordNumber)) {
return;
}
System.out.println("Would you like to change:\n 1.budget\n 2.time frame?\n 3.category"); // Find out what the user wants to
// amend
input = App.userIn.readLine();
switch (input) {
case "1": // Switch statement to deal with both cases of amendments
System.out.println("Please enter an amount (£)");
String amountString = App.userIn.readLine();
double amount;
if(!Validation.isDouble(amountString) || (amount = 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;
}
}
RetrieveAndStore
.sqlExecute("UPDATE tblBudget SET BudgetAmount = " + amount + " WHERE BudgetID = " + recordNumber);
break;
case "2":
int days = timeInput();
RetrieveAndStore
.sqlExecute("UPDATE tblBudget SET NumberOfDays = " + days + " WHERE BudgetID = " + recordNumber);
break;
case "3":
displayCategories();
String category = App.userIn.readLine();
while (!exists(category)){
System.out.println("Category does not exist, please enter an existent one.");
category = App.userIn.readLine();
}
RetrieveAndStore
.sqlExecute("UPDATE tblBudget SET Category = '" + category + "' WHERE BudgetID = " + recordNumber);
break;
default:
System.out.println("Not an option, try again"); // Filters out invalid input
}
}
/**
* Take input of time period and convert to days
*
* @return time period for goal in days
* @throws IOException Handled in main along with other IO exceptions to reduce
* error handling code
*/
private int timeInput() throws IOException {
int timePeriod = 0;
int lengthTime = 0;
String input; // To hold input from user via console
System.out.println("Would you like to input your budget in: \n 1.Days \n 2.Calendar Months \n 3.Years");
input = App.userIn.readLine();
if (!Validation.isInteger(input)) {
return 0;
}
timePeriod = Integer.parseInt(input);
if (!Validation.isRangeValid(1, 3, timePeriod)) {
return 0;
}
System.out.println("Please enter the number of days/months/years you would like this goal for");
input = App.userIn.readLine();
if (!Validation.isInteger(input)) {
return 0;
}
lengthTime = Integer.parseInt(input);
// if statement to normalise input to number of days ready to be stored
if (timePeriod == 1) {
return lengthTime;
} else if (timePeriod == 2) {
lengthTime *= 28;
return lengthTime;
} else if (timePeriod == 3) {
lengthTime *= 365;
return lengthTime;
}
return 0;
}
/**
* Called from the main program to give user budget options Handles all
* IOExceptions thrown at other points in class
*
* @throws IOException
*/
public void mainMenu() throws IOException {
boolean loop = true;
while (loop) {
System.out.println(
"Would you like to:\n 1. Add a budget\n 2. Remove a budget\n 3. Amend a budget\n 4. View all budgets\n 5. Quit to main menu");
String input = App.userIn.readLine();
switch (input) { // Use user input to decide which action to complete
case "1":
addBudget();
break;
case "2":
deleteBudget();
break;
case "3":
amendBudget();
break;
case "4":
printBudgets();
break;
case "5":
loop = false;
default: // Filter out invalid inputs
System.out.println("Not an option, try again");
}
}
}
}