Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class StudentTest {
- public static void main(String[] args) {
- // Input scanner object
- Scanner sc = new Scanner(System.in);
- // List of electives
- ArrayList<Elective> electives = new ArrayList<>();
- // List of students
- ArrayList<Student> students = new ArrayList<>();
- // Add electives to electives list
- electives.add(new Elective("Art", 5));
- electives.add(new Elective("Cooking", 4));
- electives.add(new Elective("Music", 3));
- // Add students to studentNames list
- students.add(new Student("A"));
- students.add(new Student("B"));
- students.add(new Student("C"));
- students.add(new Student("D"));
- students.add(new Student("E"));
- for(int i = 0; i < students.size() - 1; i++) {
- System.out.println("Enter 1: Art ; Enter 2: Cooking ; Enter 3: Music");
- int electiveInput = sc.nextInt();
- if(electiveInput == 1) {
- if(electives.get(0).getClassSize() < electives.get(0).getMaxClassSize()) {
- students.get(i).addElective(electives.get(0));
- electives.get(0).addStudent(students.get(i));
- }
- }
- if(electiveInput == 2) {
- }
- if(electiveInput == 3) {
- }
- }
- System.out.println(electives.toString());
- sc.close();
- }
- }
- // ELECTIVE CLASS
- import java.util.ArrayList;
- public class Elective {
- private String name; //contains the name of the elective
- private int maxClassSize; //contains the maximum number that can enroll in the course
- private int classSize; //contains the current number enrolled in the course
- private ArrayList<Student> enrollees;
- //constructs an object of type Elective
- public Elective(String name, int maxClassSize){
- this.name = name;
- this.maxClassSize = maxClassSize;
- this.classSize = 0;
- enrollees = new ArrayList<Student>();
- }
- //returns the name of the elective
- public String getName(){
- return name;
- }
- //returns the maximum number to be enrolled in the course
- public int getMaxClassSize(){
- return maxClassSize;
- }
- //returns the current enrollment in the course
- public int getClassSize(){
- return classSize;
- }
- //adds Student s to this elective
- //returns true if successfully added
- //returns false if unable to add the student
- public boolean addStudent(Student student){
- if(classSize <= maxClassSize){
- enrollees.add(student);
- classSize += 1;
- System.out.println("ClassSize: " + classSize);
- return true;
- }
- else{
- return false;
- }
- }
- //returns the ArrayList of Students in the course
- public ArrayList<Student> getStudents(){
- return enrollees;
- }
- @Override
- public String toString() {
- return "\nElective " + name + ": [maxClassSize: " + maxClassSize + " classSize: " + classSize + " enrollees: "
- + enrollees + "]";
- }
- }
- // STUDENT CLASS
- import java.util.ArrayList;
- public class Student {
- //the student's name
- private String name;
- //the list of electives that the student has enrolled in
- private ArrayList<Elective> electives = new ArrayList<Elective>();
- public Student(){
- name = "NO NAME";
- electives = new ArrayList<Elective>();
- }
- //creates an object of type Student
- public Student(String name){
- this.name = name;
- }
- //returns the name of the Student object
- public String getName(){
- return name;
- }
- //registers the student for the input Elective, if able
- //returns true if the Student was able to register for Elective elective
- //returns false if the Student cannot register
- public void addElective(Elective elective) {
- // Check to see if the class is full
- electives.add(elective);
- }
- //returns the ArrayList of Electives that the student is
- // registered for
- public ArrayList<Elective> getElectives(){
- return electives;
- }
- @Override
- public String toString() {
- return "Student [name=" + name + ", electives=" + electives + "]";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement