Click here to Skip to main content
15,348,645 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
This Assignment is divided into four stages. The overall task is to write a scaled-down student database for a university, containing the following information:

Every student has:
a student number (a seven digit integer)
a family name
given name(s)
a degree (one of "Science", "Arts" or "Medicine")
results for each topic
Each topic result contains:
a topic code (8 characters, e.g. COMP2008)
the grade ("FL","PS","CR","DN" or "HD")
a mark (0 - 100), which is optional
Medicine students also have a list of (zero or more) prizes
Arts students have a major and a minor
You are required to read in the student information from a file in the following format:

Each line of input is either a student details line, a topic result line, or a comment
Each data field in each line is separated by commas (like an Excel .csv - file)
The fields in a student details line are in the following order:
a character 'A', 'M' or 'S' representing the degree (arts, medicine or science respectively)
the student number
the family name
the given names (separated by spaces)
a (possibly empty) list of prizes, separated by commas (only if the degree is medicine)
the major (only if the degree is arts)
the minor (only if the degree is arts)
Topic results appear in the following order (separated by commas):
the letter 'R' (upper case)
the student number
the topic code
the grade
the mark (if it exists)
Prizes for medical students are as follows:
an input line beginning with a 'P' indicates a new prize
the second field is the name of the prize
the third field is the topics that the prize is awarded for
e.g. MMED2 means the prize is awarded for performance in all topics with a code beginning with MMED2
the fourth field, min, is the minimum number of topics attempted which match the third field
the prize is awarded to the medical student who has the highest average mark in their best min matching topics
e.g. for the input line P,Neuroscience 1 Prize,MMED1904,1, the prize goes to the student with the highest mark in MMED1904
e.g. for the input line P,3rd Year Medicine Prize,MMED3,5, the prize goes to the student with the highest average mark in their best 5 MMED3xxx topics
a prize description may appear anywhere within the input file
prizes should be calculated and added to the students' prize lists in the order the prize descriptions appear, and after any prizes in the student details line
there will be no more than 10 prizes
You are required to print, and write to file, an academic record for each student, with the following requirements:

students appear in the order that the student details lines appear in the input file
topic results for each student appear in the same order as they appear in the input file
each academic record contains the following lines of output, in order:
the string "Academic record for" followed by the given name(s), the family name, and the student number in parentheses (each separated by spaces)
the string "Degree: " followed by the degree
the string "Major: " followed by the major (if applicable)
the string "Minor: " followed by the minor (if applicable)
for each prize, the string "Prize: " followed by the prize name
for each topic result, the topic code, grade and mark (if given), separated by spaces
a blank line, to indicate the end of that student's record
You may make the following assumptions:

each student record occurs before any of that student's results
no student attempts more than 40 topics
no student is awarded more than 10 prizes
there are no more than 1000 students
any line not beginning with 'A', 'M', 'P', 'R' or 'S' is a comment, and should be ignored
A sample input file and corresponding output are shown below.

// Sample input file
// Student details
S,9800123, Smith, John Paul
M,9821012, Jones,Mary, Chemistry Prize 1998
A,9987654, Howard, John, Politics, Economics
// Topic results
R,9821012,BIOL1000,HD,89
R, 9821012, CHEM1001 ,HD,92
R,9800123, COMP1000, PS,55
R.9821012.COMP1000.DN.75
R,9800123, COMP1001.DN.77
R,9800123,HIST1234, HD
R. 9821012, PHYS1010 ,HD, 93
R, 9800123 ,PSYC0123, FL, 42

Figure 1.Sample Input File

Academic record for John Paul Smith (9800123)
Degree: Science
COMP1000 PS 55
COMP1001 DN 77
HIST1234 HD
PSYC0123 FL 42
Academic record for Mary Jones (9821012)
Degree: Medicine
Prize: Chemistry Prize 1998
BIOL1000 HD 89
CHEM1001 HD 92
COMP1000 DN 75
PHYS1010 HD 93
Academic record for John Howard (9987654)
Degree: Arts
Major: Politics
Minor: Economics

