import java.util.Scanner;
import java.io.*;
public class Project
{
public static final int MAX_MODULES=15;
public static final int MAX_STUDENTS=40;
public static void main(String[] args) throws IOException
{
String[] userId = new String[MAX_STUDENTS];
String[] password = new String[MAX_STUDENTS];;
String[] fName = new String[MAX_STUDENTS];;
String[] lName = new String[MAX_STUDENTS];;
int[] userType = new int[MAX_STUDENTS];
String[] choice1 =new String[MAX_MODULES];;
String[] choice2 = new String[MAX_MODULES];;
String[] mCode = new String[MAX_MODULES];;
String[] mName = new String[MAX_MODULES];;
int [] numberRegistered = new int [MAX_MODULES];
int numUsers = readUsers(userId, password, fName, lName, userType, choice1, choice2);
int numModules = readModules(mCode, mName, numberRegistered);
int logedInIndex=logIn(userId, password, numUsers);
if (userType[logedInIndex]==1)
{
studentMenu(fName,lName,mCode, mName, numberRegistered, numModules, logedInIndex, choice1, choice2, numUsers,userId,userType);
}
else
{
adminMenu( mCode, mName, numberRegistered, numModules, userId,
fName, lName, numUsers,logedInIndex,userType);
}
}
public static int readModules(String[] moduleCode, String[] moduleName, int[] numberRegistered) throws FileNotFoundException {
// TODO Auto-generated method stub
File file = new File("module.txt");
Scanner inputFile = new Scanner(file);
int i=0;
while(inputFile.hasNext())
{
moduleCode[i]=inputFile.nextLine();
moduleName[i]=inputFile.nextLine();
numberRegistered[i]=inputFile.nextInt();
inputFile.nextLine();
i++;
}
inputFile.close();
return i;
}
public static int readUsers(String[] userId, String[] password,String[] firstName, String[] lastName, int[] userType,String[] choice1, String[] choice2) throws FileNotFoundException {
File file = new File("users.txt");
Scanner inputFile = new Scanner(file);
int i=0;
while(inputFile.hasNext())
{
userId[i]=inputFile.nextLine();
password[i]=inputFile.nextLine();
firstName[i]=inputFile.nextLine();
lastName[i]=inputFile.nextLine();
userType[i]=inputFile.nextInt();
inputFile.nextLine();
choice1[i]=inputFile.nextLine();
choice2[i]=inputFile.nextLine();
i++;
}
inputFile.close();
return i;
}
/**
* Read and displays the contents of an input file.
* @param inputFile The Scanner object that refers to the file that is to be read.
* @param answers - the array of type boolean that will hold the answers to the questions but have to be "true or false".
* @param question - the array of type String that stores all the questions for the test.
* @return - returns the contents of the file.
*/
public static int readFile(Scanner inputFile, String[] userId, String[] password)
{
// Read lines from the file until no more are left.
int i = 0;
while (inputFile.hasNext() && i < MAX_STUDENTS)
{
// Read the next line.
userId[i] = inputFile.nextLine();
//System.out.println(question[i]);
password[i] = inputFile.nextLine();
//System.out.println(answers[i]);
inputFile.nextLine();
// Display the last line read.
//System.out.println(str);
i++;
}
return i;
}
/**
* This method checks to use that the file name that was entered is valid.
* the user wil be prompted to re-enter the name of the file until an acceptable file name is entered.
* @param prompt - this is a prompt that is used to tell the user what to do,
* for example: "please enter your password".
* @return returns the valid file name.
*/
public static File getValidFile(String prompt)
{
String filename; // The name of the file
File file;
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Get a valid file name.
do
{
System.out.print(prompt);
filename = keyboard.nextLine();
file = new File(filename);
if (!file.exists())
System.out.println("The specifed file does not exist - please try again!");
}while( !file.exists());
return file;
}
public static int logIn(String[] userId, String[] password, int numUsers)
{
System.out.println("CIT MODULE REGISTRATION - Login\n"+"===========================================================================");
Scanner keyboard = new Scanner(System.in);
int index;
do{
String userIdEntered=readStringUpper("ID number: ");
String passwordEntered=readString("password: ");
index=find( userId, userIdEntered, numUsers);
if (index!=-1)
{
if (!userIdEntered.equals(userId[index]))
{
System.out.println("Login Failed because user Id is wrong Try Again!");
index=-1;
}
if (!passwordEntered.equals(password[index]))
{
System.out.println("Login Failed because password is wrong Try Again!");
index=-1;
}
}
}while(index==-1);
return index;
}
/**Finds the index of a specified item in an array
* @param x The array to be searched
* @param value The value to be found
* @return The index in the array at which the item is found */
public static int find(String[] x, String value, int numValues) {
int index = 0;
boolean found = false;
while(index < numValues && !found)
{
if (x[index].equals( value))
found = true;
else
index++;
}
if (found)
return index;
else
return -1;
}
public static String readString(String prompt)
{
String name;
Scanner keyboard = new Scanner(System.in);
do
{
System.out.print(prompt);
name=keyboard.nextLine();
if (name.length() <=1)
{
System.out.println("Error password must have more than one character");
}
}while (name.length() <=1);
return name;
}
public static String readStringUpper(String prompt)
{
String name;
Scanner keyboard = new Scanner(System.in);
do
{
System.out.print(prompt);
name=keyboard.nextLine();
if (name.length() <=1)
{
System.out.println("Error Id number must have more than one character");
}
}while (name.length() <=1);
return name.toUpperCase();
}
/*public static int getCustomerMenuChoice()
{
System.out.println();
int menuChioce;
do {
menuChioce = readInteger ("Please enter one of the above options");
if (menuChioce <1 || menuChioce > 6)
{
System.out.println("You must enter a number between 1 and 6.");
}
}while (menuChioce <1 || menuChioce > 6);
return menuChioce;//The correct menu choice has been chosen.
}*/
//public static void menuChoice(int userType[],int logedInIndex)
//{
//}
public static void adminMenu(String[] mCode, String[] mName, int[] numberRegistered, int numModules,String[] userId,
String[] fName, String[] lName,int numUsers,int logedInIndex,int[] userType) throws IOException
{
int select;
Scanner keyboard = new Scanner(System.in);
do
{
System.out.println( fName[logedInIndex] +" " + lName[logedInIndex] +" "+ "[Admin]"+" "+"MODULE REGISTRATION "+" "+"[Main Menu]");
System.out.println("===========================================");
System.out.println(
"1 - Add a student\n" +
"2 - List all students\n" +
"3 - Add a module\n" +
"4 - List all modules\n" +
"5 - List students registered for a module\n" +
"6 - Exit\n");
select=readPositiveInteger("Please Choose Options 1 - 6: ");
switch (select)
{
case 1:
addStudent( fName, lName,logedInIndex);
break;
case 2:
listStudents( fName, lName, numberRegistered, numModules,logedInIndex,userType);
break;
case 3:
addModule( fName, lName,logedInIndex);
break;
case 4:
listAllModules(fName, lName,mCode, mName, numberRegistered, numModules,logedInIndex,userType);
break;
case 5:
studentsRegistered( userId, fName, lName, mCode,mName,numberRegistered, numUsers,logedInIndex);
break;
}
}while (select!=6);
goodbye();
}
private static void goodbye()
{
System.out.println("Goodbye Thank You For Using The CIT MODULE REGISTRATION");
System.out.println("You Are Logged Out");
}
public static void studentMenu(String[]fName,String[]lName,String[] mCode, String[] mName, int[] numberRegistered, int numModules, int logedInIndex, String[] choice1, String[] choice2, int numUsers,String[] userId ,int [] userType) throws IOException
{
int select;
Scanner keyboard = new Scanner(System.in);
do
{
System.out.println( fName[logedInIndex] +" " + lName[logedInIndex] +" "+ "[Student]"+" "+"MODULE REGISTRATION "+" "+"[Main Menu]");
System.out.println("===========================================");
System.out.println(
"1 - List my current modules\n" +
"2 - Register for a module\n" +
"3 - Deregister for a module\n" +
"4 - List all modules\n" +
"5 - Exit\n");
select=readPositiveInteger("Please Choose Options 1 - 5: ");
switch (select)
{
case 1:
listCurrentModules(fName,lName,choice1, choice2, numUsers, mCode, mName, numModules, logedInIndex);
break;
case 2:
registerForModule(mCode,userId, fName, lName, logedInIndex);
break;
case 3:
deregisterForModule(fName, lName,logedInIndex,numberRegistered);
break;
case 4:
listAllModules(fName, lName,mCode, mName, numberRegistered, numModules,logedInIndex,userType);
break;
}
}
while (select!=5);
goodbye();
}
public static void listCurrentModules(String[]fName, String[]lName,String[] choice1, String[] choice2, int numUsers, String[] moduleCode, String[] moduleName, int numModules,int logedInIndex) {
System.out.println( fName[logedInIndex] +" " + lName[logedInIndex] +" "+ "[Student]"+" "+"MODULE REGISTRATION "+" "+"[My Modules]");
System.out.println("===========================================");
System.out.println("Module Code\tModule Name");
System.out.println("-----------\t------------------");
int moduleChoice1Index = find(moduleCode, choice1[logedInIndex], numModules);
int moduleChoice2Index = find(moduleCode, choice2[logedInIndex], numModules);
if (moduleChoice1Index != -1)
{
System.out.println("1. " + choice1[logedInIndex] + "\t" + moduleName[moduleChoice1Index]);
}
else
{
System.out.println("1. " + choice1[logedInIndex] + "\tUnspecified");
}
if (moduleChoice2Index != -1)
{
System.out.println("2. " + choice2[logedInIndex] + "\t" + moduleName[moduleChoice2Index]);
}
else
{
System.out.println("2. " + choice2[logedInIndex] + "\tUnspecified");
}
}
public static String listAllModules(String[] fName,String[] lName,String[] moduleCode, String[] moduleName, int[] numberRegistered, int numModules,int logedInIndex,int[] userType) {
if (userType[logedInIndex]==1)
{
System.out.println( fName[logedInIndex] +" " + lName[logedInIndex] +" "+ "[Student]"+" "+"MODULE REGISTRATION "+" "+"[List All Modules]");
System.out.println("===========================================");
}
else
{
System.out.println( fName[logedInIndex] +" " + lName[logedInIndex] +" "+ "[Admin]"+" "+"MODULE REGISTRATION "+" "+"[List All Modules]");
System.out.println("===========================================");
}
System.out.println("Code\t" + "\tModule Name" + "\t\tNumbers");
for(int i=0; i<numModules; i++)
{
System.out.println(moduleCode[i]+ " "+"\t" + moduleName[i] + "\t" + numberRegistered[i]);
}
return null;
}
public static void deregisterForModule(String[] fName,String[] lName,int logedInIndex,int []numberRegistered) throws IOException {
System.out.println( fName[logedInIndex] +" " + lName[logedInIndex] +" "+ "[Student]"+" "+"MODULE REGISTRATION "+" "+"[Deregister For Module]");
System.out.println("===========================================");
Scanner keyboard = new Scanner(System.in);
String moduleCode;
// Get data and write it to the file.
// Open the file for appending
FileWriter fw = new FileWriter("module.txt", true);
PrintWriter outputFile = new PrintWriter(fw);
// Get the name of a friend.
System.out.print("Enter the Module Code");
moduleCode = keyboard.nextLine();
// Close the file.
outputFile.close();
System.out.println("Data written to the file.");
}
public static void registerForModule(String [] moduleCode,String[] userId,String[] fName,String[] lName, int logedInIndex) throws IOException
{
System.out.println( fName[logedInIndex] +" " + lName[logedInIndex] +" "+ "[Student]"+" "+"MODULE REGISTRATION "+" "+"[Register For Module]");
System.out.println("===========================================");
Scanner keyboard = new Scanner(System.in);
String filename;
String moduleReg;
// Consume the remaining newline character.
// Get data and write it to the file.
// Open the file for appending
FileWriter fw = new FileWriter("user.txt", true);
PrintWriter outputFile = new PrintWriter(fw);
// Get the name of a friend.
System.out.print("Enter the Module Code");
moduleReg = keyboard.nextLine();
// Write the name to the file.
outputFile.println(moduleReg);
// Close the file.
outputFile.close();
System.out.println("Data written to the file.");
// method must be written
}
public static void addStudent(String[] fName,String[] lName,int logedInIndex) throws IOException
{
System.out.println( fName[logedInIndex] +" " + lName[logedInIndex] +" "+ "[Admin]"+" "+"MODULE REGISTRATION "+" "+"[Add Student]");
System.out.println("===========================================");
Scanner keyboard = new Scanner(System.in);
String studentId;
String password;
String firstName;
String lastName;
String module="unspecfied";
int userType=1;
// Get data and write it to the file.
// Open the file for appending
FileWriter fw = new FileWriter("user.txt", true);
PrintWriter outputFile = new PrintWriter(fw);
// Get the name of a friend.
System.out.print("Enter Student Id");
studentId = keyboard.nextLine();
System.out.print("Enter Password");
password = keyboard.nextLine();
System.out.print("Enter Students First Name");
firstName=keyboard.nextLine();
System.out.print("Enter Students Last Name");
lastName=keyboard.nextLine();
// Write the name to the file.
outputFile.println(studentId);
outputFile.println(password);
outputFile.println(firstName);
outputFile.println(lastName);
outputFile.println(userType);
outputFile.println(module);
outputFile.println(module);
// Close the file.
outputFile.close();
System.out.println("Data written to the file.");
}
public static void studentsRegistered(String[] userId,
String[] fName, String[] lName,String[] mCode, String[] mName,int[] numberRegistered, int numUsers,int logedInIndex)
{
System.out.println( fName[logedInIndex] +" " + lName[logedInIndex] +" "+ "[Admin]"+" "+"MODULE REGISTRATION "+" "+"[Stuents Registered]");
System.out.println("===========================================");
Scanner keyboard = new Scanner(System.in);
String mod;
{
System.out.println("Please enter module code:");
mod=keyboard.nextLine();
if(mod.equals(mCode[logedInIndex]))
{
System.out.println(fName[logedInIndex]);
}
else{
System.out.println("no");
}
} }
public static void addModule(String[] fName, String[] lName,int logedInIndex) throws IOException
{
System.out.println( fName[logedInIndex] +" " + lName[logedInIndex] +" "+ "[Admin]"+" "+"MODULE REGISTRATION "+" "+"[Add Module]");
System.out.println("===========================================");
Scanner keyboard = new Scanner(System.in);
String moduleCode;
String moduleName;
int numRegister=0;
// Get data and write it to the file.
// Open the file for appending
FileWriter fw = new FileWriter("Modules.txt", true);
PrintWriter outputFile = new PrintWriter(fw);
// Get the name of a friend.
System.out.print("Enter the Module Code");
moduleCode = keyboard.nextLine();
System.out.print("Enter the name of Module");
moduleName = keyboard.nextLine();
// Write the name to the file.
outputFile.println(moduleCode);
outputFile.println(moduleName);
outputFile.println(numRegister);
// Close the file.
outputFile.close();
System.out.println("Data written to the file.");
}
public static void listStudents(String[] fName, String[] lName, int[] numberRegistered,int numModules,int logedInIndex,int[] userType ) throws FileNotFoundException
{
System.out.println( fName[logedInIndex] +" " + lName[logedInIndex] +" "+ "[Admin]"+" "+"MODULE REGISTRATION "+" "+"[List Students]");
System.out.println("===========================================");
System.out.println("Module code\tModule Name\tStudents Regestered\n===========\t============\t=============");
{
}
}
public static int readInteger(String prompt)
{
// Create a scanner object for input from the console window
Scanner keyboard = new Scanner(System.in);
// boolean flag which is used to control the
// data validation loop
boolean numGood = false; // Assume the worst
do
{
System.out.print(prompt); // ask for the value
if(!keyboard.hasNextInt()) // check if what's in the keyboard buffer is not an integer
{
System.out.println("You must enter an integer value!"); // display an error message
keyboard.nextLine(); // consume the bad value entered
}
else
numGood = true; // value entered is good
} while(!numGood);
// at this point we know the value in the
// keyboard buffer is numeric so we can go ahead and
// return it.
return keyboard.nextInt();
}
public static int readPositiveInteger(String prompt)
{
int value;
do
{
value = readInteger(prompt); // ask for and read an integer value
if (value < 0) // check if the value entered is less than 0
// display an error message
System.out.println("Error - you must enter a positive integer value!");
} while (value <0);
// at this point we know the value entered is positive
// so return it
return value;
}
}