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;
public class Prize {
}
package studentdatabase;
public class Student {
}
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' -> {
}
default -> {
}
}
}
studentDB.printRecords();
}
}
What I have tried:
here are my codes for this assignment so far
studentdatabase.java
public class StudentDatabase implements Constants {
Student[] db;
int studentCount;
public StudentDatabase() {
db = new Student[NUMBER_OF_STUDENTS];
studentCount = 0;
}
public void addStudent(String s) {
Scanner scan = new Scanner(s);
scan.useDelimiter(",");
String[] stringList = s.split(",");
Student placeMan = new Student();
db[studentCount] = placeMan;
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) {
String[] stringList = s.split(",");
for(Student each: db){
String searchID = stringList[1];
if(Objects.equals(each.getID(), searchID)){
if(stringList.length == 5){
each.addResult(stringList[2], stringList[3], stringList[4]);
break;
}
else{
each.addResult(stringList[2], stringList[3], null);
break;
}
}
}
}
public void printRecords() throws IOException {
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());
if(student.getDegree() == "Medicine"){
System.out.println("Prize: " );
}
else if( student.getDegree()=="Art"){
System.out.println("Major: "+student.getMajor());
System.out.println(("Minor: "+student.getMinor()));
}
System.out.println(student.getResults());
System.out.println("\n");
}
}
}
prize.java
package studentdatabase;
public class Prize {
}
Medstudent.java
package studentdatabase;
public class MedStudent extends Student {
}
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)
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' -> {
}
default -> {
}
}
}
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(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){
Result working = new Result();
working.setTopicCode(code);
working.setGrade(grade);
working.setMark(mark);
results.add(working);
}
public String getResults(){
int size = results.size();
if(results.size() == 0) {
System.out.println("No results entered");
}
else {
for(int i = 0; i < size; i++) {
results.get(i).printResults();
}
}
return " ";
}
}
constants.java
package studentdatabase;
public interface Constants {
int NUMBER_OF_PRIZES = 10;
int NUMBER_OF_STUDENTS = 1000;
int NUMBER_OF_TOPICS = 40;
}
ArtStudent.java
package studentdatabase;
public class ArtsStudent extends Student {
}
here is input value to run the code
/Users/venunaidu/Desktop/SE3/assignment 2/group2/StudentDatabase/data/testFile.txt