Figure 2. Output for the Sample Input

Stage 1
Assume that every input line begins with 'S', that is, all the students are science students and there are no topic results.

Write a Student class containing the necessary data elements for a science student and the methods to set and print that student's details.

Stage 2
Allow for input beginning with any character except 'P' or 'R', that is, student details or comment lines, but still no topic results.

Extend the Student class to provide a MedStudent class and an ArtsStudent class. Re-use as much code from the Student class as possible. i.e. DON'T repeat (copy-and-paste) any code from the Student class.

Stage 3
Complete the implementation of the student database, from stages 1 and 2, by allowing topic results to be stored and printed.

Write a Result class and store the topic results for each student in an array of Result objects.

Stage 4
Challenging
Extend the program to calculate prizes for medical students as follows:

an input line beginning with a 'P' indicates a new prize
the second field is the name of the prize
the third field is the topics that the prize is awarded for
e.g. "MMED2" means the prize is awarded for performance in all topics with a code beginning with "MMED2"
the fourth field, min, is the minimum number of topics attempted which match the third field
the prize is awarded to the medical student who has the highest average mark in their best min matching topics
e.g. for the input line "P,Neuroscience 1 Prize,MMED1904,1", the prize goes to the student with the highest mark in MMED1904
e.g. for the input line "P,3rd Year Medicine Prize,MMED3,5", the prize goes to the student with the highest average mark in their best 5 MMED3xxx topics
a prize description may appear anywhere within the input file
prizes should be calculated and added to the students' prize lists in the order the prize descriptions appear, and after any prizes in the student details line
there will be no more than 10 prizes
If the input file above is extended with the following lines:

    P,Medicine 1 prize,MMED1,4
    P,Physics prize,PHYS,1
then the output file should contain the line:

    Prize: Physics prize
after the line:

    Prize: Chemistry Prize 1998



package studentdatabase;

/**
 * A class to create a Prize and the means to award it.
 */
public class Prize {
// todo your code goes here
}
package studentdatabase;

/**
 * A class that represents a Student.
 */
public class Student {
    // todo your code goes here
}
package studentdatabase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class StudentDatabase {

    private final ArrayList studentDatabse;

    public StudentDatabase() {
        studentDatabse = new ArrayList<>();
    }

    public void addStudent(String s) {
        Scanner vars = new Scanner(s);
        vars.useDelimiter(",");
        switch (s.charAt(0)) {
            case 'A' -> {

            }
            case 'M' -> {

            }
            case 'S' -> {

            }
        }
    }


    public Student findStudent(String ID) {

        return null;
    }

    public void addResult(String s) {

    }

    public void awardPrize(String prize, String template, int topicsRequired) {

    }

    public void printRecords() throws IOException {

    }

    public void clearRecords() {
        studentDatabse.clear();
    }

}
package studentdatabase;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class StudentDatabaseDriver {

    public static void main(String[] args) throws IOException {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a file name to process: ");
        String fileName = scan.nextLine().trim();
        Scanner fileReader = new Scanner(new File(fileName));

        String s;
        int prizeCount = 0;
        StudentDatabase studentDB = new StudentDatabase();
        Prize[] prizeList = new Prize[10];
        while (fileReader.hasNextLine()) {
            s = fileReader.nextLine();
            if (s.length() > 0)
                switch (s.charAt(0)) { //
                    case 'A', 'M', 'S' -> {
                        studentDB.addStudent(s);
                    }
                    case 'R' -> {
                        studentDB.addResult(s);
                    }
                    case 'P' -> {
                        //    prizeList[prizeCount++] = new Prize(s);
                    }
                    default -> {
                    }
                }
        }
//        for (int i = 0; i < prizeCount; i++)
//            prizeList[i].awardPrize(studentDB);
        studentDB.printRecords();

    }
}


What I have tried:

here are my codes for this assignment so far

studentdatabase.java

