-
Notifications
You must be signed in to change notification settings - Fork 0
Lesson 05 #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Lesson 05 #5
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| package lessons.lesson05.educationCentre; | ||
|
|
||
| import java.util.ArrayList; | ||
|
|
||
| public class Course { | ||
| private String name; | ||
| private String teacher; | ||
| private ArrayList<Student> student = new ArrayList<>(); | ||
| private ArrayList<Student> attendanceJournal = new ArrayList<>(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Посещаемость можно реализовать как отдельный журнал где будет указан (id, course_id, student_id, attendance_date, status(enums)) |
||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public String getTeacher() { | ||
| return teacher; | ||
| } | ||
|
|
||
| public void setTeacher(String teacher) { | ||
| this.teacher = teacher; | ||
| } | ||
|
|
||
| public Course(String name, String teacher) { | ||
| this.name = name; | ||
| this.teacher = teacher; | ||
| } | ||
|
|
||
| public void addStudent(Student s) { | ||
| for (Student student1 : student) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Проверка дубликатов это хорошо, и как ее можно улучшить? Чтобы не перебирать весь список, можно использовать Set вместо List или же Map |
||
| if (student1.getEmail().equals(s.getEmail())) { | ||
| System.out.println("This student is already studying!"); | ||
| return; | ||
| } | ||
| } | ||
| student.add(s); | ||
| } | ||
|
|
||
| public void recordAttendance(Student s, boolean present) { | ||
| if (s.getEnrolledCourse().equals(name) && present) { | ||
| attendanceJournal.add(s); | ||
| System.out.println(s.getName() + " has attended!"); | ||
| } else { | ||
| System.out.println(s.getName() + " was absent."); | ||
| } | ||
| } | ||
|
|
||
| public void recordGrade(Student s, double grade) { | ||
| s.setGrades(grade); | ||
| System.out.println("Teacher set grade " + grade + " for " + s.getName()); | ||
| } | ||
|
|
||
| public void showStudents() { | ||
| System.out.println("The list of attendants of " + name + " course:"); | ||
| for (Student student1 : student) { | ||
| student1.studentInfo(); | ||
| } | ||
| System.out.println(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package lessons.lesson05.educationCentre; | ||
|
|
||
| public class MainEducation { | ||
| public static void main(String[] args) { | ||
| Teacher teacher = new Teacher("Yevgeny", 2309, "yevgeny@uzum.com"); | ||
| Course java = new Course("Java Junior", "none"); | ||
| Student student = new Student("Feruz", 25091, "feruz@uzum.com", "", 0); | ||
| Student student1 = new Student("Matvey", 25092, "matvey@uzum.com", java.getName(), 0); | ||
|
|
||
| student.enroll(java); | ||
| java.addStudent(student1); | ||
| java.showStudents(); | ||
| System.out.println(); | ||
|
|
||
| student.attendCourse(java); | ||
| java.recordAttendance(student, true); | ||
| System.out.println(); | ||
|
|
||
| teacher.assignCourse(java); | ||
| teacher.gradeStudent(student, java, 100.0); | ||
| java.showStudents(); | ||
|
|
||
| Student student2 = new Student("Feruz", 25091, "feruz@uzum.com", "", 0); | ||
| java.addStudent(student2); | ||
| java.showStudents(); | ||
|
|
||
|
|
||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package lessons.lesson05.educationCentre; | ||
|
|
||
| public abstract class Person { | ||
| private String name; | ||
| private int id; | ||
| private String email; | ||
|
|
||
| public Person(String name, int id, String email) { | ||
| this.name = name; | ||
| this.id = id; | ||
| this.email = email; | ||
| } | ||
|
|
||
| public String getInfo() { | ||
| return "Name: " + name + "\nID: " + id + "\nEmail: " + email; | ||
| } | ||
|
|
||
| public void introduce() { | ||
| System.out.println("Hello. My name is " + name); | ||
| } | ||
|
|
||
| public String getEmail() { | ||
| return email; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public int getId() { | ||
| return id; | ||
| } | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package lessons.lesson05.educationCentre; | ||
|
|
||
| public class Student extends Person { | ||
| private String enrolledCourse; | ||
| private double grades; | ||
|
|
||
| public String getEnrolledCourse() { | ||
| return enrolledCourse; | ||
| } | ||
|
|
||
| public double getGrades() { | ||
| return grades; | ||
| } | ||
|
|
||
| public void setGrades(double grades) { | ||
| this.grades = grades; | ||
| } | ||
|
|
||
| public Student(String name, int id, String email, String enrolledCourse, double grades) { | ||
| super(name, id, email); | ||
| this.enrolledCourse = enrolledCourse; | ||
| this.grades = grades; | ||
| } | ||
|
|
||
| public void enroll(Course course) { | ||
| enrolledCourse = course.getName(); | ||
| System.out.println(this.getName() + " enrolled " + enrolledCourse); | ||
| course.addStudent(this); | ||
| } | ||
|
|
||
| public void attendCourse(Course course) { | ||
| System.out.println("I attended!"); | ||
| course.recordAttendance(this, true); | ||
| } | ||
|
|
||
| public void studentInfo() { | ||
| System.out.println(getInfo()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package lessons.lesson05.educationCentre; | ||
|
|
||
| public class Teacher extends Person { | ||
| private String specialization; | ||
|
|
||
| public Teacher(String name, int id, String email) { | ||
| super(name, id, email); | ||
| } | ||
|
|
||
| public void assignCourse(Course course) { | ||
| specialization = course.getName(); | ||
| System.out.println(getName() + " is teacher of " + specialization); | ||
| course.setTeacher(this.getName()); | ||
| } | ||
|
|
||
| public void gradeStudent(Student student, Course course, double grade) { | ||
| if (this.getName().equals(course.getTeacher())) { | ||
| if (student.getEnrolledCourse().equals(course.getName())) { | ||
| student.setGrades(grade); | ||
| course.recordGrade(student, grade); | ||
| } else { | ||
| System.out.println(student.getName() + " is not studing in this course!"); | ||
| } | ||
| } else { | ||
| System.out.println(this.getName() + " is not teaching this course"); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package lessons.lesson05.onlineQuiz; | ||
|
|
||
| public class MainQuiz { | ||
| public static void main(String[] args) { | ||
| Quiz quiz = new Quiz("Java Basics"); | ||
| quiz.addQuestion(new MultipleChoiceQuestion("What is JVM?", "Java virtual machine", 10)); | ||
| quiz.addQuestion(new MultipleChoiceQuestion("What is the main purpose of the JVM?", "Execution", 10)); | ||
| quiz.addQuestion(new MultipleChoiceQuestion("What concept allows one class to inherit from another?", "Inheritance", 10)); | ||
| quiz.addQuestion(new MultipleChoiceQuestion("What keyword creates an instance of a class?", "New", 10)); | ||
| quiz.addQuestion(new MultipleChoiceQuestion("Which keyword is used to inherit a class in Java?\nA) implement\nB) inherits\nC) extends\nD) import", "C", 5)); | ||
|
|
||
| Team team = new Team("LABUBU", 0); | ||
| Team team1 = new Team("SPINER", 0); | ||
|
|
||
| quiz.addTeam(team); | ||
| quiz.addTeam(team1); | ||
|
|
||
| quiz.startQuiz(); | ||
|
|
||
| quiz.showResults(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package lessons.lesson05.onlineQuiz; | ||
|
|
||
| import java.util.ArrayList; | ||
|
|
||
| public class MultipleChoiceQuestion extends Question { | ||
| private ArrayList<String> options = new ArrayList<>(); | ||
|
|
||
| public MultipleChoiceQuestion(String text, String correctAnswer, int points) { | ||
| super(text, points); | ||
| setCorrectAnswer(correctAnswer); | ||
| } | ||
|
|
||
| @Override | ||
| public void ask() { | ||
| System.out.println("Answer for this question: " + getText() + "\nYou need to choose only one answer. (A-D)"); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean checkAnswer(String answer) { | ||
| return getCorrectAnswer().equalsIgnoreCase(answer); | ||
| } | ||
|
|
||
| // public ArrayList<String> getOptions() { | ||
| // return options; | ||
| // } | ||
| // | ||
| // public void setOptions(ArrayList<String> options) { | ||
| // this.options = options; | ||
| // } | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package lessons.lesson05.onlineQuiz; | ||
|
|
||
| public abstract class Question { | ||
| private String text; | ||
| private String correctAnswer; | ||
| private int points; | ||
|
|
||
| public Question(String text, int points) { | ||
| this.text = text; | ||
| this.points = points; | ||
| } | ||
|
|
||
| public void ask() { | ||
| System.out.println("Answer for this question: " + text); | ||
| } | ||
|
|
||
| public boolean checkAnswer(String answer) { | ||
| return correctAnswer.equalsIgnoreCase(answer); | ||
| } | ||
|
|
||
| public int getPoints() { | ||
| return points; | ||
| } | ||
|
|
||
| public void setPoints(int points) { | ||
| this.points = points; | ||
| } | ||
|
|
||
| public String getText() { | ||
| return text; | ||
| } | ||
|
|
||
| public String getCorrectAnswer() { | ||
| return correctAnswer; | ||
| } | ||
|
|
||
| public void setCorrectAnswer(String correctAnswer) { | ||
| this.correctAnswer = correctAnswer; | ||
| } | ||
|
|
||
| public void setText(String text) { | ||
| this.text = text; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package lessons.lesson05.onlineQuiz; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Quiz { | ||
| private String title; | ||
| private ArrayList<Question> questions = new ArrayList<>(); | ||
| private ArrayList<Team> teams = new ArrayList<>(); | ||
|
|
||
| public Quiz(String title) { | ||
| this.title = title; | ||
| } | ||
|
|
||
| public void addQuestion(Question question) { | ||
| questions.add(question); | ||
| } | ||
|
|
||
| public void addTeam(Team team) { | ||
| teams.add(team); | ||
| } | ||
|
|
||
| public void startQuiz() { | ||
| System.out.println("The Quiz of " + title + " is started! Two teams will be versus each other" + "\nEvery question has their individual points"); | ||
| Scanner sc = new Scanner(System.in); | ||
|
|
||
| for (int q = 0; q < questions.size(); q++) { | ||
| Question question = questions.get(q); | ||
|
|
||
| for (int t = 0; t < teams.size(); t++) { | ||
|
|
||
| Team currentTeam = teams.get(t % teams.size()); | ||
| System.out.println("Question " + q + " - answers team " + currentTeam.getName()); | ||
|
|
||
| System.out.println(question.getText()); | ||
| String answer = sc.nextLine().trim().toUpperCase(); | ||
| // if (answer.isEmpty()) { | ||
| // System.out.println("Please, input answer!"); | ||
| // continue; | ||
| // } | ||
|
|
||
| if (question.checkAnswer(answer)) { | ||
| currentTeam.addScore(question.getPoints()); | ||
| System.out.println("Correct! " + currentTeam.getName() + " got " + question.getPoints() + " points!"); | ||
| } else { | ||
| System.out.println("Wrong! 0 points."); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| public void showResults() { | ||
| System.out.println("\nQuiz results for " + "\"" + title + "\""); | ||
| int maxScore = 0; | ||
| for (Team team : teams) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. два одинаковых цикла не хорошо, лучше реализовать всю логику в одном цикле There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Понял, спасибо за комментарии |
||
| System.out.println("Team " + team.getName() + ": " + team.getScore() + " scores"); | ||
| if (team.getScore() > maxScore) { | ||
| maxScore = team.getScore(); | ||
| } | ||
| } | ||
| for (Team team : teams) { | ||
| if (team.getScore() == maxScore) { | ||
| System.out.println("The winner of " + "\"" + title + "\"" + " is " + team.getName()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package lessons.lesson05.onlineQuiz; | ||
|
|
||
| import lessons.lesson05.educationCentre.Student; | ||
|
|
||
| import java.util.ArrayList; | ||
|
|
||
| public class Team { | ||
| private String name; | ||
| private ArrayList<Student> members = new ArrayList<>(); | ||
| private int score; | ||
|
|
||
| public Team(String name, int score) { | ||
| this.name = name; | ||
| this.score = score; | ||
| } | ||
|
|
||
| public void addMember(Student student) { | ||
| members.add(student); | ||
| } | ||
|
|
||
| public void addScore(int points) { | ||
| score += points; | ||
| } | ||
|
|
||
| public int getScore() { | ||
| return score; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
списки всегда именуем во множественном числе