Assignment Brief:
Write a program that will allow a teacher to input student details such as student first name, student
second name and examination mark at the keyboard for at least 20 students. Once all examination
marks have been , the program must display the mark and grade for each of the students.
Download full brief here
Download zip of all source code for 'Student Grades' here

StudentGrades is the main class:
Download source code for 'StudentGrades' here

// Start of code---------------------
package studentgrades;

import java.util.ArrayList;
import java.util.Scanner;
import java.util.List;

/* Usage notes:
 * Redistribution, alteration and use of this code with or without
 * modification, are permitted provided that the following conditions are met:
 * In no event shall the copyright owner be liable for any direct, indirect,
 * incidental, special, exemplary, or consequential damages arising in any
 * way out of the use of this software, even if advised of the possibility
 * of such damage.
 */


public class StudentGrades {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        String fname = "";
        String lname = "";
        int mark = 0;
        int maxInput = 0;
        Boolean menuLoop = false;
        final String inputError = "\nInput error!!";
       
        Header();

        ArrayList arrayStudent = new ArrayList();

        addStudent(fname, maxInput, lname, mark, input, arrayStudent, inputError);

        StudentList(input, maxInput, arrayStudent, menuLoop, fname, lname, mark, inputError);
    }

    public static void StudentList(Scanner input, int maxInput,
            ArrayList arrayStudent, Boolean menuLoop, String fname,
            String lname, int mark, final String inputError) {

        String formatedString = "";

        menuLoop = false;
        do {
            menuLoop = false;
            final String menuTitle = "\nStudent database menu";

            final String menu = "\nPlease input numeric digit to continue (Numeric digits only please!)"
                    + "\n1. Display all students in list"
                    + "\n2. Add student a student to list"
                    + "\n3. Delete a student from the list"
                    + "\n4. Quit program";

            System.out.println(menuTitle + menu);

            //Input from the user must be a numeric digit
            while (!input.hasNextInt()) {
                System.out.println(inputError + menu);
                input.next();
            }
                       
            int listMenu = input.nextInt();

            while (listMenu < 1 || listMenu > 4) {
                System.out.println(inputError + menu);

                listMenu = input.nextInt();
            }
            switch (listMenu) {
            case 1: {
                formatedString = arrayStudent.toString().
                        replace(",", "\n") // remove the commas
                        .replace("[", " ") // remove the right bracket
                        .replace("]", ""); // remove the left bracket
                System.out.println("\nYou have chosen to view all students\nStudent total in the " +
                        "database is " + arrayStudent.size() + "\n\n" + formatedString);
                menuLoop = menuLoopOption(input, menuLoop, arrayStudent);
                break;
            }
            case 2: {
                formatedString = arrayStudent.toString().
                        replace(",", "\n") // remove the commas
                        .replace("[", " ") // remove the right bracket
                        .replace("]", ""); // remove the left bracket
                System.out.println("\nYou have chosen to add a student\nStudent total in the database is "
                                + arrayStudent.size() + "\n" + formatedString);
                addStudent(fname, maxInput, lname, mark, input, arrayStudent, inputError);
                menuLoop = menuLoopOption(input, menuLoop, arrayStudent);
                break;
            }
            case 3: {
                int deleteStudent = 0;
               
                formatedString = arrayStudent.toString().
                        replace(",", "\n") // remove the commas
                        .replace("[", " ") // remove the right bracket
                        .replace("]", ""); // remove the left bracket
                System.out.println("\nYou have chosen to delete a student\nStudent total in the database is "
                                + arrayStudent.size() + "\n" + formatedString);
                do {
                    System.out
                            .println("\nPlease input the correnponding number "
                                    + "for the student you want to delete");
                    deleteStudent = input.nextInt();
                } while (deleteStudent > arrayStudent.size());
                               
                System.out.println("\nYou have chosen to delete \n" + arrayStudent.get(deleteStudent - 1) 
                        + " from the student database");
                arrayStudent.remove(deleteStudent - 1);
                menuLoop = menuLoopOption(input, menuLoop, arrayStudent);
                break;
            }
            case 4: {
                formatedString = arrayStudent.toString().
                        replace(",", "\n") // remove the commas
                        .replace("[", " ") // remove the right bracket
                        .replace("]", ""); // remove the left bracket
                System.out.println("\nYou have chosen to quit. Goodbye.\n");
                System.out.println("Final list of students as follows:\n\n"
                        + formatedString);
                System.exit(0);
                break;
            }
            }
        } while (menuLoop);
    }

    public static boolean menuLoopOption(Scanner input, Boolean menuLoop, ArrayList arrayStudent) {

        String menuLoopInput = "";

        System.out
                .println("\nWould you like to return to menu\nInput y for yes or q to quit");
        menuLoopInput = input.next();

        if (menuLoopInput.equals("y")) {
            menuLoop = true;
        } else {
            String formatedString = arrayStudent.toString().
                    replace(",", "\n") // remove the commas
                    .replace("[", " ") // remove the right bracket
                    .replace("]", ""); // remove the left bracket
            System.out.println("\nYou have chosen to quit. Goodbye.\n");
            System.out.println("Final list of students as follows:\n\n"
                    + formatedString);
            System.exit(0);
        }
        return menuLoop;
    }

    public static ArrayList addStudent(String fname, int maxInput,
            String lname, int mark, Scanner input,
            ArrayList arrayStudent, final String inputError) {
                final String maxStudentInput = "\nHow many students results would you like to input:\n(Numeric digits only please!)";
        System.out
                .print(maxStudentInput);
       
        //Input from the user must be a numeric digit
        while (!input.hasNextInt()) {
            System.out.println(inputError + maxStudentInput);
            input.next();
        }
       
        maxInput = input.nextInt();

        int studentCount = maxInput;

        while (maxInput >= 1) {
            System.out.print("\nPlease enter students first name: ");
            fname = input.next();

            System.out.print("Please enter students last name: ");
            lname = input.next();

            System.out.print("Please enter " + fname + " " + lname
                    + "'s exam result: ");
            mark = input.nextInt();

            arrayStudent.add(new Student(fname, lname, mark));

            studentCount--;

            if (studentCount > 1) {
                System.out
                        .println("\n" + studentCount + " student inputs left");
            } else if (studentCount == 1) {
                System.out.println("\n" + studentCount + " student input left");
            }
            maxInput--;
        }
        return arrayStudent;

    }

    public static void Header() {
        final String Header = "\t***Student Grading database***";
        System.out.println(Header);
    }
}
//End of code---------------------

Student Class:
Download source code for 'Student' here

// Start of code---------------------
package studentgrades;

/* Usage notes:
 * Redistribution, alteration and use of this code with or without
 * modification, are permitted provided that the following conditions are met:
 * In no event shall the copyright owner be liable for any direct, indirect,
 * incidental, special, exemplary, or consequential damages arising in any
 * way out of the use of this software, even if advised of the possibility
 * of such damage.
 */


public class Student {

    private String fName;
    private String lName;
    private String grade;
    private int mark;

    public Student(String fName, String lName, int mark) {
        this.fName = fName;
        this.lName = lName;
        this.mark = mark;

    }
   
    public String getfName() {
        return fName;
    }

    public void setfName(String fName) {
        this.fName = fName;
    }

    public String getlName() {
        return lName;
    }

    public void setlName(String lName) {
        this.lName = lName;
    }

    public int getMark() {
        return mark;
    }

    public void setMark(int mark) {
        this.mark = mark;
    }

    public String setGrade(int mark) {

        String grade = "";
        if (mark >= 85) {
            grade = "distinction";
        } else if (mark >= 65) {
            grade = "merit";
        } else if (mark >= 40) {
            grade = "pass";
        } else if (mark >= 0) {
            grade = "fail";
        }
        return grade;
    }

    // this printout is called into student class
    public String toString() {

        return getfName() + " " + getlName() + " received a " + setGrade(mark)
                + " for a mark of " + getMark();
    }

    }//End of code---------------------

0 comments:

Post a Comment

Please feel free to leave comments or ask questions related to the tutorials.