diff --git a/homework02/.gitignore b/homework02/.gitignore new file mode 100644 index 00000000..d62a16b7 --- /dev/null +++ b/homework02/.gitignore @@ -0,0 +1,39 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ +.idea/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/README.md b/homework02/README.md similarity index 100% rename from README.md rename to homework02/README.md diff --git a/homework02/TRAKING.txt b/homework02/TRAKING.txt new file mode 100644 index 00000000..50ae0bf0 --- /dev/null +++ b/homework02/TRAKING.txt @@ -0,0 +1,22 @@ +Friday_21/02/2025 + +We have decided that homework is done by each one. +Álvaro has selected: +-the class Course +-fuctions: ENROLL, LOOKUP COURSE, SHOW TEACHERS +Moreover, Álvaro is goint to presente the homework and he is going to be the main coder. + +Fernando has selected: +-the class Teacher +-fuctions: SHOW COURSES, LOOKUP STUDENT, SHOW PROFIT + +Esteban has selected: +-the class Student +-fuctions: ASSIGN, SHOW STUDENTS, LOOKUP TEACHER +-the menú + + +This time, we are goint to create three branch: +-Álvaro is going to work in main01 +-Fernando is going to work in main02 +-Esteban is going to work in main03 diff --git a/homework02/pom.xml b/homework02/pom.xml new file mode 100644 index 00000000..73f45724 --- /dev/null +++ b/homework02/pom.xml @@ -0,0 +1,25 @@ + + + 4.0.0 + + org.example + homework02 + 1.0-SNAPSHOT + + + 17 + 17 + UTF-8 + + + + org.junit.jupiter + junit-jupiter + RELEASE + test + + + + \ No newline at end of file diff --git a/homework02/src/main/java/com/ironhack/CommandFunction.java b/homework02/src/main/java/com/ironhack/CommandFunction.java new file mode 100644 index 00000000..6d2191b2 --- /dev/null +++ b/homework02/src/main/java/com/ironhack/CommandFunction.java @@ -0,0 +1,331 @@ +package com.ironhack; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class CommandFunction { + + public CommandFunction() { + } + + //Change the empty method. + public void showProfit() { + double totalMoneyCourses = 0; + double totalSalaries = 0; + + // Total money earned from all courses + for (Course course : Main.courseList) { + totalMoneyCourses += course.getMoneyEarned(); + } + + // Sum of all teacher's salaries + for (Teacher teacher : Main.teacherList) { + totalSalaries += teacher.getSalary(); + } + + double totalProfit = totalMoneyCourses - totalSalaries; + if (totalProfit < 0) { + System.out.println("The profit is: " + Style.ERROR + totalProfit + Style.RESET + "€"); + } else { + System.out.println("The profit is: " + Style.SALARY + totalProfit + Style.RESET + "€"); + } + } + + public void showCourses() { + String result = ""; + // If no courses in list + if (Main.courseList.isEmpty()) { + result = Style.NOT_FOUND + "There are no courses available." + Style.RESET; + } else { + for (Course course : Main.courseList) { + // Append course to the result string + result += course.toString() + "\n"; + } + } + System.out.println(result); + } + + public void showStudents() { + System.out.println(); + + if (Main.studentList.isEmpty()) { + System.out.println(Style.NOT_FOUND + "There are no students at the school." + Style.RESET); + } else { + for (Student student : Main.studentList) { + System.out.println(student.toString()); + } + } + } + + public void showTeachers() { + System.out.println(); + + if (Main.teacherList.isEmpty()) { + System.out.println(Style.NOT_FOUND + "There are no teachers at the school." + Style.RESET); + } else { + for (Teacher teacher : Main.teacherList) { + System.out.println(teacher.toString()); + } + } + } + + public void lookupCourse(String courseId){ + boolean found = false; + String result = ""; + // If no students in list + if (Main.courseList.isEmpty()) { + result = Style.NOT_FOUND + "There are no courses." + Style.RESET; + } else { + for (Course course : Main.courseList) { + if (course.getCourseId().equals(courseId)) { + // Store found student info + result = course.toString(); + found = true; + break; + } + } + // If not found + if (!found) { + result = Style.NOT_FOUND + "Not Found course with ID ( " + courseId + " )." + Style.RESET; + } + } + System.out.println(result); + } + + public void lookupStudent(String studentId) { + boolean found = false; + String result = ""; + // If no students in list + if (Main.studentList.isEmpty()) { + result = Style.NOT_FOUND + "There are no students." + Style.RESET; + } else { + for (Student student : Main.studentList) { + if (student.getStudentId().equals(studentId)) { + // Store found student info + result = student.toString(); + found = true; + break; + } + } + // If not found + if (!found) { + result = Style.NOT_FOUND + "Not Found student with ID ( " + studentId + " )." + Style.RESET; + } + } + System.out.println(result); + } + + public void lookupTeacher(String teacherId) { + boolean isFound = false; + String result = ""; + if (Main.teacherList.isEmpty()) { + System.out.println(Style.NOT_FOUND + "There are no teachers." + Style.RESET); + } else { + for (Teacher teacher : Main.teacherList) { + if (teacher.getTeacherId().equals(teacherId)) { + // Store found teacher info + result=teacher.toString(); + isFound = true; + break; + } + } + // If not found + if (!isFound) { + result=Style.NOT_FOUND + "Not Found teacher with ID ( " + teacherId + " )." + Style.RESET; + } + } + System.out.println(result); + } + + public void enroll(String studentId, String courseId) { + boolean isFoundStudent = false; + boolean isFoundCourse = false; + int j=0; + int k=0; + + if (Main.studentList.isEmpty() || Main.courseList.isEmpty()) { + System.out.println(Style.ERROR + "Something went wrong." + Style.RESET); + } else { + for (int i = 0; i teacherList = new ArrayList<>(); + public static List courseList = new ArrayList<>(); + public static List studentList = new ArrayList<>(); + + public static void main(String[] args) { + //Menu + InitialMenu initialMenu = new InitialMenu(); + ServerMenu serverMenu = new ServerMenu(); + + initialMenu.startMenu(); + initialMenu.numberTeacher(); + initialMenu.createTeacher(); + initialMenu.numberCourse(); + initialMenu.createCourse(); + initialMenu.numberStudent(); + initialMenu.createStudent(); + + + serverMenu.showMenu(); + boolean res = true; + do{ + res = serverMenu.selectCommand(); + }while(res); + + serverMenu.closeScanner(); + + } +} \ No newline at end of file diff --git a/homework02/src/main/java/com/ironhack/ServerMenu.java b/homework02/src/main/java/com/ironhack/ServerMenu.java new file mode 100644 index 00000000..596bb44d --- /dev/null +++ b/homework02/src/main/java/com/ironhack/ServerMenu.java @@ -0,0 +1,156 @@ +package com.ironhack; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class ServerMenu { + Scanner scanner = new Scanner(System.in); + public ServerMenu() { + } + + public void showMenu() { + System.out.println(Style.SUCCESS+Style.BOLD + "\nFantastic! All data introduced is correct. Then, we provide a command list " + + "for managing or adding a new information." + Style.RESET); + System.out.println(Style.BOLD + "\n--------------------------------- COMMAND LIST ---------------------------------\n" + Style.RESET); + System.out.println( + "- "+Style.SUCCESS_BG+Style.BOLD+" ENROLL [STUDENT_ID] [COURSE_ID] "+Style.RESET+" "+Style.HIGHLIGHT+"Example: ENROLL S3 C6" + Style.RESET + + "\n- "+Style.SUCCESS_BG+Style.BOLD+" ASSIGN [TEACHER_ID] [COURSE_ID] "+Style.RESET+" "+Style.HIGHLIGHT+"Example: ASSIGN T2 C3" + Style.RESET + + "\n- "+Style.SUCCESS_BG+Style.BOLD+" SHOW COURSES " +Style.RESET+ + "\n- "+Style.SUCCESS_BG+Style.BOLD+" LOOKUP COURSE [COURSE_ID] "+ Style.RESET+" "+Style.HIGHLIGHT+"Example: LOOKUP COURSE C8" + Style.RESET + + "\n- "+Style.SUCCESS_BG+Style.BOLD+" SHOW STUDENTS " +Style.RESET+ + "\n- "+Style.SUCCESS_BG+Style.BOLD+" LOOKUP STUDENT [STUDENT_ID] "+ Style.RESET+" "+Style.HIGHLIGHT+"Example: LOOKUP STUDENT S5" + Style.RESET + + "\n- "+Style.SUCCESS_BG+Style.BOLD+" SHOW TEACHERS " +Style.RESET+ + "\n- "+Style.SUCCESS_BG+Style.BOLD+" LOOKUP TEACHER [TEACHER_ID] "+ Style.RESET+" "+Style.HIGHLIGHT+"Example: LOOKUP TEACHER T10" + Style.RESET + + "\n- "+Style.SUCCESS_BG+Style.BOLD+" SHOW PROFIT " +Style.RESET+ + "\n- "+Style.SUCCESS_BG+Style.BOLD+" SHOW MONEY EARNED" +Style.RESET+ + "\n- "+Style.SUCCESS_BG+Style.BOLD+" SHOW MONEY SPENT" +Style.RESET+ + "\n- "+Style.SUCCESS_BG+Style.BOLD+" SHOW ENLISTED [COURSE_ID]" +Style.RESET+ + "\n- "+Style.SUCCESS_BG+Style.BOLD+" SHOW ASSIGNED [COURSE_ID]" +Style.RESET+ + "\n- " + Style.HIGHLIGHT + " EXIT" + Style.RESET); + System.out.println(Style.BOLD + "\n--------------------------------------------------------------------------------" + Style.RESET); + } + + public boolean selectCommand(){ + boolean option = true; + + System.out.println("\nPlease introduce a command:"); + String cmd = scanner.nextLine(); + cmd = cmd.toUpperCase(); + if(cmd.equals("EXIT")){ + option = false; + scanner.close(); + }else{ + try{ + sanitize(cmd); + } catch (Exception e) { + //System.out.println("Command not recognized, please try again."); + System.out.println(e.getMessage()); + } + } + //scanner.close(); + return option; + } + + public void sanitize(String args){ + //boolean sanitize = false; + String[] values = args.split( " "); + if (values.length > 3 && !values[0].equals("EXIT")) throw new IllegalArgumentException(Style.ERROR + "Too many arguments." + Style.RESET); + if (values.length < 2 && !values[0].equals("EXIT")) throw new IllegalArgumentException(Style.ERROR + "Too few arguments." + Style.RESET); + + CommandFunction command = new CommandFunction(); + + // COMMANDO ENROLL + if(values.length == 3 && values[0].equals("ENROLL")){ + if(isValidStudentId(values[1]) && isValidCourseId(values[2])){ + command.enroll(values[1],values[2]); + }else{ + throw new IllegalArgumentException(Style.ERROR + "Parameters for ENROLL are incorrect." + Style.RESET); + } + // COMMANDO ASSIGN + }else if (values.length == 3 && values[0].equals("ASSIGN")){ + if(isValidTeacherId(values[1]) && isValidCourseId(values[2])){ + command.assign(values[1],values[2]); + }else{ + throw new IllegalArgumentException(Style.ERROR + "Parameters for ASSIGN are incorrect." + Style.RESET); + } + // COMMANDO SHOW + }else if(values[0].equals("SHOW") && values.length == 2){ + if(values[1].equals("COURSES")){ + command.showCourses(); + }else if(values[1].equals("STUDENTS")){ + command.showStudents(); + }else if(values[1].equals("TEACHERS")){ + command.showTeachers(); + }else if(values[1].equals("PROFIT")){ + command.showProfit(); + }else{ + throw new IllegalArgumentException(Style.ERROR + "Invalid parameters for SHOW." + Style.RESET); + } + // COMMANDO LOOKUP + }else if(values[0].equals("LOOKUP") && values.length == 3) { + if (values[1].equals("STUDENT") && isValidStudentId(values[2])) { + command.lookupStudent(values[2]); + } else if (values[1].equals("COURSE") && isValidCourseId(values[2])) { + command.lookupCourse(values[2]); + } else if (values[1].equals("TEACHER") && isValidTeacherId(values[2])) { + command.lookupTeacher(values[2]); + } else { + throw new IllegalArgumentException(Style.ERROR + "Invalid parameters for LOOKUP." + Style.RESET); + } + //showStudentsByCourse and showTeachersByCourse + }else if(values[0].equals("SHOW") && values.length == 3){ + if(values[1].equals("ENLISTED") && isValidCourseId(values[2])){ + command.showStudentsByCourse(values[2]); + }else if(values[1].equals("ASSIGNED") && isValidCourseId(values[2])){ + command.showTeachersByCourse(values[2]); + }else if(values[1].equals("MONEY") && values[2].equals("SPENT")){ + command.showMoneySpent(); + }else if(values[1].equals("MONEY") && values[2].equals("EARNED")){ + command.showMoneyEarned(); + }else{ + throw new IllegalArgumentException("Invalid parameters for SHOW"); + } + }else{ + throw new IllegalArgumentException(Style.ERROR + "Invalid Command." + Style.RESET); + } + //return sanitize; + } + + public boolean isValidStudentId(String value) { + boolean result = false; + for(Student s: Main.studentList){ + if(s.getStudentId().equals(value)){ + result = true; + break; + } + } + return result; + } + + public boolean isValidCourseId(String value) { + boolean result = false; + for(Course c: Main.courseList){ + if(c.getCourseId().equals(value)){ + result = true; + break; + } + } + return result; + } + + public boolean isValidTeacherId(String value) { + boolean result = false; + for (Teacher t : Main.teacherList) { + if (t.getTeacherId().equals(value)) { + result = true; + break; + } + } + return result; + } + + public void closeScanner() { + this.scanner.close(); + } +} \ No newline at end of file diff --git a/homework02/src/main/java/com/ironhack/Student.java b/homework02/src/main/java/com/ironhack/Student.java new file mode 100644 index 00000000..faec9042 --- /dev/null +++ b/homework02/src/main/java/com/ironhack/Student.java @@ -0,0 +1,100 @@ +package com.ironhack; + +import java.util.regex.Pattern; + +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 = "S" + Main.counterStudent++; + this.name = name; + this.address = address; + this.email = email; + this.course=null; + } + + + +// Delete comment. This is for auto-generate the id. +// Student myStudent1 = new Student("Ernest","we","wedfv"); +// Student myStudent2 = new Student("Erest","we34r","wsdedfv"); +// +// System.out.println(myStudent1.getStudentId()); +// System.out.println(myStudent2.getStudentId()); + + public String getStudentId() { + return studentId; + } + + public void setStudentId(String studentId) { + this.studentId = 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; + } + + // Set email with validation + public void setEmail(String email) { + if (isValidEmail(email)) { + this.email = email; + } else { + throw new IllegalArgumentException("Invalid email format. Please use a valid format like 'example@mail.com'."); + } + } + + //Code Fernando + private boolean isValidEmail(String email) { + String emailRegex = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"; + Pattern pattern = Pattern.compile(emailRegex); + return pattern.matcher(email).matches(); + } + + public Course getCourse() { + return course; + } + + public void setCourse(Course course) { + this.course = course; + } + + @Override + public String toString() { + String courseText; + try{ + courseText = this.course.getName(); + } catch (Exception e) { + courseText = "enrollment pending"; + } + return " /b_,dM\\__,_\n" + + " _YMMMMMMMMMMMM( STUDENT\n" + + " `MMMMMM/ / \\ "+name+"\n" + + " MMM| __ / __/ \n" + + " YMM/_/# \\__/# \\ Lives at: "+address+"\n" + + " (. \\__/ \\__/ Email: "+email+" \n" + + " ) _, | Enrolled in: "+ courseText +"\n" + + "_____/\\ _ / \n" + + " \\ `._____, STUDENT ID: "+studentId+" \n"+ + " `..___(__\n"; + } +} diff --git a/homework02/src/main/java/com/ironhack/Style.java b/homework02/src/main/java/com/ironhack/Style.java new file mode 100644 index 00000000..8fd9cf46 --- /dev/null +++ b/homework02/src/main/java/com/ironhack/Style.java @@ -0,0 +1,35 @@ +package com.ironhack; + +public class Style { + // ANSI Code + public static final String RESET = "\u001B[0m"; + public static final String BOLD = "\u001B[1m"; + public static final String UNDERLINE = "\u001B[4m"; + public static final String REVERSED = "\u001B[7m"; // Invierte los colores + + // RGB COLORS + public static final String SCHOOL = rgbColor(70, 130, 255); + public static final String TEACHER = rgbColor(186, 85, 211); + public static final String COURSE = rgbColor(255, 165, 50); + public static final String STUDENT = rgbColor(0, 179, 168); + public static final String HIGHLIGHT = rgbColor(255, 255, 100); + public static final String ERROR = rgbColor(255, 99, 71); + public static final String SUCCESS = rgbColor(144, 238, 144); + public static final String SALARY = rgbColor(179, 173, 0); + public static final String NOT_FOUND = rgbColor(255, 165, 50); + + // RGB BACKGROUND +// public static final String SUCCESS_BG = rgbBackground(64, 93, 72); + public static final String SUCCESS_BG = rgbBackground(58, 58, 58); +// public static final String SUCCESS_BG = rgbBackground(30, 30, 30); // CONSOLE COLOR + public static final String WARNING_BG = rgbBackground(255, 255, 102); + public static final String ERROR_BG = rgbBackground(255, 69, 69); + + public static String rgbColor(int r, int g, int b) { + return "\u001B[38;2;" + r + ";" + g + ";" + b + "m"; + } + + public static String rgbBackground(int r, int g, int b) { + return "\u001B[48;2;" + r + ";" + g + ";" + b + "m"; + } +} diff --git a/homework02/src/main/java/com/ironhack/Teacher.java b/homework02/src/main/java/com/ironhack/Teacher.java new file mode 100644 index 00000000..225c46e8 --- /dev/null +++ b/homework02/src/main/java/com/ironhack/Teacher.java @@ -0,0 +1,61 @@ +package com.ironhack; + +import java.util.UUID; + +public class Teacher { + private String teacherId; + private String name; + private double salary; + + public Teacher(String name, double salary) { + this.teacherId = autoTeacherId(); + this.name = name; + this.salary = salary; + } + + public void setTeacherId(String teacherId) { + this.teacherId = teacherId; + } + + // Auto-generated teacherId method + private String autoTeacherId() { +// return "T-" + UUID.randomUUID().toString().substring(0, 3); // T-XXX + Main.teacherId++; + return "T" + Main.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 " ||=|=|=|=|=| | __..\\/ | |_| ||#|| |\n" + + " || | | | | | /\\ \\ \\\\|++|=| ||#|| | TEACHER\n" + + " ||_|_|_|_|_|__/_/\\_.___\\__|_|__||_||__| "+name+"\n" + + " |___________ /\\~()/()~//\\ ____________|\n" + + " | __ __ \\_ (_ . _/ _ _ _ _ _| Salary: "+salary+" $\n" + + " ||~~|_|..| _ \\ //\\\\ / |=|__|~|~| | | \n" + + " ||--|+|^^|| |__/\\ __ /\\__| |==|x|x| | | \n" + + " ||__|_|__|| / \\ \\ / / \\_|__|_|_|_|_| ID: '"+teacherId+"'\n" + + " |________ _/ \\/\\/\\/ \\_ _________| \n" + + " | _____ |/ \\../ \\| __ ___|\n"; + } +} diff --git a/homework02/src/test/java/com/ironhack/CommandFunctionTest.java b/homework02/src/test/java/com/ironhack/CommandFunctionTest.java new file mode 100644 index 00000000..27774c4b --- /dev/null +++ b/homework02/src/test/java/com/ironhack/CommandFunctionTest.java @@ -0,0 +1,158 @@ +package com.ironhack; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class CommandFunctionTest { + + private CommandFunction commandFunction; +// private List courseList; +// private List teacherList; +// private List studentList; + + @BeforeEach + public void setUp() { +// courseList = new ArrayList<>(); +// teacherList = new ArrayList<>(); +// studentList = new ArrayList<>(); + + Student student1 = new Student("Diego", "Madrid", "diego@mail.com"); + student1.setStudentId("S33"); + student1.setCourse(new Course("Math", 200)); + Student student2 = new Student("David", "Murcia", "dvd@mail.com"); + student2.setCourse(new Course("Programming", 300)); + Main.studentList.add(student1); + Main.studentList.add(student2); + +// Teacher teacher1 = new Teacher("Alice", 1400); +// Teacher teacher2 = new Teacher("Bob", 1300); +// teacherList.add(teacher1); +// teacherList.add(teacher2); +// +// Course javaCourse = new Course("Java", 500); +// Course pythonCourse = new Course("Python", 600); +// courseList.add(javaCourse); +// courseList.add(pythonCourse); + + commandFunction = new CommandFunction(); + + } + + @AfterEach + public void tearDown(){ + Main.courseList = new ArrayList<>(); + Main.teacherList= new ArrayList<>(); + Main.studentList = new ArrayList<>(); + + } + // ENROLL & ASSIGM Command Test + @Test + public void assignCommand_validInput_teacherAssigned(){ + Teacher teacher1 = new Teacher("Alice", 400); + teacher1.setTeacherId("T10"); + Main.teacherList.add(teacher1); + Course javaCourse = new Course("Java", 500); + javaCourse.setCourseId("C55"); + Main.courseList.add(javaCourse); + commandFunction.assign("T10","C55"); + assertEquals("Alice",Main.courseList.get(0).getTeacher().getName()); + }; + + @Test + public void enrollCommand_validInput_studentEnrolled(){ + Course javaCourse = new Course("Java", 500); + javaCourse.setCourseId("C30"); + Main.courseList.add(javaCourse); + Student student = new Student("John", "City", "test@mail.com"); + student.setStudentId("S75"); + Main.studentList.add(student); + commandFunction.enroll("S75","C30"); + assertEquals("Java",Main.studentList.get(2).getCourse().getName()); + } + // SHOW COURSES Command Test + @Test + public void testShowCourses_WithCourses() { + Teacher teacher1 = new Teacher("Alice", 400); + + Course javaCourse = new Course("Java", 500); + javaCourse.setTeacher(teacher1); + Course pythonCourse = new Course("Python", 600); + pythonCourse.setTeacher(teacher1); + Main.courseList.add(javaCourse); + Main.courseList.add(pythonCourse); + + String courses = commandFunction.showCoursesTest(); + + assertTrue(courses.contains("Java")); + assertTrue(courses.contains("Python")); + } + + // LOOK UP STUDENT Command Tests + @Test + void lookupStudent_Found() { + String student = commandFunction.lookupStudentTest("S33"); + System.out.println(Main.studentList); + assertTrue(student.contains("Diego")); + } + + @Test + public void lookupStudent_notFound() { + String student = commandFunction.lookupStudentTest("S5"); + System.out.println(Main.studentList); + assertEquals("Not Found student with ID ( S5 ).", student); + } + + // SHOW PROFIT Command Tests + @Test + void showProfit_noCoursesNoTeachers_zeroProfit() { + + Main.courseList = new ArrayList<>(); + Main.teacherList= new ArrayList<>(); + + assertEquals(0, commandFunction.showProfitTest()); + } + + @Test + void showProfit_coursesEarnMoney_teachersHaveSalary_correctProfit() { + Teacher teacher1 = new Teacher("Alice", 400); + Teacher teacher2 = new Teacher("Bob", 200); + Main.teacherList.add(teacher1); + Main.teacherList.add(teacher2); + + Course javaCourse = new Course("Java", 500); + javaCourse.setMoneyEarned(500); + Course pythonCourse = new Course("Python", 600); + pythonCourse.setMoneyEarned(300); + + Main.courseList.add(javaCourse); + Main.courseList.add(pythonCourse); + + // (500+300) - (400+200) = 200 + assertEquals(200, commandFunction.showProfitTest()); + } + + @Test + void showProfit_teachersEarnMoreThanCourses_negativeProfit() { + Teacher teacher1 = new Teacher("Alice", 1400); + Teacher teacher2 = new Teacher("Bob", 1200); + Main.teacherList.add(teacher1); + Main.teacherList.add(teacher2); + + Course javaCourse = new Course("Java", 500); + javaCourse.setMoneyEarned(500); + Course pythonCourse = new Course("Python", 600); + pythonCourse.setMoneyEarned(300); + + Main.courseList.add(javaCourse); + Main.courseList.add(pythonCourse); + + // (500+300) - (1400+1200) = -1800 + assertEquals(-1800, commandFunction.showProfitTest()); + } +} \ No newline at end of file diff --git a/homework02/src/test/java/com/ironhack/ServerMenuTest.java b/homework02/src/test/java/com/ironhack/ServerMenuTest.java new file mode 100644 index 00000000..e60c81f9 --- /dev/null +++ b/homework02/src/test/java/com/ironhack/ServerMenuTest.java @@ -0,0 +1,43 @@ +package com.ironhack; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; +class ServerMenuTest { + + @BeforeAll + public static void setUp() { + Main list = new Main(); + Student student01 = new Student("Maria","Calle Colon", "maria@gmail.com"); + Main.studentList.add(student01); + } + + @Test + void sanitize_Incorrect() { + assertThrows(IllegalArgumentException.class, () -> { + ServerMenu test01 = new ServerMenu(); + test01.sanitize("Enroll Show C4 D3"); + }); + + assertThrows(IllegalArgumentException.class, () -> { + ServerMenu test01 = new ServerMenu(); + test01.sanitize("Enroll"); + }); + } + + @Test + void isValidStudentId_NotExist(){ + ServerMenu test02 = new ServerMenu(); + + assertFalse(test02.isValidStudentId("S2")); + } + + @Test + void isValidStudentId_Correct(){ + + ServerMenu test03 = new ServerMenu(); + + assertTrue(test03.isValidStudentId("S1")); + } +} \ No newline at end of file diff --git a/homework02/src/test/java/com/ironhack/StudentTest.java b/homework02/src/test/java/com/ironhack/StudentTest.java new file mode 100644 index 00000000..548fc4c8 --- /dev/null +++ b/homework02/src/test/java/com/ironhack/StudentTest.java @@ -0,0 +1,49 @@ +package com.ironhack; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class StudentTest { + + // Constructor + @Test + void student_validEmail_createStudent() { + Student student = new Student("Test Student", "City", "test@mail.com"); + + assertEquals("test@mail.com", student.getEmail()); + } + + // Set Method + @Test + void setEmail_validEmail_correctSetEmail() { + Student student = new Student("Test Student", "City", "test@mail.com"); + + student.setEmail("valid@mail.com"); + + assertEquals("valid@mail.com", student.getEmail()); + } + + @Test + void setEmail_invalidEmail_throwException() { + Student student = new Student("Test Student", "City", "test@mail.com"); + + Exception e = assertThrows(IllegalArgumentException.class, () -> { + student.setEmail("invalid-email.com"); + }); + + assertEquals("Invalid email format. Please use a valid format like 'example@mail.com'.", e.getMessage()); + } + + @Test + void setEmail_emptyEmail_throwException() { + Student student = new Student("Test Student", "City", "test@mail.com"); + + Exception e = assertThrows(IllegalArgumentException.class, () -> { + student.setEmail(""); + }); + + assertEquals("Invalid email format. Please use a valid format like 'example@mail.com'.", e.getMessage()); + } + +} \ No newline at end of file