Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.47 KB | None | 0 0
  1. import java.io.EOFException;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.io.RandomAccessFile;
  7. import java.util.*;
  8. public class Main {
  9. // This variable represents the users input in the selected() function
  10. public static Scanner scanner = new Scanner(System.in); // Setting a scanner variable
  11.  
  12. // This function will present the user with various selections to choose from.
  13. public static void main(String[] args) throws IOException
  14. {
  15. int userinput;
  16. boolean cont = true;
  17. boolean flag = false;
  18. final int recsize = 92;
  19. String inputFile = ("");
  20. String outputFile = ("");
  21.  
  22. RandomAccessFile raf = null;
  23. Student rec = null;
  24. Scanner keyIn = new Scanner(System.in);
  25. FileWriter writer = new FileWriter("students.txt", true);
  26.  
  27. while(cont)
  28. {
  29. System.out.println("1- Create a random-access file "
  30. + "\n2- Display a random-access file "
  31. + "\n3- Retrieve a record "
  32. + "\n4- Modify a record "
  33. + "\n5- Add a new record "
  34. + "\n6- Delete a record "
  35. + "\n7- Exit"
  36. + "\nChoose a selection:");
  37.  
  38. userinput = scanner.nextInt(); // Asking the user for an input between 1 and 7
  39. switch(userinput)
  40. {
  41.  
  42. default: //If userinput is less than 1, than the program will present an error
  43. System.out.println("Please enter a valid input");
  44. break;
  45. case 1: //COMPLETE.
  46.  
  47.  
  48. System.out.println("Please enter an input file name: ");
  49. Scanner inputScanner = new Scanner(System.in);
  50. inputFile = inputScanner.nextLine();
  51.  
  52. System.out.println("Please enter an output file name: ");
  53. Scanner outputScanner = new Scanner(System.in);
  54. outputFile = outputScanner.nextLine();
  55.  
  56. File file = new File(inputFile + ".txt");
  57. File file2 = new File(outputFile + ".txt");
  58.  
  59. if (file.createNewFile()){
  60. System.out.println("File is created!");
  61. }else{
  62. System.out.println("File already exists. ");
  63. writer.flush();
  64. new RandomAccessFile(inputFile,"rw").setLength(0);
  65. System.out.println("File has been cleared. ");
  66. }
  67.  
  68. if (file2.createNewFile()){
  69. System.out.println("File is created!");
  70. }else{
  71. System.out.println("File already exists. ");
  72. writer = new FileWriter(outputFile + ".txt");
  73. System.out.println("File has been cleared. ");
  74. }
  75.  
  76. // Create input and output file streams
  77. Scanner fin = new Scanner (new FileInputStream(outputFile + ".txt"));
  78. raf = new RandomAccessFile(inputFile, "rw");
  79. rec = new Student();
  80.  
  81. // Read data from the input file and write them to the random access file
  82. while (fin.hasNext()){
  83. rec.readFromTextFile(fin);
  84. rec.writeToFile(raf);
  85. }
  86. flag = true;
  87. break;
  88. case 2: //NEEDS WORK (Menu almost finished.)
  89.  
  90. Scanner scanner = new Scanner(System.in);
  91. String filename = ("");
  92. System.out.println("\nWhich file would you like to display?");
  93. rec = new Student();
  94. filename = scanner.nextLine();
  95. raf = new RandomAccessFile(filename, "rw");
  96. print (raf, rec, -2);
  97. flag = true;
  98. break;
  99.  
  100. case 3: //COMPLETE
  101. if(flag)
  102. {
  103. System.out.println("Please choose a record number:");
  104. int user = keyIn.nextInt();
  105. print(raf, rec, user);
  106. }
  107. else
  108. System.out.println("faggot");
  109. break;
  110. case 4: //COMPLETE - Make it also work with the txt
  111. if(flag)
  112. {
  113. System.out.println("Please choose a record number:");
  114. int user = keyIn.nextInt();
  115. print(raf, rec, user);
  116. System.out.println("What would you like to change? 1(Name) 2(LastName) 3(ID) 4(GPA)");
  117. int inp = keyIn.nextInt();
  118. Scanner tester = new Scanner(System.in);
  119. switch(inp)
  120. {
  121. default:
  122. break;
  123. case 1:
  124. raf.seek(92*(user-1));
  125. System.out.println("Enter a First Name: ");
  126. raf.writeChars(Student.pad(keyIn.next(), 20));
  127.  
  128. print (raf, rec, -1);
  129. case 2:
  130. raf.seek(92*(user-1));
  131. System.out.println("Enter a Last Name: ");
  132. raf.writeChars(Student.pad(keyIn.next(), 20));
  133.  
  134. print (raf, rec, -1);
  135. break;
  136. case 3:
  137. raf.seek(92*(user-1));
  138. System.out.println("Enter an ID: ");
  139. raf.writeChars(Student.pad(keyIn.next(), 10));
  140.  
  141. print (raf, rec, -1);
  142. break;
  143. case 4:
  144. raf.seek(92*(user-1));
  145. System.out.println("Enter a GPA: ");
  146. raf.writeChars(Student.pad(keyIn.next(), 10));
  147.  
  148. print (raf, rec, -1);
  149. break;
  150. }
  151. }
  152. else
  153. System.out.println("faggot");
  154. break;
  155. case 5: //COMPLETE
  156. if(flag)
  157. {
  158. System.out.println("");
  159. // Write a new record to the end of the random access file
  160. System.out.print("\nEnter your first name, last name, student ID, and GPA: ");
  161. rec.readFromTextFile(keyIn);
  162. raf.seek(raf.length());
  163.  
  164. System.out.print("\nHere is your updated record: ");
  165. writer.write("\n" + rec.toString());
  166. writer.close();
  167.  
  168. rec.writeToFile(raf);
  169. print (raf, rec, -1);
  170.  
  171. }
  172. else
  173. System.out.println("faggot");
  174. break;
  175. case 6: //NEEDS WORK
  176. if(flag)
  177. {
  178. int number = keyIn.nextInt();
  179. try{
  180. raf.seek(number*recsize);
  181.  
  182.  
  183.  
  184. }catch (EOFException e){
  185.  
  186.  
  187. }
  188.  
  189. }
  190. else
  191. System.out.println("faggot");
  192. break;
  193. case 7: //COMPLETE
  194. cont = false;
  195. //output exit statement
  196. break;
  197. }
  198. }
  199. keyIn.close();
  200.  
  201. }
  202.  
  203. public static void print (RandomAccessFile raf, Student rec, int user) throws IOException
  204. {
  205. raf.seek(0);
  206. int incr = 0;
  207. int count = 0;
  208.  
  209. boolean meme = false;
  210.  
  211. if(user == -1) meme = true;
  212. try {
  213. while (true){
  214. if(meme)
  215. {
  216. rec.readFromFile(raf);
  217. System.out.println(++incr + ": " + rec);
  218. }
  219. else if(user == -2)
  220. {
  221. while(count<5)
  222. {
  223. raf.seek(count*92);
  224. rec.readFromFile(raf);
  225. System.out.println(rec);
  226. count ++;
  227. }
  228. if(count == 5)
  229. {
  230. int list = count + 5;
  231. System.out.println("(M)for main menu (N)for next screen (A)to display all");
  232. String minimenu = scanner.next().toLowerCase();
  233. switch(minimenu)
  234. {
  235. case "m":
  236. System.out.println("Mainmenu"); //break to main menu. Wont go back....
  237. break;
  238.  
  239. case "n":
  240. try
  241. {
  242. while(count<list)
  243. {
  244. raf.seek(count*92);
  245. rec.readFromFile(raf);
  246. System.out.println(rec);
  247. count ++;
  248. }
  249. }
  250. catch(EOFException e)
  251. {
  252. System.out.println("Out of records.\n");
  253. }
  254. break;
  255.  
  256.  
  257. case "a": print(raf, rec, -1);// print all (Works)
  258. break;
  259.  
  260. default:
  261. System.out.println("Please enter a valid option");
  262. break;
  263.  
  264.  
  265. }
  266. }
  267. }
  268. else
  269. {
  270. rec.readFromFile(raf);
  271. if(++incr == user)
  272. {
  273. System.out.println(incr + ": " + rec);
  274. }
  275. }
  276. }
  277. }
  278. catch (EOFException e){
  279.  
  280. }
  281. }
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement