-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJoyfulAdventuresPark.java
More file actions
146 lines (126 loc) · 4.77 KB
/
Copy pathJoyfulAdventuresPark.java
File metadata and controls
146 lines (126 loc) · 4.77 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
/*This Java program simulates a theme park management system, featuring user classes for visitors and payment
managers. It enables functionalities such as user registration, attraction booking, total bill calculation, and
data persistence via file I/O. The main class, JoyfulAdventuresPark, offers a user-friendly menu interface for tasks
like registration, login (future enhancement), and program termination. Leveraging object-oriented principles and
error-handling mechanisms, the system ensures robustness and data integrity.*/
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
class User {
String username;
String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
}
class Visitor extends User {
ArrayList<String> bookedAttractions;
public Visitor(String username, String password) {
super(username, password);
this.bookedAttractions = new ArrayList<>();
}
public void bookAttraction(String attractionName) {
bookedAttractions.add(attractionName);
}
public void cancelAttraction(String attractionName) {
bookedAttractions.remove(attractionName);
}
public ArrayList<String> getBookedAttractions() {
return bookedAttractions;
}
}
class PaymentManager extends User {
private double totalBill;
public PaymentManager(String username, String password) {
super(username, password);
this.totalBill = 0.0;
}
public void calculateTotalBill(ArrayList<String> bookedAttractions) {
for (String attraction : bookedAttractions) {
switch (attraction) {
case "Merry-Go-Round":
totalBill += 5.0;
break;
case "Roller Coaster":
totalBill += 8.0;
break;
case "Haunted House":
totalBill += 10.0;
break;
// Add more attractions and their prices as needed
default:
System.out.println("Invalid attraction: " + attraction);
}
}
}
public double getTotalBill() {
return totalBill;
}
}
public class JoyfulAdventuresPark {
static ArrayList<User> users = new ArrayList<>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
loadUsers();
while (true) {
System.out.println("\nJoyful Adventures Park");
System.out.println("1. Register");
System.out.println("2. Login");
System.out.println("3. Exit");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
registerVisitor(scanner);
break;
case 2:
// Implement login logic here
break;
case 3:
System.out.println("Exiting... Goodbye!");
System.exit(0);
default:
System.out.println("Invalid option! Please choose again.");
}
}
}
public static void registerVisitor(Scanner scanner) {
System.out.print("Enter your username: ");
String username = scanner.nextLine();
System.out.print("Enter your password: ");
String password = scanner.nextLine();
users.add(new Visitor(username, password)); // Creating Visitor object during registration
saveUsers();
System.out.println("Registration successful! You can now login.");
}
public static void loadUsers() {
try {
File file = new File("users.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String[] parts = scanner.nextLine().split(",");
users.add(new Visitor(parts[0], parts[1]));
}
scanner.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void saveUsers() {
try {
FileWriter writer = new FileWriter("users.txt");
for (User user : users) {
if (user instanceof Visitor) {
writer.write(((Visitor) user).username + "," + ((Visitor) user).password + "\n");
}
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}