trên đây là đề bài em muốn nhờ mọi người giúp ạ còn dưới là code em. Em đang học về generics nhưng code em viết ra khi chạy toàn báo lỗi và chưa làm đc phần sắp xếp các item mong mọi người sửa giúp ạ
class Student
public class Student implements Comparable<Student>{
private int id;
private String name;
private LocalDate dob;
public Student() {
}
public Student(int id, String name, LocalDate dob) {
this.id = id;
this.name = name;
this.dob = dob;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public LocalDate getDob() {
return dob;
}
public void setDob(LocalDate dob) {
this.dob = dob;
}
@Override
public String toString() {
return "Student{" + "id=" + id + ", name=" + name + ", dob=" + dob + '}';
}
@Override
public int compareTo(Student t) {
return this.getId() - t.getId();
}
}
class Employee
public class Employee implements Comparable<Employee>{
private int id;
private String name;
private String email;
public Employee() {
}
public Employee(int id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Employee{" + "id=" + id + ", name=" + name + ", email=" + email + '}';
}
@Override
public int compareTo(Employee t) {
return this.getId() - t.getId();
}
}
class Item
public class Item <T>{
private T item;
public Item() {
}
public Item(T item) {
this.item = item;
}
public T getItem() {
return item;
}
public void setItem(T item) {
this.item = item;
}
@Override
public String toString() {
return "Item{" + "item=" + item + '}';
}
}
class Group
public class Group <T>{
ArrayList<T> item;
public Group() {
}
public Group(T t) {
item = new ArrayList<T>();
}
public void add(T s){
item.add(s);
}
public void remove(T s){
for (int i = 0; i < item.size(); i++) {
if (s == item.get(i)) {
remove(item.get(i));
}
}
}
public void addIndex(T s, int index){
item.add(index, s);
}
public void display(){
for (int i = 0; i < item.size() ; i++) {
System.out.println(item.get(i));
}
}
}
class Main
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("1 - Working with student");
System.out.println("2 - Working with employee");
System.out.print("Enter TC(1,2): ");
int check;
check = sc.nextInt();
switch(check){
case 1:{
Group<Student> t = new Group<>();
System.out.println("1 - Add new student");
System.out.println("2 - Remove student");
System.out.println("3 - Add student in the index");
System.out.println("4 - Display list student");
int tmp;
System.out.print("Enter TC (1,2,3,4): ");
tmp = sc.nextInt();
switch(tmp){
case 1:{
int id;
String name;
LocalDate dob;
System.out.println("Enter information new student");
System.out.println("Enter id: ");
id = sc.nextInt();
System.out.println("Enter name: ");
sc.nextLine();
name = sc.nextLine();
System.out.println("Emter dob: ");
dob = LocalDate.parse(sc.nextLine() , DateTimeFormatter.ofPattern("dd/MM/yyyy"));
t.add(new Student(id, name, dob));
}
case 2:{
int id;
String name;
LocalDate dob;
System.out.print("Enter id student you want remove: ");
id = sc.nextInt();
System.out.print("Enter name studen you want remove: ");
sc.nextLine();
name = sc.nextLine();
System.out.print("Enter dob student you want remove: ");
dob = LocalDate.parse(sc.nextLine() , DateTimeFormatter.ofPattern("dd/MM/yyyy"));
Student s1 = new Student(id, name, dob);
t.remove(s1);
}
case 3:{
int id;
String name;
LocalDate dob;
System.out.print("Enter id: ");
id = sc.nextInt();
System.out.print("Enter name: ");
sc.nextLine();
name = sc.nextLine();
System.out.print("Enter dob: ");
dob = LocalDate.parse(sc.nextLine() , DateTimeFormatter.ofPattern("dd/MM/yyyy"));
Student s2 = new Student(id, name, dob);
int index;
System.out.println("Enter index you want add: ");
index = sc.nextInt();
t.addIndex(s2, index);
}
case 4:{
t.display();
}
}
}
case 2:{
Group<Employee> e = new Group<>();
System.out.println("1 - Add new employee");
System.out.println("2 - Remove employee");
System.out.println("3 - Add employee in the index");
System.out.println("4 - Display list employee");
int sum;
System.out.println("Enter TC (1,2,3,4): ");
sum = sc.nextInt();
switch(sum){
case 1:{
int id;
String name;
String email;
System.out.println("Enter information new student");
System.out.println("Enter id: ");
id = sc.nextInt();
System.out.println("Enter name: ");
sc.nextLine();
name = sc.nextLine();
System.out.println("Emter email: ");
email = sc.next();
e.add(new Employee(id, name, email));
}
case 2:{
int id;
String name;
String email;
System.out.println("Enter information employee you want remove");
System.out.println("Enter id: ");
id = sc.nextInt();
System.out.println("Enter name: ");
sc.nextLine();
name = sc.nextLine();
System.out.println("Emter email: ");
email = sc.next();
Employee e1 = new Employee(id, name, email);
e.remove(e1);
}
case 3:{
int id;
String name;
String email;
System.out.println("Enter information employee you want remove");
System.out.println("Enter id: ");
id = sc.nextInt();
System.out.println("Enter name: ");
sc.nextLine();
name = sc.nextLine();
System.out.println("Emter email: ");
email = sc.next();
Employee e2 = new Employee(id, name, email);
int index;
System.out.println("Enter index you want add: ");
index = sc.nextInt();
e.addIndex(e2, index);
}
case 4:{
e.display();
}
}
}
}
}
}
Mong mọi người giúp em ạ