public class StudentDatabase implements Constants {
    Student[] db; // you could use an ArrayList here if you wish
    int studentCount;

    public StudentDatabase() {
        db = new Student[NUMBER_OF_STUDENTS];
        studentCount = 0;
    }

    public void addStudent(String s) {

        //tjk scans the line and breaks up by commas assigns "words"
        Scanner scan = new Scanner(s);
        scan.useDelimiter(",");
        String[] stringList = s.split(",");

        //tjk creates a blank student and assigns to the array of students
        Student placeMan = new Student();
        db[studentCount] = placeMan;

//takes the bits of info from the string and uses the appropriate setters to set the properties from string
        switch (stringList[0]) {
            case "A" -> {

                db[studentCount].addID(stringList[1]);
                db[studentCount].addFamily(stringList[2]);
                db[studentCount].giveName(stringList[3]);;
                db[studentCount].setDegree("Art");
                db[studentCount].setMajor(stringList[4]);
                db[studentCount].setMinorArt(stringList[5]);
            }
            case "M"-> {
                db[studentCount].addID(stringList[1]);
                db[studentCount].addFamily(stringList[2]);
                db[studentCount].giveName(stringList[3]);
                db[studentCount].setDegree("Medicine");
            }
            case "S" -> {
                db[studentCount].addID(stringList[1]);
                db[studentCount].addFamily(stringList[2]);
                db[studentCount].giveName(stringList[3]);
                db[studentCount].setDegree("Science");
            }
            default -> throw new IllegalStateException("Unexpected value: " + stringList[0]);
        }
        studentCount++;
    }

    public void parseResult(String s) {
        //tjk takes input string, splits it by , then assigns to string array.
        String[] stringList = s.split(",");
        //tjk looks through all students in db to find a one with a matching ID #
        for(Student each: db){
            String searchID = stringList[1];
            if(Objects.equals(each.getID(), searchID)){
                //adds topic, grade, and mark if enough args exist
                if(stringList.length == 5){
                each.addResult(stringList[2], stringList[3], stringList[4]);
                break;
                }
                //adds result if only topic and grade are provided
                else{
                    each.addResult(stringList[2], stringList[3], null);
                    break;
                }
            }
        }
    }

    public void printRecords() throws IOException {
        //for loop loops trough each student and prints out using getters
        for(int o =0; o < studentCount; o++){
            Student student = db[o];
            System.out.println("Academic record for "+ student.getGivenName()+" "+student.getFamilyName() +" (" + student.getID() + ")");
            System.out.println("Degree: "+student.getDegree());
            //add prizes capabilities here
            if(student.getDegree() == "Medicine"){
                System.out.println("Prize: " );
            }
            //prints major/minor iff art student
            else if( student.getDegree()=="Art"){
               System.out.println("Major: "+student.getMajor());
                System.out.println(("Minor: "+student.getMinor()));
            }
            //this function runs a loop w/i itself
            System.out.println(student.getResults());
            System.out.println("\n");
        }

    }
}
prize.java

package studentdatabase;

/**
 * A class to represent a Prize
 */
public class Prize {
    // todo your code goes here
}
Medstudent.java
package studentdatabase;

/**
 * A class that represents a Medicine Student
 */
public class MedStudent extends Student {
    // todo your code goes here
}
Studentdatabasedriver.java
package studentdatabase;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class StudentDatabaseDriver implements Constants {

    public static void main(String[] args) throws IOException {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a file name to process: ");
        String fileName = scan.nextLine().trim();
        File inputFileName = new File(fileName);
        BufferedReader fileReader = new BufferedReader(new FileReader(inputFileName));

        String s;
        int prizeCount = 0;
        StudentDatabase studentDB = new StudentDatabase();
        Prize[] prizeList = new Prize[NUMBER_OF_PRIZES];
        while ((s = fileReader.readLine()) != null) {
            if (s.length() > 0)
                //all student types use the same addStudent function
                switch (s.charAt(0)) {

                    case 'A' -> {
                        studentDB.addStudent(s);
                    }
                    case 'M' -> {
                        studentDB.addStudent(s);
                    }
                    case 'S' -> {
                        studentDB.addStudent(s);
                    }

                    case 'R' -> {
                        studentDB.parseResult(s);
                    }
                    case 'P' -> {
                        // your code here
                    }
                    default -> {
                        // your code here
                    }
                } // switch
        } // while

        //this function prints out all records in the appropriate form
        //ideally this would also contain the code for saving copies
        studentDB.printRecords();
    }
}
Result.java

