Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package school;
- import java.util.Scanner;
- import java.util.ArrayList;
- public class StudentInfo {
- static public Student[] students = new Student[0];
- static public Scanner scanner = new Scanner(System.in);
- static private String[] letterGrades = new String[] { "F", "D", "C", "B", "A"};
- public static void main(String[] args) {
- boolean Exit = false;
- while(!Exit) {
- int selection = printMenu();
- switch(selection) {
- case 1: names(); break;
- case 2: students(); break;
- case 3: search(); break;
- case 4: searchGrade(); break;
- case 5: failing(); break;
- case 7: highest(); break;
- case 6: lowest(); break;
- case 8: average(); break;
- case 9: Exit = true; break;
- default:System.out.println("Uh oh, an error occured!"); break;
- }
- }
- }
- public static int printMenu() {
- System.out.println("Please choose an option. Start by adding the students' names.");
- System.out.println("1. Add students");
- System.out.println("2. Display students");
- System.out.println("3. Search for student by name");
- System.out.println("4. Find students by letter grade");
- System.out.println("5. Shows the failing students");
- System.out.println("6. Shows the student with the highest grade");
- System.out.println("7. Shows the with the lowest grade");
- System.out.println("8. Displays the class average");
- System.out.println("9. Exit");
- return scanner.nextInt();
- }
- public static void names() {
- boolean Exit = false;
- while(!Exit) {
- System.out.println();
- System.out.println("Enter name");
- String name = scanner.next();
- System.out.println("Enter a grade out of 100%");
- int grade = scanner.nextInt();
- addStudent(new Student(name, grade));
- System.out.println("Would you like to continue? Y or N");
- String choice = scanner.next();
- if (choice.equalsIgnoreCase("N"))Exit = true;
- }
- }
- public static void addStudent(Student student) {
- Student[] newStudents = new Student[students.length + 1];
- System.arraycopy(students, 0, newStudents, 0, students.length);
- newStudents[newStudents.length - 1] = student;
- students = newStudents;
- }
- public static void highest() {
- Student highestStudent = students[0];
- for (Student student : students) {
- if (highestStudent.grade < student.grade) highestStudent = student;
- }
- printStudent(highestStudent);
- }
- public static void search() {
- System.out.println();
- System.out.println("What is the name you're looking for?");
- scanner.nextLine();
- String name = scanner.nextLine();
- for (Student student : students) {
- if (!student.name.equals(name)) continue;
- printStudent(student);
- }
- }
- public static void students() {
- for (Student student : students) printStudent(student);
- }
- public static void average() {
- int grades = 0;
- for (Student student : students) {
- grades += student.grade;
- }
- System.out.println("The class average is " + grades / students.length);
- }
- public static void lowest() {
- Student lowestStudent = students[0];
- for (Student student : students) {
- if (lowestStudent.grade > student.grade) lowestStudent = student;
- }
- printStudent(lowestStudent);
- }
- public static void failing() {
- for (Student student : students) if (student.searchGrade.equals("F")) printStudent(student);
- }
- public static void printStudent(Student student) {
- System.out.println(student.name + " Grade: " + student.grade + " Letter Grade: " + student.searchGrade);
- }
- public static void searchGrade() {
- System.out.println();
- System.out.println("Enter the letter grade you would like to use");
- String searchGrade;
- while (true) {
- boolean canExit = false;
- searchGrade = scanner.nextLine();
- for (String letter : letterGrades) {
- if (searchGrade.equals(letter)) canExit = true;
- }
- if (!canExit) System.out.println("Please enter a letter grade");
- else break;
- }
- for (Student student : students) {
- if (!student.searchGrade.equals(searchGrade)) continue;
- printStudent(student);
- }
- }
- }
- class Student {
- public String name;
- public int grade;
- public String searchGrade;
- static private int[] searchGradeGrades = new int[]{ 49, 59, 69, 79, Integer.MAX_VALUE };
- static private String[] letterGrades = new String[] { "F", "D", "C", "B", "A"};
- Student(String name, int grade) {
- this.name = name;
- this.grade = grade;
- for (int i = 0; i < letterGrades.length; ++i) {
- if (grade <= searchGradeGrades[i]) {
- this.searchGrade = letterGrades[i];
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement