diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..91775a24 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea/* +IronSchool/target/* +out/* \ No newline at end of file diff --git a/IronSchool/pom.xml b/IronSchool/pom.xml new file mode 100644 index 00000000..9fc99c74 --- /dev/null +++ b/IronSchool/pom.xml @@ -0,0 +1,37 @@ + + + 4.0.0 + + org.example + IronSchool + 1.0-SNAPSHOT + + + 21 + 21 + UTF-8 + + + + org.junit.jupiter + junit-jupiter-engine + 5.9.2 + + + org.junit.jupiter + junit-jupiter-api + 5.9.2 + + + + org.mockito + mockito-core + 4.11.0 + + + + + + \ No newline at end of file diff --git a/IronSchool/src/main/java/Administration.java b/IronSchool/src/main/java/Administration.java new file mode 100644 index 00000000..80fcbc5e --- /dev/null +++ b/IronSchool/src/main/java/Administration.java @@ -0,0 +1,190 @@ +import java.util.Scanner; + +public class Administration { + private Registration registration = new Registration(); + + public Administration() { + } + + public static final String ANSI_RESET = "\u001B[0m"; + public static final String ANSI_RED = "\u001B[31m"; + public static final String ANSI_YELLOW = "\u001B[33m"; + public static final String ANSI_PURPLE = "\u001B[35m"; + public static final String ANSI_GREEN = "\u001B[32m"; + public static final String ANSI_RED_BACKGROUND = "\u001B[41m"; + public static final String ANSI_YELLOW_BACKGROUND = "\u001B[43m"; + public static final String ANSI_GREEN_BACKGROUND = "\u001B[42m"; + public static final String ANSI_CYAN_BACKGROUND = "\u001B[46m"; + public static final String ANSI_PURPLE_BACKGROUND = "\u001B[45m"; + + public void executeAdmid() { + + Scanner scanner = new Scanner(System.in); + System.out.println(ANSI_CYAN_BACKGROUND + "\uD83C\uDFEBIntroduce el nombre de la escuela:" + ANSI_RESET); + String nameSchool = scanner.nextLine(); + + scanTeachers(); + scanCourses(); + scanStudents(); + + scanCommands(); + } + + public void scanTeachers() { + Scanner scanner = new Scanner(System.in); + System.out.println(ANSI_PURPLE_BACKGROUND + "\uD83D\uDC68\u200D\uD83C\uDFEB¿Cuantos profesores quieres crear?:" + ANSI_RESET); + int numTeachers = scanner.nextInt(); + // Skip the newline + scanner.nextLine(); + for (int i = 0; i < numTeachers; i++) { + System.out.println(ANSI_PURPLE + "Introduce el nombre del profesor:" + ANSI_RESET); + String name = scanner.nextLine(); + + System.out.println(ANSI_PURPLE + "Introduce el salario del profesor:" + ANSI_RESET); + double salary = scanner.nextDouble(); + scanner.nextLine(); + + Teacher teacher = new Teacher(salary, name); +// System.out.println(teacher.toString()); + registration.addTeacher(teacher); + } + } + + public void scanCourses() { + Scanner scanner = new Scanner(System.in); + System.out.println(ANSI_GREEN_BACKGROUND + "\uD83D\uDCDA¿Cuantos cursos quieres crear?:" + ANSI_RESET); + int numCourses = scanner.nextInt(); + // Skip the newline + scanner.nextLine(); + for (int i = 0; i < numCourses; i++) { + System.out.println(ANSI_GREEN + "Introduce el nombre del curso:" + ANSI_RESET); + String name = scanner.nextLine(); + + System.out.println(ANSI_GREEN + "Introduce el precio del curso:" + ANSI_RESET); + double price = scanner.nextDouble(); + scanner.nextLine(); + + Course course = new Course(name, price); +// System.out.println(course.toString()); + registration.addCourse(course); + } + } + + public void scanStudents() { + Scanner scanner = new Scanner(System.in); + System.out.println(ANSI_YELLOW_BACKGROUND +"\uD83D\uDC69\u200D\uD83C\uDF93¿Cuantos estudiantes quieres crear?:" + ANSI_RESET); + int numStudents = scanner.nextInt(); + // Skip the newline + scanner.nextLine(); + for (int i = 0; i < numStudents; i++) { + System.out.println(ANSI_YELLOW + "Introduce el nombre del estudiante:" + ANSI_RESET); + String name = scanner.nextLine(); + + System.out.println(ANSI_YELLOW + "Introduce la dirección del estudiante:" + ANSI_RESET); + String address = scanner.nextLine(); + + System.out.println(ANSI_YELLOW + "Introduce el email del estudiante:" + ANSI_RESET); + String email = scanner.nextLine(); + + Student student = new Student(name, address, email); + System.out.println(student.toString()); + registration.addStudent(student); + } + } + + void scanCommands() { + Scanner scanner = new Scanner(System.in); + + boolean exit = false; + while (!exit) { + System.out.println("\n########################################################################"); + System.out.println("# INTRODUCE UN COMANDO VÁLIDO DE LA SIGUIENTE LISTA (EXIT PARA SALIR): #"); + System.out.println("########################################################################"); + printCommands(); + + String commandStr = scanner.nextLine(); + + if (commandStr.startsWith("ENROLL")) { + String[] commandSplited = commandStr.split(" "); + String studentId = replaceBrackets(commandSplited[1]); + String courseId = replaceBrackets(commandSplited[2]); +// System.out.println(studentId); +// System.out.println(courseId); + System.out.println("ejecutando ENROLL con los parametros: " + studentId + " " + courseId); + registration.enroll(studentId, courseId); + + } else if (commandStr.startsWith("ASSIGN")) { + String[] commandSplited = commandStr.split(" "); + String teacherId = replaceBrackets(commandSplited[1]); + String courseId = replaceBrackets(commandSplited[2]); +// System.out.println(teacherId); +// System.out.println(courseId); + System.out.println("ejecutando ASSIGN con los parametros: " + teacherId + " " + courseId); + registration.assing(teacherId, courseId); + + } else if (commandStr.startsWith("SHOW COURSES")) { + registration.showCourses(); + + } else if (commandStr.startsWith("LOOKUP COURSE")) { + String[] commandSplited = commandStr.split(" "); + String courseId = replaceBrackets(commandSplited[2]); +// System.out.println(courseId); + System.out.println("ejecutando LOOKUP COURSE con los parametros: " + courseId); + registration.lookupCourse(courseId); + + } else if (commandStr.startsWith("SHOW STUDENTS")) { + System.out.println("ejecutando SHOW STUDENTS:"); + registration.showStudents(); + + } else if (commandStr.startsWith("LOOKUP STUDENT")) { + String[] commandSplited = commandStr.split(" "); + String studentId = replaceBrackets(commandSplited[2]); +// System.out.println(studentId); + System.out.println("ejecutando LOOKUP STUDENT con los parametros: " + studentId); + registration.lookupStudent(studentId); + + } else if (commandStr.startsWith("SHOW TEACHERS")) { + System.out.println("ejecutando SHOW TEACHERS:"); + registration.showTeachers(); + + } else if (commandStr.startsWith("LOOKUP TEACHER")) { + String[] commandSplited = commandStr.split(" "); + String teacherId = replaceBrackets(commandSplited[2]); +// System.out.println(teacherId); + System.out.println("ejecutando LOOKUP TEACHER con los parametros: " + teacherId); + registration.lookupTeacher(teacherId); + + } else if (commandStr.startsWith("SHOW PROFIT")) { + System.out.println("ejecutando SHOW PROFIT:"); + registration.showProfit(); + + } else if (commandStr.startsWith("EXIT")) { + exit = true; + } + + } + } + + public String replaceBrackets(String idWithBrackets) { + String studentId = idWithBrackets.substring(1);//eliminamoso el principio + studentId = studentId.replaceFirst(".$", "");// eliminamos el final + System.out.println("studentId=" + studentId); + + return studentId; + } + + public void printCommands(){ + System.out.println("*********************************************"); + System.out.println(ANSI_RED_BACKGROUND + "*\uD83D\uDD04\tENROLL \uD83D\uDC68\u200D\uD83C\uDF93[STUDENT_ID] \uD83D\uDCDA[COURSE_ID]\t\t\t*" + ANSI_RESET); + System.out.println(ANSI_RED_BACKGROUND + "*\uD83D\uDD04\tASSIGN \uD83D\uDC69\u200D\uD83C\uDFEB[TEACHER_ID] \uD83D\uDCDA[COURSE_ID]\t\t\t*" + ANSI_RESET); + System.out.println(ANSI_RED_BACKGROUND + "*\uD83D\uDCDA\tSHOW COURSES\t\t\t\t\t\t\t*" + ANSI_RESET); + System.out.println(ANSI_RED_BACKGROUND + "*\uD83D\uDCDA\tLOOKUP COURSE [COURSE_ID]\t\t\t\t*" + ANSI_RESET); + System.out.println(ANSI_RED_BACKGROUND + "*\uD83D\uDC68\u200D\uD83C\uDF93\tSHOW STUDENTS\t\t\t\t\t\t\t*" + ANSI_RESET); + System.out.println(ANSI_RED_BACKGROUND + "*\uD83D\uDC68\u200D\uD83C\uDF93\tLOOKUP STUDENT [STUDENT_ID]\t\t\t\t*" + ANSI_RESET); + System.out.println(ANSI_RED_BACKGROUND + "*\uD83D\uDC69\u200D\uD83C\uDFEB\tSHOW TEACHERS\t\t\t\t\t\t\t*" + ANSI_RESET); + System.out.println(ANSI_RED_BACKGROUND + "*\uD83D\uDC69\u200D\uD83C\uDFEB\tLOOKUP TEACHER [TEACHER_ID]\t\t\t\t*" + ANSI_RESET); + System.out.println(ANSI_RED_BACKGROUND + "*\uD83D\uDCB5\tSHOW PROFIT\t\t\t\t\t\t\t\t*" + ANSI_RESET); + System.out.println(ANSI_RED_BACKGROUND + "*\tEXIT\t\t\t\t\t\t\t\t\t*" + ANSI_RESET); + System.out.println("*********************************************"); + } +} diff --git a/IronSchool/src/main/java/Course.java b/IronSchool/src/main/java/Course.java new file mode 100644 index 00000000..70503b0d --- /dev/null +++ b/IronSchool/src/main/java/Course.java @@ -0,0 +1,70 @@ +import java.util.UUID; + +public class Course { + private String courseId = UUID.randomUUID().toString().substring(0,4); + private String name; + private double price; + private double moneyEarned; + private Teacher teacher; + + public Course(String name, double price) { + setName(name); + if (price < 0.0) { + throw new IllegalArgumentException("Error el precio no puede ser nulo"); + } else { + setPrice(price); + } + } + + public String getCourseId() { + return courseId; + } + + public void setCourseId(String courseId) { + this.courseId = courseId; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public double getPrice() { + return price; + } + + public void setPrice(double price) { + this.price = price; + } + + public double getMoneyEarned() { + return moneyEarned; + } + + public void setMoneyEarned(double moneyEarned) { + + this.moneyEarned = moneyEarned; + } + + public Teacher getTeacher() { + return teacher; + } + + public void setTeacher(Teacher teacher) { + this.teacher = teacher; + } + + @Override + public String toString() { + return "Course{" + + "courseId='" + courseId + '\'' + + ", name='" + name + '\'' + + ", price=" + price + + ", moneyEarned=" + moneyEarned + + ", teacher=" + teacher + + '}'; + } +} diff --git a/IronSchool/src/main/java/IRegistration.java b/IronSchool/src/main/java/IRegistration.java new file mode 100644 index 00000000..9d429734 --- /dev/null +++ b/IronSchool/src/main/java/IRegistration.java @@ -0,0 +1,19 @@ +public interface IRegistration { + void enroll(String studentId, String courseId); + + void assing(String teacherId, String courseId); + + void showCourses(); + + void lookupCourse(String courseId); + + void showStudents(); + + void lookupStudent(String studentId); + + void showTeachers(); + + void lookupTeacher(String teacherId); + + void showProfit(); +} diff --git a/IronSchool/src/main/java/Main.java b/IronSchool/src/main/java/Main.java new file mode 100644 index 00000000..2a308d8f --- /dev/null +++ b/IronSchool/src/main/java/Main.java @@ -0,0 +1,7 @@ +public class Main { + + public static void main(String[] args) { + Administration administration = new Administration(); + administration.executeAdmid(); + } +} diff --git a/IronSchool/src/main/java/Registration.java b/IronSchool/src/main/java/Registration.java new file mode 100644 index 00000000..b27d981f --- /dev/null +++ b/IronSchool/src/main/java/Registration.java @@ -0,0 +1,166 @@ +import java.util.ArrayList; +import java.util.List; + +// datos y comandos + +public class Registration implements IRegistration { + + private List studentList = new ArrayList<>(); + private List courseList= new ArrayList<>(); + private List teacherList= new ArrayList<>(); + + public Registration(){ + } + + public Registration(List studentList, List courseList, List teacherList) { + this.studentList = studentList; + this.courseList = courseList; + this.teacherList = teacherList; + } + + + @Override + public void enroll(String studentId, String courseId) { + Student student = null; + for (Student s : studentList) { + if (s.getStudentId().equals(studentId)) { + student = s; + break; + } + } + + Course course = null; + for (Course c : courseList) { + if (c.getCourseId().equals(courseId)) { + course = c; + break; + } + } + + if (student == null || course == null) { + throw new IllegalArgumentException("Student or Course not found."); + } + + student.setCourse(course); + double totalMoneyErned = course.getMoneyEarned() + course.getPrice(); + course.setMoneyEarned(totalMoneyErned); + System.out.println("Student enrolled in course."); + + } + + @Override + public void assing(String teacherId, String courseId) { + Teacher teacher = null; + for (Teacher teac : this.teacherList) { + if (teac.getTeacherId().equals(teacherId)) { + teacher = teac; + break; + } + } + + Course course = null; + for (Course cour : this.courseList) { + if (cour.getCourseId().equals(courseId)) { + course = cour; + break; + } + } + + if (teacher != null && course != null) { + course.setTeacher(teacher); + System.out.println("Teacher with ID " + teacherId + " assigned to course with ID " + courseId + "."); + } else { + System.out.println("The teacher could not be assigned to the course."); + } + } + + @Override + public void showCourses() { + for(Course course: courseList){ + System.out.println(course); + } + } + + @Override + public void lookupCourse(String courseId) { + if(courseId == null) { + throw new IllegalArgumentException("CourseId no puede ser null"); + } + + for(Course course: courseList){ + if(course.getCourseId().equals(courseId)){ + System.out.println(course); + break; + } + } + + } + + @Override + public void showStudents() { + for(Student student: studentList){ + System.out.println(student); + } + } + + @Override + public void lookupStudent(String studentId) { + if(studentId == null) { + throw new IllegalArgumentException("StudentId no puede ser null"); + } + + for(Student student: studentList){ + if(student.getStudentId().equals(studentId)){ + System.out.println(student); + break; + } + } + } + + @Override + public void showTeachers() { + for(Teacher teacher: teacherList) { + System.out.println(teacher); + } + } + + @Override + public void lookupTeacher(String teacherId) { + if(teacherId == null) { + throw new IllegalArgumentException("teacherId no puede ser null"); + } + + for(Teacher teacher: teacherList){ + if(teacher.getTeacherId().equals(teacherId)){ + System.out.println(teacher); + break; + } + } + } + + @Override + public void showProfit() { + double totalMoneyEarned = 0; + for(Course coursePrice: courseList){ + totalMoneyEarned += coursePrice.getMoneyEarned(); + } + double totalTeacherSalary = 0; + for(Teacher teacher: teacherList){ + totalTeacherSalary += teacher.getSalary(); + } + System.out.println("Total Money Earned: " + totalMoneyEarned); + System.out.println("Total Teacher Salary: " + totalTeacherSalary); + } + + public void addTeacher(Teacher teacher) { + this.teacherList.add(teacher); + } + + public void addCourse(Course course){ + this.courseList.add(course); + } + + public void addStudent(Student student) { + this.studentList.add(student); + } +} diff --git a/IronSchool/src/main/java/Student.java b/IronSchool/src/main/java/Student.java new file mode 100644 index 00000000..bb98987f --- /dev/null +++ b/IronSchool/src/main/java/Student.java @@ -0,0 +1,69 @@ +import java.util.UUID; + +public class Student { + private String studentId; + private String name; + private String address; + private String email; + private Course course; + + public Student(String name, String address, String email) { + this.studentId = UUID.randomUUID().toString().substring(0,4); + this.name = name; + this.address = address; + this.email = email; + } + + public Student(String studentId){ + this.studentId = studentId; + } + + + public String getStudentId() { + return studentId; + } + + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public Course getCourse() { + return course; + } + + public void setCourse(Course course) { + this.course = course; + } + + @Override + public String toString() { + return "Student{" + + "studentId='" + studentId + '\'' + + ", name='" + name + '\'' + + ", address='" + address + '\'' + + ", email='" + email + '\'' + + ", course=" + course + + '}'; + } +} diff --git a/IronSchool/src/main/java/Teacher.java b/IronSchool/src/main/java/Teacher.java new file mode 100644 index 00000000..851bdebb --- /dev/null +++ b/IronSchool/src/main/java/Teacher.java @@ -0,0 +1,46 @@ +import java.util.UUID; + +public class Teacher { + private String teacherId; + private String name; + private double salary; + + public Teacher(double salary, String name) { + this.teacherId = UUID.randomUUID().toString().substring(0,4); + this.salary = salary; + this.name = name; + } + + public Teacher(String teacherId) { + this.teacherId = teacherId; + } + + public String getTeacherId() { + return teacherId; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public double getSalary() { + return salary; + } + + public void setSalary(double salary) { + this.salary = salary; + } + + @Override + public String toString() { + return "Teacher{" + + "teacherId='" + teacherId + '\'' + + ", name='" + name + '\'' + + ", salary=" + salary + + '}'; + } +} diff --git a/IronSchool/src/test/java/CourseTest.java b/IronSchool/src/test/java/CourseTest.java new file mode 100644 index 00000000..5cee5a63 --- /dev/null +++ b/IronSchool/src/test/java/CourseTest.java @@ -0,0 +1,5 @@ +import static org.junit.jupiter.api.Assertions.*; + +class CourseTest { + +} \ No newline at end of file diff --git a/IronSchool/src/test/java/RegistrationTest.java b/IronSchool/src/test/java/RegistrationTest.java new file mode 100644 index 00000000..0c9f81c7 --- /dev/null +++ b/IronSchool/src/test/java/RegistrationTest.java @@ -0,0 +1,45 @@ +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +class RegistrationTest { + @Test + public void assign() { + List studentList = new ArrayList<>(); + List courseList = new ArrayList<>(); + List teacherList = new ArrayList<>(); + + Teacher teacher1 = new Teacher(1200, "Jaime"); + Course course1 = new Course("Ingles", 100); + + teacherList.add(teacher1); + courseList.add(course1); + + Registration registration = new Registration(studentList, courseList, teacherList); + registration.assing(teacher1.getTeacherId(), course1.getCourseId()); + + assertEquals(teacher1, course1.getTeacher(), "The teacher could not be assigned to the course"); + + } + + @Test + public void lookupStudent() { + List studentList = new ArrayList<>(); + List courseList = new ArrayList<>(); + List teacherList = new ArrayList<>(); + + Student student1 = new Student("Pedro", "Calle las manzanas", "Pedro@gpedro.com"); + + studentList.add(student1); + + Registration registration = new Registration(studentList, courseList, teacherList); + registration.lookupStudent(student1.getStudentId()); + + assertNotNull(student1,"Student found"); + } + +} \ No newline at end of file diff --git a/IronSchool/src/test/java/StudentTest.java b/IronSchool/src/test/java/StudentTest.java new file mode 100644 index 00000000..ad9f7d6b --- /dev/null +++ b/IronSchool/src/test/java/StudentTest.java @@ -0,0 +1,5 @@ +import static org.junit.jupiter.api.Assertions.*; + +class StudentTest { + +} \ No newline at end of file diff --git a/IronSchool/src/test/java/TeacherTest.java b/IronSchool/src/test/java/TeacherTest.java new file mode 100644 index 00000000..1d564646 --- /dev/null +++ b/IronSchool/src/test/java/TeacherTest.java @@ -0,0 +1,5 @@ +import static org.junit.jupiter.api.Assertions.*; + +class TeacherTest { + +} \ No newline at end of file