public class Result {
    String topicCode;
    String grade;
    String mark;

    public void setTopicCode(String code){
        topicCode = code;
    }

    public void setGrade(String g){
        grade = g;
    }

    public void setMark(String m){
        mark = m;
    }

    public String printResults(){
        //if statement makes sure mark prints as a blank instead of "null"
        if(mark == null){
            mark = "";
        }
        System.out.println(topicCode + " " + grade + " " + mark);
        return " ";

    }
}
Student.java

public class Student implements Constants {

    private String studentNo;
    private String familyName;
    private String givenName;
    private String degree;
    private String majorArt;
    private String minorArt;
    private ArrayList<String> prizes = new ArrayList<>();

    ArrayList<Result> results = new ArrayList<>();

    public void addID(String idNum){
        studentNo = idNum;
    }

    public String getID(){
        return studentNo;
    }

    public void addFamily(String surname){
        familyName = surname;
    }

    public String getFamilyName(){
        return familyName;
    }

    public void giveName(String giveName){
        givenName = giveName;
    }

    public String getGivenName(){
        return givenName;
    }

    public void setDegree(String d){

        degree = d;
    }

    public String getDegree(){

        return degree;
    }

public void setPrizes(String s){
        prizes.add(s);
}

public void getPrizes(){
        for(String each: prizes){
            System.out.println(each);
        }
        return;
}

public void setMajor(String m){
        majorArt = m;
    }

public String getMajor(){
        return majorArt;
}

public void setMinorArt(String m){
        minorArt = m;
}

public String getMinor(){
        return minorArt;
}

public void addResult(String code, String grade, String mark){
        //creates new obj
        Result working = new Result();
                working.setTopicCode(code);
        working.setGrade(grade);
        working.setMark(mark);
        //adds obj to the arrayList
        results.add(working);
    }

public String getResults(){
        int size = results.size();

        if(results.size() == 0) {
           System.out.println("No results entered");
        }
        else {
            //loops through and prints all student results
            for(int i = 0; i < size; i++) {
            results.get(i).printResults();
        }
        }

        return " ";
}
}
constants.java

package studentdatabase;

/**
 * An interface to enable the sharing of constant values
 */
public interface Constants {

    /**
     * The maximum number of Prizes allowed
     */
    int NUMBER_OF_PRIZES = 10;
    /**
     * The maximum number of students
     */
    int NUMBER_OF_STUDENTS = 1000;
    /**
     * The maximum number of topics a student can undertake
     */
    int NUMBER_OF_TOPICS = 40;
}
ArtStudent.java

package studentdatabase;

/**
 * A class that represents an Arts Student
 */
public class ArtsStudent extends Student {
    // todo your code goes here
}
here is input value to run the code

/Users/venunaidu/Desktop/SE3/assignment 2/group2/StudentDatabase/data/testFile.txt
Posted
Updated 24-May-22 22:14pm
v2
Comments
Richard MacCutchan 25-May-22 4:06am
   
What is the question?
OriginalGriff 25-May-22 5:33am
   
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with - we get no other context for your project.
Imagine this: you go for a drive in the country, but you have a problem with the car. You call the garage, say "it broke" and turn off your phone. How long will you be waiting before the garage arrives with the right bits and tools to fix the car given they don't know what make or model it is, who you are, what happened when it all went wrong, or even where you are?

That's what you've done here. So stop typing as little as possible and try explaining things to people who have no way to access your project!

Use the "Improve question" widget to edit your question and provide better information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900