I tried to do a students registration program. So I wrote a class file also. Using that class file I put values to an ArrayList. But the problem is, If I enter details about 3 students, at last It only prints only the details about the last student twice. I'm a begginer and please help me to overcome this problem.
Here is the main method I wrote;
Student obj = new Student();
Scanner in = new Scanner(System.in);
ArrayList<Student> List = new ArrayList<Student>();
for (;;) {
System.out.print("Enter the Student ID: ");
obj.setStudentID(in.next());
System.out.print("Enter the first name: ");
obj.setFirstName(in.next());
System.out.print("Enter the last name: ");
obj.setLastName(in.next());
System.out.print("Enter the Course Work 1 marks: ");
obj.setCwk01(in.nextInt());
System.out.print("Enter the Course Work 2 marks: ");
obj.setCwk02(in.nextInt());
System.out.print("Enter the final exam marks: ");
obj.setExam(in.nextInt());
List.add(obj);
System.out.print("Enter q to stop (or any other to continue): ");
String str = in.next();
if (str.equals("q") || str.equals("Q")) {
System.out.println("\n");
break;
}
System.out.println("\n");
}
for (Student value : List) {
System.out.println(value.getCwk01());
}
and here's the class file;
public class Student {
private String studentID;
private String firstName;
private String lastName;
private int cwk01;
private int cwk02;
private int exam;
private int fMarks;
public int getfMarks() {
return fMarks;
}
public String getStudentID() {
return studentID;
}
public void setStudentID(String studentID) {
this.studentID = studentID;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getCwk01() {
return cwk01;
}
public void setCwk01(int cwk01) {
this.cwk01 = cwk01;
}
public int getCwk02() {
return cwk02;
}
public void setCwk02(int cwk02) {
this.cwk02 = cwk02;
}
public int getExam() {
return exam;
}
public void setExam(int exam) {
this.exam = exam;
}
public void calculateFMarks() {
this.fMarks = (int) ((this.cwk01 * 0.2) + (this.cwk02 * 0.2) + (this.exam * 0.6));
}
}