Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1. public class Student
  2. {
  3.     // instance variables - replace the example below with your own
  4.     private String forename;
  5.     private String surname;
  6.     private int id;
  7.     private String address;
  8.  
  9.     //create numberOfCredits as an integer as of now
  10.     private int studentCredits;
  11.  
  12.     //Create list of module student is enrolled on
  13.     ObjectArrayList studentModules = new ObjectArrayList();
  14.  
  15.     public Student(String forename, String surname, int id, String address)
  16.     {
  17.         this.forename = forename;
  18.         this.surename = surname;
  19.         this.id = id;
  20.         this.address = address;
  21.  
  22.     }
  23.  
  24.     //Taking mod as module name
  25.     public boolean addEnrolledOn(Module mod)    
  26.     {
  27.         //check if the module already exist in studentModules
  28.         if (this.studentModules.contains(mod))  {
  29.             return false;
  30.         }
  31.         else if (this.studentCredits + Module.numberOfCredits() <= 120) {
  32.             //Add mod to studentModules
  33.             //add the module's number of credits to studentCredits
  34.             this.studentModules.add(mod);
  35.             this.studentCredits += mod.numberOfCredits();
  36.             return true;
  37.         }
  38.         else    {
  39.             return false;
  40.         }
  41.  
  42.     }
  43.  
  44.     public boolean removeEnrolledOn(Module mod)
  45.     {
  46.         //If student has not recorded it is enrolled on module, return false
  47.         // otherwise if successfully remove module, return true
  48.         if (!Module.students.contains(student)) {
  49.             return false;
  50.         }
  51.         else if (this.studentModules.remove(mod))    {
  52.             this.studentCredits -= mod.numberOfCredits();
  53.             return true;
  54.         }
  55.     }
  56.  
  57.     //Return amount of credits currently enrolled as in Integer
  58.     public int creditsEnrolledOn()  
  59.     {
  60.         return this.studentCredits;
  61.     }
  62.  
  63.     //Check if object comparing is the same object, else return false
  64.     public boolean equals(Object obj)  
  65.     {
  66.         if (obj instanceof Student)  {
  67.             Student other = (Student) obj;
  68.             if (other.forename.equals(this.forename) &&
  69.             other.surname.equals(this.surname) &&
  70.             other.id.equals(this.id) &&
  71.             other.address.equals(this.address))    {
  72.                 return true;
  73.             }
  74.  
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement