Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.95 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class Main {
  5.  
  6. public static void main(String[] args) {
  7. ArrayList<Student> students = new ArrayList<Student>();
  8. Scanner in = new Scanner(System.in);
  9.  
  10. students.add(new Student("Dozie Anazia", 123123, 32, "Computer Science", "PS", "Senior", 4.0));
  11.  
  12.  
  13. while (true) {
  14. initialPrompt();
  15.  
  16. switch (in.nextLine()) {
  17. case "0":
  18. in.close();
  19. System.exit(0);
  20.  
  21. case "1":
  22. System.out.println("What's the student's name?");
  23. String newName = in.nextLine();
  24.  
  25. System.out.println("What's the student's ID #?");
  26. int newId = Integer.parseInt(in.nextLine());
  27.  
  28. System.out.println("What's the student's age?");
  29. int newAge = Integer.parseInt(in.nextLine());
  30.  
  31. System.out.println("What's the student's major?");
  32. String newMajor = in.nextLine();
  33.  
  34. System.out.println("What's the student's minor?");
  35. String newMinor = in.nextLine();
  36.  
  37. System.out.println("What's the student's grade level?");
  38. String newGradeLevel = in.nextLine();
  39.  
  40. System.out.println("What's the student's GPA?");
  41. double newGpa = Double.parseDouble(in.nextLine());
  42.  
  43. students.add(new Student(newName, newId, newAge, newMajor, newMinor, newGradeLevel, newGpa));
  44. break;
  45.  
  46. case "2":
  47. System.out.println("1: View all students");
  48. System.out.println("2: View all students with a specific major");
  49.  
  50. switch (in.nextLine()) {
  51. case "1":
  52. if (students.isEmpty()) {
  53. System.out.println("You haven't entered any students yet!");
  54. break;
  55. }
  56.  
  57. for (Student s : students) {
  58. System.out.println(s);
  59. }
  60. break;
  61.  
  62. case "2":
  63. System.out.println("Which major would you like to view?");
  64.  
  65. if (students.isEmpty()) {
  66. System.out.println("You haven't entered any students yet!");
  67. break;
  68. }
  69.  
  70. for (Student s : indexOfAllMajors(students, in.nextLine())) {
  71. System.out.println(s);
  72. }
  73. }
  74. break;
  75.  
  76. case "3":
  77. System.out.println("What's the name of the student you'd like to delete?");
  78. students.remove(getIndexByName(students, in.nextLine()));
  79. break;
  80.  
  81. case "4":
  82. System.out.println("What's the name of the student you'd like to update?");
  83.  
  84. int index = getIndexByName(students, in.nextLine());
  85. Student stud = students.get(index);
  86.  
  87. updateLoop: while (true) {
  88. System.out.println("What would you like to update?");
  89. System.out.println("0: Done updating");
  90. System.out.println("1: Name");
  91. System.out.println("2: ID");
  92. System.out.println("3: Age");
  93. System.out.println("4: Major");
  94. System.out.println("5: Minor");
  95. System.out.println("6: Grade Level");
  96. System.out.println("7: GPA");
  97.  
  98. switch (in.nextLine()) {
  99. case "0":
  100. break updateLoop;
  101.  
  102. case "1":
  103. System.out.println("What's the new name?");
  104. stud.setName(in.nextLine());
  105. break;
  106.  
  107. case "2":
  108. System.out.println("What's the new ID?");
  109. stud.setId(Integer.parseInt(in.nextLine()));
  110. break;
  111.  
  112. case "3":
  113. System.out.println("What's the new Age?");
  114. stud.setAge(Integer.parseInt(in.nextLine()));
  115. break;
  116.  
  117. case "4":
  118. System.out.println("What's the new Major?");
  119. stud.setMajor(in.nextLine());
  120. break;
  121.  
  122. case "5":
  123. System.out.println("What's the new Minor?");
  124. stud.setMinor(in.nextLine());
  125. break;
  126.  
  127. case "6":
  128. System.out.println("What's the new Grade Level?");
  129. stud.setGradeLevel(in.nextLine());
  130. break;
  131.  
  132. case "7":
  133. System.out.println("What's the new GPA?");
  134. stud.setGpa(Double.parseDouble(in.nextLine()));
  135. break;
  136. }
  137. }
  138.  
  139. students.set(index, stud);
  140. break;
  141.  
  142. case "5":
  143. System.out.println("What's the name of the student you'd like to update?");
  144.  
  145. int index_5 = getIndexByName(students, in.nextLine());
  146. Student stud_5 = new Student(students.get(index_5));
  147. stud_5.setName(stud_5.getName() + " (1)");
  148.  
  149. students.add(stud_5);
  150. break;
  151. }
  152. }
  153. }
  154.  
  155. private static void initialPrompt() {
  156. System.out.println();
  157. System.out.println("Hello, please enter an option from below:");
  158. System.out.println("0: Exit");
  159. System.out.println("1: Add a student");
  160. System.out.println("2: View a student");
  161. System.out.println("3: Delete a student");
  162. System.out.println("4: Update a student");
  163. System.out.println("5: Copy a student");
  164. }
  165.  
  166. private static int getIndexByName(ArrayList<Student> students, String key){
  167.  
  168. for (int i=0; i< students.size();i++) {
  169. if(students.get(i).getName().equals(key)) {
  170. return i;
  171.  
  172. }
  173. }
  174. return -1;
  175. }
  176.  
  177. private static ArrayList<Student> indexOfAllMajors(ArrayList<Student> students, String major) {
  178. final ArrayList<Student> studentsToReturn = new ArrayList<>();
  179. for (Student s : students) {
  180. if (s.getMajor().equals(major.trim())) {
  181. studentsToReturn.add(s);
  182. }
  183. }
  184. return studentsToReturn;
  185. }
  186.  
  187. private static ArrayList<Student> findByName(ArrayList<Student> students, String key) {
  188. final ArrayList<Student> studentsToReturn = new ArrayList<>();
  189.  
  190. for (Student s : students) {
  191. if (s.getName().equals(key.trim())) {
  192. studentsToReturn.add(s);
  193. }
  194. }
  195.  
  196. return studentsToReturn;
  197. }
  198. private static ArrayList<Student> findById(ArrayList<Student> students, int key) {
  199. final ArrayList<Student> studentIdToReturn = new ArrayList<>();
  200.  
  201. for (Student s : students) {
  202. if (s.getId()== key) {
  203. studentIdToReturn.add(s);
  204. }
  205. }
  206.  
  207. return studentIdToReturn;
  208. }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement