diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..61069b7a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/.idea/* \ No newline at end of file diff --git a/homework-java/pom.xml b/homework-java/pom.xml new file mode 100644 index 00000000..55057c41 --- /dev/null +++ b/homework-java/pom.xml @@ -0,0 +1,31 @@ + + + 4.0.0 + + com.example + homework-java + 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 + + + + + + \ No newline at end of file diff --git a/homework-java/src/main/java/com/example/CommandHandler.java b/homework-java/src/main/java/com/example/CommandHandler.java new file mode 100644 index 00000000..2c396f64 --- /dev/null +++ b/homework-java/src/main/java/com/example/CommandHandler.java @@ -0,0 +1,189 @@ +package com.example; +import java.util.*; + +public class CommandHandler { + private List students; + private List teachers; + private List courses; + + final String ROJO = "\u001B[31m"; + final String RESET = "\u001B[0m"; + + public CommandHandler(List students, List courses, List teachers) { + this.students = students; + this.courses = courses; + this.teachers = teachers; + } + + public boolean executeCommand(String wholeCommand){ + String[] commandPart = wholeCommand.split(" "); + String command = commandPart[0]; + switch (command){ + case "ENROLL": + enrollStudent(commandPart[1],commandPart[2]); + return true; + case "ASSIGN": + assignTeacher(commandPart[1],commandPart[2]); + return true; + case "SHOW": + //System.out.println(command); + handleShow(commandPart); + return true; + case "LOOKUP": + lookUp(commandPart); + return true; + default: + System.out.println("❌ Not a command."); + return false; + } + } + + public Student findStudentById(String student_id){ + Student student = null; + for(Student s: students){ + if(s.getStudentId().equals(student_id)){ + student = s; + return student; + } + } + return null; + } + + public Course findCourseById(String course_id){ + Course course = null; + for(Course c: courses){ + if(c.getCourseId().equals(course_id)){ + course = c; + return course; + } + } + return null; + } + + public Teacher findTeacherById(String teacher_id){ + Teacher teacher = null; + for(Teacher t: teachers){ + if(t.getTeacherId().equals(teacher_id)){ + teacher = t; + return teacher; + } + } + return null; + } + + private void lookUp(String[] commandPart) { + switch (commandPart[1]){ + case "COURSE": + Course course = findCourseById(commandPart[2]); + if (course != null) + System.out.println(course.toString()); + else + System.out.println("Course not found."); + break; + case "STUDENT": + Student student = findStudentById(commandPart[2]); + if (student != null) + System.out.println(student.toString()); + else + System.out.println("Student not found."); + break; + case "TEACHER": + Teacher teacher = findTeacherById(commandPart[2]); + if (teacher != null) + System.out.println(teacher.toString()); + else + System.out.println("Teacher not found."); + break; + } + } + + private void enrollStudent(String student_id, String course_id) { + Student student = findStudentById(student_id); + Course course = findCourseById(course_id); + + if (student != null && course!= null) { + student.setCourse(course); + updateMoneyEarned(course); + System.out.println(student.toString()); + } else { + System.out.println("Couldn't assign the course"); + } + + } + + public void assignTeacher(String teacher_id, String course_id){ + Teacher teacher = findTeacherById(teacher_id); + Course course = findCourseById(course_id); + + if (teacher != null && course!=null){ + course.setTeacher(teacher); + System.out.println(course.toString()); + }else { + System.out.println("Couldn't assign the course"); + } + } + + private void updateMoneyEarned(Course course){ + double total = course.getMoney_earned() + course.getPrice(); + course.setMoney_earned(total); + } + + private void handleShow(String[] command) { + //System.out.println(Arrays.toString(command) + "del handle"); + switch (command[1].toUpperCase()){ + case "COURSES": + showCourses(); + break; + case "STUDENTS": + showStudents(); + break; + case "TEACHERS": + showTeachers(); + break; + case "PROFIT": + showProfit(); + break; + } + } + + private void showStudents() { + //System.out.println("llego a show students"); + StringBuilder studentsString = new StringBuilder(); + for (Student student: students){ + studentsString.append(student.toString()).append("\n"); + } + // System.out.println("Student usando el toString() es:"); + System.out.println(studentsString); + } + + private void showTeachers() { + StringBuilder teachersString = new StringBuilder(); + for (Teacher teacher: teachers){ + teachersString.append(teacher.toString()).append("\n"); + } + System.out.println(teachersString); + } + + private void showProfit() { + double totalEarned = courses.stream().mapToDouble(Course::getMoney_earned).sum(); + double totalSalaries = teachers.stream().mapToDouble(Teacher::getSalary).sum(); + + + System.out.println(ROJO + "===== FINANCIAL REPORT =====" + RESET); + System.out.println("Total income from courses: $" + totalEarned); + System.out.println("Total expenses (teachers' salaries): $" + totalSalaries); + System.out.println("------------------------------"); + System.out.printf("Net profit: $%.2f%n", (totalEarned - totalSalaries)); + System.out.println(ROJO +"==============================" + RESET); + } + + private void showCourses() { + StringBuilder coursesString = new StringBuilder(); + for (Course course: courses){ + coursesString.append(course.toString()).append("\n"); + } + System.out.println(coursesString); + } + + +} diff --git a/homework-java/src/main/java/com/example/Course.java b/homework-java/src/main/java/com/example/Course.java new file mode 100644 index 00000000..89d2e492 --- /dev/null +++ b/homework-java/src/main/java/com/example/Course.java @@ -0,0 +1,96 @@ +package com.example; + +import java.util.HashSet; +import java.util.Random; +import java.util.Set; +import java.util.UUID; + +public class Course { + private static final Set usedIds = new HashSet<>(); + + private String courseId; + private String name; + private double price; + private double money_earned; + private Teacher teacher; + + //constructor de name y price + public Course(String name, double price) { + this.courseId = generateUniqueCourseId(); + this.name = name; + this.price = price; + } + //Constructor vacio con id + + public Course() { + this.courseId = generateUniqueCourseId(); + } + + private String generateUniqueCourseId() { + String id; + Random random = new Random(); + do { + int number = random.nextInt(900) + 100; + id = "C" + number; + } while (usedIds.contains(id)); + + usedIds.add(id); + return id; + } + + //getters y setters + + 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 String getCourseId() { + return courseId; + } + + public void setCourseId(String courseId) { + this.courseId = courseId; + } + + public double getMoney_earned() { + return money_earned; + } + + public void setMoney_earned(double money_earned) { + this.money_earned = money_earned; + } + + public Teacher getTeacher() { + return teacher; + } + + public void setTeacher(Teacher teacher) { + this.teacher = teacher; + } + + + @Override + public String toString() { + return String.format( + "πŸ“š Course Info:\n" + + "πŸ”’ ID : %s\n" + + "πŸ“– Name : %s\n" + + "πŸ’΅ Price : $%.2f\n" + + "πŸ’° Money Earned : $%.2f\n" + + "πŸ‘¨β€πŸ« Teacher : %s", + courseId, name, price, money_earned, teacher + ); + } +} diff --git a/homework-java/src/main/java/com/example/Main.java b/homework-java/src/main/java/com/example/Main.java new file mode 100644 index 00000000..182805d1 --- /dev/null +++ b/homework-java/src/main/java/com/example/Main.java @@ -0,0 +1,10 @@ +package com.example; + +//TIP To Run code, press or +// click the icon in the gutter. +public class Main { + public static void main(String[] args) { + +//comentario + } +} \ No newline at end of file diff --git a/homework-java/src/main/java/com/example/Menu.java b/homework-java/src/main/java/com/example/Menu.java new file mode 100644 index 00000000..4d262b8e --- /dev/null +++ b/homework-java/src/main/java/com/example/Menu.java @@ -0,0 +1,185 @@ +package com.example; + +import java.sql.ResultSet; +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class Menu { + private School school; + public static final String ROJO = "\u001B[31m"; + public static final String RESET = "\u001B[0m"; + + public static void main(String[] args) { + + + Scanner myScanner = new Scanner(System.in); + System.out.println("Please, enter a name for the new school: "); + String schoolName = myScanner.nextLine(); + var school = new School(schoolName); + + + System.out.println("Now, we need some "+ROJO+"TEACHER/S"+ RESET +" for your school " + schoolName + ". How wany teachers should be created? Please enter a number."); + + int numberOfTeachers = 0; + boolean validInput = false; + + while (!validInput) { + String input = myScanner.nextLine(); + try { + numberOfTeachers = Integer.parseInt(input); + if (numberOfTeachers < 0) { + System.out.println("The number of teachers cannot be negative. Please enter a valid positive integer."); + } else { + validInput = true; + } + } catch (NumberFormatException e) { + System.out.println("Invalid input. Please enter a valid whole number (e.g., 3) for the number of teachers."); + } + } + + + + for (int i = 0; i < numberOfTeachers; i++) { + System.out.println("\nEnter the details for teacher " + (i+1)); + + System.out.println("Name: "); + String name = myScanner.nextLine(); + + double salary = 0.0; + boolean validSalary = false; + + while (!validSalary) { + System.out.println("Salary: "); + String salaryInput = myScanner.nextLine(); + + try { + + salary = Double.parseDouble(salaryInput); + + if (salary <= 0) { + System.out.println("Salary must be a positive number greater than 0. Please try again."); + } else { + validSalary = true; + } + } catch (NumberFormatException e) { + System.out.println("Invalid input. Please enter a valid number (e.g., 199.99 or 150) for the price."); + } + + } + + Teacher teacher = new Teacher(name, salary); + school.addTeacher(teacher); + + System.out.println("Teacher " + (i+1) + " added."); + } + System.out.println(numberOfTeachers + "teachers created for " + schoolName); + + System.out.println("Now, we need some "+ROJO+"COURSE/S"+ RESET + " How wany should be created? Please enter a number."); + int numberOfCourses = 0; + boolean validInput2 = false; + + while (!validInput2) { + String input = myScanner.nextLine(); + try { + numberOfCourses = Integer.parseInt(input); + if (numberOfCourses < 0) { + System.out.println("The number of courses cannot be negative. Please enter a valid positive integer."); + } else { + validInput2 = true; + } + } catch (NumberFormatException e) { + System.out.println("Invalid input. Please enter a valid whole number (e.g., 3) for the number of courses."); + } + } + + + for (int i = 0; i < numberOfCourses; i++) { + System.out.println("\nEnter the details for course " + (i+1)); + + System.out.println("Name: "); + String name = myScanner.nextLine(); + + double price = 0.0; + boolean validPrice = false; + + while (!validPrice) { + System.out.println("Price: "); + String priceInput = myScanner.nextLine(); + + try { + price = Double.parseDouble(priceInput); + + if (price <= 0) { + System.out.println("Price must be a positive number greater than 0. Please try again."); + } else { + validPrice = true; + } + } catch (NumberFormatException e) { + System.out.println("Invalid input. Please enter a valid number (e.g., 199.99 or 150) for the price."); + } + } + + Course course = new Course(name, price); + school.addCourse(course); + + System.out.println("Course " + (i+1) + " added."); + } + System.out.println(numberOfCourses + "courses created for " + schoolName); + + System.out.println("Let's create the "+ROJO+"STUDENTS/S"+RESET+". How many should be created? Please enter a number."); + int numberOfStudents = 0; + boolean validInput3 = false; + + while (!validInput3) { + String input = myScanner.nextLine(); + try { + numberOfStudents = Integer.parseInt(input); + if (numberOfStudents < 0) { + System.out.println("The number of students cannot be negative. Please enter a valid positive integer."); + } else { + validInput3 = true; + } + } catch (NumberFormatException e) { + System.out.println("Invalid input. Please enter a valid whole number (e.g., 3) for the number of students."); + } + } + + + for (int i = 0; i < numberOfStudents; i++) { + System.out.println("\nEnter the details for student " + (i+1)); + + System.out.println("Name: "); + String name = myScanner.nextLine(); + + System.out.println("Address: "); + String address = myScanner.nextLine(); + + System.out.println("Email: "); + String email = myScanner.nextLine(); + + Student student = new Student(name, address, email); + school.addStudent(student); + + System.out.println("Student " + (i+1) + " added."); + } + System.out.println(numberOfStudents + "students created for " + schoolName); + + //comandos + boolean continueRunning = true; + while (continueRunning) { + System.out.println("Enter a command from the next list: \n ENROLL [STUDENT_ID] [COURSE_ID] \n ASSIGN [TEACHER_ID] [COURSE_ID] \n SHOW COURSES \n LOOKUP COURSE [COURSE_ID] \n SHOW STUDENTS \n LOOKUP STUDENT [STUDENT_ID] \n SHOW TEACHERS \n LOOKUP TEACHER [TEACHER_ID] \n SHOW PROFIT "); + var input = myScanner.nextLine().toUpperCase(); + + CommandHandler commandHandler = new CommandHandler (school.getStudents(), school.getCourses(), school.getTeachers()); + boolean validCommand = commandHandler.executeCommand(input); + if (!validCommand) { + System.out.println("Press any key to try it again..."); + myScanner.nextLine(); // Espera que el usuario pulse algo + continue; // vuelve al principio del while + } + + + } + } +} \ No newline at end of file diff --git a/homework-java/src/main/java/com/example/School.java b/homework-java/src/main/java/com/example/School.java new file mode 100644 index 00000000..6f953dd0 --- /dev/null +++ b/homework-java/src/main/java/com/example/School.java @@ -0,0 +1,41 @@ +package com.example; +import java.util.ArrayList; +import java.util.List; + +public class School { + private String name; + private List teachers; + private List students; + private List courses; + + public School(String name) { + this.name = name; + this.teachers = new ArrayList<>(); + this.students = new ArrayList<>(); + this.courses = new ArrayList<>(); + } + public void addTeacher (Teacher teacher){ + teachers.add(teacher); + } + public List getTeachers () { + return teachers; + } + + + public void addCourse (Course course){ + courses.add(course); + } + public List getCourses () { + return courses; + } + + + public void addStudent (Student student){ + students.add(student); + } + public List getStudents () { + return students; + } + +} + diff --git a/homework-java/src/main/java/com/example/Student.java b/homework-java/src/main/java/com/example/Student.java new file mode 100644 index 00000000..b61ca413 --- /dev/null +++ b/homework-java/src/main/java/com/example/Student.java @@ -0,0 +1,90 @@ +package com.example; +import java.util.HashSet; +import java.util.Random; +import java.util.Set; + + +public class Student { + private static final Set usedIds = new HashSet<>(); + + private final String studentId; + private String name; + private String address; + private String email; + private Course course; + + public Student(String name,String address,String email) { + this.studentId = generateUniqueStudentId(); + this.name = name; + this.address = address; + this.email = email; + this.course = null; + } + + public Student(){ + this.studentId = generateUniqueStudentId(); + } + + private String generateUniqueStudentId() { + String id; + Random random = new Random(); + do { + int number = random.nextInt(900) + 100; + id = "S" + number; + } while (usedIds.contains(id)); + + usedIds.add(id); + return id; + } + + public String getStudentId() { + return studentId; + } + + public Course getCourse() { + return course; + } + + public void setCourse(Course course) { + this.course = course; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public String getName() { + return name; + } + + public void setName(String name) { + if (!name.matches("[a-zA-ZΓ‘Γ©Γ­Γ³ΓΊΓΓ‰ΓΓ“ΓšΓ±Γ‘ ]+")) { + throw new IllegalArgumentException("El nombre no puede contener nΓΊmeros ni caracteres especiales."); + } + this.name = name; + } + + @Override + public String toString() { + return "\nπŸ“˜ Student Info\n" + + "------------------------\n" + + "πŸ†” ID : " + studentId + "\n" + + "πŸ‘€ Name : " + name + "\n" + + "🏠 Address : " + address + "\n" + + "πŸ“§ Email : " + email + "\n" + + "πŸ“š Course : " + (course != null ? getCourse().getName() : "Not enrolled") + "\n"; + } +} + diff --git a/homework-java/src/main/java/com/example/Teacher.java b/homework-java/src/main/java/com/example/Teacher.java new file mode 100644 index 00000000..ff178e04 --- /dev/null +++ b/homework-java/src/main/java/com/example/Teacher.java @@ -0,0 +1,65 @@ +package com.example; + +import java.util.*; + +public class Teacher { + private String teacherId; + private String name; + private double salary; + + + public Teacher (String name, double salary) { + this.teacherId = generateUniqueTeacherId(); + setName(name); + setSalary(salary); + } + public Teacher(){ + this.teacherId = generateUniqueTeacherId(); + } + private static final Set usedIds = new HashSet<>(); + private String generateUniqueTeacherId() { + String id; + Random random = new Random(); + do { + int number = random.nextInt(900) + 100; // genera nΓΊmero entre 100 y 999 + id = "T" + number; + } while (usedIds.contains(id)); + + usedIds.add(id); + return id; + } + + 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 String.format( + "πŸ‘¨β€πŸ« Teacher Info:\n" + + "πŸ†” ID : %s\n" + + "πŸ“› Name : %s\n" + + "πŸ’° Salary : $%.2f", + teacherId, name, salary + ); + } + + public void setCourse(Course course) { + } +} diff --git a/homework-java/src/test/java/CommandHandlerTest.java b/homework-java/src/test/java/CommandHandlerTest.java new file mode 100644 index 00000000..8f525f1b --- /dev/null +++ b/homework-java/src/test/java/CommandHandlerTest.java @@ -0,0 +1,60 @@ + +import com.example.CommandHandler; +import com.example.Course; +import com.example.Student; +import com.example.Teacher; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import java.util.List; +import static org.junit.jupiter.api.Assertions.*; + +public class CommandHandlerTest { + @Test + @DisplayName("Test sobre comando ENROLL") + public void testEnrollStudentUpdatesStudentAndCourse(){ + + Student student = new Student("Studiant Diez", "Calle Su casa 12", "mail@mail"); + Course course = new Course("Ethics", 20.00); + + var studentList = List.of(student); + var courseList = List.of(course); + List teacherList = List.of(); + + CommandHandler commandHandler = new CommandHandler(studentList, courseList, teacherList); + + String commandWithIds = "ENROLL "+ student.getStudentId()+ " " + course.getCourseId(); + System.out.println(commandWithIds); + + commandHandler.executeCommand(commandWithIds); + + assertEquals("Ethics", student.getCourse().getName()); + + } + + @Test + @DisplayName("Test sobre comando ASSIGN") + public void testAssignTeacher(){ + + Teacher teacher = new Teacher("Ana Paula", 20000); + Course course = new Course("Physics", 30.00); + + var teacherList = List.of(teacher); + var courseList = List.of(course); + List studentList = List.of(); + + CommandHandler commandHandler = new CommandHandler(studentList, courseList, teacherList); + + String commandWithIds = "ASSIGN "+ teacher.getTeacherId()+ " " + course.getCourseId(); + System.out.println(commandWithIds); + + commandHandler.executeCommand(commandWithIds); + + assertEquals("Ana Paula", teacher.getName()); + + } + + +} + + + diff --git a/homework-java/target/classes/com/example/CommandHandler.class b/homework-java/target/classes/com/example/CommandHandler.class new file mode 100644 index 00000000..4f27e610 Binary files /dev/null and b/homework-java/target/classes/com/example/CommandHandler.class differ diff --git a/homework-java/target/classes/com/example/Course.class b/homework-java/target/classes/com/example/Course.class new file mode 100644 index 00000000..cb3b69e4 Binary files /dev/null and b/homework-java/target/classes/com/example/Course.class differ diff --git a/homework-java/target/classes/com/example/Main.class b/homework-java/target/classes/com/example/Main.class new file mode 100644 index 00000000..3f9a731f Binary files /dev/null and b/homework-java/target/classes/com/example/Main.class differ diff --git a/homework-java/target/classes/com/example/Menu.class b/homework-java/target/classes/com/example/Menu.class new file mode 100644 index 00000000..f5250ae5 Binary files /dev/null and b/homework-java/target/classes/com/example/Menu.class differ diff --git a/homework-java/target/classes/com/example/School.class b/homework-java/target/classes/com/example/School.class new file mode 100644 index 00000000..72342c0b Binary files /dev/null and b/homework-java/target/classes/com/example/School.class differ diff --git a/homework-java/target/classes/com/example/Student.class b/homework-java/target/classes/com/example/Student.class new file mode 100644 index 00000000..075e98b2 Binary files /dev/null and b/homework-java/target/classes/com/example/Student.class differ diff --git a/homework-java/target/classes/com/example/Teacher.class b/homework-java/target/classes/com/example/Teacher.class new file mode 100644 index 00000000..3df0498b Binary files /dev/null and b/homework-java/target/classes/com/example/Teacher.class differ diff --git a/homework-java/target/test-classes/CommandHandlerTest.class b/homework-java/target/test-classes/CommandHandlerTest.class new file mode 100644 index 00000000..64a6bcf5 Binary files /dev/null and b/homework-java/target/test-classes/CommandHandlerTest.class differ