Advertisement
Guest User

TestingComposition

a guest
Feb 20th, 2020
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. import java.util.*;
  2. public class StudentTest {
  3.  
  4. public static void main(String[] args) {
  5. // Input scanner object
  6. Scanner sc = new Scanner(System.in);
  7.  
  8. // List of electives
  9. ArrayList<Elective> electives = new ArrayList<>();
  10. // List of students
  11. ArrayList<Student> students = new ArrayList<>();
  12.  
  13. // Add electives to electives list
  14. electives.add(new Elective("Art", 5));
  15. electives.add(new Elective("Cooking", 4));
  16. electives.add(new Elective("Music", 3));
  17.  
  18. // Add students to studentNames list
  19. students.add(new Student("A"));
  20. students.add(new Student("B"));
  21. students.add(new Student("C"));
  22. students.add(new Student("D"));
  23. students.add(new Student("E"));
  24.  
  25. for(int i = 0; i < students.size() - 1; i++) {
  26.  
  27. System.out.println("Enter 1: Art ; Enter 2: Cooking ; Enter 3: Music");
  28. int electiveInput = sc.nextInt();
  29.  
  30. if(electiveInput == 1) {
  31. if(electives.get(0).getClassSize() < electives.get(0).getMaxClassSize()) {
  32. students.get(i).addElective(electives.get(0));
  33. electives.get(0).addStudent(students.get(i));
  34. }
  35.  
  36. }
  37.  
  38. if(electiveInput == 2) {
  39.  
  40. }
  41.  
  42. if(electiveInput == 3) {
  43.  
  44. }
  45.  
  46.  
  47. }
  48.  
  49. System.out.println(electives.toString());
  50. sc.close();
  51. }
  52. }
  53.  
  54.  
  55. // ELECTIVE CLASS
  56. import java.util.ArrayList;
  57.  
  58. public class Elective {
  59. private String name; //contains the name of the elective
  60. private int maxClassSize; //contains the maximum number that can enroll in the course
  61. private int classSize; //contains the current number enrolled in the course
  62. private ArrayList<Student> enrollees;
  63.  
  64. //constructs an object of type Elective
  65. public Elective(String name, int maxClassSize){
  66. this.name = name;
  67. this.maxClassSize = maxClassSize;
  68. this.classSize = 0;
  69. enrollees = new ArrayList<Student>();
  70. }
  71. //returns the name of the elective
  72. public String getName(){
  73. return name;
  74. }
  75.  
  76. //returns the maximum number to be enrolled in the course
  77. public int getMaxClassSize(){
  78. return maxClassSize;
  79. }
  80.  
  81. //returns the current enrollment in the course
  82. public int getClassSize(){
  83. return classSize;
  84. }
  85.  
  86. //adds Student s to this elective
  87. //returns true if successfully added
  88. //returns false if unable to add the student
  89. public boolean addStudent(Student student){
  90. if(classSize <= maxClassSize){
  91. enrollees.add(student);
  92. classSize += 1;
  93. System.out.println("ClassSize: " + classSize);
  94. return true;
  95. }
  96. else{
  97. return false;
  98. }
  99. }
  100.  
  101. //returns the ArrayList of Students in the course
  102. public ArrayList<Student> getStudents(){
  103.  
  104. return enrollees;
  105. }
  106. @Override
  107. public String toString() {
  108. return "\nElective " + name + ": [maxClassSize: " + maxClassSize + " classSize: " + classSize + " enrollees: "
  109. + enrollees + "]";
  110. }
  111.  
  112.  
  113.  
  114. }
  115.  
  116.  
  117. // STUDENT CLASS
  118. import java.util.ArrayList;
  119.  
  120. public class Student {
  121.  
  122. //the student's name
  123. private String name;
  124.  
  125. //the list of electives that the student has enrolled in
  126. private ArrayList<Elective> electives = new ArrayList<Elective>();
  127.  
  128. public Student(){
  129. name = "NO NAME";
  130. electives = new ArrayList<Elective>();
  131. }
  132. //creates an object of type Student
  133. public Student(String name){
  134. this.name = name;
  135. }
  136.  
  137. //returns the name of the Student object
  138. public String getName(){
  139. return name;
  140. }
  141.  
  142. //registers the student for the input Elective, if able
  143. //returns true if the Student was able to register for Elective elective
  144. //returns false if the Student cannot register
  145. public void addElective(Elective elective) {
  146. // Check to see if the class is full
  147. electives.add(elective);
  148. }
  149.  
  150. //returns the ArrayList of Electives that the student is
  151. // registered for
  152. public ArrayList<Elective> getElectives(){
  153. return electives;
  154. }
  155.  
  156. @Override
  157. public String toString() {
  158. return "Student [name=" + name + ", electives=" + electives + "]";
  159. }
  160.  
  161.  
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement