forked from wwatncm3/comp360-project3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegistrationSystem.java
More file actions
162 lines (131 loc) · 4.65 KB
/
Copy pathRegistrationSystem.java
File metadata and controls
162 lines (131 loc) · 4.65 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
/**
* RegistrationSystem class for managing student registrations
* and handling exceptions related to class capacity.
*/
public class RegistrationSystem {
private final int MAX_SEATS = 30;
private boolean[] seats;
private String[] registrationDates;
private String[] studentIds;
private String[] studentNames;
// Constructor for RegistrationSystem
public RegistrationSystem() {
seats = new boolean[MAX_SEATS];
registrationDates = new String[MAX_SEATS];
studentIds = new String[MAX_SEATS];
studentNames = new String[MAX_SEATS];
// Initialize all seats to false (unoccupied)
for (int i = 0; i < MAX_SEATS; i++) {
seats[i] = false;
}
}
// Check if the class is full
public boolean isClassRoomFull() {
for (boolean seat : seats) {
if (!seat) {
return false;
}
}
// All seats are occupied
return true;
}
// Register a student
public int register(String studentName, String studentId) throws ClassFullException{
// Get current date
LocalDate currentDate = LocalDate.now();
String date = currentDate.format(DateTimeFormatter.ofPattern("d"));
String month = currentDate.format(DateTimeFormatter.ofPattern("MMMM"));
// Check if class is full
if (isClassRoomFull()) {
throw new ClassFullException(date, month);
}
// Find the first available seat
int seatNum = - 1;
for (int i = 0; i < seats.length;i++) {
if (!seats[i]) {
seats[i] = true;
registrationDates[i] = month + " "+ date;
studentNames[i] = studentName;
studentIds[i] = studentId;
seatNum = i + 1; // adding 1 to make it 1-indexed
break;
}
}
// Display Registration details
System.out.println("Registration successful!");
System.out.println("Student: " + studentName + " (ID: " + studentId + ")");
System.out.println("Seat Number: " + seatNum);
System.out.println("Registration Date: " + month + " "+ date);
return seatNum;
}
// Cancel registration
public boolean cancelRegistration(int seatNum) {
// Check if seat number is valid
if (seatNum < 1 || seatNum > MAX_SEATS) {
System.out.println("Invalid seat number. Must be between 1 and " + MAX_SEATS);
return false;
}
// Check if seat is already occupied
int index = seatNum -1;
if (!seats[index]) {
System.out.println("Seat: " + seatNum + " is not currently registered.");
return false;
}
// Cancel registration
String studentName = studentNames[index];
seats[index] = false;
studentNames[index] = null;
studentIds[index] = null;
registrationDates[index] = null;
System.out.println("Registration canceled for " + studentName+ " (Seat " + seatNum + ")");
System.out.println("Seat Number: " + seatNum + " is now available.");
return true;
}
// Getters for current number of registered students
public int getRegisteredCount() {
int count = 0;
for (boolean seat : seats) {
if (seat) {
count++;
}
}
return count;
}
// Getters for current number of available seats
public int getAvailableSeats() {
return MAX_SEATS - getRegisteredCount();
}
// Getters for the name of students in a specific seat
public String getStudentName(int seatNum) {
if (seatNum < 1 || seatNum > MAX_SEATS) {
return null;
}
return studentNames[seatNum - 1];
}
// Check if specifc seat is occupied
public boolean isSeatOccupied(int seatNum) {
if (seatNum < 1 || seatNum > MAX_SEATS) {
return false;
}
return seats[seatNum - 1];
}
// Get the seat number for a student by ID
public int findSeatByStudentId(String studentId) {
for (int i = 0; i < MAX_SEATS; i++) {
if (seats[i] && studentIds[i] != null && studentIds[i].equals(studentId)) {
return i + 1; // Return 1-indexed seat number
}
}
return -1; // Not found
}
// Get the max number of seats
public int getMaxSeats() {
return MAX_SEATS;
}
// An array of all seats seat statuses
public boolean[] getAllSeatStatuses() {
return seats;
}
}