Skip to content

Instantly share code, notes, and snippets.

@jacobwalkr
Created November 7, 2013 14:16
Show Gist options
  • Save jacobwalkr/7355218 to your computer and use it in GitHub Desktop.
Save jacobwalkr/7355218 to your computer and use it in GitHub Desktop.
Code team 10
/*
* COM1001 Crossover Assignment 1
* Team 10
*/
import sheffield.*;
public class CodeTeam10 {
private static Student[] students;
private static Lecturer[] lecturers;
private static int numberOfStudents;
private static int numberOfLecturers;
public static void main(String[] args) {
EasyWriter writer = new EasyWriter();
//Shows a list of instructions to aid the user
writer.println(" ");
writer.println("Instructions:");
writer.println(" Exit = 1");
writer.println(" Input data = 2");
writer.println(" Validate = 3");
writer.println(" Compute student marks = 4");
writer.println("Compute lecturer workload = 5");
writer.println(" ");
writer.println("1 or 2 will only be accepted");
writer.println(" ");
boolean inputData = false;
while(true) {
//read integer from user
EasyReader keyboard = new EasyReader();
int num = keyboard.readInt("Please enter an instruction: ");
writer.println();
//perform tasks
//exit program
if(num == 1) {
writer.println(" ");
writer.println(" ");
writer.println("***** End of program *****");
System.exit(0);
}
//input
else if (num == 2) {
/*
* Begin "InputData"
*/
EasyReader studentReader = new EasyReader("students.txt");
EasyReader lecturerReader = new EasyReader("lecturers.txt");
// Read in the numbers of students and lecturers - the first line from
// each file. This also puts the cursor on the second line.
numberOfStudents = studentReader.readInt();
numberOfLecturers = lecturerReader.readInt();
students = new Student[numberOfStudents];
lecturers = new Lecturer[numberOfLecturers];
for (int student = 0; student < numberOfStudents; student++) {
Student newStudent = new Student();
newStudent.modules = new StudentModule[6];
newStudent.ucard = Integer.parseInt(studentReader.readString().trim());
newStudent.year = Integer.parseInt(studentReader.readString().trim());
for (int module = 0; module < 6; module++) {
StudentModule newModule = new StudentModule();
// Each module has its own line
String[] moduleData = studentReader.readString().trim().split("\\s+");
// Multiplies module by 3 to get the position of the module
// in the data string, then adds 2 to account for the first
// two data points in the string, then adds 0, 1 or 2 to read
// the correct value in the data string for that field of the
// module record - breathe
newModule.code = moduleData[0];
newModule.credits = Integer.parseInt(moduleData[1]);
newModule.mark = Integer.parseInt(moduleData[2]);
// Save the module to the student
newStudent.modules[module] = newModule;
}
// Save the new student to the students array
students[student] = newStudent;
}
for (int lecturer = 0; lecturer < numberOfLecturers; lecturer++) {
Lecturer newLecturer = new Lecturer();
newLecturer.modules = new LecturerModule[6];
newLecturer.ucard = Integer.parseInt(lecturerReader.readString().trim());
for (int module = 0; module < 2; module++) {
LecturerModule newModule = new LecturerModule();
// Each module has its own line
String[] moduleData = lecturerReader.readString().trim().split("\\s+");
// Multiplies module by 3 to get the position of the module
// in the data string, then adds 2 to account for the first
// two data points in the string, then adds 0, 1 or 2 to read
// the correct value in the data string for that field of the
// module record - breathe
newModule.code = moduleData[0];
newModule.weight = Integer.parseInt(moduleData[1]);
newModule.size = Integer.parseInt(moduleData[2]);
// Save the module to the lecturer
newLecturer.modules[module] = newModule;
}
// Save the new student to the lecturers array
lecturers[lecturer] = newLecturer;
}
writer.println("Data read from files successfully \\o/");
/*
* End "InputData"
*/
inputData = true;
}
//validate
else if ((num == 3) && inputData) {
//Counts the number of total errors in the program
int errors=0;
//Check for student data
for (int i=0; i<numberOfStudents; i++) {
//Year is between 1 and 3
if (students[i].year<0 || students[i].year>4) {
writer.println("Student " + students[i].ucard + ": year = invalid. Must be a value between 1 and 3.");
errors=(errors+1);
}
//Module analysis
for (int j=0; j<6; j++) {
//Number credits is positive
if (students[i].modules[j].credits<1) {
writer.println("Student " + students[i].ucard + ": module " + students[i].modules[j].code + ": credits = invalid. Must be a positive value." );
errors=(errors+1);
}
//Module mark is positive
if (students[i].modules[j].mark<1) {
writer.println("Student " + students[i].ucard + ": module " + students[i].modules[j].code + ": mark = invalid. Must be a positive value.");
errors=(errors+1);
}
}
}
//Check for lecturer data
for (int i=0; i<numberOfLecturers; i++) {
//Module analysis
for (int j=0; j<2; j++) {
//Module weight is either 2 or 4
if ((lecturers[i].modules[j].weight != 2) && (lecturers[i].modules[j].weight != 4)) {
writer.println("Lecturer " + lecturers[i].ucard + ": module " + lecturers[i].modules[j].code + ": weight = invalid. Must be either 2 or 4.");
errors=(errors+1);
}
//Number of registered students is positive
if (lecturers[i].modules[j].size<1) {
writer.println("Lecturer " + lecturers[i].ucard + ": module " + lecturers[i].modules[j].code + ": size = invalid. Must be a positive value.");
errors=(errors+1);
}
}
}
//If there are no errors tell the user so
if (errors==0) {
writer.println("Student and staff data are validated.");
}
//If there are errors tell the user they must re-input their data
else {
writer.println("There are " + errors + " total errors in your data. Please re-input your data");
}
}
//compute student marks
else if ((num == 4) && inputData) {
writer.println("Nowt here");
}
//compute lecturer workload
else if ((num == 5) && inputData) {
/*
* Begin "Lecturer Workload"
*/
writer.println ( " Lecturer Ucard | Workload " );
writer.println ( "================|==========" );
for (int i=0; i<numberOfLecturers; i++) {
for (int j=0; j<2; j++) {
LecturerModule module = lecturers[i].modules[j];
int moduleWorkload = module.weight*module.size;
writer.print(lecturers[i].ucard, 15);
writer.print(" | ");
writer.println(moduleWorkload, 8);
}
}
/*
* End "Lecturer Workload"
*/
}
//reject instruction if integer entered is above 5 or less than 1
else {
writer.println("Invalid instruction.");
}
writer.println();
}
}
}
class Student {
int ucard;
int year;
StudentModule[] modules;
}
class StudentModule {
String code;
int credits;
int mark;
}
class Lecturer {
int ucard;
LecturerModule[] modules;
}
class LecturerModule {
String code;
int weight;
int size;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment