-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDonationPage.java
More file actions
92 lines (80 loc) · 2.64 KB
/
DonationPage.java
File metadata and controls
92 lines (80 loc) · 2.64 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
import java.util.Scanner;
import java.util.List;
public class DonationPage
{
private static Scanner scanner = new Scanner(System.in);
public static void handleDonationPage()
{
while (true)
{
System.out.println();
System.out.println("1. Make a donation");
System.out.println("2. View donation history");
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)
{
makeDonation();
}
else if (choice == 2)
{
viewDonationHistory();
}
else
{
System.out.println("Invalid choice");
}
}
}
private static void makeDonation()
{
System.out.println();
System.out.print("Enter charity name: ");
String charity = scanner.nextLine();
System.out.print("Enter donation amount: ");
double amount = scanner.nextDouble();
scanner.nextLine();
Donation donation = new Donation(charity, amount);
if (DonationValidator.isValid(donation))
{
if (PaymentGateway.processPayment(donation))
{
DonationService.saveDonation(donation);
System.out.println();
System.out.println("Thank you for your donation to " + charity);
}
else
{
System.out.println("Payment processing failed");
}
}
else
{
System.out.println("Error: Amount must be positive and charity name cannot be empty");
}
}
private static void viewDonationHistory()
{
List<Donation> donations = DonationService.getAllDonations();
if (donations.isEmpty())
{
System.out.println("No donations found");
return;
}
System.out.println();
System.out.println("Donation History:");
for (int i = 0; i < donations.size(); i++)
{
Donation d = donations.get(i);
System.out.println((i + 1) + ". " + d.getCharity() + " - " + d.getAmount());
}
}
}