Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Level-1.4.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Binary file added out/production/Level-1.4/Main.class
Binary file not shown.
Binary file added out/production/Level-1.4/Student.class
Binary file not shown.
Binary file added out/production/Level-1.4/StudentFrame.class
Binary file not shown.
5 changes: 0 additions & 5 deletions src/App.java

This file was deleted.

148 changes: 148 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
public class Main{
static ArrayList<Student> studentList;
public static void main(String[] args) throws FileNotFoundException {
studentList = initialize();
Scanner input = new Scanner(System.in);
boolean flag = true;

do{
try{
System.out.print("""
Please select one of the following operations:
1- Add a new student
2- View student info in records
3- Exit program
""");

short choice = input.nextShort();

switch( choice ){
case 1:
int id;
String fName, lName;
System.out.print("Please insert ID: ");
id = input.nextInt();
if(find(id) != -1)
throw new IllegalArgumentException("ID already exists!");

System.out.print("Please insert first name: ");
fName = input.next();
System.out.print("Please insert last name: ");
lName = input.next();

Student temp = new Student(id, fName, lName);
System.out.println("Please insert the amount of absences in the following subjects (out of 50):");
for(short i = 0; i < 4; i++){
String[] courseList = {"Math", "Phys", "Chem", "Comp"};
short attendanceNum;
System.out.printf("%s: ", courseList[i]);
attendanceNum = input.nextShort();
temp.setAttendance(attendanceNum, i);
}

System.out.print("Any excused absences? (y/n): ");
if(input.next().toLowerCase().charAt(0) == 'y'){
input.nextLine();
System.out.print("Please insert ALL excuses here: ");
Scanner reader = new Scanner(input.nextLine());
while(reader.hasNext())
if( reader.next().equalsIgnoreCase("hospital"))
temp.attendanceIncrease();
reader.close();
}
else input.nextLine();

System.out.print("Please insert grade (0.0 - 5.0): ");
temp.setGpa(input.nextDouble());


view(temp);
studentList.add(temp);
break;

case 2:
int index;
System.out.print("Please insert student ID here: ");
index = find(input.nextInt());
if(index == -1)
throw new IllegalArgumentException("ID not in records!");
view(studentList.get(index));
break;

case 3:
flag = false;


}
}
catch(InputMismatchException e){
System.err.println("Wrong type of input!");
input.nextLine();
}
catch (IllegalArgumentException f){
System.err.println(f.getMessage());
}
finally {
System.out.println("-----------------------------------------------------------------------------\n");
}
}
while(flag);







save();
}

static ArrayList<Student> initialize(){
ArrayList<Student> temp = new ArrayList<>();
try{
Scanner reader = new Scanner( new FileInputStream("studentList.txt"));
while(reader.hasNext() ) {
temp.add(new Student(reader.nextInt(), reader.next(), reader.next()));
for (short i = 0; i < 4; i++)
temp.getLast().setAttendance(reader.nextShort(), i);
temp.getLast().setGpa(reader.nextDouble());
}
}
catch(FileNotFoundException e){
new File("studentList.txt");
return new ArrayList<>();
}
return temp;
}

static void save() throws FileNotFoundException {
PrintWriter pout = new PrintWriter(new FileOutputStream("studentList.txt"));
for(Student i : studentList ) {
pout.print(String.format("%d\t%s\t", i.getID(), i.getName()));
for(short j : i.getAttendanceList())
pout.print(j + "\t");
pout.println(i.getGPA());
}
pout.close();
}

static int find(int id){
for(int i = 0; i < studentList.size(); i++)
if(id == studentList.get(i).getID())
return i;
return -1;
}

static void view(Student std){
System.out.printf("-----------------------------------------------------------------------------\nID: %d\nName: %s\nATTENDANCE [MATH, PHYS, CHEM, COMP]:\n%s\nGPA: %.2f\n\n%s\n",std.getID(),std.getName(), Arrays.toString(std.getAttendanceList()), std.getGPA(),std.getStatus());
}
}
59 changes: 59 additions & 0 deletions src/Student.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
public class Student {
private final int ID;
private final String NAME;
private double gpa;



//Math: 0 Phys: 1 Chem: 2 Prog: 3
private static final short attendanceLimit[] = {44,42,40,47};
private final short[] attendanceList = new short[4];

public Student(int id, String fName, String lName){
ID = id;
NAME = String.format("%s\t%s",fName,lName);
}

public int getID(){
return ID;
}

public String getName(){
return NAME;
}

public double getGPA(){
return gpa;
}

public short[] getAttendanceList(){
return attendanceList;
}

public void setGpa(double newGrade)throws IllegalArgumentException{
if(newGrade > 5 || newGrade < 0)
throw new IllegalArgumentException("Inserted grade is not within acceptable range.");
else gpa = newGrade;
}

public void setAttendance(short absenceNum, short courseIndex){
if(absenceNum > 50 || absenceNum < 0)
throw new IllegalArgumentException("Attendance number not within range!");
attendanceList[ courseIndex ] = absenceNum;
}

public void attendanceIncrease(){
for(short i = 0; i < attendanceList.length; i++)
if(attendanceList[i] < 50)
setAttendance((short) (attendanceList[i] + 1), i);
}

public String getStatus(){
for(short i = 0; i < 4; i++)
if(attendanceList[i] < attendanceLimit[i])
return "FAIL";
if( getGPA() < 3.5)
return "FAIL";
return "PASS";
}
}
2 changes: 2 additions & 0 deletions studentList.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
310 Super Mario 50 50 50 50 5.0
935 Edward Richtofen 41 43 41 49 4.5