-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebtRepaymentPage.java
More file actions
114 lines (100 loc) · 3.53 KB
/
DebtRepaymentPage.java
File metadata and controls
114 lines (100 loc) · 3.53 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
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.List;
import java.util.Scanner;
public class DebtRepaymentPage
{
private static final Scanner scanner = new Scanner(System.in);
public static void handleDebtRepaymentPage()
{
while (true)
{
System.out.println();
System.out.println("1. Add a new debt");
System.out.println("2. View all existing debts");
System.out.println("3. View total debts");
System.out.println("0. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine();
if (choice == 0)
{
System.out.println();
System.out.println("-----------------------------------");
return;
}
else if (choice == 1)
{
addNewDebt();
}
else if (choice == 2)
{
viewExistingDebts();
}
else if (choice == 3)
{
viewTotalDebt();
}
else
{
System.out.println("Invalid choice");
}
}
}
private static void addNewDebt()
{
System.out.println();
System.out.println("Enter new debt details:");
System.out.print("Amount: ");
double amount = scanner.nextDouble();
System.out.print("APR (%): ");
double apr = scanner.nextDouble();
scanner.nextLine();
System.out.print("Due Date (yyyy-MM-dd): ");
String dueDate = scanner.nextLine();
try
{
LocalDate deptDate = LocalDate.parse(dueDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
Debt debt = new Debt(amount, apr, deptDate);
if (DebtValidator.isValid(debt))
{
DebtService.saveDebt(debt);
System.out.println();
System.out.println("Debt added successfully");
System.out.println("Updated total debt: " + DebtService.calculateTotalDebt());
System.out.println();
}
else
{
System.out.println("Error: Invalid data (amount must be positive, APR must be positive, due date must be in the in future).");
}
}
catch(DateTimeParseException e)
{
System.out.println("Invalid date format. Please use yyyy-MM-dd");
}
}
private static void viewExistingDebts()
{
List<Debt> debts = DebtService.getAllDebts();
if (debts.isEmpty())
{
System.out.println("No debts found.");
return;
}
System.out.println();
System.out.println("All Existing Debts:");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
for (int i = 0; i < debts.size(); i++)
{
Debt debt = debts.get(i);
System.out.println((i + 1) + ". " + debt.getAmount() + " - " + debt.getApr() + " - " + debt.getDueDate().format(formatter));
}
}
private static void viewTotalDebt()
{
System.out.println();
System.out.println("Total Debt: " + DebtService.calculateTotalDebt());
}
}