Advertisement
Guest User

COSC311Project1

a guest
Jan 18th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.56 KB | None | 0 0
  1. /**
  2. * This is a menu based program which creates and utilizes RandomAccessFiles to store, look up and modify
  3. * "student" data including first and last names, student ID and GPA
  4. *
  5. * Sam Orosz
  6. * 1/17/2018
  7. */
  8.  
  9. import java.io.*;
  10. import java.util.*;
  11.  
  12. public class Main {
  13.  
  14. public static Scanner keyboard = new Scanner(System.in);
  15.  
  16. public static void main (String args[]) throws IOException
  17. {
  18. try {
  19. printMenu();
  20. menuSelect();
  21. }
  22. catch(IOException e) { //handle all thrown exceptions
  23. System.out.println("specified file does not exist, returning to menu");
  24. printMenu();
  25. menuSelect();
  26. }
  27.  
  28. }
  29.  
  30. public static void printMenu()
  31. {
  32. //print menu
  33. System.out.println("1- Create a random-access file");
  34. System.out.println("2- Display a random-access file");
  35. System.out.println("3- Retrieve a record");
  36. System.out.println("4- Modify a record");
  37. System.out.println("5- Add a new record");
  38. System.out.println("6- Delete a record");
  39. System.out.println("7- Exit");
  40.  
  41. }
  42.  
  43. public static void menuSelect() throws IOException
  44. {
  45. int num = 0;
  46. try
  47. {
  48. System.out.println("Please select an action you wish to perform."); //prompt user
  49. num = keyboard.nextInt();
  50. keyboard.nextLine();
  51. }
  52. catch(InputMismatchException e)
  53. {
  54. System.out.println("Please use a number 1-7 to select your action."); //make sure inputs are ints
  55. keyboard.next();
  56. menuSelect();
  57. return;
  58.  
  59. }
  60.  
  61. if(num == 1)
  62. {
  63. Create();
  64. }
  65. else if(num == 2)
  66. {
  67. Display();
  68. }
  69. else if(num == 3)
  70. {
  71. System.out.println("What is the name of the file you would like to access?");
  72. String file = keyboard.nextLine();
  73. RandomAccessFile File = new RandomAccessFile(file + ".txt", "r" );
  74. Retrieve(File);
  75. }
  76. else if(num == 4)
  77. {
  78. System.out.println("What is the name of the file you would like to access?");
  79. String file = keyboard.nextLine();
  80. RandomAccessFile File = new RandomAccessFile(file + ".txt", "rw" );
  81.  
  82. Modify(File);
  83. }
  84. else if(num == 5)
  85. {
  86. System.out.println("What is the name of the file you would like to access?");
  87. String file = keyboard.nextLine();
  88. RandomAccessFile File = new RandomAccessFile(file + ".txt", "rw" );
  89. Add(File);
  90. }
  91. else if(num == 6)
  92. {
  93. System.out.println("What is the name of the file you would like to access?");
  94. String file = keyboard.nextLine();
  95. RandomAccessFile File = new RandomAccessFile(file + ".txt", "rw" );
  96. Delete(File);
  97. }
  98. else if(num == 7)
  99. {
  100. System.out.println("Exiting Program.");
  101. System.exit(0);
  102. }
  103. else
  104. {
  105. System.out.println("Please use a number 1-7 to select your action."); //make sure input is a supported number
  106. }
  107. }
  108.  
  109. public static void Create() throws IOException
  110. {
  111. //prompt user for name of input file
  112. System.out.println("Please give the name of the text file you would like to create the file from");
  113. String firstName = keyboard.nextLine();
  114. //prompt user for name of file they wish to create
  115. System.out.println("Please give the desired output file name");
  116. String secondName = keyboard.nextLine();
  117.  
  118. //delete output file if it already exists
  119. File f = new File(secondName + ".txt");
  120. if(f.exists() && !f.isDirectory()) {
  121. f.delete();
  122. }
  123.  
  124. //create input and output files
  125. Reader originalFile = new FileReader(firstName + ".txt");
  126. RandomAccessFile newFile = new RandomAccessFile(secondName + ".txt", "rw" );
  127.  
  128. //write data to RAF
  129. newFile.seek(0);
  130. boolean done = false;
  131. do {
  132. //first name
  133. char newCharTest = (char) originalFile.read();
  134. while (!Character.isLetter(newCharTest))
  135. {
  136. newCharTest = (char) originalFile.read();
  137. }
  138. newFile.writeChar(newCharTest);
  139. for(int i = 0; i < 19; i++)
  140. {
  141. char newChar = (char) originalFile.read();
  142. if (Character.isLetter(newChar))
  143. {
  144. newFile.writeChar(newChar);
  145. }
  146. else
  147. {
  148. for( ; i < 19; i++)
  149. {
  150. newFile.writeChar(' ');
  151. }
  152. }
  153.  
  154. }
  155. //last name
  156. newCharTest = (char) originalFile.read();
  157. while (!Character.isLetter(newCharTest))
  158. {
  159. newCharTest = (char) originalFile.read();
  160. }
  161. newFile.writeChar(newCharTest);
  162. for(int i = 0; i < 19; i++)
  163. {
  164. char newChar = (char) originalFile.read();
  165. if (Character.isLetter(newChar))
  166. {
  167. newFile.writeChar(newChar);
  168. }
  169. else
  170. {
  171. for( ; i < 19; i++)
  172. {
  173. newFile.writeChar(' ');
  174. }
  175. }
  176.  
  177. }
  178. //student id
  179. char newDigit = (char) originalFile.read();
  180. while (!Character.isDigit(newDigit))
  181. {
  182. newDigit = (char) originalFile.read();
  183. }
  184.  
  185. int ID = Character.getNumericValue(newDigit);
  186. for(int i = 0; i < 3; i++)
  187. {
  188. newDigit = (char) originalFile.read();
  189. ID = (ID * 10) + Character.getNumericValue(newDigit);
  190. }
  191.  
  192. newFile.writeInt(ID);
  193. //GPA
  194. newDigit = (char) originalFile.read();
  195. while (!Character.isDigit(newDigit))
  196. {
  197. newDigit = (char) originalFile.read();
  198. }
  199. double GPA = Character.getNumericValue(newDigit);
  200.  
  201. originalFile.read();
  202. for(int i = -1; i > -3; i--)
  203. {
  204. newDigit = (char) originalFile.read();
  205. GPA = GPA + (Character.getNumericValue(newDigit) * java.lang.Math.pow(10,i));
  206. }
  207. newFile.writeDouble(GPA);
  208. //ends loop if the end of the input file is reached
  209. if( originalFile.read() == -1)
  210. {
  211. done = true;
  212. }
  213.  
  214. } while(!done);
  215. originalFile.close();
  216. newFile.close();
  217.  
  218. printMenu();
  219. menuSelect();
  220. }
  221. public static void Display() throws IOException
  222. {
  223. System.out.println("What is the name of the file you would like to access?");
  224. String file = keyboard.nextLine();
  225. RandomAccessFile File = new RandomAccessFile(file + ".txt", "r" );
  226.  
  227. while(File.getFilePointer() < (File.length() - 91))
  228. {
  229. System.out.println("First Name Last Name Student ID GPA");
  230.  
  231. for (int i = 0; i < 5; i++)
  232. {
  233. if (File.getFilePointer() < (File.length() - 91)) //make sure file won't end before data has been printed
  234. {
  235. char D = File.readChar(); //make sure to not display deleted students
  236. char L = File.readChar();
  237. char T = File.readChar();
  238. char D2 = File.readChar();
  239. if ( D == 'D' && L == 'L' && T == 'T' && D2 == 'D')
  240. {
  241. i--;
  242. File.seek(File.getFilePointer() + 84); //skip forward if deleted
  243. continue;
  244.  
  245. }
  246. else
  247. {
  248. File.seek(File.getFilePointer() - 8); // skip back to beginning if not
  249. }
  250.  
  251. for (int j = 0; j < 20; j++) { //first name
  252. System.out.print(File.readChar());
  253. }
  254. System.out.print(" ");
  255. for (int j = 0; j < 20; j++) { //last name
  256. System.out.print(File.readChar());
  257. }
  258. System.out.print(" ");
  259. System.out.print(File.readInt()); //ID
  260. System.out.print(" ");
  261. System.out.println(String.format("%.3g%n",File.readDouble())); //formatted GPA (only displays 2 decimal places
  262. }
  263. else
  264. {
  265. //since the end of the file was reached, users are given a choice
  266. //of returning to the menu or repeating Display()
  267. System.out.println("End of File.");
  268. System.out.println("Enter any letter to return to menu, or any non-letter character to display a new file");
  269. if (Character.isLetter(keyboard.next().charAt(0)))
  270. {
  271. keyboard.nextLine();
  272. printMenu();
  273. menuSelect();
  274. return;
  275. }
  276. else
  277. {
  278. Display();
  279. return;
  280. }
  281. }
  282. }
  283.  
  284. //display and ask user for choice of options
  285. System.out.println("M- Return to menu.");
  286. System.out.println("N- Display next page.");
  287. System.out.println("A- Display all remaining students.");
  288. System.out.println("Please choose an action to continue");
  289. char option = ' ';
  290. boolean success = false;
  291. while (success == false) //repeat selection process until one of the correct chars is selected
  292. {
  293. option = keyboard.next().charAt(0);
  294. keyboard.nextLine();
  295. if(option == 'M' || option == 'm' || option == 'N' || option == 'n' || option == 'A' || option == 'a' )
  296. {
  297. success = true;
  298. }
  299. else
  300. {
  301. System.out.println("Please input M, N, or A."); //prompt user
  302. }
  303.  
  304. }
  305. if (option == 'M' || option == 'm' ) //selection goes back to menu
  306. {
  307. printMenu();
  308. menuSelect();
  309. File.close();
  310. return;
  311. }
  312. //an actual n is redundant as the loop automatically does it if neither other option is selected
  313.  
  314. else if (option == 'A' || option == 'a' ) //selection displays the rest of the file
  315. {
  316.  
  317. System.out.println("First Name Last Name Student ID GPA");
  318. while (File.getFilePointer() < (File.length() - 91)) {
  319. for (int j = 0; j < 20; j++) {
  320. System.out.print(File.readChar());
  321. }
  322. System.out.print(" ");
  323. for (int j = 0; j < 20; j++) {
  324. System.out.print(File.readChar());
  325. }
  326. System.out.print(" ");
  327. System.out.print(File.readInt());
  328. System.out.print(" ");
  329. System.out.println(String.format("%.3g%n",File.readDouble()));
  330. }
  331. //since the end of the file was reached, users are given a choice
  332. //of returning to the menu or repeating Display()
  333. System.out.println("Enter any letter to return to menu, or any non-letter character to display a new file");
  334. if (Character.isLetter(keyboard.next().charAt(0)))
  335. {
  336. keyboard.nextLine();
  337. printMenu();
  338. menuSelect();
  339. File.close();
  340. return;
  341. }
  342. else
  343. {
  344. keyboard.nextLine();
  345. Display();
  346. return;
  347. }
  348. }
  349. }
  350.  
  351. }
  352. public static void Retrieve(RandomAccessFile File) throws IOException
  353. {
  354. //prompt user
  355. System.out.println("Which student file would you like to retrieve? (1 - 1st, 2 - 2nd, etc...)");
  356. int number = keyboard.nextInt();
  357. keyboard.nextLine();
  358.  
  359. File.seek((number - 1) * 92); //find student
  360. if (File.getFilePointer() < (File.length() - 91)) //check if student number exists
  361. {
  362. char D = File.readChar(); //make sure the student is not deleted
  363. char L = File.readChar();
  364. char T = File.readChar();
  365. char D2 = File.readChar();
  366. if ( D != 'D' || L != 'L' || T != 'T' || D2 != 'D')
  367. {
  368. //print student data + labels
  369. File.seek((number - 1) * 92);
  370. System.out.println("First Name Last Name Student ID GPA");
  371. for (int j = 0; j < 20; j++) {
  372. System.out.print(File.readChar());
  373. }
  374. System.out.print(" ");
  375. for (int j = 0; j < 20; j++) {
  376. System.out.print(File.readChar());
  377. }
  378. System.out.print(" ");
  379. System.out.print(File.readInt());
  380. System.out.print(" ");
  381. System.out.println(String.format("%.3g%n",File.readDouble()));
  382. }
  383. else
  384. {
  385. System.out.println("Student does not exist: Deleted");
  386. }
  387. }
  388. else
  389. {
  390. System.out.println("Student does not exist: Past end of file");
  391. }
  392. //since the end of the task was reached, users are given a choice
  393. //of returning to the menu or repeating Retrieve()
  394. System.out.println("Enter any letter to return to menu, or any non-letter character to retrieve another student");
  395. if (Character.isLetter(keyboard.next().charAt(0)))
  396. {
  397. keyboard.nextLine();
  398. printMenu();
  399. menuSelect();
  400. return;
  401. }
  402. else
  403. {
  404. keyboard.nextLine();
  405. Retrieve(File);
  406. return;
  407. }
  408.  
  409.  
  410. }
  411. public static void Modify(RandomAccessFile File) throws IOException
  412. {
  413. System.out.println("Which student file would you like to modify? (1 - 1st, 2 - 2nd, etc...)");
  414. int number = keyboard.nextInt();
  415. keyboard.nextLine();
  416.  
  417. File.seek((number - 1) * 92); //find student
  418. if (File.getFilePointer() < (File.length() - 91)) //check if student number exists
  419. {
  420. char D = File.readChar(); //make sure the student is not deleted
  421. char L = File.readChar();
  422. char T = File.readChar();
  423. char D2 = File.readChar();
  424. if ( D != 'D' || L != 'L' || T != 'T' || D2 != 'D')
  425. {
  426. //print student data for reference by user
  427. File.seek((number - 1) * 92);
  428. for (int j = 0; j < 20; j++) {
  429. System.out.print(File.readChar());
  430. }
  431. System.out.print(" ");
  432. for (int j = 0; j < 20; j++) {
  433. System.out.print(File.readChar());
  434. }
  435. System.out.print(" ");
  436. System.out.print(File.readInt());
  437. System.out.print(" ");
  438. System.out.println(String.format("%.3g%n",File.readDouble()));
  439.  
  440. System.out.println("What do you wish to modify?");
  441. System.out.println("F - First Name");
  442. System.out.println("L - Last Name"); //user options for modification
  443. System.out.println("I - Student ID");
  444. System.out.println("G - GPA");
  445.  
  446. char option = ' ';
  447. boolean success = false;
  448. while (success == false) //make sure a correct char is selected
  449. {
  450. option = keyboard.next().charAt(0);
  451. keyboard.nextLine();
  452. if(option == 'F' || option == 'f' || option == 'L' || option == 'l' || option == 'I' || option == 'i' || option == 'G' || option == 'g' )
  453. {
  454. success = true;
  455. }
  456. else
  457. {
  458. System.out.println("Please input F, L, I, or G.");
  459. }
  460.  
  461. }
  462.  
  463. if (option == 'F' || option == 'f') //change first name
  464. {
  465. System.out.println("What would you like to change the first name to?");
  466. File.seek((number - 1) * 92);
  467. String fName = keyboard.nextLine();
  468.  
  469. for(int i = 0; i < fName.length(); i++)
  470. {
  471. File.writeChar(fName.charAt(i));
  472. if( i == 19)
  473. break;
  474. }
  475. for(int i = fName.length(); i < 20; i++)
  476. {
  477. File.writeChar(' ');
  478. }
  479. }
  480. if (option == 'L' || option == 'l') //change last name
  481. {
  482. System.out.println("What would you like to change the last name to?");
  483. File.seek(((number - 1) * 92) + 40);
  484. String lName = keyboard.nextLine();
  485.  
  486. for(int i = 0; i < lName.length(); i++)
  487. {
  488. File.writeChar(lName.charAt(i));
  489. if( i == 19)
  490. break;
  491. }
  492. for(int i = lName.length(); i < 20; i++)
  493. {
  494. File.writeChar(' ');
  495. }
  496. }
  497. if (option == 'I' || option == 'i') //change ID
  498. {
  499. System.out.println("What would you like to change the student ID to?");
  500. File.seek(((number - 1) * 92) + 80);
  501. File.writeInt( keyboard.nextInt());
  502. keyboard.nextLine();
  503.  
  504. }
  505. if (option == 'G' || option == 'g') //change GPA
  506. {
  507. System.out.println("What would you like to change the GPA to?");
  508. File.seek(((number - 1) * 92) + 84);
  509. File.writeDouble( keyboard.nextDouble());
  510. keyboard.nextLine();
  511. }
  512. }
  513. else
  514. {
  515. System.out.println("Student does not exist: Deleted");
  516. }
  517. }
  518. else
  519. {
  520. System.out.println("Student does not exist: Past end of file");
  521. }
  522.  
  523.  
  524. //since the end of the task was reached, users are given a choice
  525. //of returning to the menu or repeating Modify()
  526. System.out.println("Enter any letter to return to menu, or any non-letter character to modify another student");
  527. if (Character.isLetter(keyboard.next().charAt(0)))
  528. {
  529. keyboard.nextLine();
  530. printMenu();
  531. menuSelect();
  532. File.close();
  533. return;
  534. }
  535. else
  536. {
  537. keyboard.nextLine();
  538. Modify(File);
  539. return;
  540. }
  541.  
  542. }
  543. public static void Add(RandomAccessFile File) throws IOException
  544. {
  545. File.seek(File.length());
  546. System.out.println("What is the first name of the new student?");
  547. String fName = keyboard.nextLine();
  548.  
  549. for(int i = 0; i < fName.length(); i++)
  550. {
  551. File.writeChar(fName.charAt(i));
  552. if( i == 19)
  553. break;
  554. }
  555. for(int i = fName.length(); i < 20; i++)
  556. {
  557. File.writeChar(' ');
  558. }
  559.  
  560. System.out.println("What is the last name?");
  561. String lName = keyboard.nextLine();
  562.  
  563. for(int i = 0; i < lName.length(); i++)
  564. {
  565. File.writeChar(lName.charAt(i));
  566. if( i == 19)
  567. break;
  568. }
  569. for(int i = lName.length(); i < 20; i++)
  570. {
  571. File.writeChar(' ');
  572. }
  573.  
  574. System.out.println("What is the student ID?");
  575. File.writeInt( keyboard.nextInt());
  576. keyboard.nextLine();
  577.  
  578. System.out.println("What is their GPA?");
  579. File.writeDouble( keyboard.nextDouble());
  580. keyboard.nextLine();
  581.  
  582. //since the end of the task was reached, users are given a choice
  583. //of returning to the menu or repeating Add()
  584. System.out.println("Enter any letter to return to menu, or any non-letter character to add a new student");
  585. if (Character.isLetter(keyboard.next().charAt(0)))
  586. {
  587. keyboard.nextLine();
  588. printMenu();
  589. menuSelect();
  590. File.close();
  591. return;
  592. }
  593. else
  594. {
  595. keyboard.nextLine();
  596. Add(File);
  597. return;
  598. }
  599.  
  600. }
  601. public static void Delete(RandomAccessFile File) throws IOException
  602. {
  603. System.out.println("Which student file would you like to delete? (1 - 1st, 2 - 2nd, etc...)");
  604. int number = keyboard.nextInt();
  605. keyboard.nextLine();
  606.  
  607. File.seek((number - 1) * 92); //find student
  608. if (File.getFilePointer() < (File.length() - 92)) //check if student number exists
  609. {
  610. char D = File.readChar(); //check if already deleted
  611. char L = File.readChar();
  612. char T = File.readChar();
  613. char D2 = File.readChar();
  614. if ( D != 'D' || L != 'L' || T != 'T' || D2 != 'D')
  615. {
  616. File.seek((number - 1) * 92);
  617. File.writeChar('D'); //overwrite first four chars with deletion marker
  618. File.writeChar('L');
  619. File.writeChar('T');
  620. File.writeChar('D');
  621. }
  622. else
  623. {
  624. System.out.println("Student already deleted");
  625. }
  626. }
  627.  
  628. else
  629. {
  630. System.out.println("Student does not exist: Past end of file");
  631. }
  632. //since the end of the task was reached, users are given a choice
  633. //of returning to the menu or repeating Delete()
  634. System.out.println("Enter any letter to return to menu, or any non-letter character to delete another new student");
  635. if (Character.isLetter(keyboard.next().charAt(0)))
  636. {
  637. keyboard.nextLine();
  638. printMenu();
  639. menuSelect();
  640. File.close();
  641. return;
  642. }
  643. else
  644. {
  645. keyboard.nextLine();
  646. Delete(File);
  647. return;
  648. }
  649.  
  650. }
  651. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement