使用Java dropCourse方法在课程中放学分

时间:2018-12-02 03:04:32

标签: java methods driver

我有两个代码段。我特别遇到的问题是关于学生驱动程序中的dropCourse方法。我的代码中的所有内容似乎都可以正常运行,除非当我放弃某门课程时,它并没有减少课程学分。例如,如果我放下一个名为“ Computer101”的课程,该课程有4个学分,则它将从时间表中删除“ Computer101”,但是当我选择选项5来打印总学分时,则不会删除“ 4”学分。因此,如果我们有完整的时间表:

位置101的计算机101可获得4个学分

排名1的Physics105拥有4个学分

第2位的Art101,价值4个学分

排名3的Chem101价值4个学分

我决定从时间表中删除Computer101,所有其他类将按照预期的方式在数组中向上移动,在打印时间表时该类将不再显示,并且在搜索时将找不到,但是值得的信用额仍然存在。我觉得解决方案就在我眼前,但是我非常疲倦,看不到它。我非常感谢您的帮助,希望我能清楚自己的要求。

在这里可以找到我的dropCourse方法:

public class Student
{
   //Instance Data
   String studentName;
   String studentID;
   String streetAddress;
   String city;
   String state;
   String zipCode;
   String major;
   final int MAX_CREDITS = 18;
   int courseNumber = 0;  //start out with no courses
   int  totalCredits;
   final int SIZE = 6;
   String [ ] schedule = new String [SIZE];

   //Create Constructor:
   //Initializes the student data at instantiation time.
   //-------------------------------------------------------
   //  Sets up the student's information.
   //-------------------------------------------------------
   public Student (String name, String id, String address, String cityName, String stateName, String zip, String area )
   {
     studentName = name;
     studentID = id;
     streetAddress = address;
     city = cityName;
     state = stateName;
     zipCode = zip;
     major = area;
   }//end Student Constructor

   //Method to Return student information as string:
   //-------------------------------------------------------
   //  Returns the student information as a formatted string.
   //-------------------------------------------------------
   public String toString()
   {
     String studentInfo;

     studentInfo = "Name:\t\t\t" + studentName + "\n" + "ID:\t\t\t" + studentID + "\n" + "Address:\t\t" + streetAddress
           + "\n" + "City:\t\t\t" + city + "\n" + "State:\t\t\t" + state + "\n" + "Zip Code:\t\t" + zipCode
           + "\n" + "Major:\t\t\t" + major + "\n";

     return studentInfo;
   }// end toString
   //Method to determine if maximum allowed credits have been exceeded
   //-------------------------------------------------------
   //  Returns true if total credits does not exceed 18.
   //-------------------------------------------------------
   private boolean checkCredits(int numCredits)
   {
      if (numCredits + totalCredits <= MAX_CREDITS)  //make sure max credits not exceeded
      {
         return true;  //return a true if still less than 18 credits
      }
      else
      {
         return false;  //return a false if 18 credit limit is exceeded
      }//end numCredits
   }//checkCredits

   //Method to add a course to the student’s schedule
   //-------------------------------------------------------
   //  Adds a course to the array if total credits does not exceed 18.
   //-------------------------------------------------------
   public void addCourse(String course, int numCredits)
   {
      if (courseNumber <  SIZE )  //make sure array is not full. 
      {
         if (checkCredits(numCredits) == true) //if we’re under 18 credits
         {
            //add course
            schedule [courseNumber] = course + ":\t\t" + numCredits + "\tCredits\n";
            //increment number of credits
            totalCredits = totalCredits + numCredits;
            //increment number of courses
            courseNumber = courseNumber + 1;
            System.out.println("The course has been added!");
         }
         else  //oops – can’t do more than 18 credits
         {
            System.out.println("You have exceeded the maximum allowed credits.");
         }//end checkCredits
      }
      else  //oops – can’t do more than 6 courses
      {
         System.out.println("You have exceeded 6 courses.");
      }//end courseNumber

   }//addCourse
   public void displaySchedule( )
   {
      for (int index = 0; index < courseNumber; index++)
         {
            System.out.println("Course #" + (index + 1) + " " + schedule[index] + "\n");
         }//end for
   }//end display schedule

