Repository navigation
Expand file tree
/
Copy pathRecordSystem.java
More file actions
117 lines (105 loc) · 3.41 KB
/
Copy pathRecordSystem.java
File metadata and controls
117 lines (105 loc) · 3.41 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
//import java.util.*;
import java.util.ArrayList;
import java.util.Scanner;
class Student {
private int id;
private String name;
private double marks;
public Student(int id, String name, double marks) {
this.id = id;
this.name = name;
this.marks = marks;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public double getMarks() {
return marks;
}
public void setName(String name) {
this.name = name;
}
public void setMarks(double marks) {
this.marks = marks;
}
@Override
public String toString() {
return "ID: " + id + ", Name: " + name + ", Marks: " + marks;
}
}
public class RecordSystem {
private static ArrayList<Student> students = new ArrayList<>();
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
while (true) {
System.out.println("\n1. Add Student\n2. View Students\n3. Update Student\n4. Delete Student\n5. Exit");
System.out.print("Choose option: ");
int choice = sc.nextInt();
switch (choice) {
case 1: addStudent();
break;
case 2: viewStudents();
break;
case 3: updateStudent();
break;
case 4: deleteStudent();
break;
case 5: System.exit(0);
default: System.out.println("Invalid option.");
}
}
}
private static void addStudent() {
System.out.print("Enter ID: ");
int id = sc.nextInt();
sc.nextLine(); // consume newline
System.out.print("Enter Name: ");
String name = sc.nextLine();
System.out.print("Enter Marks: ");
double marks = sc.nextDouble();
students.add(new Student(id, name, marks));
System.out.println("Student added.");
}
private static void viewStudents() {
if (students.isEmpty()) {
System.out.println("No students found.");
return;
}
for (Student s : students) {
System.out.println(s);
}
}
private static void updateStudent() {
System.out.print("Enter ID to update: ");
int id = sc.nextInt();
for (Student s : students) {
if (s.getId() == id) {
sc.nextLine(); // consume newline
System.out.print("Enter new Name: ");
String name = sc.nextLine();
System.out.print("Enter new Marks: ");
double marks = sc.nextDouble();
s.setName(name);
s.setMarks(marks);
System.out.println("Student updated.");
return;
}
}
System.out.println("Student not found.");
}
private static void deleteStudent() {
System.out.print("Enter ID to delete: ");
int id = sc.nextInt();
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId() == id) {
students.remove(i);
System.out.println("Student deleted.");
return;
}
}
System.out.println("Student not found.");
}
}