diff --git a/src/main/java/com/github/curriculeon/Classroom.java b/src/main/java/com/github/curriculeon/Classroom.java new file mode 100644 index 0000000..ef6feb3 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Classroom.java @@ -0,0 +1,42 @@ +package com.github.curriculeon; +/* +Create a Classroom singleton. +The class should declare a field that references the instance of Students called students. DONE +The class should declare a field that references the instance of Instructors called instructors. Done +The class should define a method hostLecture which makes use of a Teacher teacher, + double numberOfHours parameter to host a lecture to the composite personList field in the students reference.Done +The class should define a method hostLecture which makes use of a long id, + double numberOfHours parameter to identify a respective Instructor to host a lecture to the composite personList field + in the cohort reference. Done +The class should define a method getStudyMap which returns a new instance of a mapping from Student objects to + Double objects, representative of each respective student's totalStudyTime. + + */ + +import java.util.ArrayList; +import java.util.Map; +import java.util.TreeMap; + +public class Classroom { + Students students = Students.getInstance(); + Instructors instructors = Instructors.getInstance(); + + public void hostLecture(Teacher teacher, Double numberOfHours) { + teacher.lecture(students.toArray(), numberOfHours); + } + + public void hostLecture(Long id, Double numberOfHours) { + instructors.findByID(id).lecture(students.toArray(), numberOfHours); + } + + public Map getStudyMap() { + Map map = new TreeMap<>(); + ArrayList slist = students.personList; + for (Student stu : slist) { + map.put(stu.getId(),stu.getTotalStudyTime()); + } + return map; + } + +} + diff --git a/src/main/java/com/github/curriculeon/Educator.java b/src/main/java/com/github/curriculeon/Educator.java new file mode 100644 index 0000000..08ede48 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Educator.java @@ -0,0 +1,48 @@ +package com.github.curriculeon; + +public enum Educator implements Teacher { + LEON, + HASEEB; + + private Double hoursWorked; + private final Instructor instructor; + + @Override + public void teach(Learner learner, Double numberOfHours) { + + } + + @Override + public void lecture(Learner[] learners, Double numberOfHours) { + + } + Educator() { + long id = this.ordinal(); // inherited from `Enum` implicit super class + String name = this.name(); // inherited from `Enum` implicit super class + this.instructor = new Instructor(id, name); + Instructors.getInstance().add(instructor); + } +} + /* private Double hoursWorked; + private final Instructor instructor; + + Educator() { + long id = this.ordinal(); // inherited from `Enum` implicit super class + String name = this.name(); // inherited from `Enum` implicit super class + this.instructor = new Instructor(id, name); + Instructors.getInstance().add(instructor); + } + + @Override + public void teach(Learner learner, Double numberOfHours) { + instructor.teach(learner, numberOfHours); + hoursWorked += numberOfHours; + } + + @Override + public void lecture(Learner[] learners, Double numberOfHours) { + instructor.lecture(learners, numberOfHours); + hoursWorked += numberOfHours; + } +} +*/ \ No newline at end of file diff --git a/src/main/java/com/github/curriculeon/Instructor.java b/src/main/java/com/github/curriculeon/Instructor.java new file mode 100644 index 0000000..bdddcfc --- /dev/null +++ b/src/main/java/com/github/curriculeon/Instructor.java @@ -0,0 +1,33 @@ +package com.github.curriculeon; + +/* +Create an Instructor class such that: +Instructor is a subclass of Person +Instructor implements the Teacher interface +Instructor should have a concrete implementation of the teach method which invokes the learn method on the specified Learner object. +Instructor should have a concrete implementation of the lecture method, + which invokes the learn method on each of the elements in the specified array of Learner objects. +numberOfHours should be evenly split amongst the learners. +double numberOfHoursPerLearner = numberOfHours / learners.length; + + */ +public class Instructor extends Person implements Teacher { + Double numberOfHoursPerLearner =0.0; + + Instructor(long id, String name) { + super(id, name); + } + + @Override + public void teach(Learner learner, Double numberOfHours) { + learner.learn(numberOfHours); + } + + @Override + public void lecture(Learner[] learners, Double numberOfHours) { + this.numberOfHoursPerLearner += numberOfHours; + for (Learner learner : learners) {//I got this from Chris fulton + this.teach(learner, numberOfHours); + } + } +} diff --git a/src/main/java/com/github/curriculeon/Instructors.java b/src/main/java/com/github/curriculeon/Instructors.java new file mode 100644 index 0000000..f410dab --- /dev/null +++ b/src/main/java/com/github/curriculeon/Instructors.java @@ -0,0 +1,58 @@ +package com.github.curriculeon; +/* +Use Part 7 as a reference. +Create a Instructors singleton which represents the set of instructors. +Create a TestInstructors class. + + */ + + +import java.util.ArrayList; + +public class Instructors extends People { + // ArrayList personList=new ArrayList(); + final static Instructors instance = new Instructors(); + String [] cohort = new String[]{"Leon","Haseeb"}; + private Instructors(){ + super(); + + for(Instructor instructor:peopleMaker(cohort)){ + this.add(instructor); + } + } + + @Override + public Instructor[] toArray() { + Instructor[] p = new Instructor[this.personList.size()]; + for (int i = 0; i < p.length; i++) { + p[i] = this.personList.get(i); + } + return p; + + } + + public static Instructors getInstance(){ + return instance; + } + + public ArrayList peopleMaker(String[] names){ + ArrayList people=new ArrayList(); + for(int i =0;i and define a method named iterator which makes use of the personList field to generate a new a Iterator. + */ +abstract public class People implements Iterable { + ArrayList personList =new ArrayList(); + + public People() { + + } + + + public void add(SomePerson person) { + this.personList.add(person); + } + + People(ArrayList people){ + this.personList=people; + } + + public SomePerson findByID(Long id) { + for (SomePerson person : this.personList) { + if (id == person.getId()) return person; + } + return null; + } + + + public Boolean removePerson(SomePerson person) { + return personList.remove(person); + + } + + public Boolean removePerson(Long id) { + for (Person p : this.personList) { + if (id.equals(p)) this.personList.remove(p); + return true; + } + return false; + + } + + public void removeAll() { + for (SomePerson p : this.personList) this.personList.remove(p); + } + + public Integer sizeList() { + return this.personList.size(); + } + + /* public SomePerson[] toArr() { + SomePerson[] p = new SomePerson[this.personList.size() - 1]; + for (int i = 0; i < p.length; i++) { + p[i] = this.personList.get(i + 1); + } + return p; + } +*/ + @Override + public Iterator iterator() { + return personList.iterator(); + } + + abstract public SomePerson[] toArray(); +} diff --git a/src/main/java/com/github/curriculeon/Person.java b/src/main/java/com/github/curriculeon/Person.java index 3c8350b..a88d30d 100644 --- a/src/main/java/com/github/curriculeon/Person.java +++ b/src/main/java/com/github/curriculeon/Person.java @@ -1,5 +1,25 @@ package com.github.curriculeon; public class Person { + final Long id; + public long getId() { + return id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + String name; + + Person(long id, String name){ + + this.id = id; + this.name=name; + } } diff --git a/src/main/java/com/github/curriculeon/Student.java b/src/main/java/com/github/curriculeon/Student.java new file mode 100644 index 0000000..7d04586 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Student.java @@ -0,0 +1,29 @@ +package com.github.curriculeon; + +/* +Create a Student class such that: +Student is a subclass of Person +Student implements the Learner interface +Student should have an instance variable totalStudyTime of type double +Student should have a concrete implementation of the learn method which increments the totalStudyTime variable by the specified numberOfHours argument. +Student should have a getTotalStudyTime() method which returns the totalStudyTime instance variable. + */ + +public class Student extends Person implements Learner { +Double totalStudyTime; + + Student(long id, String name) { + super(id, name); + this.totalStudyTime=0.0; + } + + @Override + public void learn(Double numOfHours) { + this.totalStudyTime=this.totalStudyTime+numOfHours; + } + + @Override + public Double getTotalStudyTime() { + return this.totalStudyTime; + } +} diff --git a/src/main/java/com/github/curriculeon/Students.java b/src/main/java/com/github/curriculeon/Students.java new file mode 100644 index 0000000..a825aa2 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Students.java @@ -0,0 +1,64 @@ +package com.github.curriculeon; +/* +Part 7.1 - Create Students singleton +Note: The creation of this class will demonstrate an implementation of singleton design pattern. +Create a Students class. (Done) +The class should be an unextendable subclass of the People class. (Done with final) +The class should statically instantiate a final field named INSTANCE of type Students. +The class should define a private nullary constructor which populates the INSTANCE field with respective +Student representations of your colleagues. +Each student should have a relatively unique id field. +The class should define a getInstance method which returns the INSTANCE field. +Part 7.0 - Test Students singleton +Create a TestStudents class. +Create a test method which ensures that each of the students in your current cohort are in your Students singleton. + + */ + + +import java.util.ArrayList; + +public class Students extends People { + // ArrayList personList=new ArrayList<>(); + final static Students instance = new Students(); + String [] cohort = new String[]{"David","Chris","Marcus","Leon","Mondira","Deepti","Steve","Yuru","Monica","Akila","Julia","David","Alonzo","Rachid","Emmanuel","Lionel","Solomon","People who don't speak"}; + private Students(){ + super(); + + for(Student student:peopleMaker(cohort)){ + this.add(student); + } + } + public static Students getInstance(){ + return instance; + } + + public ArrayList peopleMaker(String[] names){ + ArrayList stu =new ArrayList<>(); + for(int i =0;i