-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpenseTracker.java
More file actions
93 lines (83 loc) · 3.47 KB
/
ExpenseTracker.java
File metadata and controls
93 lines (83 loc) · 3.47 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
import java.util.Scanner;
public class ExpenseTracker {
static String[][] expenses = new String[100][3]; // 2D array to store Date, Description, and Amount
static int expenseCount = 0; // Counter for number of expenses
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
System.out.println(welcomeHeading());
while (true) {
System.out.println(menu());
System.out.print("Select an option: ");
String choice = scanner.nextLine();
if (choice.equals("1")) {
System.out.print("Date: ");
String date = scanner.nextLine();
System.out.print("Description: ");
String description = scanner.nextLine();
System.out.print("Amount: ");
try {
String amount = scanner.nextLine();
System.out.println(addExpense(date, description, amount));
} catch (NumberFormatException e) {
System.out.println("\nInvalid input for amount. Please enter a number.\n");
}
} else if (choice.equals("2")) {
System.out.println(viewAllExpenses());
} else if (choice.equals("3")) {
System.out.println(calculateTotalExpenses());
} else if (choice.equals("4")) {
System.out.println("Thank you for using Semicolon's ExpenseTracker App <<<<<SIIIUUUUU>>>>> ");
break;
} else {
System.out.println("Oga input a valid option from the Menu.");
}
}
}
public static String welcomeHeading() {
return """
Welcome To Semicolon Expense Tracker Application
________________________________________________
""";
}
public static String menu() {
return """
1. Add An Expense
2. View All Expenses
3. Calculate Total
4. Exit
""";
}
public static String addExpense(String date, String description, String amount) {
if (expenseCount < expenses.length) {
expenses[expenseCount][0] = date;
expenses[expenseCount][1] = description;
expenses[expenseCount][2] = amount;
expenseCount++;
return "\n<<< Expense Added Successfully >>>\n";
} else {
return "\nExpense list is full! Cannot add more expenses.\n";
}
}
public static String viewAllExpenses() {
if (expenseCount == 0) {
return "Oga can't you see that expenses have not been recorded? Please add an expense.";
}
String result = "\n <<< Expenses >>> \n";
for (int i = 0; i < expenseCount; i++) {
result += (i + 1) + ". Date: " + expenses[i][0] +
", Description: " + expenses[i][1] +
", Amount: #" + expenses[i][2] + "\n";
}
return result;
}
public static String calculateTotalExpenses() {
if (expenseCount == 0) {
return "Oga, Oga, Oga there is no expense to calculate.\nPlease add an expense.";
}
int total = 0;
for (int i = 0; i < expenseCount; i++) {
total += Integer.parseInt(expenses[i][2]);
}
return "Total Expenses: #" + total;
}
}