forked from samuelstacey/Financial-Tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandleCategories.java
More file actions
192 lines (178 loc) · 6.48 KB
/
HandleCategories.java
File metadata and controls
192 lines (178 loc) · 6.48 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
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* Class which handles the category management
* @author Paul
*/
public class HandleCategories {
/**
* Prints the option when handling the user's purchases.
* @throws IOException
*/
public void mainMenu() throws IOException{
boolean running = true;
while (running){
System.out.println("Please select an option by using the number associated with each command:\n" +
"1. Add new category\n" +
"2. View all categories\n" +
"3. Edit name of a category\n" +
"4. Remove category\n" +
"5. Return to main menu");
String input = App.userIn.readLine();
switch (input) {
case "1":
addCategory();
break;
case "2":
viewCategories();
break;
case "3":
System.out.println("Please enter the category you would like" +
" to edit the name for (exactly as it appears)");
editDetails();
break;
case "4":
System.out.println("Please enter the category you would like" +
" to remove (exactly as it appears)");
removeCategory();
break;
case "5":
running = false;
break;
default:
System.out.println("Not an option. Choose again");
break;
}
}
}
/**
* Creates a new category and adds it
* to the properties file and to the existent list of categories
* Note: each new category is automatically created with budget 0 and expenditure 0
* @throws IOException
*/
private void addCategory() throws IOException {
System.out.println("Enter the name of the category");
String category = App.userIn.readLine();
if (category.equals("")) {
System.out.println("Empty name...\n\n");
return;
}
if(!existsCategory(category)){
RetrieveAndStore.sqlExecute("INSERT INTO tblCategory (CategoryID, CategoryName, Expenditure) VALUES" +
" (" + (RetrieveAndStore.maxID("tblCategory", "CategoryID") + 1) + ", '" + category + "', 0 )");
System.out.print("Successfully added the category");
} else{
System.out.println("This category already exists.");
}
RetrieveAndStore.rowNumberUpdater("tblCategory","CategoryID");
}
/**
* Checks to see if the category already exists or not
* @param name name of the category
* @return true if the category exists
* false if not
*/
public static boolean existsCategory(String name){
ResultSet rs = RetrieveAndStore.readAllRecords("tblCategory");
try {
while (rs.next()) //Loop through the resultset
{
if (name.equals(rs.getString("CategoryName"))) {
return true;
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
/**
* List all categories
*/
private void viewCategories(){
ResultSet rs = RetrieveAndStore.readAllRecords("tblCategory");
try {
while (rs.next()) //Loop through the resultset
{
int id = rs.getInt("CategoryID");
String name = rs.getString("CategoryName");
int expenditure = rs.getInt("Expenditure");
System.out.format("%s. Category Name = %s, Expenditure = £%s\n", id, name, expenditure);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void outputCategoryNames() {
ResultSet rs = RetrieveAndStore.readAllRecords("tblCategory");
try {
while (rs.next()) //Loop through the result set
{
System.out.println(rs.getString("CategoryName"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Edit the details of a category (name or budget)
* @throws IOException
* @throws NumberFormatException
*/
private void editDetails() throws NumberFormatException, IOException{
viewCategories();
System.out.println("Please enter a category number to amend");
String value = App.userIn.readLine();
if (!Validation.isInteger(value)) { //Record number input validated
return;
}
int recordNumber = Integer.parseInt(value);
if (!Validation.isRangeValid(1, RetrieveAndStore.maxID("tblCategory", "CategoryID"), recordNumber)) {
return;
}
// Modified method to change name only (no need for budgets here anymore)
System.out.println("Enter the new name of the category: ");
String newName = App.userIn.readLine();
if(existsCategory(newName)){
System.out.println("A category with this name already exists so we can't edit the name.");
return;
}
RetrieveAndStore.sqlExecute("UPDATE tblCategory SET CategoryName = '" + newName + "' WHERE CategoryID = " + recordNumber);
}
/**
* Remove a certain category from both the properties file and from the array list
* Update records for all purchases in that category and move them to the Unknown category
* @throws IOException
*/
private void removeCategory() throws IOException{
viewCategories();
String value = App.userIn.readLine();
System.out.println("Please enter a category to be deleted");
if(value.equals("Unknown") || value.equals("Clothes") ||
value.equals("Transport") || value.equals("Groceries")){
System.out.println("You cannot remove this category");
return;
}
ResultSet rs = RetrieveAndStore.readAllRecords("tblCategory");
try {
while (rs.next()) //Loop through the resultset
{
String name = rs.getString("CategoryName");
int expenditure = rs.getInt("Expenditure");
if (name.equals(value)) { //check this works
RetrieveAndStore.sqlExecute("UPDATE tblCategory SET Expenditure = Expenditure + " + expenditure + " WHERE CategoryName = 'Unknown'");
RetrieveAndStore.sqlExecute("DELETE FROM tblCategory WHERE CategoryName = '" + value + "'");
RetrieveAndStore.rowNumberUpdater("tblCategory","CategoryID");
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}