Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. package school;
  2.  
  3. import java.util.Scanner;
  4.  
  5. import java.util.ArrayList;
  6.  
  7. public class StudentInfo {
  8.  
  9. static public Student[] students = new Student[0];
  10.  
  11. static public Scanner scanner = new Scanner(System.in);
  12.  
  13. static private String[] letterGrades = new String[] { "F", "D", "C", "B", "A"};
  14.  
  15. public static void main(String[] args) {
  16.  
  17.  
  18. boolean Exit = false;
  19. while(!Exit) {
  20.  
  21. int selection = printMenu();
  22. switch(selection) {
  23. case 1: names(); break;
  24. case 2: students(); break;
  25. case 3: search(); break;
  26. case 4: searchGrade(); break;
  27. case 5: failing(); break;
  28. case 7: highest(); break;
  29. case 6: lowest(); break;
  30. case 8: average(); break;
  31. case 9: Exit = true; break;
  32. default:System.out.println("Uh oh, an error occured!"); break;
  33. }
  34. }
  35. }
  36.  
  37. public static int printMenu() {
  38.  
  39.  
  40. System.out.println("Please choose an option. Start by adding the students' names.");
  41. System.out.println("1. Add students");
  42. System.out.println("2. Display students");
  43. System.out.println("3. Search for student by name");
  44. System.out.println("4. Find students by letter grade");
  45. System.out.println("5. Shows the failing students");
  46. System.out.println("6. Shows the student with the highest grade");
  47. System.out.println("7. Shows the with the lowest grade");
  48. System.out.println("8. Displays the class average");
  49. System.out.println("9. Exit");
  50.  
  51. return scanner.nextInt();
  52. }
  53.  
  54. public static void names() {
  55. boolean Exit = false;
  56. while(!Exit) {
  57. System.out.println();
  58. System.out.println("Enter name");
  59. String name = scanner.next();
  60. System.out.println("Enter a grade out of 100%");
  61. int grade = scanner.nextInt();
  62.  
  63. addStudent(new Student(name, grade));
  64.  
  65. System.out.println("Would you like to continue? Y or N");
  66. String choice = scanner.next();
  67. if (choice.equalsIgnoreCase("N"))Exit = true;
  68. }
  69. }
  70.  
  71. public static void addStudent(Student student) {
  72.  
  73. Student[] newStudents = new Student[students.length + 1];
  74. System.arraycopy(students, 0, newStudents, 0, students.length);
  75.  
  76. newStudents[newStudents.length - 1] = student;
  77.  
  78. students = newStudents;
  79. }
  80. public static void highest() {
  81.  
  82. Student highestStudent = students[0];
  83. for (Student student : students) {
  84. if (highestStudent.grade < student.grade) highestStudent = student;
  85. }
  86.  
  87. printStudent(highestStudent);
  88. }
  89. public static void search() {
  90.  
  91. System.out.println();
  92. System.out.println("What is the name you're looking for?");
  93.  
  94. scanner.nextLine();
  95. String name = scanner.nextLine();
  96.  
  97. for (Student student : students) {
  98. if (!student.name.equals(name)) continue;
  99. printStudent(student);
  100. }
  101. }
  102.  
  103. public static void students() {
  104. for (Student student : students) printStudent(student);
  105. }
  106.  
  107. public static void average() {
  108. int grades = 0;
  109. for (Student student : students) {
  110. grades += student.grade;
  111. }
  112. System.out.println("The class average is " + grades / students.length);
  113. }
  114.  
  115.  
  116. public static void lowest() {
  117. Student lowestStudent = students[0];
  118. for (Student student : students) {
  119. if (lowestStudent.grade > student.grade) lowestStudent = student;
  120. }
  121.  
  122. printStudent(lowestStudent);
  123. }
  124.  
  125. public static void failing() {
  126.  
  127. for (Student student : students) if (student.searchGrade.equals("F")) printStudent(student);
  128. }
  129.  
  130. public static void printStudent(Student student) {
  131.  
  132. System.out.println(student.name + " Grade: " + student.grade + " Letter Grade: " + student.searchGrade);
  133. }
  134.  
  135. public static void searchGrade() {
  136.  
  137. System.out.println();
  138. System.out.println("Enter the letter grade you would like to use");
  139. String searchGrade;
  140. while (true) {
  141.  
  142. boolean canExit = false;
  143. searchGrade = scanner.nextLine();
  144.  
  145. for (String letter : letterGrades) {
  146.  
  147. if (searchGrade.equals(letter)) canExit = true;
  148. }
  149.  
  150. if (!canExit) System.out.println("Please enter a letter grade");
  151. else break;
  152. }
  153.  
  154. for (Student student : students) {
  155.  
  156. if (!student.searchGrade.equals(searchGrade)) continue;
  157. printStudent(student);
  158. }
  159. }
  160. }
  161.  
  162. class Student {
  163. public String name;
  164. public int grade;
  165. public String searchGrade;
  166.  
  167. static private int[] searchGradeGrades = new int[]{ 49, 59, 69, 79, Integer.MAX_VALUE };
  168. static private String[] letterGrades = new String[] { "F", "D", "C", "B", "A"};
  169.  
  170. Student(String name, int grade) {
  171.  
  172. this.name = name;
  173.  
  174. this.grade = grade;
  175.  
  176. for (int i = 0; i < letterGrades.length; ++i) {
  177. if (grade <= searchGradeGrades[i]) {
  178. this.searchGrade = letterGrades[i];
  179. break;
  180.  
  181.  
  182. }
  183. }
  184. }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement