Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.EOFException;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.RandomAccessFile;
- import java.util.*;
- public class Main {
- // This variable represents the users input in the selected() function
- public static Scanner scanner = new Scanner(System.in); // Setting a scanner variable
- // This function will present the user with various selections to choose from.
- public static void main(String[] args) throws IOException
- {
- int userinput;
- boolean cont = true;
- boolean flag = false;
- final int recsize = 92;
- String inputFile = ("");
- String outputFile = ("");
- RandomAccessFile raf = null;
- Student rec = null;
- Scanner keyIn = new Scanner(System.in);
- FileWriter writer = new FileWriter("students.txt", true);
- while(cont)
- {
- System.out.println("1- Create a random-access file "
- + "\n2- Display a random-access file "
- + "\n3- Retrieve a record "
- + "\n4- Modify a record "
- + "\n5- Add a new record "
- + "\n6- Delete a record "
- + "\n7- Exit"
- + "\nChoose a selection:");
- userinput = scanner.nextInt(); // Asking the user for an input between 1 and 7
- switch(userinput)
- {
- default: //If userinput is less than 1, than the program will present an error
- System.out.println("Please enter a valid input");
- break;
- case 1: //COMPLETE.
- System.out.println("Please enter an input file name: ");
- Scanner inputScanner = new Scanner(System.in);
- inputFile = inputScanner.nextLine();
- System.out.println("Please enter an output file name: ");
- Scanner outputScanner = new Scanner(System.in);
- outputFile = outputScanner.nextLine();
- File file = new File(inputFile + ".txt");
- File file2 = new File(outputFile + ".txt");
- if (file.createNewFile()){
- System.out.println("File is created!");
- }else{
- System.out.println("File already exists. ");
- writer.flush();
- new RandomAccessFile(inputFile,"rw").setLength(0);
- System.out.println("File has been cleared. ");
- }
- if (file2.createNewFile()){
- System.out.println("File is created!");
- }else{
- System.out.println("File already exists. ");
- writer = new FileWriter(outputFile + ".txt");
- System.out.println("File has been cleared. ");
- }
- // Create input and output file streams
- Scanner fin = new Scanner (new FileInputStream(outputFile + ".txt"));
- raf = new RandomAccessFile(inputFile, "rw");
- rec = new Student();
- // Read data from the input file and write them to the random access file
- while (fin.hasNext()){
- rec.readFromTextFile(fin);
- rec.writeToFile(raf);
- }
- flag = true;
- break;
- case 2: //NEEDS WORK (Menu almost finished.)
- Scanner scanner = new Scanner(System.in);
- String filename = ("");
- System.out.println("\nWhich file would you like to display?");
- rec = new Student();
- filename = scanner.nextLine();
- raf = new RandomAccessFile(filename, "rw");
- print (raf, rec, -2);
- flag = true;
- break;
- case 3: //COMPLETE
- if(flag)
- {
- System.out.println("Please choose a record number:");
- int user = keyIn.nextInt();
- print(raf, rec, user);
- }
- else
- System.out.println("faggot");
- break;
- case 4: //COMPLETE - Make it also work with the txt
- if(flag)
- {
- System.out.println("Please choose a record number:");
- int user = keyIn.nextInt();
- print(raf, rec, user);
- System.out.println("What would you like to change? 1(Name) 2(LastName) 3(ID) 4(GPA)");
- int inp = keyIn.nextInt();
- Scanner tester = new Scanner(System.in);
- switch(inp)
- {
- default:
- break;
- case 1:
- raf.seek(92*(user-1));
- System.out.println("Enter a First Name: ");
- raf.writeChars(Student.pad(keyIn.next(), 20));
- print (raf, rec, -1);
- case 2:
- raf.seek(92*(user-1));
- System.out.println("Enter a Last Name: ");
- raf.writeChars(Student.pad(keyIn.next(), 20));
- print (raf, rec, -1);
- break;
- case 3:
- raf.seek(92*(user-1));
- System.out.println("Enter an ID: ");
- raf.writeChars(Student.pad(keyIn.next(), 10));
- print (raf, rec, -1);
- break;
- case 4:
- raf.seek(92*(user-1));
- System.out.println("Enter a GPA: ");
- raf.writeChars(Student.pad(keyIn.next(), 10));
- print (raf, rec, -1);
- break;
- }
- }
- else
- System.out.println("faggot");
- break;
- case 5: //COMPLETE
- if(flag)
- {
- System.out.println("");
- // Write a new record to the end of the random access file
- System.out.print("\nEnter your first name, last name, student ID, and GPA: ");
- rec.readFromTextFile(keyIn);
- raf.seek(raf.length());
- System.out.print("\nHere is your updated record: ");
- writer.write("\n" + rec.toString());
- writer.close();
- rec.writeToFile(raf);
- print (raf, rec, -1);
- }
- else
- System.out.println("faggot");
- break;
- case 6: //NEEDS WORK
- if(flag)
- {
- int number = keyIn.nextInt();
- try{
- raf.seek(number*recsize);
- }catch (EOFException e){
- }
- }
- else
- System.out.println("faggot");
- break;
- case 7: //COMPLETE
- cont = false;
- //output exit statement
- break;
- }
- }
- keyIn.close();
- }
- public static void print (RandomAccessFile raf, Student rec, int user) throws IOException
- {
- raf.seek(0);
- int incr = 0;
- int count = 0;
- boolean meme = false;
- if(user == -1) meme = true;
- try {
- while (true){
- if(meme)
- {
- rec.readFromFile(raf);
- System.out.println(++incr + ": " + rec);
- }
- else if(user == -2)
- {
- while(count<5)
- {
- raf.seek(count*92);
- rec.readFromFile(raf);
- System.out.println(rec);
- count ++;
- }
- if(count == 5)
- {
- int list = count + 5;
- System.out.println("(M)for main menu (N)for next screen (A)to display all");
- String minimenu = scanner.next().toLowerCase();
- switch(minimenu)
- {
- case "m":
- System.out.println("Mainmenu"); //break to main menu. Wont go back....
- break;
- case "n":
- try
- {
- while(count<list)
- {
- raf.seek(count*92);
- rec.readFromFile(raf);
- System.out.println(rec);
- count ++;
- }
- }
- catch(EOFException e)
- {
- System.out.println("Out of records.\n");
- }
- break;
- case "a": print(raf, rec, -1);// print all (Works)
- break;
- default:
- System.out.println("Please enter a valid option");
- break;
- }
- }
- }
- else
- {
- rec.readFromFile(raf);
- if(++incr == user)
- {
- System.out.println(incr + ": " + rec);
- }
- }
- }
- }
- catch (EOFException e){
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement