Arraylist content expansion

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 563 Views - Last Post: 26 May 2020 - 01:18 PM Rate Topic: -----

#1 seyidesh   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 83
  • Joined: 24-November 10

Arraylist content expansion

Posted 25 May 2020 - 02:21 AM

Hi guys,
I am stilling a java beginner: I am trying to write a code that Generates Math questions, User are required to first Register their Surname, Other Name, and Age (which are created and stored in Arraylist object). At the point of taking the Math Challenge, Users are required to Enter their Surname to check if they have registered.

User Register code

private static void addStudent() {
        System.out.println("Enter your Surname: ");
        String Sname = scanner.nextLine().toLowerCase();
        System.out.println("Enter your other name:");
        String Fname = scanner.nextLine().toLowerCase();
        System.out.println("Enter your age: ");
        int age = scanner.nextInt();
        Students register = Students.createStudentRecord(Sname, Fname, age);
        Results result = Results.createResultSheet(Sname, Fname, age);
        if (student.addStudent(register, result)) {
            System.out.println("You have successfully registered");
        } else {
            System.out.println("Record already exist, You can only register once");
        }
    }



Here is where I need Help; After the user has verified and taken the exam I want to store user Exam Score and remark along with other records,

As in to increase my Arraylist Object veriables from:
Results result = Results.createResultSheet(Sname, Fname, age);


to this
Results result = Results.createResultSheet(Sname, Fname, age, score, remark);

without asking the users to enter his/her details again since the record exists.

I really don't know if that is possible to do but I believe in programming, Everything is possible.

or is there any way I can call details of the old arraylist and create a new arraylist with the additional field variables?

Thank You.

Is This A Good Question/Topic? 0
  • +

Replies To: Arraylist content expansion

#2 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 834
  • View blog
  • Posts: 6,402
  • Joined: 25-December 13

Re: Arraylist content expansion

Posted 25 May 2020 - 04:44 AM

Looks like there is overlap between what is contained in the Students object and the Results object.
Could the Results object hold a reference to the Students object instead of having its own copies of the data?
Was This Post Helpful? 0
  • +
  • -

#3 seyidesh   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 83
  • Joined: 24-November 10

Re: Arraylist content expansion

Posted 25 May 2020 - 05:11 AM

View PostNormR, on 25 May 2020 - 04:44 AM, said:

Looks like there is overlap between what is contained in the Students object and the Results object.
Could the Results object hold a reference to the Students object instead of having its own copies of the data?


Yes, there is overlap, I really don't know if I could code it that way anyway. But can I increase the field variable of Student Object after the exam to include scores and remarks

as from
Student register = Student.createResultSheet(Sname, Fname, age);



to this

Student register = Student.createResultSheet(Sname, Fname, age, score, remark);


Is it possible to do that?
Was This Post Helpful? 0
  • +
  • -

#4 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3702
  • View blog
  • Posts: 16,962
  • Joined: 20-September 08

Re: Arraylist content expansion

Posted 25 May 2020 - 05:43 AM

Quote

as from
Student register = Student.createResultSheet(Sname, Fname, age);



to this

Student register = Student.createResultSheet(Sname, Fname, age, score, remark);



That doesn't really make sense. Firstly, a 'result sheet' is not a student and (unless you have a highly restrictive use case) a student should really be connected with results in the form of a collection.

As a point of analysis and design and coding, you shouldn't be using static in your methods. See http://technojeeves....is-a-swear-word
Was This Post Helpful? 1
  • +
  • -

#5 seyidesh   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 83
  • Joined: 24-November 10

Re: Arraylist content expansion

Posted 25 May 2020 - 09:33 AM

Exactly what am trying to achieve, I want the result ArrayList to collect information from the register Arraylist but am stuck because I don't how to retrieve that information such as name, age, and use it to generate a result sheet to be store in result Arraylist.

Thank you for your observation about static.
Was This Post Helpful? 0
  • +
  • -

#6 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 834
  • View blog
  • Posts: 6,402
  • Joined: 25-December 13

Re: Arraylist content expansion

Posted 25 May 2020 - 09:56 AM

Quote

want the result ArrayList to collect information from the register Arraylist

That is an invalid idea. ArrayLists hold data, they do not collect data from other places.
If there is a need for data in one ArrayList to be copied to another ArrayList, then the program needs code in a method to do that job. What is in register that needs to be copied to result? Can you write a method to search register and copy what is needed to result?

This post has been edited by NormR: 25 May 2020 - 09:57 AM

Was This Post Helpful? 0
  • +
  • -

#7 seyidesh   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 83
  • Joined: 24-November 10

Re: Arraylist content expansion

Posted 25 May 2020 - 10:21 AM

View PostNormR, on 25 May 2020 - 09:56 AM, said:

Quote

want the result ArrayList to collect information from the register Arraylist

That is an invalid idea. ArrayLists hold data, they do not collect data from other places.
If there is a need for data in one ArrayList to be copied to another ArrayList, then the program needs code in a method to do that job. What is in register that needs to be copied to result? Can you write a method to search register and copy what is needed to result?


This is

Quote

Can you write a method to search register and copy what is needed to result?
exactly what I wanted to do but do not no how to go about it, I need to search the register and copy the student details like name and age to create a result.
Was This Post Helpful? 0
  • +
  • -

#8 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 834
  • View blog
  • Posts: 6,402
  • Joined: 25-December 13

Re: Arraylist content expansion

Posted 25 May 2020 - 10:34 AM

Quote

I need to search the register and copy the student details like name and age to create a result

What objects are in the register ArrayList?
Can you write a loop that looks at each object in register one at a time and find the object with the desired contents?
When you find the desired object in the register list, then copy out the fields for creating the object that you want to add to register, create the object and add it to register.

Where are the lists: register and result declared? In the posted code,
 
        Students register = Students.createStudentRecord(Sname, Fname, age);
        Results result = Results.createResultSheet(Sname, Fname, age);


register is defined as a Students object, not as an ArrayList
result is defined as a Results object, not as an ArrayList

This post has been edited by NormR: 25 May 2020 - 10:37 AM

Was This Post Helpful? 0
  • +
  • -

#9 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3702
  • View blog
  • Posts: 16,962
  • Joined: 20-September 08

Re: Arraylist content expansion

Posted 25 May 2020 - 10:55 AM

Quote

I need to search the register and copy the student details like name and age to create a result.

OK - so the 'register' is something that holds information about students? If so, your Register class would hold Collection<Student>, wouldn't it? That should give you the framework
Was This Post Helpful? 0
  • +
  • -

#10 seyidesh   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 83
  • Joined: 24-November 10

Re: Arraylist content expansion

Posted 25 May 2020 - 11:06 AM

View PostNormR, on 25 May 2020 - 10:34 AM, said:

Quote

I need to search the register and copy the student details like name and age to create a result

What objects are in the register ArrayList?
Can you write a loop that looks at each object in register one at a time and find the object with the desired contents?
When you find the desired object in the register list, then copy out the fields for creating the object that you want to add to register, create the object and add it to register.

Where are the lists: register and result declared? In the posted code,
 
        Students register = Students.createStudentRecord(Sname, Fname, age);
        Results result = Results.createResultSheet(Sname, Fname, age);


register is defined as a Students object, not as an ArrayList
result is defined as a Results object, not as an ArrayList



Sorry, Those codes you copied was a middle reply to a member response.

Here what am trying to achieve;


I here this code to populate the Register ArrayList:
private static void addStudent() {
        System.out.println("Enter your Surname: ");
        String Sname = scanner.nextLine().toLowerCase();
        System.out.println("Enter your other name:");
        String Fname = scanner.nextLine().toLowerCase();
        System.out.println("Enter your age: ");
        int age = scanner.nextInt();
        Students register = Students.createStudentRecord(Sname, Fname, age);
        Results result = Results.createResultSheet(Sname, Fname, age);
        if (student.addStudent(register, result)) {
            System.out.println("You have successfully registered");
        } else {
            System.out.println("Record already exist, You can only register once");
        }
    }


Here is where I need Help; After the user has verified and taken the exam I want to store user Exam Score and remark along with other records,

As in to increase my Arraylist Object variables from:

Results result = Results.createResultSheet(Sname, Fname, age);



to this

Results result = Results.createResultSheet(Sname, Fname, age, score, remark);


without asking the users to enter his/her details again since the record exists. I want to be able to search register and copy Surname, Other Name, and Age and create a new ArrayList of Result with a field like
Results result = Results.createResultSheet(Sname, Fname, age, score, remark);


I really don't know if that is possible to do but I believe in programming, Everything is possible.

I need to know which methods to call under ArrayList that will copy those fields for me.
Was This Post Helpful? 0
  • +
  • -

#11 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 834
  • View blog
  • Posts: 6,402
  • Joined: 25-December 13

Re: Arraylist content expansion

Posted 25 May 2020 - 11:18 AM

Quote

code to populate the Register ArrayList:

Where is the Register ArrayList declared? Note that variable names should start with a lowercase letter.
Is Register the name of a class?
Is there a statement like this in your code:
   ArrayList<Register> registers = new ArrayList<>();  // define a list of Register items


student.addStudent(register, result)

Where is the variable: student defined? Is it an ArraList? It is a confusing name for a list as its name implies it contains data for a single student, not for a list of students.


Quote

which methods to call under ArrayList that will copy those fields

There are no ArrayList methods for copying data from the contents of an object contained in the list.
There are methods for getting references to objects that are in the list. EG get()


I think you should stop trying to find methods for copying data from objects in a list and go back to redoing the design for the program.
What is the purpose of the program?
What data does it need from the user?
Where should that data be stored?
How will the program use that data to solve the problem?

This post has been edited by NormR: 25 May 2020 - 11:23 AM

Was This Post Helpful? 0
  • +
  • -

#12 seyidesh   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 83
  • Joined: 24-November 10

Re: Arraylist content expansion

Posted 25 May 2020 - 12:00 PM

Quote

code to populate the Register ArrayList:

Quote

Where is the Register ArrayList declared? Note that variable names should start with a lowercase letter.
Is Register the name of a class?


package Academy.learn;

import java.util.ArrayList;
import java.util.LinkedList;

public class Register {
    private String gameName;
    private ArrayList<Students> studentList;
    private ArrayList<Results> resultsList;



    public Register(String gameName) {
        this.gameName = gameName;
        this.studentList = new ArrayList<Students>();
        this.resultsList = new ArrayList<Results>();
    }
    public boolean addStudent(Students register, Results reg ){
        if(findStudent(register.getSname()) >= 0){
            System.out.println("Student already in the Register");
            return false;
        }
        studentList.add(register);
        resultsList.add(reg);
        return true;
    }
    private int findStudent(Students register){
        return this.studentList.indexOf(register);
    }

    private int findStudent(String name){
        for(int i=0; i <this.studentList.size(); i++){
           Students register = this.studentList.get(i);

            if(register.getSname().equals(name)){

                return i;
            }
        }

        return -1;
    }
    public boolean addResult(Results details){
        if(findStudent(details.getSname()) >= 0){
            System.out.println("Student records exist.");
            return false;
        }
       resultsList.add(details);
        return true;
    }

    public boolean updateStudentRegister(Students oldDetails, Students newDetails){
        int indexPosition = findStudent(oldDetails);
        if(indexPosition < 0){
            System.out.println(oldDetails.getSname() + " Was not found");
            return false;
        }

        return true;
    }

    public boolean removeStudent(Students details){
        int foundIndex = findStudent(details);

        if(foundIndex < 0){
            System.out.println(details.getSname() + " Records was not found");
            return  false;
        }
        this.studentList.remove(details);
        this.resultsList.remove(details);
        return true;

    }

    public Students queryDetails(String name){
        int indexPosition = findStudent(name);
        if (indexPosition >= 0){

            return studentList.get(indexPosition);
        }

        return null;
    }
    public Results queryResultDetails(String name){
        int indexPosition = findStudent(name);
        if (indexPosition < 0){
            return resultsList.get(indexPosition);
        }
        return null;
    }

    public void printRegister(){
        System.out.println("Student register list");
        for(int i = 0; i <studentList.size(); i++){
            System.out.println((i+1) + ". " + this.studentList.get(i).getSname() + " \t " + this.studentList.get(i).getFname() + " --> Age: "
                                + this.studentList.get(i).getAge());
        }
    }

    public void printResult(){
        System.out.println("Student Result Sheet ");
        for(int i = 0; i <resultsList.size(); i++){
            System.out.println((i+1) + ". " + this.resultsList.get(i).getSname() + " \t " + this.resultsList.get(i).getFname() + " \t  Age: "
                    + this.resultsList.get(i).getAge() + " \tscored --> " + this.resultsList.get(i).getScore() + " -> " + this.resultsList.get(i).getRemark());
        }
    }




}



Is there a statement like this in your code:
   ArrayList<Register> registers = new ArrayList<>();  // define a list of Register items


student.addStudent(register, result)

Quote

Where is the variable: student defined? Is it an ArraList? It is a confusing name for a list as its name implies it contains data for a single student, not for a list of students.


public class Students {
    private String Sname;
    private String Fname;
    private int age;

    public Students(String Sname, String Fname, int age) {
        this.Sname = Sname;
        this.Fname = Fname;
        this.age = age;
    }

    public String getSname() {
        return Sname;
    }

    public String getFname() {
        return Fname;
    }

    public int getAge() {
        return age;
    }

    public static Students createStudentRecord(String Sname, String Fname, int age){
        return new Students(Sname, Fname, age);
    }

}


and

public class Results {
    private String Sname;
    private String Fname;
    private int age;
    private double score;
    private String Remark;

    public Results(String sname, String fname, int age) {

        Sname = sname;
        Fname = fname;
        this.age = age;
        this.score = score;
        this.Remark = remark;
    }

  

    public String getSname() {
        return Sname;
    }

    public String getFname() {
        return Fname;
    }

    public int getAge() {
        return age;
    }

    public double getScore() {
        return score;
    }

    public String getRemark() {
        return Remark;
    }

    public static Results createResultSheet(String sname, String fname, int age, String score, String Remark){
        return new Results(sname, fname, age, score, remark);
    }

  
}



Quote

which methods to call under ArrayList that will copy those fields

There are no ArrayList methods for copying data from the contents of an object contained in the list.
There are methods for getting references to objects that are in the list. EG get()


I think you should stop trying to find methods for copying data from objects in a list and go back to redoing the design for the program.

Quote

What is the purpose of the program?

The program is to examine students, and before students can be examined, they need to confirm that they registered.

Quote

What data does it need from the user?

After students have registered, The program should be able to use the student information to generate his result

Quote

Where should that data be stored?

The student result should be stored in the Result Arraylist.

Quote

How will the program use that data to solve the problem?

The program should be able to use the data to create result for the student without asking the student to re-enter his/her details

This post has been edited by seyidesh: 25 May 2020 - 12:05 PM

Was This Post Helpful? 0
  • +
  • -

#13 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 834
  • View blog
  • Posts: 6,402
  • Joined: 25-December 13

Re: Arraylist content expansion

Posted 25 May 2020 - 12:09 PM

Where is any of the description of what the program is supposed to do documented as comments in the code?
I do not see ANY comments in the code that will tell me what the code is supposed to do.
When I read the code how can I know what it is supposed to do?

Where is the main method needed to test the code?

This post has been edited by NormR: 25 May 2020 - 12:10 PM

Was This Post Helpful? 0
  • +
  • -

#14 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 1027
  • View blog
  • Posts: 3,951
  • Joined: 13-June 14

Re: Arraylist content expansion

Posted 25 May 2020 - 12:12 PM

Shouldn't a Result be associated with a Student in some way (i.e. Result holding a reference to a Student, or vice versa, rather than holding pieces of a student like their name and age)?
Was This Post Helpful? 0
  • +
  • -

#15 seyidesh   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 83
  • Joined: 24-November 10

Re: Arraylist content expansion

Posted 25 May 2020 - 12:26 PM

View PostNormR, on 25 May 2020 - 12:09 PM, said:

Where is any of the description of what the program is supposed to do documented as comments in the code?
I do not see ANY comments in the code that will tell me what the code is supposed to do.
When I read the code how can I know what it is supposed to do?

Quote

Where is the main method needed to test the code?


Here is the full code.


Main Method

package Academy.learn;

import java.sql.SQLOutput;
import java.util.Random;
import java.util.Scanner;

public class Main {
    private static Scanner scanner = new Scanner(System.in);
    private static Register student = new Register("Math Game");
    private static Random random = new Random();


    public static void main(String[] args) {
        boolean quite = false;
        int action;
        startApp();
        System.out.println("***************************");
        while (!quite) {
            System.out.println("\nPress 0 to view menu");
            System.out.println("***************************");
            action = scanner.nextInt();

            scanner.nextLine();


            switch (action) {
                case 0:
                    printMenu();
                    break;
                case 1:

                    student.printRegister();
                    break;
                case 2:
                    System.out.println("You need to register first for this Exam ");
                    addStudent();
                    break;
                case 3:
                    updateRegister();
                    break;
                case 4:
                    removeStudentRecord();
                    break;
                case 5:
                    queryContact();
                    break;
                case 6:
                    examRoom();
                    break;
                case 7:
                    student.printResult();
                case 8:
                    quite = true;
                    break;


            }
        }


    }
    //Add Student Records
    private static void addStudent() {
        System.out.println("Enter your Surname: ");
        String Sname = scanner.nextLine().toLowerCase();
        System.out.println("Enter your other name:");
        String Fname = scanner.nextLine().toLowerCase();
        System.out.println("Enter your age: ");
        int age = scanner.nextInt();
        Students register = Students.createStudentRecord(Sname, Fname, age);
        Results result = Results.createResultSheet(Sname, Fname, age);
        if (student.addStudent(register, result)) {
            System.out.println("You have successfully registered");
        } else {
            System.out.println("Record already exist, You can only register once");
        }
    }
    
    //Update Studnet Records
    private static void updateRegister() {
        System.out.println("Enter existing Surname: ");
        String Sname = scanner.nextLine().toLowerCase();
        Students saveRecord = student.queryDetails(Sname);

        if (saveRecord == null) {
            System.out.println("Record not found");
            return;
        }
        System.out.println("\n \tEnter new Details:");
        System.out.println("Enter new Surname:");
        String newSname = scanner.nextLine().toLowerCase();
        System.out.println("Enter new Other Name:");
        String newFname = scanner.nextLine().toLowerCase();
        System.out.println("Enter new Age:");
        int newAge = scanner.nextInt();
        Students newRecord = Students.createStudentRecord(newSname, newFname, newAge);
        if (student.updateStudentRegister(saveRecord, newRecord)) {
            System.out.println("Register successfully updated");
        } else {
            System.out.println("Error updating record");
        }
    }

        //Remove Studnet Records
    private static void removeStudentRecord() {
        System.out.println("Enter student Surname: ");
        String Sname = scanner.nextLine().toLowerCase();
        Students oldRecord = student.queryDetails(Sname);


        if (oldRecord == null) {
            System.out.println("Student Record not found");
        }
        if (student.removeStudent(oldRecord)) {
            System.out.println("Student Record Successfully Deleted");
        } else {
            System.out.println("Error Deleting Student Record.");
        }
    }
    //query Student records
    private static void queryContact() {
        System.out.println("Enter existing contact name: ");
        String name = scanner.nextLine().toLowerCase();
        Students existingContactRecord = student.queryDetails(name);
        if (existingContactRecord == null) {
            System.out.println("Contact not found.");
            return;
        }

        System.out.println("Name: " + existingContactRecord.getSname() + " phone number is " + existingContactRecord.getFname());
    }


    private static void startApp() {
        System.out.println("");
        System.out.println("Starting Application...");
        System.out.println("");
        System.out.println("\nInstruction: ");
        System.out.println("Before you start your Exam,\n you are require to enter" +
                "\n your name and age to mark register ");

    }

    private static void printMenu() {
        System.out.println("\nAvailable Menu:\npress");
        System.out.println("0  - To Print Menu\n" +
                "1  - To print contacts.\n" +
                "2  - To add a new contact.\n" +
                "3  - To update existing an existing contact.\n" +
                "4  - To remove an existing contact.\n" +
                "5  - Search if an existing contact exists.\n" +
                "6  - To take the challenge \n" +
                "7  - To print a list of available actions. \n" +
                "8  - To shutdown.");
        System.out.println("Choose your Menu: \n");
    }

    //To participate in the exam
    private static void examRoom() {
        System.out.println("Welcome to brain Challenge Vol. 0.01");
        System.out.println("You are required to answer 10 question within 1min");
        System.out.println("");
        System.out.println("Enter your Surname to confirm if your registered for this challenge");
        String Sname = scanner.nextLine().toLowerCase();
        Students saveRecord = student.queryDetails(Sname);
        if (saveRecord == null) {
            System.out.println("Record not found");
            System.out.println("Kindly choose option 2 from the Menu list to register");

        }

        double total;
        char operator = '?';
        int a;
        int b;


        int score = 0;
        int count = 0;
        boolean check = true;
        while (check) {
            count++;
            a = random.nextInt(100);
            b = random.nextInt(10);
            Random mathOperator = new Random();
            int sign = mathOperator.nextInt(4);

            switch (sign) {
                case 0:
                    operator = '+';
                    total = a + b;
                    System.out.println("Question " + count + ": " + a + operator + b + " = \r");
                    double answer = scanner.nextDouble();
                    if (answer == total) {
                        score += 5;

                    }
                    break;
                case 1:
                    operator = '-';
                    total = a - b;
                    System.out.println("Question " + count + ": " + a + operator + b + " = \r");
                    answer = scanner.nextDouble();
                    if (answer == total) {
                        score += 5;

                    }
                    break;
                case 2:
                    operator = '*';
                    total = a * b;
                    System.out.println("Question " + count + ": " + a + operator + b + " = \r");
                    answer = scanner.nextDouble();
                    if (answer == total) {
                        score += 5;
                    }
                    break;
                case 3:
                    operator = '/';
                    total = a / b;
                    System.out.println("Question " + count + ": " + a + operator + b + " = \r");
                    answer = scanner.nextDouble();
                    if (answer == total) {
                        score += 5;
                    }
                    break;
                default:
                    operator = '?';

            }
            if (count >= 10) {
                check = false;
            }


        }

        String Remark;
        if (score >= 40 && score <= 50) {
            Remark = "Excellent ";
        } else if (score >= 30) {
            Remark = "Very Good ";
        } else if (score >= 20) {
            Remark = "Good";
        } else if (score >= 10) {
            Remark = "Pass";
        } else {
            Remark = "Fail";
        }
        Results results = Results.addScore(score, Remark);
        if(student.addResult(results)){
            System.out.println("Record are successfully saved");
            System.out.println("");
            System.out.println("Congratulation ! You score " + score);
            System.out.println("********************");
            student.printResult();
        }else{
            System.out.println("There is problem saving this record");
        }

    }

}




And the Registered Code

import java.util.ArrayList;
import java.util.LinkedList;

public class Register {
    private String gameName;
    private ArrayList<Students> studentList;
    private ArrayList<Results> resultsList;



