-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentData.java
More file actions
100 lines (88 loc) · 3.33 KB
/
StudentData.java
File metadata and controls
100 lines (88 loc) · 3.33 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
import java.util.Scanner;
import java.util.HashMap;
class Student {
public String name;
public int rollno;
public int age;
public int marks;
public Student(String name, int rollno, int age, int marks) { // constructor funct.
this.name = name;
this.rollno = rollno;
this.age = age;
this.marks = marks;
}
public void ViewStudent() {
System.out.println("Name:" + name);
System.out.println("Rollno:" + rollno);
System.out.println("Age:" + age);
System.out.println("Marks:" + marks);
}
}
public class StudentData {
public static void main(String[] args) {
try{
Scanner sc = new Scanner(System.in);
HashMap<Integer, Student> database = new HashMap<>();
while (true) {
System.out.println(" 1. Add student");
System.out.println(" 2. View student");
System.out.println(" 3. Search student");
System.out.println(" 4. CAlculate avg marks");
System.out.println(" 5. Exit");
System.out.print("Enter your Choice :");
int choice = sc.nextInt();
sc.nextLine();
switch (choice) {
case 1:
System.out.print("Enter your name:");
String name = sc.nextLine();
System.out.print("Enter your roll number :");
int rollno = sc.nextInt();
System.out.print("Enter your age :");
int age = sc.nextInt();
System.out.print("Enter your marks :");
int marks = sc.nextInt();
Student std = new Student(name, rollno, age, marks);
database.put(rollno, std);
System.out.println("Student added successfully");
break;
case 2:
System.out.println("List of Students:");
for (Student i : database.values()) {
i.ViewStudent();
}
break;
case 3:
System.out.println("Enter your rollno.");
int roll = sc.nextInt();
Student data = database.get(roll);
if (data != null) {
data.ViewStudent();
} else {
System.out.println("Student not found");
}
System.out.println("*************************************************");
break;
case 4:
int totalStudents = database.size();
int totalMarks = 0;
for (Student j : database.values()) {
totalMarks = totalMarks + j.marks;
double average = (double) totalMarks / totalStudents;
System.out.println("Average Marks:" + average);
}
break;
case 5:
System.exit(0);
break;
default:
System.out.println("Invaild Choice :");
break;
}
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
}