   public int searchCourse(String courseName)
   {    
      String course;
      boolean flag = false;
      int index = 0;
      while(index < courseNumber && flag == false) 
      {
         String extract = schedule[index].substring(0,6);
         if (extract.contains(courseName) == true)
         {
            flag = true;
         } 
         else 
         {
            index++;
         }
      }
      if (flag == false)
      {  
         return -1;
      } 
      else
      {   
         return index++;   
      }
   }
   public void dropCourse(String courseName)
   {
      int found;
      found = searchCourse(courseName);
      int index = 0;
      if (found == -1)
      {
         System.out.println("Course not in schedule");
      }
      else
      {
         if (found == 5)
         {
            courseNumber = courseNumber -1;    
         }
         else
         {
            for (index = found; index < courseNumber; index ++)
            {
               schedule[index] = schedule[index + 1];
            }
            courseNumber = courseNumber -1;
            System.out.println("The course has been dropped!");
         }
      }
   }
   public int totalCredits()
   {
      System.out.println("The total credits are " + totalCredits);
      return totalCredits;
   }   
}//end class Student

这是与上述代码一起使用的菜单在这里:

//This is a Driver program to test the external Class named Student
import java.util.Scanner;
public class TheMenu //BEGIN Class Definition
{
 //****************  Main Method*************************
    static Scanner scan = new Scanner(System.in);
    public static void main (String[] args)
    {
      //Data Definitions:

      //Instance Data
      String name;
      String id;
      String street;
      String city;
      String state;
      String zip;
      String major;
      String courseName;
      int courseCredits;
      int menu = 0;

      //Initialize first Student
      name = "Florence Welch";//look her up
      id = "7777";
      street = "777 Heaven Street";
      city = "Witchville";
      state = "NY";
      zip = "12345";
      major = "Arts";
      //instantiate the Student object
      Student student1 = new Student(name, id, street, city, state, zip, major);
      while (menu < 6)//begin while for menu options
      {
         System.out.println("1. Add a course ");
         System.out.println("2. Search for a course");
         System.out.println("3. Drop a course");
         System.out.println("4. Print out a student's schedule");
         System.out.println("5. Print out total credits");
         System.out.println("6. Exit");
         System.out.println("Enter an option: ");
         menu = scan.nextInt();

         if (menu == 1)//option to add course
         {
            System.out.println("Please enter the name of the course: ");
            courseName = scan.next();
            System.out.println("How many credits is the course? ");
            courseCredits = scan.nextInt();
            student1.addCourse(courseName, courseCredits);//uses method to store course information in array
         }  

            else if (menu == 2)//option to search for course
            {
               int x;
               System.out.println("Please enter the name of the course: ");
               courseName = scan.next();
               student1.searchCourse(courseName);
               x = student1.searchCourse(courseName);

               if (x == -1)//course is not found if value returns -1
               {
                  System.out.println("Course not found");
               }
               else
               { 
                  System.out.println("Course found in position " + student1.searchCourse(courseName));//shows user the position of course
               }
            }
         else if (menu == 3)//will drop course from users schedule
         {
            System.out.println("Please enter the course you wish to drop: ");
            courseName = scan.next();
            student1.dropCourse(courseName);
         }
         else if (menu == 4)//displays the users schedule
         {
            System.out.println(name + "'s Schedule:\n\n");
            student1.displaySchedule();
         }
         else if (menu == 5)//will display users credits for semester
         {
            student1.totalCredits();
         }
         else
         {
            System.out.println("Enjoy your semester!");//option 6 will exit program
         }
      }
   }
}

0 个答案:

没有答案