    public Register(String gameName) {
        this.gameName = gameName;
        this.studentList = new ArrayList<Students>();
        this.resultsList = new ArrayList<Results>();
    }

    //add Student Records
    public boolean addStudent(Students register, Results reg ){
        if(findStudent(register.getSname()) >= 0){
            System.out.println("Student already in the Register");
            return false;
        }
        studentList.add(register);
        resultsList.add(reg);
        return true;
    }


    private int findStudent(Students register){
        return this.studentList.indexOf(register);
    }

    private int findStudent(String name){
        for(int i=0; i <this.studentList.size(); i++){
           Students register = this.studentList.get(i);

            if(register.getSname().equals(name)){

                return i;
            }
        }

        return -1;
    }

    //Add result Details
    public boolean addResult(Results details){
        if(findStudent(details.getSname()) >= 0){
            System.out.println("Student records exist.");
            return false;
        }
       resultsList.add(details);
        return true;
    }
    //update Student Register
    public boolean updateStudentRegister(Students oldDetails, Students newDetails){
        int indexPosition = findStudent(oldDetails);
        if(indexPosition < 0){
            System.out.println(oldDetails.getSname() + " Was not found");
            return false;
        }

        return true;
    }
        //Remove Student details
    public boolean removeStudent(Students details){
        int foundIndex = findStudent(details);

        if(foundIndex < 0){
            System.out.println(details.getSname() + " Records was not found");
            return  false;
        }
        this.studentList.remove(details);
        this.resultsList.remove(details);
        return true;

    }

        //Search for student details
    public Students queryDetails(String name){
        int indexPosition = findStudent(name);
        if (indexPosition >= 0){

            return studentList.get(indexPosition);
        }

        return null;
    }
    public Results queryResultDetails(String name){
        int indexPosition = findStudent(name);
        if (indexPosition < 0){
            return resultsList.get(indexPosition);
        }
        return null;
    }

    public void printRegister(){
        System.out.println("Student register list");
        for(int i = 0; i <studentList.size(); i++){
            System.out.println((i+1) + ". " + this.studentList.get(i).getSname() + " \t " + this.studentList.get(i).getFname() + " --> Age: "
                                + this.studentList.get(i).getAge());
        }
    }

    public void printResult(){
        System.out.println("Student Result Sheet ");
        for(int i = 0; i <resultsList.size(); i++){
            System.out.println((i+1) + ". " + this.resultsList.get(i).getSname() + " \t " + this.resultsList.get(i).getFname() + " \t  Age: "
                    + this.resultsList.get(i).getAge() + " \tscored --> " + this.resultsList.get(i).getScore() + " -> " + this.resultsList.get(i).getRemark());
        }
    }




}



Student Class

public class Students {
    private String Sname;
    private String Fname;
    private int age;

    public Students(String Sname, String Fname, int age) {
        this.Sname = Sname;
        this.Fname = Fname;
        this.age = age;
    }

    public String getSname() {
        return Sname;
    }

    public String getFname() {
        return Fname;
    }

    public int getAge() {
        return age;
    }

    public static Students createStudentRecord(String Sname, String Fname, int age){
        return new Students(Sname, Fname, age);
    }

}



Result class

package Academy.learn;

public class Results {
    private String Sname;
    private String Fname;
    private int age;
    private double score;
    private String Remark;

    public Results(String sname, String fname, int age) {

        Sname = sname;
        Fname = fname;
        this.age = age;
//        this.score = score;
//        this.Remark = remark;
    }

    public Results(double score, String remark) {
        this.score = score;
        this.Remark = remark;
    }

    public String getSname() {
        return Sname;
    }

    public String getFname() {
        return Fname;
    }

    public int getAge() {
        return age;
    }

    public double getScore() {
        return score;
    }

    public String getRemark() {
        return Remark;
    }

    public static Results createResultSheet(String sname, String fname, int age){
        return new Results(sname, fname, age);
    }

    public static Results addScore( double score, String remark){
        return new Results(score, remark);
    }
}


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2