Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- ArrayList<Student> students = new ArrayList<Student>();
- Scanner in = new Scanner(System.in);
- students.add(new Student("Dozie Anazia", 123123, 32, "Computer Science", "PS", "Senior", 4.0));
- while (true) {
- initialPrompt();
- switch (in.nextLine()) {
- case "0":
- in.close();
- System.exit(0);
- case "1":
- System.out.println("What's the student's name?");
- String newName = in.nextLine();
- System.out.println("What's the student's ID #?");
- int newId = Integer.parseInt(in.nextLine());
- System.out.println("What's the student's age?");
- int newAge = Integer.parseInt(in.nextLine());
- System.out.println("What's the student's major?");
- String newMajor = in.nextLine();
- System.out.println("What's the student's minor?");
- String newMinor = in.nextLine();
- System.out.println("What's the student's grade level?");
- String newGradeLevel = in.nextLine();
- System.out.println("What's the student's GPA?");
- double newGpa = Double.parseDouble(in.nextLine());
- students.add(new Student(newName, newId, newAge, newMajor, newMinor, newGradeLevel, newGpa));
- break;
- case "2":
- System.out.println("1: View all students");
- System.out.println("2: View all students with a specific major");
- switch (in.nextLine()) {
- case "1":
- if (students.isEmpty()) {
- System.out.println("You haven't entered any students yet!");
- break;
- }
- for (Student s : students) {
- System.out.println(s);
- }
- break;
- case "2":
- System.out.println("Which major would you like to view?");
- if (students.isEmpty()) {
- System.out.println("You haven't entered any students yet!");
- break;
- }
- for (Student s : indexOfAllMajors(students, in.nextLine())) {
- System.out.println(s);
- }
- }
- break;
- case "3":
- System.out.println("What's the name of the student you'd like to delete?");
- students.remove(getIndexByName(students, in.nextLine()));
- break;
- case "4":
- System.out.println("What's the name of the student you'd like to update?");
- int index = getIndexByName(students, in.nextLine());
- Student stud = students.get(index);
- updateLoop: while (true) {
- System.out.println("What would you like to update?");
- System.out.println("0: Done updating");
- System.out.println("1: Name");
- System.out.println("2: ID");
- System.out.println("3: Age");
- System.out.println("4: Major");
- System.out.println("5: Minor");
- System.out.println("6: Grade Level");
- System.out.println("7: GPA");
- switch (in.nextLine()) {
- case "0":
- break updateLoop;
- case "1":
- System.out.println("What's the new name?");
- stud.setName(in.nextLine());
- break;
- case "2":
- System.out.println("What's the new ID?");
- stud.setId(Integer.parseInt(in.nextLine()));
- break;
- case "3":
- System.out.println("What's the new Age?");
- stud.setAge(Integer.parseInt(in.nextLine()));
- break;
- case "4":
- System.out.println("What's the new Major?");
- stud.setMajor(in.nextLine());
- break;
- case "5":
- System.out.println("What's the new Minor?");
- stud.setMinor(in.nextLine());
- break;
- case "6":
- System.out.println("What's the new Grade Level?");
- stud.setGradeLevel(in.nextLine());
- break;
- case "7":
- System.out.println("What's the new GPA?");
- stud.setGpa(Double.parseDouble(in.nextLine()));
- break;
- }
- }
- students.set(index, stud);
- break;
- case "5":
- System.out.println("What's the name of the student you'd like to update?");
- int index_5 = getIndexByName(students, in.nextLine());
- Student stud_5 = new Student(students.get(index_5));
- stud_5.setName(stud_5.getName() + " (1)");
- students.add(stud_5);
- break;
- }
- }
- }
- private static void initialPrompt() {
- System.out.println();
- System.out.println("Hello, please enter an option from below:");
- System.out.println("0: Exit");
- System.out.println("1: Add a student");
- System.out.println("2: View a student");
- System.out.println("3: Delete a student");
- System.out.println("4: Update a student");
- System.out.println("5: Copy a student");
- }
- private static int getIndexByName(ArrayList<Student> students, String key){
- for (int i=0; i< students.size();i++) {
- if(students.get(i).getName().equals(key)) {
- return i;
- }
- }
- return -1;
- }
- private static ArrayList<Student> indexOfAllMajors(ArrayList<Student> students, String major) {
- final ArrayList<Student> studentsToReturn = new ArrayList<>();
- for (Student s : students) {
- if (s.getMajor().equals(major.trim())) {
- studentsToReturn.add(s);
- }
- }
- return studentsToReturn;
- }
- private static ArrayList<Student> findByName(ArrayList<Student> students, String key) {
- final ArrayList<Student> studentsToReturn = new ArrayList<>();
- for (Student s : students) {
- if (s.getName().equals(key.trim())) {
- studentsToReturn.add(s);
- }
- }
- return studentsToReturn;
- }
- private static ArrayList<Student> findById(ArrayList<Student> students, int key) {
- final ArrayList<Student> studentIdToReturn = new ArrayList<>();
- for (Student s : students) {
- if (s.getId()== key) {
- studentIdToReturn.add(s);
- }
- }
- return studentIdToReturn;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement