-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainMethod.java
137 lines (116 loc) · 3.52 KB
/
MainMethod.java
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
package com.tyss.finalAssessment;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Scanner;
public class MainMethod {
public static Student AddStudent(List<Student> list) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter\n1.id\n2.name\n3.marks\n4.course");
int id = scan.nextInt();
String name = scan.next();
int marks = scan.nextInt();
String course = scan.next();
Student student = new Student(id, name, marks, course);
for (Student stu : list) {
if(student.getId()== stu.getId());
}
return student;
}
public static void main(String[] args) {
List<Student> list = new LinkedList<Student>();
Scanner sc = new Scanner(System.in);
int choice;
System.out.println("\n 1.Add Student \n 2.Edit Student \n 3.Delete Student \n 4.List Of Students \n 5.Get Student \n 6.Sort \n 7.Exit");
do {
System.out.println("Enter the choice");
choice = sc.nextInt();
switch (choice) {
case 1:
list.add(AddStudent(list));
break;
// AddStudent addStudent = new AddStudent();
// list.add(addStudent.method(list));
// break;
case 2:
System.out.println("Enter the id to be update");
int oldid = sc.nextInt();
ListIterator<Student> listIterator = list.listIterator();
while (listIterator.hasNext()) {
Student s1 = listIterator.next();
if (s1.getId() == oldid) {
System.out.println(" enter the name:");
String newname = sc.next();
System.out.println("enter the marks:");
int newmark = sc.nextInt();
System.out.println("enter the course:");
String newcourse = sc.next();
listIterator.set(new Student(oldid, newname, newmark, newcourse));
System.out.println("edited.");
}
}
break;
// EditStudent editStudent = new EditStudent();
// editStudent.method1(list);
// break;
case 3:
System.out.println("enter the id to delete");
int deleteid = sc.nextInt();
Iterator<Student> iterator2 = list.iterator();
while (iterator2.hasNext()) {
Student s = iterator2.next();
if (s.getId() == deleteid) {
list.remove(s);
}
}
System.out.println("deleted successfully");
break;
case 4:
Iterator<Student> iterator = list.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
break;
case 5:
System.out.println("enter the id to get");
int id = sc.nextInt();
Iterator<Student> iterator3 = list.iterator();
while (iterator3.hasNext()) {
Student s = iterator3.next();
if (id == s.getId()) {
System.out.println(s);
}
}
break;
case 6:
System.out.println("sort the list of students");
System.out.println("1.ById\n2.ByName\n3.ByMarks");
int choice6 = sc.nextInt();
switch (choice6) {
case 1: {
SortById sortById = new SortById();
Collections.sort(list, sortById);
System.out.println("id sorted");
break;
}
case 2: {
SortByName sortByName = new SortByName();
Collections.sort(list, sortByName);
System.out.println("name sorted");
break;
}
case 3: {
SortByMarks sortByMarks = new SortByMarks();
Collections.sort(list, sortByMarks);
System.out.println("marks sorted");
break;
}
}
break;
}
} while (choice < 7);
System.out.println("program exits");
}
}