/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package studentrecs;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import static studentrecs.StudentRecs.binarySearch;
import static studentrecs.StudentRecs.numstu;
import static studentrecs.StudentRecs.stu;
/**
*
* @author 066992389
*/
//TO MR.MARTIN THIS METHOD IS NOT INCLUSIVE IN WHAT YOU ARE MARKING NOT HIDING ANYTHING
//I SIMPLY DON'T WANT YOU TO WASTE YOUR TIME READING THIS
//MY MAIN WORKS FOR THE PROJECT IS STUDENTRECS.JAVA & STUREC.JAVA
public class Menu {
//DECLARATION OF PUBLIC VARIABLES THAT NEED TO BE ACCESSED BY MULTIPLE METHODS
/** IN THIS CLASS THE PROCCESS OF READING A FILE SPECIFIED BY THE USER IS BEING RUN
* A MENU PROMPTS UP WITH AN ASSORTMENT OF OPTIONS SUCH AS VIEW AVERAGES
* AND CHANGE SPECIFIC MARKS
* THESE OPERATIONS ARE HANDLDED BY DIFFERENT METHODS IN ORDER TO KEEP ORDER
* THE DECLARATION OF EACH SECTION OF THE STYLESHEET IS ALSO IMPLEMENTED HERE
* STATING THAT FOR EVERY SECTION BEFORE A COMMA THAT IS THE NAME AND THE BEHOLDER
* OF THAT METHOD. EG(14504871,THOMASON) THE SECTION BEFORE THE COMMA IS THE STUDENTID
*/
public static String other;
public static String user;
public int StuRec;
public static int numstu;
public static double average;
public static String choice;
public static StuRec[] stu = new StuRec[10000]; //Declaring that sturec has 1000lines of field
public static Boolean readFile(String filename) throws IOException { //readFile method reads the prompted file
float timer = System.nanoTime(); //BEGGINING TIMER VARIABLE TO EVAULTE THE STARTUP TIME PROCCESS
try { //since this is handling files with possibles errors try must be instianiated in cases of error handling
Scanner userInput = new Scanner(System.in); //scanner for user input
System.out.println("Type R To Read a File or Type Default for the default file"); //prompt for what the first step is
user = userInput.nextLine(); //whatever the user types
if (user.equalsIgnoreCase("r")) { //if the user types the key r (case ignored)
user = userInput.nextLine(); //user equals whatever the userInput is
}
filename = user; //the filename is what the user types
if (user.equalsIgnoreCase("default")) { //if user types default (case ignored)
filename = "newreg2.csv"; //then the file being read will be the newreg2.csv premade file
}
other = filename; //declaration for another method below for the file to be read outside
Scanner input = new Scanner(new FileReader(filename)); //Scanner to scan the given file
while (input.hasNext()) { //while the file has another field
in(input.nextLine());
numstu++; //counter for the amount of fields
}
input.close(); //closing the file
return true;
} catch (IOException e) { //error catching
System.err.println(e.getMessage()); //error response
}
return false;
}
public static void in(String reader) {
String splitter[]; //SPLITTER TO SPLIT THE FIELDS
splitter = reader.split(","); //THE SPLLITER WORKS IN DIVISIONS OF THE COMMAS
stu[numstu] = new StuRec();
stu[numstu].studentID = splitter[0]; //STUDENTID = FIELD BEFORE THE SPLITTER
stu[numstu].lastName = splitter[1]; //LASTNAME = FIELD BEFORE THE SPLITTER
stu[numstu].firstName = splitter[2];//FIRSTNAME = FIELD BEFORE THE SPLITTER
stu[numstu].phoneNumber = splitter[3];//PHONENUMBER = FIELD BEFORE THE SPLITTER
stu[numstu].courseCode = splitter[4];//COURSECODE = FIELD BEFORE THE SPLITTER
stu[numstu].periodNumber = Integer.parseInt(splitter[5]); // parseInt turns a string of digits into an integer //PERIODNUMBER = FIELD BEFORE THE SPLITTER
stu[numstu].mark = Integer.parseInt(splitter[6]); //MARK IN THAT COURSE
}
public static boolean menu() throws IOException {
//THIS MENU PROMPTS ALLOWS YOU TO WORK WITH THE FILE AND READ AND WRITE SPECIFIC DATA
Scanner userInput = new Scanner(System.in); //SCANNER FOR THE USERS INPUT
do { //DO LOOP FOR AN EXECUTE FOR A GIVEN NUMBER OF TIMES (NUMBER OF TIMES DECLARED BY WHILE SOMETHING)
choice = "";
System.out.println("============================== ==============="); //MENU OPTIONS (THEY ARE LOOPED TO BE DONE INDEFINATLY)
System.out.println("Type R To Read Another File");
System.out.println("Type L To Print all File Records");
System.out.println("Type AA To Print The Average Of All The Marks");
System.out.println("Type AC To Print Marks For a Specific Course");
System.out.println("Type NM To Change a Students Mark");
System.out.println("Type X To Exit The Program");
choice = userInput.nextLine(); //READS THE USERS CHOICE FOR WHAT THEY WANT TO DO WITH THE FILE
if (choice.equalsIgnoreCase("L")) { //IF THE CHOICE WAS THE LETTER L THEN RUN METHOD BELOW IT (SO IT CALLS THAT METHOD AND THAT METHOD IS RUN)
printList();
} else if (choice.equalsIgnoreCase("R")) {
fileChange();
} else if (choice.equalsIgnoreCase("AA")) {
average();
} else if (choice.equalsIgnoreCase("X")) {
exit();
} else if (choice.equalsIgnoreCase("AC")) {
courseAverage();
} else if (choice.equalsIgnoreCase("NM")) {
newMark();
} else if (choice.equalsIgnoreCase("RM")) {
} else {
System.err.println("Unknown Key Try Again..."); //IF THE USER INPUT WAS NONE OF THESE CHOICES THEN THROW THEM A NOTICE
}
} while (!choice.equalsIgnoreCase("x")); //DO WHILE THE CHOICE IS NOT X WHICH EXITS THE PROGRAM
return false;
}
public static void fileChange() throws IOException { //FILE CHANGE METHOD READS ANOTHER FILE THAT IS ASKED BY THE USER
readFile(user); //CALLS THE READFILE METHOD IN ORDER TO READ THAT FILE AND RUN IT PROPERLY AND IT READS THE FILE PROMPTED BY THE USER
}
public static void printList() { //PRINTS OUT ALL THE FIELDS OF DATA FOR EVERY STUDENT
for (int i = 0; i < numstu; i++) { //FROM 0 TO THE END OF THE FIELDS IN THE FILE
System.out.println(stu[i].toString()); //PRINT OUT THE REQUESTED CLASS
}
}
public static void average() { //AN AVERAGE CALCULATOR FOR THE ENTIRE STUDENT BODY
for (int i = 0; i < numstu; i++) { //FOR LOOP TO READ EVERY STUDENT RECORD
average += stu[i].mark; // keep adding to average //FOR EVERY RECORD ADD THE MARK OF THAT RECORD TO THE VARIABLE AVERAGE
}
// dividision by zero protection
if (choice.equalsIgnoreCase("AA") && numstu > 0) {
average = average / numstu; // compute the average. Always use the size in terms of a variable whenever possible.
System.out.println(average); // as noted below, if this is an integer value, < #of students computations will eval to 0.
} else if (!choice.equalsIgnoreCase("AA") && numstu < 0) { //IF THE MARK IS LESS THEN 0 THEN THERE MUST BE A PROBLEM
System.out.println("Oops! No Marks To Calculate!"); //PRINT OUT A NOTICE OF THE ISSUE
}
}
//THIS METHOD CALCULATES THE AVERAGE FOR A SPECIFIED COURSE
public static void courseAverage() {
System.out.println("Please Type In The Class Course Code"); //PROMPTS USER TO SELECT A COURSE TO CALCULATE
int markAdder = 0; //COUNTER FOR THE MARKS (IT ADDS UP)
int counter = 0; //COUNTER FOR THE AMOUNT OF STUDENTS IN THAT COURSE
int finalAverage; //FINALAVERAGE CALCULATOR
String choicer; //USERINPUT CHOICE
Scanner reader = new Scanner(System.in); //SCANNER FOR WHAT THE USER TYPES
choicer = reader.nextLine(); //WHATEVER THE USER TYPED IS STORED IN THE VARIABLE CHOICER
for (int i = 0; i < numstu; i++) { //FOR LOOP EVERY SINGLE FIELD IN THE FILE
if (stu[i].courseCode.equalsIgnoreCase(choicer)) { //IF THE COURSECODE EQUALS TO WHAT THE USER TYPES THEN...
markAdder += stu[i].mark; //GO TO THAT STUDENTS MARK AND ADD IT TO THE MARKADDER VARIABLE
counter++; //THIS COUNTS THE AMOUNT OF STUDENTS IN THAT COURSE FOR THE FINAL CALCULATION
}
}
if (numstu > 0) { //SIMPLE ERROR CHECKING THAT SOME FIELDS WERE CALCULATED
finalAverage = markAdder / counter; //THE AVERAGE EQUALS THE MARKS DIVIDED BY THE NUMBER OF STUDENTS IN THAT COURSE
System.out.println(finalAverage);
}
}
//THIS METHOD WRITES A NEW MARK TO EITHER THE FILE A NEW FILE OR TEMPORARILY VIEWED FILE (WHICH IS THE MENU VIEW)
public static void newMark() throws IOException { //FILE HANDLING, SO ERROR CHECKING IS A MUST
System.out.println("Type In The Student ID"); //PROMPT FOR STUDENTID
Scanner input = new Scanner(System.in); //USER INPUT SCANNER FOR WHAT THEY TYPE
int newMarkk; //VARIABLE FOR THEIR NEW MARK
String choiceForID; //THE ID THEY TYPE
choiceForID = input.nextLine(); //A REQUEST FOR THE USER TO FILL IN THE REQUIRED FIELD
for (int i = 0; i < numstu; i++) { //FOR EVERY FIELD IN THE FILE
if (stu[i].studentID.equalsIgnoreCase(choiceForID)) { //IF THE STUDENTID AT THE POINT OF THE FILE IS THE SAME AS WHAT THE USER TYPED
System.out.println("Please Type The New Mark"); //PROMPT FOR NEW UP TO DATE MARK
newMarkk = input.nextInt(); //NEWMARK EQUALS WHATEVER THEY TYPE
stu[i].setMark(newMarkk); //SET WHAT THEY TYPE AS THE NEW MARK
}else
System.out.println("error...");
//saver(); //CALL THE METHOD THAT WILL GIVE THEM CHOICES OF SAVING
}
}
//SAVER MENU THIS MENU OPERATES THE TYPE OF SAVE THAT THE USER WOULD LIKE
public static void saver() throws IOException { //FILE WRITING SO THERE IS ERROR HANDLING
Scanner input = new Scanner(System.in); //SCANS USER INPUT
System.out.println("Type S To Save It Here");
System.out.println("Type SA To Save It To a New File");
System.out.println("Type LP For a Temporary Save");
choice = input.nextLine();
if (choice.equalsIgnoreCase("s")) { //IF CHOICE EQUALS S
saveMark(); //GO TO METHOD SAVEMARK
} else if (choice.equalsIgnoreCase("sa")) { //IF CHOICE EQUALS SA
saveAs(); //GO TO SAVEAS METHOD
} else if (choice.equalsIgnoreCase("lp")) {
menu(); //IF THEY CHOOSE TEMPORARY SAVE THEN GO BACK TO THE MAIN MENU AS STU[I].SETMARK ALREADY EQUALS THE MARK THEY GAVE THEM
}
}
//THIS METHOD UPDATES THE MARK
public static void saveMark() throws IOException {
File fileWriter = new File(other);
PrintWriter writer = new PrintWriter(new FileWriter(fileWriter));
for (int i = 0; i < numstu; i++) {
writer.println(stu[i].lastName + ", " + stu[i].firstName + ", " + stu[i].studentID + ", " + stu[i].phoneNumber + ", " + stu[i].courseCode + ", " + stu[i].periodNumber + ", " + stu[i].mark);
}
writer.close();
}
//THIS METHOD EXITS THE PROGRAM AND ENDS IT
public static void exit() {
for (int i = 0; i < numstu; i++) {
System.exit(i);
}
}
//THIS METHOD SAVES THE MARK TO ANOTHER FILE THAT IS PROMPTED BY THE USER
public static void saveAs() throws IOException {
System.out.println("Type In The New File Name");
Scanner fileChoice = new Scanner(System.in);
String newFile;
newFile = fileChoice.nextLine();
File fileWriter = new File(newFile);
PrintWriter writeNew = new PrintWriter(new FileWriter(newFile));
for (int i = 0; i < numstu; i++) {
writeNew.println(stu[i].lastName + ", " + stu[i].firstName + ", " + stu[i].studentID + ", " + stu[i].phoneNumber + ", " + stu[i].courseCode + ", " + stu[i].periodNumber + ", " + stu[i].mark);
}
writeNew.close();
}
public static int binarySearch(String id) {
int lo = 0;
int hi = numstu - 1;
while (lo <= hi) {
// Key is in a[lo..hi] or not present.
int mid = lo + (hi - lo) / 2;
if (stu[mid].studentID.compareTo(id) > 0) { //A STUDENT ID COMPARED TO ANOTHER TO COMPARE WHICH IS GREATEST TO 0
hi = mid - 1;
} else if (stu[mid].studentID.compareTo(id) < 0) { //A STUDENT ID COMPARED TO ANOTHER TO COMPARE WHICH IS LESS THAN 0
lo = mid + 1;
} else {
return mid;
}
}
return -1;
}
public static void changeMarkBin(String id, int newmark) {
binarySearch(id);
int indx = binarySearch(id); // indx equals the index of the id that was searched
if (indx >= 0) { //if the index is greater then 0
stu[indx].setMark(newmark); //change that index's mark to whatever the arguement said so
System.out.println("The Student " + id + " " + "is at index " + indx + ", " + newmark); //it prints the index
}
}
}