I have created a linked list with objects. The object is called Student and has three parameters: name, age, grade. Here are my classes.
Link
class Link {
public Object s;
public Link next;
public Link(Object Student) {
s=Student;
next=null;
}
}
Student
class Student {
private static String name;
private static int age;
private static int grade;
public Student (String n, int a, int g) {
setName(n);
setGrade(g);
setAge(a);
}
public static void setName(String n) {
name=n;
}
public static void setGrade(int g) {
grade=g;
}
public static void setAge(int a) {
age=a;
}
public int getAge() {
return age;
}
public int getGrade() {
return grade;
}
public static String getName() {
return name;
}
public String toString() {
return "Name: " +name+ " Age: " +age+ " Grade: " +grade;
}
}
List
import java.util.*;
class List {
private static LinkedList<Student> ll = new LinkedList<Student>();
private static Link first;
public List() {
first=null;
}
public static void addStudent() {
Scanner s = new Scanner(System.in);
System.out.println("What is the student's name;");
String n = s.nextLine();
System.out.println("What is the student's age;");
int b = s.nextInt();
System.out.println("What is the student's grade;");
int g = s.nextInt();
Student nStudent = new Student(n, b, g);
Link newStudent = new Link(nStudent);
newStudent.next=first;
first=newStudent;
ll.add(nStudent);
}
public static Link findStudent() {
Scanner sc = new Scanner(System.in);
System.out.println("What is the student's name?");
String n = sc.nextLine();
Link current = first;
String name = Student.getName();
while (name!= n) {
if (current.next==null)
return null;
else
current=current.next;
}
return current;
}
public static void change () {
Scanner sc = new Scanner(System.in);
Link h = findStudent();
System.out.println("If you want to change his name press 1;");
System.out.println("If you want to change his age press 2;");
System.out.println("If you want to change his grade press 3;");
int l = sc.nextInt();
if (l==1) {
System.out.println("What name do you want the student to have?");
String newname = sc.nextLine();
Student.setName(newname);
} else if (l==2) {
System.out.println("What age do you want the student to have?");
int j = sc.nextInt();
Student.setAge(j);
}
else if (l==3) {
System.out.println("What grade do you want the student to have?");
int e = sc.nextInt();
Student.setGrade(e);
}
}
public static void show() {
System.out.println(ll);
}
}
Course
import java.util.Scanner;
public class Course {
public static void main (String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("What do you want to do?");
System.out.println("To add a student press 1");
System.out.println("To change something press 2");
System.out.println("To see your students press 3");
int v = sc.nextInt();
if (v==1) {
List.addStudent();
}else if (v==2) {
List.change();
}else if (v==3) {
List.show();
}
System.out.println("What do you want to do?");
System.out.println("To add a student press 1");
System.out.println("To change something press 2");
System.out.println("To see your students press 3");
int a = sc.nextInt();
if (a==1) {
List.addStudent();
}else if (a==2) {
List.change();
}else if (a==3) {
List.show();
}
System.out.println("What do you want to do?");
System.out.println("To add a student press 1");
System.out.println("To change something press 2");
System.out.println("To see your students press 3");
int r = sc.nextInt();
if (r==1) {
List.addStudent();
}else if (r==2) {
List.change();
}else if (r==3) {
List.show();
}
System.out.println("What do you want to do?");
System.out.println("To add a student press 1");
System.out.println("To change something press 2");
System.out.println("To see your students press 3");
int c = sc.nextInt();
if (c==1) {
List.addStudent();
}else if (c==2) {
List.change();
}else if (c==3) {
List.show();
}
System.out.println("What do you want to do?");
System.out.println("To add a student press 1");
System.out.println("To change something press 2");
System.out.println("To see your students press 3");
int d = sc.nextInt();
if (d==1) {
List.addStudent();
}else if (d==2) {
List.change();
}else if (d==3) {
List.show();
}
System.out.println("What do you want to do?");
System.out.println("To add a student press 1");
System.out.println("To change something press 2");
System.out.println("To see your students press 3");
int x = sc.nextInt();
if (x==1) {
List.addStudent();
}else if (x==2) {
List.change();
}else if (x==3) {
List.show();
}
System.out.println("What do you want to do?");
System.out.println("To add a student press 1");
System.out.println("To change something press 2");
System.out.println("To see your students press 3");
int h = sc.nextInt();
if (h==1) {
List.addStudent();
}else if (h==2) {
List.change();
}else if (h==3) {
List.show();
}
System.out.println("What do you want to do?");
System.out.println("To add a student press 1");
System.out.println("To change something press 2");
System.out.println("To see your students press 3");
int p = sc.nextInt();
if (p==1) {
List.addStudent();
}else if (p==2) {
List.change();
}else if (p==3) {
List.show();
}
System.out.println("What do you want to do?");
System.out.println("To add a student press 1");
System.out.println("To change something press 2");
System.out.println("To see your students press 3");
int m = sc.nextInt();
if (m==1) {
List.addStudent();
}else if (m==2) {
List.change();
}else if (m==3) {
List.show();
}
System.out.println("What do you want to do?");
System.out.println("To add a student press 1");
System.out.println("To change something press 2");
System.out.println("To see your students press 3");
int y = sc.nextInt();
if (y==1) {
List.addStudent();
}else if (y==2) {
List.change();
}else if (y==3) {
List.show();
}
System.out.println("What do you want to do?");
System.out.println("To add a student press 1");
System.out.println("To change something press 2");
System.out.println("To see your students press 3");
int q = sc.nextInt();
if (q==1) {
List.addStudent();
}else if (q==2) {
List.change();
}else if (q==3) {
List.show();
}
System.out.println("What do you want to do?");
System.out.println("To add a student press 1");
System.out.println("To change something press 2");
System.out.println("To see your students press 3");
int w = sc.nextInt();
if (w==1) {
List.addStudent();
}else if (w==2) {
List.change();
}else if (w==3) {
List.show();
}
System.out.println("What do you want to do?");
System.out.println("To add a student press 1");
System.out.println("To change something press 2");
System.out.println("To see your students press 3");
int f = sc.nextInt();
if (f==1) {
List.addStudent();
}else if (f==2) {
List.change();
}else if (f==3) {
List.show();
}
}
}
You will notice that in the Course class, I have written the same code multiple times but that is because I could not find a way with which after I execute a command, I would get back to the main menu and the content of the list would not be lost. Apparently, my add method is wrong and when I execute the program the output is this:
Output
What do you want to do?
To add a student press 1
To change something press 2
To see your students press 3
1
What is the student's name;
Tom
What is the student's age;
16
What is the student's grade;
90
What do you want to do?
To add a student press 1
To change something press 2
To see your students press 3
1
What is the student's name;
Eric
What is the student's age;
16
What is the student's grade;
91
What do you want to do?
To add a student press 1
To change something press 2
To see your students press 3
3
[Name: Eric Age: 16 Grade: 91, Name: Eric Age: 16 Grade: 91]
What do you want to do?
To add a student press 1
To change something press 2
To see your students press 3
Why when I add a Student in the class, it duplicates?