-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
222 lines (187 loc) · 8.09 KB
/
Copy pathMain.java
File metadata and controls
222 lines (187 loc) · 8.09 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import java.util.ArrayList;
import java.util.Scanner;
// Student Class for individual student profiles
class Student {
// Private instance variables for safety
private String name;
private String id;
private int age;
private double grade;
// Constructor to set up student variables
public Student(String name, String id, int age, double grade) {
this.name = name;
this.id = id;
this.age = age;
this.grade = grade;
}
// Getters and Setters to access data safely
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
@Override
public String toString() {
return "ID: " + id + " | Name: " + name + " | Age: " + age + " | Grade: " + grade;
}
}
// StudentManagement Class to handle the main record list using static members
class StudentManagement {
// Private static variables for shared data
private static ArrayList<Student> studentList = new ArrayList<>();
private static int totalStudents = 0;
// Static method to add a student to the list
public static void addStudent(Student student) {
studentList.add(student);
totalStudents++;
System.out.println("Success: Student added successfully.");
}
// Static method to find a student using their ID
public static Student getStudent(String id) {
for (Student s : studentList) {
if (s.getId().equalsIgnoreCase(id)) {
return s;
}
}
return null; // Returns null if the ID does not exist
}
// Static method to change a student's info
public static boolean updateStudent(String id, String newName, int newAge, double newGrade) {
Student student = getStudent(id);
if (student != null) {
student.setName(newName);
student.setAge(newAge);
student.setGrade(newGrade);
return true;
}
return false;
}
// Static getter to read the total student count
public static int getTotalStudents() {
return totalStudents;
}
}
// Main Application Class that runs the menu interface
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean running = true;
System.out.println("=================================================");
System.out.println(" Welcome to the Student Record Management System");
System.out.println("=================================================");
while (running) {
System.out.println("\n--- Administrator Menu ---");
System.out.println("1. Add a New Student");
System.out.println("2. Update Student Information");
System.out.println("3. View Student Details");
System.out.println("4. Exit Application");
System.out.print("Please enter your choice (1-4): ");
String choice = scanner.nextLine().trim();
switch (choice) {
case "1":
System.out.print("Enter Student ID: ");
String id = scanner.nextLine().trim();
// Check if ID is empty or already in the system
if (id.isEmpty()) {
System.out.println("Error: Student ID cannot be empty.");
break;
}
if (StudentManagement.getStudent(id) != null) {
System.out.println("Error: A student with this ID already exists.");
break;
}
System.out.print("Enter Student Name: ");
String name = scanner.nextLine().trim();
if (name.isEmpty()) {
System.out.println("Error: Student name cannot be empty.");
break;
}
try {
System.out.print("Enter Student Age: ");
int age = Integer.parseInt(scanner.nextLine().trim());
if (age < 0 || age > 120) {
System.out.println("Error: Please enter a realistic age.");
break;
}
System.out.print("Enter Student Grade/GPA: ");
double grade = Double.parseDouble(scanner.nextLine().trim());
if (grade < 0.0 || grade > 4.0) {
System.out.println("Error: Grade must be between 0.0 and 4.0.");
break;
}
// Create a new student and save them to the shared list
Student newStudent = new Student(name, id, age, grade);
StudentManagement.addStudent(newStudent);
} catch (NumberFormatException e) {
System.out.println("Error: Wrong input. Age must be a whole number and Grade must be a decimal.");
}
break;
case "2":
System.out.print("Enter the ID of the student to update: ");
String updateId = scanner.nextLine().trim();
Student existingStudent = StudentManagement.getStudent(updateId);
if (existingStudent == null) {
System.out.println("Error: Student ID not found.");
break;
}
System.out.println("Current Details -> " + existingStudent);
System.out.print("Enter New Name: ");
String newName = scanner.nextLine().trim();
if (newName.isEmpty()) {
System.out.println("Error: Name cannot be empty.");
break;
}
try {
System.out.print("Enter New Age: ");
int newAge = Integer.parseInt(scanner.nextLine().trim());
System.out.print("Enter New Grade/GPA: ");
double newGrade = Double.parseDouble(scanner.nextLine().trim());
boolean updated = StudentManagement.updateStudent(updateId, newName, newAge, newGrade);
if (updated) {
System.out.println("Success: Student record updated successfully.");
} else {
System.out.println("Error: Failed to update the record.");
}
} catch (NumberFormatException e) {
System.out.println("Error: Wrong input type. Update stopped.");
}
break;
case "3":
System.out.print("Enter Student ID to view details: ");
String viewId = scanner.nextLine().trim();
Student targetStudent = StudentManagement.getStudent(viewId);
if (targetStudent != null) {
System.out.println("\n--- Student Record Found ---");
System.out.println(targetStudent);
System.out.println("Total Tracked Students in System: " + StudentManagement.getTotalStudents());
} else {
System.out.println("Error: Student ID not found.");
}
break;
case "4":
running = false;
System.out.println("Exiting the application. Goodbye!");
break;
default:
System.out.println("Error: Wrong option. Pick a number between 1 and 4.");
}
}
scanner.close();
}
}