Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This is a menu based program which creates and utilizes RandomAccessFiles to store, look up and modify
- * "student" data including first and last names, student ID and GPA
- *
- * Sam Orosz
- * 1/17/2018
- */
- import java.io.*;
- import java.util.*;
- public class Main {
- public static Scanner keyboard = new Scanner(System.in);
- public static void main (String args[]) throws IOException
- {
- try {
- printMenu();
- menuSelect();
- }
- catch(IOException e) { //handle all thrown exceptions
- System.out.println("specified file does not exist, returning to menu");
- printMenu();
- menuSelect();
- }
- }
- public static void printMenu()
- {
- //print menu
- System.out.println("1- Create a random-access file");
- System.out.println("2- Display a random-access file");
- System.out.println("3- Retrieve a record");
- System.out.println("4- Modify a record");
- System.out.println("5- Add a new record");
- System.out.println("6- Delete a record");
- System.out.println("7- Exit");
- }
- public static void menuSelect() throws IOException
- {
- int num = 0;
- try
- {
- System.out.println("Please select an action you wish to perform."); //prompt user
- num = keyboard.nextInt();
- keyboard.nextLine();
- }
- catch(InputMismatchException e)
- {
- System.out.println("Please use a number 1-7 to select your action."); //make sure inputs are ints
- keyboard.next();
- menuSelect();
- return;
- }
- if(num == 1)
- {
- Create();
- }
- else if(num == 2)
- {
- Display();
- }
- else if(num == 3)
- {
- System.out.println("What is the name of the file you would like to access?");
- String file = keyboard.nextLine();
- RandomAccessFile File = new RandomAccessFile(file + ".txt", "r" );
- Retrieve(File);
- }
- else if(num == 4)
- {
- System.out.println("What is the name of the file you would like to access?");
- String file = keyboard.nextLine();
- RandomAccessFile File = new RandomAccessFile(file + ".txt", "rw" );
- Modify(File);
- }
- else if(num == 5)
- {
- System.out.println("What is the name of the file you would like to access?");
- String file = keyboard.nextLine();
- RandomAccessFile File = new RandomAccessFile(file + ".txt", "rw" );
- Add(File);
- }
- else if(num == 6)
- {
- System.out.println("What is the name of the file you would like to access?");
- String file = keyboard.nextLine();
- RandomAccessFile File = new RandomAccessFile(file + ".txt", "rw" );
- Delete(File);
- }
- else if(num == 7)
- {
- System.out.println("Exiting Program.");
- System.exit(0);
- }
- else
- {
- System.out.println("Please use a number 1-7 to select your action."); //make sure input is a supported number
- }
- }
- public static void Create() throws IOException
- {
- //prompt user for name of input file
- System.out.println("Please give the name of the text file you would like to create the file from");
- String firstName = keyboard.nextLine();
- //prompt user for name of file they wish to create
- System.out.println("Please give the desired output file name");
- String secondName = keyboard.nextLine();
- //delete output file if it already exists
- File f = new File(secondName + ".txt");
- if(f.exists() && !f.isDirectory()) {
- f.delete();
- }
- //create input and output files
- Reader originalFile = new FileReader(firstName + ".txt");
- RandomAccessFile newFile = new RandomAccessFile(secondName + ".txt", "rw" );
- //write data to RAF
- newFile.seek(0);
- boolean done = false;
- do {
- //first name
- char newCharTest = (char) originalFile.read();
- while (!Character.isLetter(newCharTest))
- {
- newCharTest = (char) originalFile.read();
- }
- newFile.writeChar(newCharTest);
- for(int i = 0; i < 19; i++)
- {
- char newChar = (char) originalFile.read();
- if (Character.isLetter(newChar))
- {
- newFile.writeChar(newChar);
- }
- else
- {
- for( ; i < 19; i++)
- {
- newFile.writeChar(' ');
- }
- }
- }
- //last name
- newCharTest = (char) originalFile.read();
- while (!Character.isLetter(newCharTest))
- {
- newCharTest = (char) originalFile.read();
- }
- newFile.writeChar(newCharTest);
- for(int i = 0; i < 19; i++)
- {
- char newChar = (char) originalFile.read();
- if (Character.isLetter(newChar))
- {
- newFile.writeChar(newChar);
- }
- else
- {
- for( ; i < 19; i++)
- {
- newFile.writeChar(' ');
- }
- }
- }
- //student id
- char newDigit = (char) originalFile.read();
- while (!Character.isDigit(newDigit))
- {
- newDigit = (char) originalFile.read();
- }
- int ID = Character.getNumericValue(newDigit);
- for(int i = 0; i < 3; i++)
- {
- newDigit = (char) originalFile.read();
- ID = (ID * 10) + Character.getNumericValue(newDigit);
- }
- newFile.writeInt(ID);
- //GPA
- newDigit = (char) originalFile.read();
- while (!Character.isDigit(newDigit))
- {
- newDigit = (char) originalFile.read();
- }
- double GPA = Character.getNumericValue(newDigit);
- originalFile.read();
- for(int i = -1; i > -3; i--)
- {
- newDigit = (char) originalFile.read();
- GPA = GPA + (Character.getNumericValue(newDigit) * java.lang.Math.pow(10,i));
- }
- newFile.writeDouble(GPA);
- //ends loop if the end of the input file is reached
- if( originalFile.read() == -1)
- {
- done = true;
- }
- } while(!done);
- originalFile.close();
- newFile.close();
- printMenu();
- menuSelect();
- }
- public static void Display() throws IOException
- {
- System.out.println("What is the name of the file you would like to access?");
- String file = keyboard.nextLine();
- RandomAccessFile File = new RandomAccessFile(file + ".txt", "r" );
- while(File.getFilePointer() < (File.length() - 91))
- {
- System.out.println("First Name Last Name Student ID GPA");
- for (int i = 0; i < 5; i++)
- {
- if (File.getFilePointer() < (File.length() - 91)) //make sure file won't end before data has been printed
- {
- char D = File.readChar(); //make sure to not display deleted students
- char L = File.readChar();
- char T = File.readChar();
- char D2 = File.readChar();
- if ( D == 'D' && L == 'L' && T == 'T' && D2 == 'D')
- {
- i--;
- File.seek(File.getFilePointer() + 84); //skip forward if deleted
- continue;
- }
- else
- {
- File.seek(File.getFilePointer() - 8); // skip back to beginning if not
- }
- for (int j = 0; j < 20; j++) { //first name
- System.out.print(File.readChar());
- }
- System.out.print(" ");
- for (int j = 0; j < 20; j++) { //last name
- System.out.print(File.readChar());
- }
- System.out.print(" ");
- System.out.print(File.readInt()); //ID
- System.out.print(" ");
- System.out.println(String.format("%.3g%n",File.readDouble())); //formatted GPA (only displays 2 decimal places
- }
- else
- {
- //since the end of the file was reached, users are given a choice
- //of returning to the menu or repeating Display()
- System.out.println("End of File.");
- System.out.println("Enter any letter to return to menu, or any non-letter character to display a new file");
- if (Character.isLetter(keyboard.next().charAt(0)))
- {
- keyboard.nextLine();
- printMenu();
- menuSelect();
- return;
- }
- else
- {
- Display();
- return;
- }
- }
- }
- //display and ask user for choice of options
- System.out.println("M- Return to menu.");
- System.out.println("N- Display next page.");
- System.out.println("A- Display all remaining students.");
- System.out.println("Please choose an action to continue");
- char option = ' ';
- boolean success = false;
- while (success == false) //repeat selection process until one of the correct chars is selected
- {
- option = keyboard.next().charAt(0);
- keyboard.nextLine();
- if(option == 'M' || option == 'm' || option == 'N' || option == 'n' || option == 'A' || option == 'a' )
- {
- success = true;
- }
- else
- {
- System.out.println("Please input M, N, or A."); //prompt user
- }
- }
- if (option == 'M' || option == 'm' ) //selection goes back to menu
- {
- printMenu();
- menuSelect();
- File.close();
- return;
- }
- //an actual n is redundant as the loop automatically does it if neither other option is selected
- else if (option == 'A' || option == 'a' ) //selection displays the rest of the file
- {
- System.out.println("First Name Last Name Student ID GPA");
- while (File.getFilePointer() < (File.length() - 91)) {
- for (int j = 0; j < 20; j++) {
- System.out.print(File.readChar());
- }
- System.out.print(" ");
- for (int j = 0; j < 20; j++) {
- System.out.print(File.readChar());
- }
- System.out.print(" ");
- System.out.print(File.readInt());
- System.out.print(" ");
- System.out.println(String.format("%.3g%n",File.readDouble()));
- }
- //since the end of the file was reached, users are given a choice
- //of returning to the menu or repeating Display()
- System.out.println("Enter any letter to return to menu, or any non-letter character to display a new file");
- if (Character.isLetter(keyboard.next().charAt(0)))
- {
- keyboard.nextLine();
- printMenu();
- menuSelect();
- File.close();
- return;
- }
- else
- {
- keyboard.nextLine();
- Display();
- return;
- }
- }
- }
- }
- public static void Retrieve(RandomAccessFile File) throws IOException
- {
- //prompt user
- System.out.println("Which student file would you like to retrieve? (1 - 1st, 2 - 2nd, etc...)");
- int number = keyboard.nextInt();
- keyboard.nextLine();
- File.seek((number - 1) * 92); //find student
- if (File.getFilePointer() < (File.length() - 91)) //check if student number exists
- {
- char D = File.readChar(); //make sure the student is not deleted
- char L = File.readChar();
- char T = File.readChar();
- char D2 = File.readChar();
- if ( D != 'D' || L != 'L' || T != 'T' || D2 != 'D')
- {
- //print student data + labels
- File.seek((number - 1) * 92);
- System.out.println("First Name Last Name Student ID GPA");
- for (int j = 0; j < 20; j++) {
- System.out.print(File.readChar());
- }
- System.out.print(" ");
- for (int j = 0; j < 20; j++) {
- System.out.print(File.readChar());
- }
- System.out.print(" ");
- System.out.print(File.readInt());
- System.out.print(" ");
- System.out.println(String.format("%.3g%n",File.readDouble()));
- }
- else
- {
- System.out.println("Student does not exist: Deleted");
- }
- }
- else
- {
- System.out.println("Student does not exist: Past end of file");
- }
- //since the end of the task was reached, users are given a choice
- //of returning to the menu or repeating Retrieve()
- System.out.println("Enter any letter to return to menu, or any non-letter character to retrieve another student");
- if (Character.isLetter(keyboard.next().charAt(0)))
- {
- keyboard.nextLine();
- printMenu();
- menuSelect();
- return;
- }
- else
- {
- keyboard.nextLine();
- Retrieve(File);
- return;
- }
- }
- public static void Modify(RandomAccessFile File) throws IOException
- {
- System.out.println("Which student file would you like to modify? (1 - 1st, 2 - 2nd, etc...)");
- int number = keyboard.nextInt();
- keyboard.nextLine();
- File.seek((number - 1) * 92); //find student
- if (File.getFilePointer() < (File.length() - 91)) //check if student number exists
- {
- char D = File.readChar(); //make sure the student is not deleted
- char L = File.readChar();
- char T = File.readChar();
- char D2 = File.readChar();
- if ( D != 'D' || L != 'L' || T != 'T' || D2 != 'D')
- {
- //print student data for reference by user
- File.seek((number - 1) * 92);
- for (int j = 0; j < 20; j++) {
- System.out.print(File.readChar());
- }
- System.out.print(" ");
- for (int j = 0; j < 20; j++) {
- System.out.print(File.readChar());
- }
- System.out.print(" ");
- System.out.print(File.readInt());
- System.out.print(" ");
- System.out.println(String.format("%.3g%n",File.readDouble()));
- System.out.println("What do you wish to modify?");
- System.out.println("F - First Name");
- System.out.println("L - Last Name"); //user options for modification
- System.out.println("I - Student ID");
- System.out.println("G - GPA");
- char option = ' ';
- boolean success = false;
- while (success == false) //make sure a correct char is selected
- {
- option = keyboard.next().charAt(0);
- keyboard.nextLine();
- if(option == 'F' || option == 'f' || option == 'L' || option == 'l' || option == 'I' || option == 'i' || option == 'G' || option == 'g' )
- {
- success = true;
- }
- else
- {
- System.out.println("Please input F, L, I, or G.");
- }
- }
- if (option == 'F' || option == 'f') //change first name
- {
- System.out.println("What would you like to change the first name to?");
- File.seek((number - 1) * 92);
- String fName = keyboard.nextLine();
- for(int i = 0; i < fName.length(); i++)
- {
- File.writeChar(fName.charAt(i));
- if( i == 19)
- break;
- }
- for(int i = fName.length(); i < 20; i++)
- {
- File.writeChar(' ');
- }
- }
- if (option == 'L' || option == 'l') //change last name
- {
- System.out.println("What would you like to change the last name to?");
- File.seek(((number - 1) * 92) + 40);
- String lName = keyboard.nextLine();
- for(int i = 0; i < lName.length(); i++)
- {
- File.writeChar(lName.charAt(i));
- if( i == 19)
- break;
- }
- for(int i = lName.length(); i < 20; i++)
- {
- File.writeChar(' ');
- }
- }
- if (option == 'I' || option == 'i') //change ID
- {
- System.out.println("What would you like to change the student ID to?");
- File.seek(((number - 1) * 92) + 80);
- File.writeInt( keyboard.nextInt());
- keyboard.nextLine();
- }
- if (option == 'G' || option == 'g') //change GPA
- {
- System.out.println("What would you like to change the GPA to?");
- File.seek(((number - 1) * 92) + 84);
- File.writeDouble( keyboard.nextDouble());
- keyboard.nextLine();
- }
- }
- else
- {
- System.out.println("Student does not exist: Deleted");
- }
- }
- else
- {
- System.out.println("Student does not exist: Past end of file");
- }
- //since the end of the task was reached, users are given a choice
- //of returning to the menu or repeating Modify()
- System.out.println("Enter any letter to return to menu, or any non-letter character to modify another student");
- if (Character.isLetter(keyboard.next().charAt(0)))
- {
- keyboard.nextLine();
- printMenu();
- menuSelect();
- File.close();
- return;
- }
- else
- {
- keyboard.nextLine();
- Modify(File);
- return;
- }
- }
- public static void Add(RandomAccessFile File) throws IOException
- {
- File.seek(File.length());
- System.out.println("What is the first name of the new student?");
- String fName = keyboard.nextLine();
- for(int i = 0; i < fName.length(); i++)
- {
- File.writeChar(fName.charAt(i));
- if( i == 19)
- break;
- }
- for(int i = fName.length(); i < 20; i++)
- {
- File.writeChar(' ');
- }
- System.out.println("What is the last name?");
- String lName = keyboard.nextLine();
- for(int i = 0; i < lName.length(); i++)
- {
- File.writeChar(lName.charAt(i));
- if( i == 19)
- break;
- }
- for(int i = lName.length(); i < 20; i++)
- {
- File.writeChar(' ');
- }
- System.out.println("What is the student ID?");
- File.writeInt( keyboard.nextInt());
- keyboard.nextLine();
- System.out.println("What is their GPA?");
- File.writeDouble( keyboard.nextDouble());
- keyboard.nextLine();
- //since the end of the task was reached, users are given a choice
- //of returning to the menu or repeating Add()
- System.out.println("Enter any letter to return to menu, or any non-letter character to add a new student");
- if (Character.isLetter(keyboard.next().charAt(0)))
- {
- keyboard.nextLine();
- printMenu();
- menuSelect();
- File.close();
- return;
- }
- else
- {
- keyboard.nextLine();
- Add(File);
- return;
- }
- }
- public static void Delete(RandomAccessFile File) throws IOException
- {
- System.out.println("Which student file would you like to delete? (1 - 1st, 2 - 2nd, etc...)");
- int number = keyboard.nextInt();
- keyboard.nextLine();
- File.seek((number - 1) * 92); //find student
- if (File.getFilePointer() < (File.length() - 92)) //check if student number exists
- {
- char D = File.readChar(); //check if already deleted
- char L = File.readChar();
- char T = File.readChar();
- char D2 = File.readChar();
- if ( D != 'D' || L != 'L' || T != 'T' || D2 != 'D')
- {
- File.seek((number - 1) * 92);
- File.writeChar('D'); //overwrite first four chars with deletion marker
- File.writeChar('L');
- File.writeChar('T');
- File.writeChar('D');
- }
- else
- {
- System.out.println("Student already deleted");
- }
- }
- else
- {
- System.out.println("Student does not exist: Past end of file");
- }
- //since the end of the task was reached, users are given a choice
- //of returning to the menu or repeating Delete()
- System.out.println("Enter any letter to return to menu, or any non-letter character to delete another new student");
- if (Character.isLetter(keyboard.next().charAt(0)))
- {
- keyboard.nextLine();
- printMenu();
- menuSelect();
- File.close();
- return;
- }
- else
- {
- keyboard.nextLine();
- Delete(File);
- return;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement