-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSavingsGoalsPage.java
More file actions
84 lines (73 loc) · 2.35 KB
/
SavingsGoalsPage.java
File metadata and controls
84 lines (73 loc) · 2.35 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
import java.util.List;
import java.util.Scanner;
public class SavingsGoalsPage
{
private static Scanner scanner = new Scanner(System.in);
public static void handleSavingsPage()
{
while (true)
{
System.out.println();
System.out.println("1. Create a new savings goal");
System.out.println("2. View all goals");
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)
{
createGoal();
}
else if(choice == 2)
{
viewAllGoals();
}
else
{
System.out.println("Invalid choice");
}
}
}
public static void createGoal()
{
System.out.println();
System.out.print("Enter goal name: ");
String goalName = scanner.nextLine();
System.out.print("Enter target amount: ");
double targetAmount = scanner.nextDouble();
scanner.nextLine();
System.out.println();
SavingsGoal goal = new SavingsGoal(goalName, targetAmount);
if (SavingsValidator.isValid(goal))
{
SavingsService.saveGoal(goal);
System.out.println("Goal saved successfully");
}
else
{
System.out.println("Invalid data (name cannot be empty, amount must be positive).");
}
}
private static void viewAllGoals()
{
List<SavingsGoal> goals = SavingsService.getAllGoals();
if (goals.isEmpty())
{
System.out.println("No goals found.");
return;
}
System.out.println();
System.out.println("All Savings Goals:");
for (int i = 0; i < goals.size(); i++)
{
SavingsGoal goal = goals.get(i);
System.out.println((i + 1) + ". " + goal.getGoalName() + " - " + goal.getTargetAmount());
}
}
}