Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.71 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class TestCourse {
  5.  
  6.     public static void printMenu(){
  7.         System.out.println("===== Menu =====");
  8.         System.out.println("1. Add new course");
  9.         System.out.println("2. Add new student");
  10.         System.out.println("3. Drop student");
  11.         System.out.println("4. View student list");
  12.         System.out.println("5. Exit");
  13.     }
  14.  
  15.     public static void main(String[] args) {
  16.  
  17.         Scanner scanner = new Scanner(System.in);
  18.         ArrayList<Course> courseList = new ArrayList<>();
  19.  
  20.         int choice = -1;
  21.         while( choice != 5 ){
  22.             printMenu();
  23.             do{
  24.                 try{
  25.                     System.out.print("Choice: ");
  26.                     choice = scanner.nextInt();
  27.                 } catch(Exception e){
  28.                     choice = -1;
  29.                 } finally {
  30.                     scanner.nextLine();
  31.                 }
  32.             } while( choice < 1 || choice > 5 );
  33.  
  34.             if( choice == 1 ){  //add new course
  35.                 System.out.print("Course Name: ");
  36.                 String courseName = scanner.nextLine();
  37.                 Course x = new Course(courseName);
  38.                 courseList.add(x);
  39.                 System.out.println(courseName + " added to the list of courses!");
  40.                 System.out.println("Press enter to continue...");
  41.                 scanner.nextLine();
  42.             } else if( choice == 2 ){   //add new student
  43.                 if( courseList.isEmpty() ){
  44.                     System.out.println("No courses listed!");
  45.                     System.out.println("Press enter to continue...");
  46.                     scanner.nextLine();
  47.                 } else {
  48.                     System.out.println("Courses:");
  49.                     for ( int i = 0 ; i < courseList.size() ; ++i ){
  50.                         System.out.println(i+1 + ". " + courseList.get(i).getCourseName());
  51.                     }
  52.                     System.out.print("Input Which course name: ");
  53.                     String courseName = scanner.nextLine();
  54.                     int contain = 0;
  55.                     int index = -1;
  56.                     for ( int i = 0 ; i < courseList.size() ; ++i ){
  57.                         if(courseList.get(i).getCourseName().equals(courseName)){
  58.                             contain = 1;
  59.                             index = i;
  60.                             break;
  61.                         }
  62.                     }
  63.                     if( contain == 1 ){
  64.                         System.out.print("Input student name: ");
  65.                         String studentName = scanner.nextLine();
  66.                         courseList.get(index).addStudent(studentName);
  67.                         System.out.println("Press enter to continue...");
  68.                         scanner.nextLine();
  69.                     } else {
  70.                         System.out.println("Course does not exist!");
  71.                         System.out.println("Press enter to continue...");
  72.                         scanner.nextLine();
  73.                     }
  74.                 }
  75.             } else if( choice == 3 ){   //drop student
  76.                 if( courseList.isEmpty() ){
  77.                     System.out.println("No courses listed!");
  78.                     System.out.println("Press enter to continue...");
  79.                     scanner.nextLine();
  80.                 } else {
  81.                     System.out.println("Courses:");
  82.                     for ( int i = 0 ; i < courseList.size() ; ++i ){
  83.                         System.out.println(i+1 + ". " + courseList.get(i).getCourseName());
  84.                     }
  85.                     System.out.print("Input Which course name: ");
  86.                     String courseName = scanner.nextLine();
  87.                     int contain = 0;
  88.                     int index = -1;
  89.                     for ( int i = 0 ; i < courseList.size() ; ++i ){
  90.                         if(courseList.get(i).getCourseName().equals(courseName)){
  91.                             contain = 1;
  92.                             index = i;
  93.                             break;
  94.                         }
  95.                     }
  96.                     if( contain == 1 ){
  97.                         ArrayList<String> studentList = courseList.get(index).getStudents();
  98.                         if( studentList.isEmpty() ){
  99.                             System.out.println("No students in this course!");
  100.                             System.out.println("Press enter to continue...");
  101.                             scanner.nextLine();
  102.                         } else {
  103.                             System.out.println("Students:");
  104.                             for (int i = 0; i < courseList.get(index).getNumberOfStudents(); ++i) {
  105.                                 System.out.println((i + 1) + ". " + studentList.get(i));
  106.                             }
  107.                             System.out.print("Input student name to be dropped: ");
  108.                             String studentName = scanner.nextLine();
  109.                             courseList.get(index).dropStudent(studentName);
  110.                             System.out.println("Press enter to continue...");
  111.                             scanner.nextLine();
  112.                         }
  113.                     } else {
  114.                         System.out.println("Course does not exist!");
  115.                         System.out.println("Press enter to continue...");
  116.                         scanner.nextLine();
  117.                     }
  118.                 }
  119.             } else if( choice == 4 ){   //view student list
  120.                 if( courseList.isEmpty() ){
  121.                     System.out.println("No courses listed!");
  122.                     System.out.println("Press enter to continue...");
  123.                     scanner.nextLine();
  124.                 } else {
  125.                     System.out.println("Courses:");
  126.                     for ( int i = 0 ; i < courseList.size() ; ++i ){
  127.                         System.out.println((i+1) + ". " + courseList.get(i).getCourseName());
  128.                     }
  129.                     System.out.print("Input Which course name: ");
  130.                     String courseName = scanner.nextLine();
  131.                     int contain = 0;
  132.                     int index = -1;
  133.                     for ( int i = 0 ; i < courseList.size() ; ++i ){
  134.                         if(courseList.get(i).getCourseName().equals(courseName)){
  135.                             contain = 1;
  136.                             index = i;
  137.                             break;
  138.                         }
  139.                     }
  140.                     if( contain == 1 ){
  141.                         ArrayList<String> studentList = courseList.get(index).getStudents();
  142.                         if( studentList.isEmpty() ){
  143.                             System.out.println("No students in this course!");
  144.                             System.out.println("Press enter to continue...");
  145.                             scanner.nextLine();
  146.                         } else {
  147.                             System.out.println("Students:");
  148.                             for (int i = 0; i < courseList.get(index).getNumberOfStudents(); ++i) {
  149.                                 System.out.println((i + 1) + ". " + studentList.get(i));
  150.                             }
  151.                             System.out.println();
  152.                             System.out.println("Press enter to continue...");
  153.                             scanner.nextLine();
  154.                         }
  155.                     } else {
  156.                         System.out.println("Course does not exist!");
  157.                         System.out.println("Press enter to continue...");
  158.                         scanner.nextLine();
  159.                     }
  160.                 }
  161.             }
  162.  
  163.         }
  164.  
  165.         System.out.println("Thank you for using this program!");
  166.  
  167.         /*  //from the book
  168.         Course course1 = new Course("Data Structures");
  169.         Course course2 = new Course("Database Systems");
  170.  
  171.         course1.addStudent("Peter Jones");
  172.         course1.addStudent("Kim Smith");
  173.         course1.addStudent("Anne Kennedy");
  174.  
  175.         course2.addStudent("Peter Jones");
  176.         course2.addStudent("Steve Smith");
  177.  
  178.         System.out.println("Number of students in course1: "
  179.                 + course1.getNumberOfStudents());
  180.         ArrayList<String> students = course1.getStudents();
  181.         for( int i = 0 ; i < course1.getNumberOfStudents(); ++i ){
  182.             if(i + 1 == course1.getNumberOfStudents()){
  183.                 System.out.print(students.get(i));
  184.             } else {
  185.                 System.out.print(students.get(i) + ", ");
  186.             }
  187.         }
  188.  
  189.         System.out.println();
  190.         System.out.println("Number of students in course2: " + course2.getNumberOfStudents());
  191.         */
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement