From 8380d437a0639f298a54fc43992884a9da39f98b Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 20 Feb 2025 11:54:27 +0100 Subject: [PATCH 01/29] First commit. Add classes and .idea in .gitignore. The proyect is homework02 --- homework02/.gitignore | 39 +++++++++++++++++++ homework02/.idea/.gitignore | 3 ++ homework02/pom.xml | 17 ++++++++ .../java/com/ironhack/CommandFunction.java | 4 ++ .../src/main/java/com/ironhack/Course.java | 4 ++ .../src/main/java/com/ironhack/Main.java | 7 ++++ .../src/main/java/com/ironhack/Student.java | 4 ++ .../src/main/java/com/ironhack/Teacher.java | 4 ++ 8 files changed, 82 insertions(+) create mode 100644 homework02/.gitignore create mode 100644 homework02/.idea/.gitignore create mode 100644 homework02/pom.xml create mode 100644 homework02/src/main/java/com/ironhack/CommandFunction.java create mode 100644 homework02/src/main/java/com/ironhack/Course.java create mode 100644 homework02/src/main/java/com/ironhack/Main.java create mode 100644 homework02/src/main/java/com/ironhack/Student.java create mode 100644 homework02/src/main/java/com/ironhack/Teacher.java 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/homework02/.idea/.gitignore b/homework02/.idea/.gitignore new file mode 100644 index 00000000..26d33521 --- /dev/null +++ b/homework02/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/homework02/pom.xml b/homework02/pom.xml new file mode 100644 index 00000000..cc6a6356 --- /dev/null +++ b/homework02/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + org.example + homework02 + 1.0-SNAPSHOT + + + 17 + 17 + UTF-8 + + + \ 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..38b43bff --- /dev/null +++ b/homework02/src/main/java/com/ironhack/CommandFunction.java @@ -0,0 +1,4 @@ +package com.ironhack; + +public class CommandFunction { +} diff --git a/homework02/src/main/java/com/ironhack/Course.java b/homework02/src/main/java/com/ironhack/Course.java new file mode 100644 index 00000000..3340b324 --- /dev/null +++ b/homework02/src/main/java/com/ironhack/Course.java @@ -0,0 +1,4 @@ +package com.ironhack; + +public class Course { +} diff --git a/homework02/src/main/java/com/ironhack/Main.java b/homework02/src/main/java/com/ironhack/Main.java new file mode 100644 index 00000000..439b92b0 --- /dev/null +++ b/homework02/src/main/java/com/ironhack/Main.java @@ -0,0 +1,7 @@ +package com.ironhack; + +public class Main { + public static void main(String[] args) { + //Menu + } +} \ 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..d59ede21 --- /dev/null +++ b/homework02/src/main/java/com/ironhack/Student.java @@ -0,0 +1,4 @@ +package com.ironhack; + +public class Student { +} 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..0bd52859 --- /dev/null +++ b/homework02/src/main/java/com/ironhack/Teacher.java @@ -0,0 +1,4 @@ +package com.ironhack; + +public class Teacher { +} From 7c4a665c3139675a3e8b1eb012d8e5bc21a7547b Mon Sep 17 00:00:00 2001 From: alvaro Date: Sun, 23 Feb 2025 21:20:29 +0100 Subject: [PATCH 02/29] first commit on branch --- .gitignore | 39 +++++++++++++++++++ .idea/.gitignore | 3 ++ .idea/homework-java-ironschool.iml | 9 +++++ .idea/misc.xml | 6 +++ .idea/modules.xml | 8 ++++ .idea/vcs.xml | 6 +++ .../src/main/java/com/ironhack/Main.java | 1 + 7 files changed, 72 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/homework-java-ironschool.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..d62a16b7 --- /dev/null +++ b/.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/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..26d33521 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/homework-java-ironschool.iml b/.idea/homework-java-ironschool.iml new file mode 100644 index 00000000..d6ebd480 --- /dev/null +++ b/.idea/homework-java-ironschool.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..6ff3c251 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..7d988043 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/homework02/src/main/java/com/ironhack/Main.java b/homework02/src/main/java/com/ironhack/Main.java index 439b92b0..a1fb48d4 100644 --- a/homework02/src/main/java/com/ironhack/Main.java +++ b/homework02/src/main/java/com/ironhack/Main.java @@ -3,5 +3,6 @@ public class Main { public static void main(String[] args) { //Menu + // Testing branch maindev01 } } \ No newline at end of file From 368ea63a046a65c9637ae442ee1f0a1a1ab57b5b Mon Sep 17 00:00:00 2001 From: alvaro Date: Sun, 23 Feb 2025 21:38:27 +0100 Subject: [PATCH 03/29] Added class Teacher --- .../src/main/java/com/ironhack/Main.java | 6 ++++ .../src/main/java/com/ironhack/Teacher.java | 34 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/homework02/src/main/java/com/ironhack/Main.java b/homework02/src/main/java/com/ironhack/Main.java index a1fb48d4..cc6c641d 100644 --- a/homework02/src/main/java/com/ironhack/Main.java +++ b/homework02/src/main/java/com/ironhack/Main.java @@ -1,8 +1,14 @@ package com.ironhack; public class Main { + public static int global_int = 0; + public static void main(String[] args) { //Menu // Testing branch maindev01 + Teacher john = new Teacher("John",23_000); + Teacher jane = new Teacher("Jane",24_000); + System.out.println(john.getTeacherId()); + System.out.println(jane.getTeacherId()); } } \ No newline at end of file diff --git a/homework02/src/main/java/com/ironhack/Teacher.java b/homework02/src/main/java/com/ironhack/Teacher.java index 0bd52859..a7d51b49 100644 --- a/homework02/src/main/java/com/ironhack/Teacher.java +++ b/homework02/src/main/java/com/ironhack/Teacher.java @@ -1,4 +1,38 @@ package com.ironhack; public class Teacher { + private String teacherId; //AUTO GEN + private String name; + private double salary; + + public Teacher(String name, double salary) { + this.name = name; + this.salary = salary; + this.teacherId = "t" + Main.global_int; + Main.global_int++; + } + + public String getTeacherId() { + return teacherId; + } + + public void setTeacherId(String teacherId) { + this.teacherId = 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; + } } From 0a9b38a30c379fde5df03b6c28d878ab4c79fa7a Mon Sep 17 00:00:00 2001 From: alvaro Date: Tue, 25 Feb 2025 12:48:22 +0100 Subject: [PATCH 04/29] Fix folder structure --- homework02/README.md | 108 ++++++++++++++++++ .../java/com/ironhack/CommandFunction.java | 12 ++ 2 files changed, 120 insertions(+) create mode 100644 homework02/README.md diff --git a/homework02/README.md b/homework02/README.md new file mode 100644 index 00000000..1d3c661a --- /dev/null +++ b/homework02/README.md @@ -0,0 +1,108 @@ +![logo_ironhack_blue 7](https://user-images.githubusercontent.com/23629340/40541063-a07a0a8a-601a-11e8-91b5-2f13e4e6b441.png) + +# HW | Java IronSchool (Unit 2 homework) + +## Introduction + +For this homework, you will be building a School Management System, that will help manage students, teachers and courses with some basic functionalities. + +## Instructions + +Let's walk through the details of the homework: + +### Classes + +Three main classes are necessary to complete this homework: **Teacher**, **Student** and **Course**. + +
+ +**Teacher class** + +This class will have: + +- Variable called `teacherId` of data type `string`, auto-generated (Private member) +- Variable called `name` of data type `string` (Private member) +- Variable called `salary` of data type `double`, representing the salary of the teacher (Private member) +- A parameterized constructor that takes `name` and `salary` +- Public Getter functions to access these variables +- Public Setter functions to change these variables +- Optional attributes are accepted if needed based on the code structure + +
+ +**Course class** + +This class will have: + +- Variable called `courseId` of data type `string`, auto-generated (Private member) +- Variable called `name` of data type `string` (Private member) +- Variable called `price` of data type `double`, representing the price of this course (Private member) +- Variable called `money_earned` of data type `double`, representing the total money earned by this course (Private member) +- Nullable variable called `teacher` of data type `Teacher` (Private member) +- A parameterized constructor that takes `name` and `price` +- Public Getter functions to access these variables +- Public Setter functions to change these variables +- Optional attributes are accepted if needed based on the code structure + +
+ +**Student class** + +This class will have: + +- Variable called `studentId` of data type `string`, auto-generated (Private member) +- Variable called `name` of data type `string` (Private member) +- Variable called `address` of data type `string` (Private member) +- Variable called `email` of data type `string` (Private member) +- Nullable variable called `course` of data type `Course`, representing the course this student is enrolled into (Private member) +- A parameterized constructor that takes `name`, `address` and `email` +- Public Getter functions to access these variables +- Public Setter functions to change these variables +- Optional attributes are accepted if needed based on the code structure + +## How the application works + +1. The application starts by asking the user for a name for the school +2. Next, the user is asked for a number of how many teachers should be created +3. Next, the user is prompted to enter the details of each teacher (based on the number chosen above) +4. Next, the user is asked for the number of courses to be created (Do not specify the teacher yet, there is a command for it) +5. Next, the user is prompted to enter details of each course based on the number chosen above +6. Next, the user is asked for the number of students to be created (Do not specify the course yet, there is a command for it) +7. Next, the user is prompted to enter details of each student based on the number chosen above +8. Next, the user is now prompted to enter any command of the list below to execute a specified action in the system. + +The IDs should be automatically generated. + +## Commands + +- **ENROLL** **[STUDENT_ID] [COURSE_ID]**: This command will help enroll the student specified in the corresponding course. While also updating the `money_earned` of that course based on its price +- **ASSIGN** **[TEACHER_ID] [COURSE_ID]**: This command will help assign the teacher specified to the corresponding course +- **SHOW COURSES**: This command will display a list of all courses +- **LOOKUP COURSE** **[COURSE_ID]**: This command will display the full details of the specified course +- **SHOW STUDENTS**: This command will display a list of all students +- **LOOKUP STUDENT** **[STUDENT_ID]**: This command will display the full details of the specified student +- **SHOW TEACHERS**: This command will display a list of all teachers +- **LOOKUP TEACHER** **[TEACHER_ID]**: This command will display the full details of the specified teacher +- **SHOW PROFIT**: This command will calculate **(The total money earned from all courses)** - **(The sum of all the teachers' salaries)** and return the result + +## Requirements + +For this project you must accomplish all of the following: + +1. Navigate through a text-based menu using Standard Input and Output. +2. Create unit tests for every method other than basic getters, setters and constructors (getters and setters with logic do require unit tests). +3. Handle all exceptions gracefully (incorrect input should not crash the program). +4. Create Teachers, Courses and Students specifying their full details. +5. Handle receiving commands in the Standard Input that corresponds to actual actions in the system. + +### Bonus + +1. Add more commands that can help display more information such as (**SHOW STUDENTS** **[COURSE_ID]**, **SHOW MONEY EARNED**, **SHOW MONEY SPENT**, etc.) + +## Important Notes + +- Everyone in the squad should contribute equally to the project in time and lines of code written. +- All code must be reviewed before it is merged into the `master` branch. +- All squad members must participate in code review. +- Every repository should have a README file with clear instructions, demo files, or any documentation needed so other teams don't have problems with the review. +- This is intended to be a challenging assignment. You will have to rely heavily on your teammates and independent research. Learning independently is a hallmark of a good developer and our job is to turn you into good developers. This process may be frustrating but you will learn a ton! \ 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 index 38b43bff..90c6c9ab 100644 --- a/homework02/src/main/java/com/ironhack/CommandFunction.java +++ b/homework02/src/main/java/com/ironhack/CommandFunction.java @@ -1,4 +1,16 @@ package com.ironhack; public class CommandFunction { + + public static boolean sanitize(String args){ + boolean sanitize = false; + String values[] = args.split( " "); + if (values.length > 3) throw new IllegalArgumentException(); + if (values.length < 2) throw new IllegalArgumentException(); + if (values[0].equals("ENROLL") || values[0].equals("ASSIGN") || values[0].equals("SHOW") || values[0].equals("LOOKUP")){ + // VAMOS BIEN + + } + return sanitize; + } } From 038544fc85b4c0d4dc69413b3eb5081e65b480bb Mon Sep 17 00:00:00 2001 From: JohnyJay Date: Tue, 25 Feb 2025 12:51:30 +0100 Subject: [PATCH 05/29] Create test test --- test | 1 + 1 file changed, 1 insertion(+) create mode 100644 test diff --git a/test b/test new file mode 100644 index 00000000..9daeafb9 --- /dev/null +++ b/test @@ -0,0 +1 @@ +test From f3b6c2ad5e3c77d9eb5e02312967a3982614f8ef Mon Sep 17 00:00:00 2001 From: JohnyJay Date: Tue, 25 Feb 2025 12:52:28 +0100 Subject: [PATCH 06/29] Delete test --- test | 1 - 1 file changed, 1 deletion(-) delete mode 100644 test diff --git a/test b/test deleted file mode 100644 index 9daeafb9..00000000 --- a/test +++ /dev/null @@ -1 +0,0 @@ -test From f7e76345b3ca56bdd30a78571c82954855db6436 Mon Sep 17 00:00:00 2001 From: JohnyJay Date: Tue, 25 Feb 2025 12:52:42 +0100 Subject: [PATCH 07/29] Delete README.md --- README.md | 108 ------------------------------------------------------ 1 file changed, 108 deletions(-) delete mode 100644 README.md diff --git a/README.md b/README.md deleted file mode 100644 index 1d3c661a..00000000 --- a/README.md +++ /dev/null @@ -1,108 +0,0 @@ -![logo_ironhack_blue 7](https://user-images.githubusercontent.com/23629340/40541063-a07a0a8a-601a-11e8-91b5-2f13e4e6b441.png) - -# HW | Java IronSchool (Unit 2 homework) - -## Introduction - -For this homework, you will be building a School Management System, that will help manage students, teachers and courses with some basic functionalities. - -## Instructions - -Let's walk through the details of the homework: - -### Classes - -Three main classes are necessary to complete this homework: **Teacher**, **Student** and **Course**. - -
- -**Teacher class** - -This class will have: - -- Variable called `teacherId` of data type `string`, auto-generated (Private member) -- Variable called `name` of data type `string` (Private member) -- Variable called `salary` of data type `double`, representing the salary of the teacher (Private member) -- A parameterized constructor that takes `name` and `salary` -- Public Getter functions to access these variables -- Public Setter functions to change these variables -- Optional attributes are accepted if needed based on the code structure - -
- -**Course class** - -This class will have: - -- Variable called `courseId` of data type `string`, auto-generated (Private member) -- Variable called `name` of data type `string` (Private member) -- Variable called `price` of data type `double`, representing the price of this course (Private member) -- Variable called `money_earned` of data type `double`, representing the total money earned by this course (Private member) -- Nullable variable called `teacher` of data type `Teacher` (Private member) -- A parameterized constructor that takes `name` and `price` -- Public Getter functions to access these variables -- Public Setter functions to change these variables -- Optional attributes are accepted if needed based on the code structure - -
- -**Student class** - -This class will have: - -- Variable called `studentId` of data type `string`, auto-generated (Private member) -- Variable called `name` of data type `string` (Private member) -- Variable called `address` of data type `string` (Private member) -- Variable called `email` of data type `string` (Private member) -- Nullable variable called `course` of data type `Course`, representing the course this student is enrolled into (Private member) -- A parameterized constructor that takes `name`, `address` and `email` -- Public Getter functions to access these variables -- Public Setter functions to change these variables -- Optional attributes are accepted if needed based on the code structure - -## How the application works - -1. The application starts by asking the user for a name for the school -2. Next, the user is asked for a number of how many teachers should be created -3. Next, the user is prompted to enter the details of each teacher (based on the number chosen above) -4. Next, the user is asked for the number of courses to be created (Do not specify the teacher yet, there is a command for it) -5. Next, the user is prompted to enter details of each course based on the number chosen above -6. Next, the user is asked for the number of students to be created (Do not specify the course yet, there is a command for it) -7. Next, the user is prompted to enter details of each student based on the number chosen above -8. Next, the user is now prompted to enter any command of the list below to execute a specified action in the system. - -The IDs should be automatically generated. - -## Commands - -- **ENROLL** **[STUDENT_ID] [COURSE_ID]**: This command will help enroll the student specified in the corresponding course. While also updating the `money_earned` of that course based on its price -- **ASSIGN** **[TEACHER_ID] [COURSE_ID]**: This command will help assign the teacher specified to the corresponding course -- **SHOW COURSES**: This command will display a list of all courses -- **LOOKUP COURSE** **[COURSE_ID]**: This command will display the full details of the specified course -- **SHOW STUDENTS**: This command will display a list of all students -- **LOOKUP STUDENT** **[STUDENT_ID]**: This command will display the full details of the specified student -- **SHOW TEACHERS**: This command will display a list of all teachers -- **LOOKUP TEACHER** **[TEACHER_ID]**: This command will display the full details of the specified teacher -- **SHOW PROFIT**: This command will calculate **(The total money earned from all courses)** - **(The sum of all the teachers' salaries)** and return the result - -## Requirements - -For this project you must accomplish all of the following: - -1. Navigate through a text-based menu using Standard Input and Output. -2. Create unit tests for every method other than basic getters, setters and constructors (getters and setters with logic do require unit tests). -3. Handle all exceptions gracefully (incorrect input should not crash the program). -4. Create Teachers, Courses and Students specifying their full details. -5. Handle receiving commands in the Standard Input that corresponds to actual actions in the system. - -### Bonus - -1. Add more commands that can help display more information such as (**SHOW STUDENTS** **[COURSE_ID]**, **SHOW MONEY EARNED**, **SHOW MONEY SPENT**, etc.) - -## Important Notes - -- Everyone in the squad should contribute equally to the project in time and lines of code written. -- All code must be reviewed before it is merged into the `master` branch. -- All squad members must participate in code review. -- Every repository should have a README file with clear instructions, demo files, or any documentation needed so other teams don't have problems with the review. -- This is intended to be a challenging assignment. You will have to rely heavily on your teammates and independent research. Learning independently is a hallmark of a good developer and our job is to turn you into good developers. This process may be frustrating but you will learn a ton! \ No newline at end of file From 827a67c5463cb75f96356b29f3210b51cb669709 Mon Sep 17 00:00:00 2001 From: JohnyJay Date: Tue, 25 Feb 2025 12:52:56 +0100 Subject: [PATCH 08/29] Delete .gitignore --- .gitignore | 39 --------------------------------------- 1 file changed, 39 deletions(-) delete mode 100644 .gitignore diff --git a/.gitignore b/.gitignore deleted file mode 100644 index d62a16b7..00000000 --- a/.gitignore +++ /dev/null @@ -1,39 +0,0 @@ -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 From 1220d7c7432941a23417872f9a22a9d072f0d80c Mon Sep 17 00:00:00 2001 From: JohnyJay Date: Tue, 25 Feb 2025 12:53:06 +0100 Subject: [PATCH 09/29] Delete .idea directory --- .idea/.gitignore | 3 --- .idea/homework-java-ironschool.iml | 9 --------- .idea/misc.xml | 6 ------ .idea/modules.xml | 8 -------- .idea/vcs.xml | 6 ------ 5 files changed, 32 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/homework-java-ironschool.iml delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 26d33521..00000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml diff --git a/.idea/homework-java-ironschool.iml b/.idea/homework-java-ironschool.iml deleted file mode 100644 index d6ebd480..00000000 --- a/.idea/homework-java-ironschool.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 6ff3c251..00000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 7d988043..00000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 35eb1ddf..00000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file From e9d5771aafd215e0325d6544e8cab03ee6656fc4 Mon Sep 17 00:00:00 2001 From: JohnyJay Date: Tue, 25 Feb 2025 12:53:44 +0100 Subject: [PATCH 10/29] Delete homework02/.idea directory --- homework02/.idea/.gitignore | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 homework02/.idea/.gitignore diff --git a/homework02/.idea/.gitignore b/homework02/.idea/.gitignore deleted file mode 100644 index 26d33521..00000000 --- a/homework02/.idea/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml From 195885cbca942ff1a73767661d431fed9715d861 Mon Sep 17 00:00:00 2001 From: alvaro Date: Tue, 25 Feb 2025 15:06:28 +0100 Subject: [PATCH 11/29] Command Function Complete v1.0 --- .../java/com/ironhack/CommandFunction.java | 184 +++++++++++++++++- .../src/main/java/com/ironhack/Course.java | 63 ++++++ .../src/main/java/com/ironhack/Main.java | 31 +++ .../src/main/java/com/ironhack/School.java | 71 +++++++ .../src/main/java/com/ironhack/Student.java | 64 ++++++ .../src/main/java/com/ironhack/Teacher.java | 9 + 6 files changed, 414 insertions(+), 8 deletions(-) create mode 100644 homework02/src/main/java/com/ironhack/School.java diff --git a/homework02/src/main/java/com/ironhack/CommandFunction.java b/homework02/src/main/java/com/ironhack/CommandFunction.java index 90c6c9ab..d7823843 100644 --- a/homework02/src/main/java/com/ironhack/CommandFunction.java +++ b/homework02/src/main/java/com/ironhack/CommandFunction.java @@ -1,16 +1,184 @@ package com.ironhack; +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + public class CommandFunction { + private School school; + + public CommandFunction(School school1) { + this.school = school1; + } + + public boolean getUserCommand(){ + boolean option = true; + Scanner scanner = new Scanner(System.in); + System.out.println("\nPlease introduce a command:"); + String cmd = scanner.nextLine(); + if(cmd.equals("EXIT")){ + option = false; + }else{ + try{ + sanitize(cmd); + } catch (Exception e) { + //System.out.println("Command not recognized, please try again."); + System.out.println(e.getMessage()); + } + } + return option; + } - public static boolean sanitize(String args){ - boolean sanitize = false; - String values[] = args.split( " "); - if (values.length > 3) throw new IllegalArgumentException(); - if (values.length < 2) throw new IllegalArgumentException(); - if (values[0].equals("ENROLL") || values[0].equals("ASSIGN") || values[0].equals("SHOW") || values[0].equals("LOOKUP")){ - // VAMOS BIEN + public void sanitize(String args){ + //boolean sanitize = false; + String[] values = args.split( " "); + if (values.length > 3 && !values[0].equals("EXIT")) throw new IllegalArgumentException("Too many arguments."); + if (values.length < 2 && !values[0].equals("EXIT")) throw new IllegalArgumentException("Too few arguments."); + + // COMMANDO ENROLL + if(values.length == 3 && values[0].equals("ENROLL")){ + if(school.isValidStudentId(values[1]) && school.isValidCourseId(values[2])){ + executeCommandEnroll(values[1],values[2]); + }else{ + throw new IllegalArgumentException("Parameters for ENROLL are incorrect."); + } + // COMMANDO ASSIGN + }else if (values.length == 3 && values[0].equals("ASSIGN")){ + if(school.isValidTeacherId(values[1]) && school.isValidCourseId(values[2])){ + executeCommandAssign(values[1],values[2]); + }else{ + throw new IllegalArgumentException("Parameters for ASSIGN are incorrect."); + } + // COMMANDO SHOW + }else if(values[0].equals("SHOW") && values.length == 2){ + if(values[1].equals("COURSES") || values[1].equals("STUDENTS") || values[1].equals("TEACHERS") || values[1].equals("PROFIT")){ + executeCommandShow(values[1]); + }else{ + throw new IllegalArgumentException("Invalid parameters for SHOW."); + } + // COMMANDO LOOKUP + }else if(values[0].equals("LOOKUP") && values.length == 3) { + if (values[1].equals("STUDENT") && school.isValidStudentId(values[2])) { + executeCommandLookUpStudent(values[2]); + } else if (values[1].equals("COURSE") && school.isValidCourseId(values[2])) { + executeCommandLookUpCourse(values[2]); + } else if (values[1].equals("TEACHER") && school.isValidTeacherId(values[2])) { + executeCommandLookUpTeacher(values[2]); + } else { + throw new IllegalArgumentException("Invalid parameters for LOOKUP"); + } + }else{ + throw new IllegalArgumentException("Invalid Command."); } - return sanitize; + //return sanitize; + } + + private void executeCommandLookUpTeacher(String teacherId) { + // GET TEACHER + for (int i = 0; i < school.getTeacherList().size(); i++){ + if(school.getTeacherList().get(i).getTeacherId().equals(teacherId)){ + // TEACHER FOUND + System.out.println(school.getTeacherList().get(i).toString()); + break; + } + } + } + + private void executeCommandLookUpCourse(String courseId) { + // GET COURSE + for (int i = 0; i < school.getCourseList().size(); i++){ + if(school.getCourseList().get(i).getCourseId().equals(courseId)){ + // COURSE FOUND + System.out.println(school.getCourseList().get(i).toString()); + break; + } + } + } + + private void executeCommandLookUpStudent(String studentId) { + // GET STUDENT + for (int i = 0; i < school.getStudentList().size(); i++){ + if(school.getStudentList().get(i).getStudentId().equals(studentId)){ + // STUDENT FOUND + System.out.println(school.getStudentList().get(i).toString()); + break; + } + } + } + + private void executeCommandShow(String element) { + if(element.equals("STUDENTS")){ + System.out.println(school.getStudentList()); + }else if(element.equals("TEACHERS")){ + System.out.println(school.getTeacherList()); + }else if(element.equals("COURSES")){ + System.out.println(school.getCourseList()); + }else{ // PROFIT + double a = 0; + double b = 0; + for(Course c: school.getCourseList()){ + a+=c.getMoney_earned(); + } + for(Teacher t: school.getTeacherList()){ + b+=t.getSalary(); + } + double res = a - b; + System.out.println("The total profit of the School is: "+res); + } + } + + private void executeCommandAssign(String teacherId, String courseId) { + List list = school.getTeacherList(); + List list2 = school.getCourseList(); + Teacher teacher = new Teacher("a",1); + Course course = new Course("c",1); + // GET TEACHER + for (int i = 0; i < list.size(); i++){ + if(list.get(i).getTeacherId().equals(teacherId)){ + // TEACHER FOUND + teacher = list.get(i); + break; + } + } + for(int i = 0; i < list2.size(); i++){ + if(list2.get(i).getCourseId().equals(courseId)){ + course = list2.get(i); + course.setTeacher(teacher); + list2.set(i,course); + break; + } + } + school.setCourseList(list2); + } + + private void executeCommandEnroll(String studentId, String courseId) { + List list = school.getStudentList(); + List list2 = school.getCourseList(); + Course course = new Course("c",1); + Student student = new Student("a","a","a"); + for(int i = 0; i < list2.size(); i++){ + if(list2.get(i).getCourseId().equals(courseId)){ + course = list2.get(i); + course.setMoney_earned(course.getMoney_earned()+course.getPrice()); + list2.set(i,course); + break; + } + } + for (int i = 0; i < list.size(); i++){ + if(list.get(i).getStudentId().equals(studentId)){ + // STUDENT FOUND + student = list.get(i); + student.setCourse(course); + list.set(i,student); + break; + } + } + school.setCourseList(list2); + school.setStudentList(list); + } + + private void executeCommand(String args) { + System.out.println("The following command will be executed: " + args); } } diff --git a/homework02/src/main/java/com/ironhack/Course.java b/homework02/src/main/java/com/ironhack/Course.java index 3340b324..3eacd6b0 100644 --- a/homework02/src/main/java/com/ironhack/Course.java +++ b/homework02/src/main/java/com/ironhack/Course.java @@ -1,4 +1,67 @@ package com.ironhack; public class Course { + private String courseId; + private String name; + private double price; + private double money_earned; + private Teacher teacher; + + public Course(String name, double price) { + this.name = name; + this.price = price; + this.courseId = "c"+Main.global_course_id; + Main.global_course_id++; + } + + 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 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 "Course{" + + "courseId='" + courseId + '\'' + + ", name='" + name + '\'' + + ", price=" + price + + ", money_earned=" + money_earned + + ", teacher=" + teacher + + '}'; + } } diff --git a/homework02/src/main/java/com/ironhack/Main.java b/homework02/src/main/java/com/ironhack/Main.java index cc6c641d..17e10728 100644 --- a/homework02/src/main/java/com/ironhack/Main.java +++ b/homework02/src/main/java/com/ironhack/Main.java @@ -1,14 +1,45 @@ package com.ironhack; +import java.util.ArrayList; +import java.util.List; + public class Main { public static int global_int = 0; + public static int global_course_id = 0; + public static int global_student_id = 0; public static void main(String[] args) { //Menu // Testing branch maindev01 Teacher john = new Teacher("John",23_000); Teacher jane = new Teacher("Jane",24_000); + Course math = new Course("Mathematics",125.5); + Course art = new Course("Art",38.6); + Student johny = new Student("Johnny","Bellville Avenue","j@example.com"); + Student janey = new Student("Janey","Lakeside Boulevard","ja@example.com"); System.out.println(john.getTeacherId()); System.out.println(jane.getTeacherId()); + System.out.println(math.getCourseId()); + System.out.println(art.getCourseId()); + System.out.println(johny.getStudentId()); + System.out.println(janey.getStudentId()); + + List teacherList = new ArrayList<>(); + List studentList = new ArrayList<>(); + List courseList = new ArrayList<>(); + teacherList.add(john); + teacherList.add(jane); + studentList.add(johny); + studentList.add(janey); + courseList.add(math); + courseList.add(art); + + School school = new School(teacherList,studentList,courseList); + + CommandFunction command = new CommandFunction(school); + boolean res = true; + do{ + res = command.getUserCommand(); + }while(res); } } \ No newline at end of file diff --git a/homework02/src/main/java/com/ironhack/School.java b/homework02/src/main/java/com/ironhack/School.java new file mode 100644 index 00000000..829bfa5a --- /dev/null +++ b/homework02/src/main/java/com/ironhack/School.java @@ -0,0 +1,71 @@ +package com.ironhack; + +import java.util.ArrayList; +import java.util.List; + +public class School { + + List teacherList = new ArrayList<>(); + List studentList = new ArrayList<>(); + List courseList = new ArrayList<>(); + + public School(List teacherList, List studentList, List courseList) { + this.teacherList = teacherList; + this.studentList = studentList; + this.courseList = courseList; + } + + public boolean isValidStudentId(String value) { + boolean result = false; + for(Student s: getStudentList()){ + if(s.getStudentId().equals(value)){ + result = true; + } + } + return result; + } + + public boolean isValidCourseId(String value) { + boolean result = false; + for(Course c: getCourseList()){ + if(c.getCourseId().equals(value)){ + result = true; + } + } + return result; + } + + public boolean isValidTeacherId(String value) { + boolean result = false; + for(Teacher t: getTeacherList()){ + if(t.getTeacherId().equals(value)){ + result = true; + } + } + return result; + } + + public List getTeacherList() { + return teacherList; + } + + public void setTeacherList(List teacherList) { + this.teacherList = teacherList; + } + + public List getStudentList() { + return studentList; + } + + public void setStudentList(List studentList) { + this.studentList = studentList; + } + + public List getCourseList() { + return courseList; + } + + public void setCourseList(List courseList) { + this.courseList = courseList; + } +} diff --git a/homework02/src/main/java/com/ironhack/Student.java b/homework02/src/main/java/com/ironhack/Student.java index d59ede21..bc55e01f 100644 --- a/homework02/src/main/java/com/ironhack/Student.java +++ b/homework02/src/main/java/com/ironhack/Student.java @@ -1,4 +1,68 @@ package com.ironhack; 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.name = name; + this.address = address; + this.email = email; + this.studentId = "s"+Main.global_student_id; + Main.global_student_id++; + } + + 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; + } + + 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/homework02/src/main/java/com/ironhack/Teacher.java b/homework02/src/main/java/com/ironhack/Teacher.java index a7d51b49..be09c91d 100644 --- a/homework02/src/main/java/com/ironhack/Teacher.java +++ b/homework02/src/main/java/com/ironhack/Teacher.java @@ -35,4 +35,13 @@ public double getSalary() { public void setSalary(double salary) { this.salary = salary; } + + @Override + public String toString() { + return "Teacher{" + + "teacherId='" + teacherId + '\'' + + ", name='" + name + '\'' + + ", salary=" + salary + + '}'; + } } From 579de1ede1195d4589aace1c20b6956e5cd18033 Mon Sep 17 00:00:00 2001 From: Fernando Date: Tue, 25 Feb 2025 22:57:30 +0100 Subject: [PATCH 12/29] Course class and SHOW COURSES, LOOKUP STUDENT, SHOW PROFIT commands, with tests --- homework02/pom.xml | 8 ++ .../java/com/ironhack/CommandFunction.java | 74 +++++++++++ .../src/main/java/com/ironhack/Course.java | 75 +++++++++++ .../src/main/java/com/ironhack/Main.java | 45 +++++++ .../src/main/java/com/ironhack/Student.java | 83 ++++++++++++ .../src/main/java/com/ironhack/Teacher.java | 47 +++++++ .../com/ironhack/CommandFunctionTest.java | 121 ++++++++++++++++++ .../test/java/com/ironhack/StudentTest.java | 49 +++++++ 8 files changed, 502 insertions(+) create mode 100644 homework02/src/test/java/com/ironhack/CommandFunctionTest.java create mode 100644 homework02/src/test/java/com/ironhack/StudentTest.java diff --git a/homework02/pom.xml b/homework02/pom.xml index cc6a6356..73f45724 100644 --- a/homework02/pom.xml +++ b/homework02/pom.xml @@ -13,5 +13,13 @@ 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 index 38b43bff..1e7207d2 100644 --- a/homework02/src/main/java/com/ironhack/CommandFunction.java +++ b/homework02/src/main/java/com/ironhack/CommandFunction.java @@ -1,4 +1,78 @@ package com.ironhack; +import java.util.ArrayList; +import java.util.List; + public class CommandFunction { + List coursesList; + List studentsList; + List teachersList; + + public CommandFunction() { + } + + public CommandFunction(List coursesList, List studentsList, List teachersList) { + this.coursesList = coursesList; + this.studentsList = studentsList; + this.teachersList = teachersList; + } + + public String showCourses(){ + String result = ""; + // If no courses in list + if (coursesList.isEmpty()) { + result = "There are no courses available."; + } else { + for (Course course : coursesList) { + // Append course to the result string + result += course.toString() + "\n"; + } + } + return result; + } + + public String lookupStudent(String studentId){ + boolean found = false; + String result = ""; + // If no students in list + if (studentsList.isEmpty()) { + result = "There are no students."; + } else { + for (Student student : studentsList) { + if (student.getStudentId().equals(studentId)) { + // Store found student info + result = student.toString(); + found = true; + break; + } + } + // If not found + if (!found) { + result = "Not Found student with ID ( " + studentId + " )."; + } + } + return result; + } + + public double showProfit() { + double totalMoneyCourses = 0; + double totalSalaries = 0; + + // Total money earned from all courses + for (Course course : coursesList) { + totalMoneyCourses += course.getMoneyEarned(); + } + + // Sum of all teacher's salaries + for (Teacher teacher : teachersList) { + totalSalaries += teacher.getSalary(); + } + +// System.out.println("Total Profit: " + (totalMoneyCourses - totalSalaries)); + return totalMoneyCourses - totalSalaries; + } + + // EXTRA COMMANDS + + } diff --git a/homework02/src/main/java/com/ironhack/Course.java b/homework02/src/main/java/com/ironhack/Course.java index 3340b324..7bb96108 100644 --- a/homework02/src/main/java/com/ironhack/Course.java +++ b/homework02/src/main/java/com/ironhack/Course.java @@ -1,4 +1,79 @@ package com.ironhack; +import java.util.UUID; + public class Course { + private String courseId; + private String name; + private double price; + private double moneyEarned; + private Teacher teacher; + + public Course() { + } + + public Course(String name, double price) { + this.courseId = generateCourseId(); + this.name = name; + this.price = price; + this.moneyEarned = 0; + this.teacher = null; //If there's no assigned teacher + } + + private String generateCourseId() { +// return "C-" + UUID.randomUUID().toString().substring(0, 3); // C-XXX + Main.courseId++; + return "C-" + Main.courseId; + } + + 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.getName() + + '}'; + } } diff --git a/homework02/src/main/java/com/ironhack/Main.java b/homework02/src/main/java/com/ironhack/Main.java index 439b92b0..32c456e2 100644 --- a/homework02/src/main/java/com/ironhack/Main.java +++ b/homework02/src/main/java/com/ironhack/Main.java @@ -1,7 +1,52 @@ package com.ironhack; +import java.util.ArrayList; +import java.util.List; + public class Main { + // For auto generated IDs + public static int courseId = 0; + public static int studentId = 0; + public static int teacherId = 0; + public static void main(String[] args) { //Menu + + List studentsList = new ArrayList<>(); + List coursesList = new ArrayList<>(); + List teachersList = new ArrayList<>(); + + Student student1 = new Student("Diego", "Madrid", "diego@mail.com"); + Student student2 = new Student("David", "Murcia", "dvd@mail.com"); + studentsList.add(student1); + studentsList.add(student2); + + Teacher teacher1 = new Teacher("Alice", 1400); + Teacher teacher2 = new Teacher("Bob", 1300); + teachersList.add(teacher1); + teachersList.add(teacher2); + + Course javaCourse = new Course("Java", 500); + Course pythonCourse = new Course("Python", 600); + coursesList.add(javaCourse); + javaCourse.setTeacher(teacher1); + javaCourse.setMoneyEarned(2000); + student1.setCourse(javaCourse); + + coursesList.add(pythonCourse); + pythonCourse.setMoneyEarned(3200); + pythonCourse.setTeacher(teacher2); + student2.setCourse(pythonCourse); + + CommandFunction commandFunction = new CommandFunction(coursesList, studentsList, teachersList); + + System.out.println(commandFunction.showCourses()); +// commandFunction.showCourses(); + + System.out.println(commandFunction.lookupStudent("C-1")); +// commandFunction.lookupStudent("C-2"); + + System.out.println(commandFunction.showProfit()); + commandFunction.showProfit(); } } \ 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 index d59ede21..2df713ea 100644 --- a/homework02/src/main/java/com/ironhack/Student.java +++ b/homework02/src/main/java/com/ironhack/Student.java @@ -1,4 +1,87 @@ package com.ironhack; +import java.util.UUID; +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 = autoStudentId(); + this.name = name; + this.address = address; +// this.email = email; + setEmail(email); +// this.course = null; + } + + // Auto-generated teacherId method + private String autoStudentId() { +// return "S-" + UUID.randomUUID().toString().substring(0, 3); // S-XXX + Main.studentId++; + return "C-" + Main.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; + } + + // 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'."); + } + } + + public Course getCourse() { + return course; + } + + public void setCourse(Course course) { + this.course = course; + } + + 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(); + } + + @Override + public String toString() { + return "Student{" + + "studentId='" + studentId + '\'' + + ", name='" + name + '\'' + + ", address='" + address + '\'' + + ", email='" + email + '\'' + + ", course=" + course.getName() + + '}'; + } } diff --git a/homework02/src/main/java/com/ironhack/Teacher.java b/homework02/src/main/java/com/ironhack/Teacher.java index 0bd52859..f00b6155 100644 --- a/homework02/src/main/java/com/ironhack/Teacher.java +++ b/homework02/src/main/java/com/ironhack/Teacher.java @@ -1,4 +1,51 @@ 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; + } + + // Auto-generated teacherId method + private String autoTeacherId() { +// return "T-" + UUID.randomUUID().toString().substring(0, 3); // T-XXX + Main.teacherId++; + return "C-" + 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 "Teacher{" + + "teacherId='" + teacherId + '\'' + + ", name='" + name + '\'' + + ", salary=" + salary + + '}'; + } } 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..35beb490 --- /dev/null +++ b/homework02/src/test/java/com/ironhack/CommandFunctionTest.java @@ -0,0 +1,121 @@ +package com.ironhack; + +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.setCourse(new Course("Math", 200)); + Student student2 = new Student("David", "Murcia", "dvd@mail.com"); + student2.setCourse(new Course("Programming", 300)); + studentList.add(student1); + 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(courseList, studentList, teacherList); + + } + + // 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); + courseList.add(javaCourse); + courseList.add(pythonCourse); + + String courses = commandFunction.showCourses(); + + assertTrue(courses.contains("Java")); + assertTrue(courses.contains("Python")); + } + + // LOOK UP STUDENT Command Tests + @Test + void lookupStudent_Found() { + String student = commandFunction.lookupStudent("C-1"); + + assertTrue(student.contains("Diego")); + } + + @Test + public void lookupStudent_notFound() { + String student = commandFunction.lookupStudent("C-5"); + + assertEquals("Not Found student with ID ( C-5 ).", student); + } + + // SHOW PROFIT Command Tests + @Test + void showProfit_noCoursesNoTeachers_zeroProfit() { + assertEquals(0, commandFunction.showProfit()); + } + + @Test + void showProfit_coursesEarnMoney_teachersHaveSalary_correctProfit() { + Teacher teacher1 = new Teacher("Alice", 400); + Teacher teacher2 = new Teacher("Bob", 200); + teacherList.add(teacher1); + teacherList.add(teacher2); + + Course javaCourse = new Course("Java", 500); + javaCourse.setMoneyEarned(500); + Course pythonCourse = new Course("Python", 600); + pythonCourse.setMoneyEarned(300); + + courseList.add(javaCourse); + courseList.add(pythonCourse); + + // (500+300) - (400+200) = 200 + assertEquals(200, commandFunction.showProfit()); + } + + @Test + void showProfit_teachersEarnMoreThanCourses_negativeProfit() { + Teacher teacher1 = new Teacher("Alice", 1400); + Teacher teacher2 = new Teacher("Bob", 1200); + teacherList.add(teacher1); + teacherList.add(teacher2); + + Course javaCourse = new Course("Java", 500); + javaCourse.setMoneyEarned(500); + Course pythonCourse = new Course("Python", 600); + pythonCourse.setMoneyEarned(300); + + courseList.add(javaCourse); + courseList.add(pythonCourse); + + // (500+300) - (1400+1200) = -1800 + assertEquals(-1800, commandFunction.showProfit()); + } +} \ 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 From 630d6c449ca94bc0013ec8a871ab121f5fc62998 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 25 Feb 2025 23:42:41 +0100 Subject: [PATCH 13/29] Add menu and class Student --- README.md => homework02/README.md | 0 homework02/TRAKING.txt | 22 +++ .../java/com/ironhack/CommandFunction.java | 29 ++++ .../src/main/java/com/ironhack/Course.java | 3 + .../main/java/com/ironhack/InitialMenu.java | 120 +++++++++++++ .../src/main/java/com/ironhack/Main.java | 15 ++ .../main/java/com/ironhack/ServerMenu.java | 162 ++++++++++++++++++ .../src/main/java/com/ironhack/Student.java | 58 +++++++ .../src/main/java/com/ironhack/Teacher.java | 3 + 9 files changed, 412 insertions(+) rename README.md => homework02/README.md (100%) create mode 100644 homework02/TRAKING.txt create mode 100644 homework02/src/main/java/com/ironhack/InitialMenu.java create mode 100644 homework02/src/main/java/com/ironhack/ServerMenu.java 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/src/main/java/com/ironhack/CommandFunction.java b/homework02/src/main/java/com/ironhack/CommandFunction.java index 38b43bff..47f7d21d 100644 --- a/homework02/src/main/java/com/ironhack/CommandFunction.java +++ b/homework02/src/main/java/com/ironhack/CommandFunction.java @@ -1,4 +1,33 @@ package com.ironhack; public class CommandFunction { + public void showProfit() { + } + + public void showCourses() { + } + + public void showStudents() { + + } + + public void showTeachers() { + } + + public void lookupCourse(String idCourse) { + } + + public void lookupStudent(String idStudent) { + } + + public void lookupTeacher(String idTeacher) { + + } + + public void enroll(String idStudent, String idCourse) { + } + + public void assign(String idTeacher, String idCourse) { + + } } diff --git a/homework02/src/main/java/com/ironhack/Course.java b/homework02/src/main/java/com/ironhack/Course.java index 3340b324..ccf166a0 100644 --- a/homework02/src/main/java/com/ironhack/Course.java +++ b/homework02/src/main/java/com/ironhack/Course.java @@ -1,4 +1,7 @@ package com.ironhack; public class Course { + //Delete constructor + public Course(String nameCourse, double priceCourse) { + } } diff --git a/homework02/src/main/java/com/ironhack/InitialMenu.java b/homework02/src/main/java/com/ironhack/InitialMenu.java new file mode 100644 index 00000000..a9dc371e --- /dev/null +++ b/homework02/src/main/java/com/ironhack/InitialMenu.java @@ -0,0 +1,120 @@ +package com.ironhack; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class InitialMenu { + Scanner scanner = new Scanner(System.in); + String nameSchool; + int numberTeacher; + int numberCourse; + int numberStudent; + List teacherList = new ArrayList<>(); + List courseList = new ArrayList<>(); + List studentList = new ArrayList<>(); + + public void startMenu(){ + System.out.println("What is the school name?"); + nameSchool = scanner.nextLine(); + System.out.println("Ok! Welcome to the " + nameSchool +"!"); + } + + public void numberTeacher() throws IllegalArgumentException{ + System.out.println("\nAnd how many teachers are going to be contracted?"); + numberTeacher = scanner.nextInt(); + System.out.println("Ok! Then, " + numberTeacher + " teacher/s will " + + "work at the " + nameSchool +"."); + } + + public void createTeacher() throws IllegalArgumentException{ + int counter; + String nameTeacher; + double salaryTeacher; + Teacher auxiliarTeacher; + + System.out.println("\nNow, add the features and properly of each teacher."); + + for(counter = 1; counter <= numberTeacher; counter++){ + System.out.println("\nTeacher "+ counter +": Fill in the following data. Firstly, introduce the name." + + "Secondly, introduce the salary."); + nameTeacher = scanner.next(); + //salaryTeacher must be scanner.nextDouble, but there is a bug. I have find this code line. + salaryTeacher = Double.parseDouble(scanner.next()); + //scanner.nextLine(); + //salaryTeacher = scanner.nextDouble(); + System.out.println("Ok! Teacher " + counter + ": "+nameTeacher+". Salary: "+salaryTeacher); + + auxiliarTeacher = new Teacher(nameTeacher, salaryTeacher); + teacherList.add(auxiliarTeacher); + } + } + + public void numberCourse() throws IllegalArgumentException{ + System.out.println("\nHow many courses are there?"); + numberCourse = scanner.nextInt(); + System.out.println("Ok! Then, " + numberCourse + " course/s there will " + + "be at the " + nameSchool +"."); + } + + public void createCourse() throws IllegalArgumentException{ + int counter; + String nameCourse; + double priceCourse; + Course auxiliarCourse; + + System.out.println("\nNow, add the features of each course."); + + for(counter = 1; counter <= numberCourse; counter++){ + System.out.println("\nCourse "+ counter +": Fill in the following data. Firstly, introduce the name." + + "Secondly, introduce the price."); + nameCourse = scanner.next(); + //priceCourse must be scanner.nextDouble, but there is a bug. I have find this code line. + priceCourse = Double.parseDouble(scanner.next()); + //priceCourse = scanner.nextInt(); + System.out.println("Ok! Course " + counter + ": "+nameCourse+". Price: "+priceCourse); + + auxiliarCourse = new Course(nameCourse, priceCourse); + courseList.add(auxiliarCourse); + } + } + + public void numberStudent() throws IllegalArgumentException{ + System.out.println("\nHow many students are inscribed?"); + numberStudent = scanner.nextInt(); + System.out.println("Ok! Then, " + numberStudent + " student/s are inscribed " + + "at the " + nameSchool +"."); + } + + public void createStudent(){ + int counter; + String nameStudent; + String addressStudent; + String emailStudent; + Student auxiliarStudent; + + System.out.println("\nNow, add the features of each student."); + + for(counter = 1; counter <= numberStudent; counter++){ + System.out.println("\nCourse "+ counter +": Fill in the following data. Firstly, introduce the name." + + "Secondly, introduce the address. Finally, introduce the email."); + nameStudent = scanner.next(); + scanner.nextLine(); + addressStudent = scanner.nextLine(); + emailStudent = scanner.next(); + scanner.nextLine(); + System.out.println("Ok! Course " + counter + ": "+nameStudent+". Address: " + addressStudent + + ". Email: " + emailStudent +"."); + + auxiliarStudent = new Student(nameStudent, addressStudent,emailStudent); + studentList.add(auxiliarStudent); + + scanner.close(); + } + } + + + + + +} diff --git a/homework02/src/main/java/com/ironhack/Main.java b/homework02/src/main/java/com/ironhack/Main.java index 439b92b0..7a4b16fe 100644 --- a/homework02/src/main/java/com/ironhack/Main.java +++ b/homework02/src/main/java/com/ironhack/Main.java @@ -2,6 +2,21 @@ public class Main { public static void main(String[] args) { + int counterStudent = 1; + //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(); + serverMenu.selectCommand(); } } \ 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..6c6ac530 --- /dev/null +++ b/homework02/src/main/java/com/ironhack/ServerMenu.java @@ -0,0 +1,162 @@ +package com.ironhack; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Scanner; + +public class ServerMenu { + + public void showMenu(){ + System.out.println("\nFantastic! All data introduced is correct. Then, we provide a command list" + + "for managing or adding a new information."); + System.out.println("\nCOMMAND LIST: "); + System.out.println( + "- ENROLL [STUDENT_ID] [COURSE_ID] Example: ENROLL S3 C6" + + "\n- ASSIGN [TEACHER_ID] [COURSE_ID] Example: ASSIGN T2 C3" + + "\n- SHOW COURSES" + + "\n- LOOKUP COURSE [COURSE_ID] Example: LOOKUP COURSE C8" + + "\n- SHOW STUDENTS" + + "\n- LOOKUP STUDENT [STUDENT_ID] Example: LOOKUP STUDENT S5" + + "\n- SHOW TEACHERS" + + "\n- LOOKUP TEACHER [TEACHER_ID] Example: LOOKUP STUDENT T10" + + "\n- SHOW PROFIT"); + } + + public void selectCommand() throws IllegalArgumentException{ + Scanner scanner = new Scanner(System.in); + //No used + //String[] firstKeyWord = {"SHOW", "LOOKUP", "ENROLL", "ASSIGN"}; + //String[] secondKeyWord = {"COURSES", "STUDENTS", "TEACHERS"}; + String selectCommand; + + System.out.println("Select an options to consult:"); + selectCommand = scanner.nextLine(); + selectCommand = selectCommand.toUpperCase(); + + String[] wordToWord = selectCommand.split(" "); + List onlyWord = new ArrayList<>(); + + for (int i = 0; i Date: Wed, 26 Feb 2025 02:05:25 +0100 Subject: [PATCH 14/29] Function ShowStudent. There is a bug. --- .../java/com/ironhack/CommandFunction.java | 15 ++++++- .../main/java/com/ironhack/InitialMenu.java | 18 ++++---- .../src/main/java/com/ironhack/Main.java | 4 +- .../src/main/java/com/ironhack/Student.java | 41 +++++++++++++++---- 4 files changed, 61 insertions(+), 17 deletions(-) diff --git a/homework02/src/main/java/com/ironhack/CommandFunction.java b/homework02/src/main/java/com/ironhack/CommandFunction.java index 47f7d21d..a7747b73 100644 --- a/homework02/src/main/java/com/ironhack/CommandFunction.java +++ b/homework02/src/main/java/com/ironhack/CommandFunction.java @@ -1,6 +1,10 @@ package com.ironhack; +import java.util.ArrayList; +import java.util.List; + public class CommandFunction { + //Change the empty method. public void showProfit() { } @@ -8,7 +12,16 @@ public void showCourses() { } public void showStudents() { - + InitialMenu lista = new InitialMenu(); + + // If no courses in list + if (lista.studentList.isEmpty()) { + System.out.println( "There are no students at the school."); + } else { + for (Student student : lista.studentList) { + System.out.println(student.toString() + "\n"); + } + } } public void showTeachers() { diff --git a/homework02/src/main/java/com/ironhack/InitialMenu.java b/homework02/src/main/java/com/ironhack/InitialMenu.java index a9dc371e..e0c013f7 100644 --- a/homework02/src/main/java/com/ironhack/InitialMenu.java +++ b/homework02/src/main/java/com/ironhack/InitialMenu.java @@ -10,9 +10,10 @@ public class InitialMenu { int numberTeacher; int numberCourse; int numberStudent; - List teacherList = new ArrayList<>(); - List courseList = new ArrayList<>(); - List studentList = new ArrayList<>(); + public List teacherList = new ArrayList<>(); + public List courseList = new ArrayList<>(); + public List studentList = new ArrayList<>(); + public void startMenu(){ System.out.println("What is the school name?"); @@ -103,14 +104,17 @@ public void createStudent(){ addressStudent = scanner.nextLine(); emailStudent = scanner.next(); scanner.nextLine(); - System.out.println("Ok! Course " + counter + ": "+nameStudent+". Address: " + addressStudent + - ". Email: " + emailStudent +"."); auxiliarStudent = new Student(nameStudent, addressStudent,emailStudent); - studentList.add(auxiliarStudent); + //This line is for proving that email format is correct + auxiliarStudent.setEmail(emailStudent); - scanner.close(); + System.out.println("Ok! Student " + counter + ": "+nameStudent+". Address: " + addressStudent + + ". Email: " + emailStudent +"."); + + studentList.add(auxiliarStudent); } + } diff --git a/homework02/src/main/java/com/ironhack/Main.java b/homework02/src/main/java/com/ironhack/Main.java index 7a4b16fe..0bae5c70 100644 --- a/homework02/src/main/java/com/ironhack/Main.java +++ b/homework02/src/main/java/com/ironhack/Main.java @@ -1,8 +1,10 @@ package com.ironhack; public class Main { + public static int counterStudent = 1; + public static void main(String[] args) { - int counterStudent = 1; + //Menu InitialMenu initialMenu = new InitialMenu(); diff --git a/homework02/src/main/java/com/ironhack/Student.java b/homework02/src/main/java/com/ironhack/Student.java index 25b4466d..82da6412 100644 --- a/homework02/src/main/java/com/ironhack/Student.java +++ b/homework02/src/main/java/com/ironhack/Student.java @@ -1,18 +1,29 @@ package com.ironhack; +import java.util.regex.Pattern; + public class Student { private String studentId; private String name; private String address; - private String email; + 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; } @@ -41,8 +52,20 @@ public String getEmail() { return email; } + // Set email with validation public void setEmail(String email) { - this.email = 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() { @@ -53,10 +76,12 @@ public void setCourse(Course course) { this.course = course; } -// 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()); + @Override + public String toString() { + return "The studentId " + studentId + + " with name " + name + + " lives at the address " + address + + ". The email contact is " + email + + ". Course=" + course; + } } From 7aaa49c5bdaa91f95f58e1e1798f24d333f42066 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 26 Feb 2025 12:00:04 +0100 Subject: [PATCH 15/29] Add all command --- .../java/com/ironhack/CommandFunction.java | 70 ++++++++++++++++--- .../main/java/com/ironhack/InitialMenu.java | 10 +-- .../src/main/java/com/ironhack/Main.java | 6 ++ 3 files changed, 69 insertions(+), 17 deletions(-) diff --git a/homework02/src/main/java/com/ironhack/CommandFunction.java b/homework02/src/main/java/com/ironhack/CommandFunction.java index a7747b73..b5fb51a5 100644 --- a/homework02/src/main/java/com/ironhack/CommandFunction.java +++ b/homework02/src/main/java/com/ironhack/CommandFunction.java @@ -4,6 +4,8 @@ import java.util.List; public class CommandFunction { + + //Change the empty method. public void showProfit() { } @@ -12,14 +14,13 @@ public void showCourses() { } public void showStudents() { - InitialMenu lista = new InitialMenu(); + System.out.println(); - // If no courses in list - if (lista.studentList.isEmpty()) { + if (Main.studentList.isEmpty()) { System.out.println( "There are no students at the school."); } else { - for (Student student : lista.studentList) { - System.out.println(student.toString() + "\n"); + for (Student student : Main.studentList) { + System.out.println(student.toString()); } } } @@ -27,20 +28,69 @@ public void showStudents() { public void showTeachers() { } - public void lookupCourse(String idCourse) { + public void lookupCourse(String courseId) { } - public void lookupStudent(String idStudent) { + public void lookupStudent(String studentId) { } - public void lookupTeacher(String idTeacher) { + public void lookupTeacher(String teacherId) { + boolean isFound = false; + if (Main.teacherList.isEmpty()) { + System.out.println("There are no students."); + } else { + for (Teacher teacher : Main.teacherList) { + if (teacher.getTeacherId().equals(teacherId)) { + // Store found teacher info + teacher.toString(); + isFound = true; + break; + } + } + // If not found + if (!isFound) { + System.out.println("Not Found student with ID ( " + teacherId + " )."); + } + } } - public void enroll(String idStudent, String idCourse) { + public void enroll(String studentId, String courseId) { } - public void assign(String idTeacher, String idCourse) { + public void assign(String teacherId, String courseId) { + boolean isFoundTeacher = false; + boolean isFoundCourse = false; + int j=0; + int k=0; + if (Main.teacherList.isEmpty() || Main.courseList.isEmpty()) { + System.out.println("There are no students."); + } else { + for (int i = 0; i teacherList = new ArrayList<>(); - public List courseList = new ArrayList<>(); - public List studentList = new ArrayList<>(); - public void startMenu(){ System.out.println("What is the school name?"); @@ -47,7 +43,7 @@ public void createTeacher() throws IllegalArgumentException{ System.out.println("Ok! Teacher " + counter + ": "+nameTeacher+". Salary: "+salaryTeacher); auxiliarTeacher = new Teacher(nameTeacher, salaryTeacher); - teacherList.add(auxiliarTeacher); + Main.teacherList.add(auxiliarTeacher); } } @@ -76,7 +72,7 @@ public void createCourse() throws IllegalArgumentException{ System.out.println("Ok! Course " + counter + ": "+nameCourse+". Price: "+priceCourse); auxiliarCourse = new Course(nameCourse, priceCourse); - courseList.add(auxiliarCourse); + Main.courseList.add(auxiliarCourse); } } @@ -112,7 +108,7 @@ public void createStudent(){ System.out.println("Ok! Student " + counter + ": "+nameStudent+". Address: " + addressStudent + ". Email: " + emailStudent +"."); - studentList.add(auxiliarStudent); + Main.studentList.add(auxiliarStudent); } } diff --git a/homework02/src/main/java/com/ironhack/Main.java b/homework02/src/main/java/com/ironhack/Main.java index 0bae5c70..caeee81b 100644 --- a/homework02/src/main/java/com/ironhack/Main.java +++ b/homework02/src/main/java/com/ironhack/Main.java @@ -1,7 +1,13 @@ package com.ironhack; +import java.util.ArrayList; +import java.util.List; + public class Main { public static int counterStudent = 1; + public static List teacherList = new ArrayList<>(); + public static List courseList = new ArrayList<>(); + public static List studentList = new ArrayList<>(); public static void main(String[] args) { From 9f72b182d7aaba553fc32dd3a83c3d6490cae184 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 26 Feb 2025 12:20:42 +0100 Subject: [PATCH 16/29] Modified --- homework02/src/main/java/com/ironhack/InitialMenu.java | 8 +++++--- homework02/src/main/java/com/ironhack/Main.java | 2 -- homework02/src/main/java/com/ironhack/ServerMenu.java | 1 - 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/homework02/src/main/java/com/ironhack/InitialMenu.java b/homework02/src/main/java/com/ironhack/InitialMenu.java index 4eb7ec39..b49fb06a 100644 --- a/homework02/src/main/java/com/ironhack/InitialMenu.java +++ b/homework02/src/main/java/com/ironhack/InitialMenu.java @@ -1,7 +1,5 @@ package com.ironhack; -import java.util.ArrayList; -import java.util.List; import java.util.Scanner; public class InitialMenu { @@ -103,7 +101,11 @@ public void createStudent(){ auxiliarStudent = new Student(nameStudent, addressStudent,emailStudent); //This line is for proving that email format is correct - auxiliarStudent.setEmail(emailStudent); + try{ + auxiliarStudent.setEmail(emailStudent); + }catch (Exception e){ + System.out.println(e.getMessage()); + } System.out.println("Ok! Student " + counter + ": "+nameStudent+". Address: " + addressStudent + ". Email: " + emailStudent +"."); diff --git a/homework02/src/main/java/com/ironhack/Main.java b/homework02/src/main/java/com/ironhack/Main.java index caeee81b..993cbc47 100644 --- a/homework02/src/main/java/com/ironhack/Main.java +++ b/homework02/src/main/java/com/ironhack/Main.java @@ -10,8 +10,6 @@ public class Main { public static List studentList = new ArrayList<>(); public static void main(String[] args) { - - //Menu InitialMenu initialMenu = new InitialMenu(); ServerMenu serverMenu = new ServerMenu(); diff --git a/homework02/src/main/java/com/ironhack/ServerMenu.java b/homework02/src/main/java/com/ironhack/ServerMenu.java index 6c6ac530..1692b41d 100644 --- a/homework02/src/main/java/com/ironhack/ServerMenu.java +++ b/homework02/src/main/java/com/ironhack/ServerMenu.java @@ -1,7 +1,6 @@ package com.ironhack; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.Scanner; From 9d4a088c9fc558e271a8481b0c314958dfadd158 Mon Sep 17 00:00:00 2001 From: alvaro Date: Wed, 26 Feb 2025 13:23:43 +0100 Subject: [PATCH 17/29] Fixed tessts --- .../java/com/ironhack/CommandFunction.java | 2 +- .../src/main/java/com/ironhack/Course.java | 2 +- .../src/main/java/com/ironhack/Student.java | 8 ++- .../com/ironhack/CommandFunctionTest.java | 70 +++++++++++-------- 4 files changed, 49 insertions(+), 33 deletions(-) diff --git a/homework02/src/main/java/com/ironhack/CommandFunction.java b/homework02/src/main/java/com/ironhack/CommandFunction.java index 4120e716..ec02007c 100644 --- a/homework02/src/main/java/com/ironhack/CommandFunction.java +++ b/homework02/src/main/java/com/ironhack/CommandFunction.java @@ -24,7 +24,7 @@ public void showProfit() { // System.out.println("Total Profit: " + (totalMoneyCourses - totalSalaries)); System.out.println("The profit is: "+ (totalMoneyCourses - totalSalaries)); } - + public void showCourses() { String result = ""; diff --git a/homework02/src/main/java/com/ironhack/Course.java b/homework02/src/main/java/com/ironhack/Course.java index 29e06192..1b355822 100644 --- a/homework02/src/main/java/com/ironhack/Course.java +++ b/homework02/src/main/java/com/ironhack/Course.java @@ -73,7 +73,7 @@ public String toString() { ", name='" + name + '\'' + ", price=" + price + ", moneyEarned=" + moneyEarned + - ", teacher=" + teacher.getName() + + ", teacher=" + teacher + '}'; } } diff --git a/homework02/src/main/java/com/ironhack/Student.java b/homework02/src/main/java/com/ironhack/Student.java index 82da6412..870266ef 100644 --- a/homework02/src/main/java/com/ironhack/Student.java +++ b/homework02/src/main/java/com/ironhack/Student.java @@ -17,6 +17,8 @@ public Student(String name, String address, String 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"); @@ -28,9 +30,9 @@ public String getStudentId() { return studentId; } -// public void setStudentId(String studentId) { -// this.studentId = studentId; -// } + public void setStudentId(String studentId) { + this.studentId = studentId; + } public String getName() { return name; diff --git a/homework02/src/test/java/com/ironhack/CommandFunctionTest.java b/homework02/src/test/java/com/ironhack/CommandFunctionTest.java index 35beb490..cd254518 100644 --- a/homework02/src/test/java/com/ironhack/CommandFunctionTest.java +++ b/homework02/src/test/java/com/ironhack/CommandFunctionTest.java @@ -1,5 +1,6 @@ package com.ironhack; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -11,22 +12,23 @@ class CommandFunctionTest { private CommandFunction commandFunction; - private List courseList; - private List teacherList; - private List studentList; +// private List courseList; +// private List teacherList; +// private List studentList; @BeforeEach public void setUp() { - courseList = new ArrayList<>(); - teacherList = new ArrayList<>(); - studentList = new ArrayList<>(); +// 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)); - studentList.add(student1); - studentList.add(student2); + Main.studentList.add(student1); + Main.studentList.add(student2); // Teacher teacher1 = new Teacher("Alice", 1400); // Teacher teacher2 = new Teacher("Bob", 1300); @@ -38,7 +40,15 @@ public void setUp() { // courseList.add(javaCourse); // courseList.add(pythonCourse); - commandFunction = new CommandFunction(courseList, studentList, teacherList); + commandFunction = new CommandFunction(); + + } + + @AfterEach + public void tearDown(){ + Main.courseList = new ArrayList<>(); + Main.teacherList= new ArrayList<>(); + Main.studentList = new ArrayList<>(); } @@ -51,10 +61,10 @@ public void testShowCourses_WithCourses() { javaCourse.setTeacher(teacher1); Course pythonCourse = new Course("Python", 600); pythonCourse.setTeacher(teacher1); - courseList.add(javaCourse); - courseList.add(pythonCourse); + Main.courseList.add(javaCourse); + Main.courseList.add(pythonCourse); - String courses = commandFunction.showCourses(); + String courses = commandFunction.showCoursesTest(); assertTrue(courses.contains("Java")); assertTrue(courses.contains("Python")); @@ -63,59 +73,63 @@ public void testShowCourses_WithCourses() { // LOOK UP STUDENT Command Tests @Test void lookupStudent_Found() { - String student = commandFunction.lookupStudent("C-1"); - + String student = commandFunction.lookupStudentTest("S33"); + System.out.println(Main.studentList); assertTrue(student.contains("Diego")); } @Test public void lookupStudent_notFound() { - String student = commandFunction.lookupStudent("C-5"); - - assertEquals("Not Found student with ID ( C-5 ).", student); + 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() { - assertEquals(0, commandFunction.showProfit()); + + 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); - teacherList.add(teacher1); - teacherList.add(teacher2); + 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); - courseList.add(javaCourse); - courseList.add(pythonCourse); + Main.courseList.add(javaCourse); + Main.courseList.add(pythonCourse); // (500+300) - (400+200) = 200 - assertEquals(200, commandFunction.showProfit()); + assertEquals(200, commandFunction.showProfitTest()); } @Test void showProfit_teachersEarnMoreThanCourses_negativeProfit() { Teacher teacher1 = new Teacher("Alice", 1400); Teacher teacher2 = new Teacher("Bob", 1200); - teacherList.add(teacher1); - teacherList.add(teacher2); + 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); - courseList.add(javaCourse); - courseList.add(pythonCourse); + Main.courseList.add(javaCourse); + Main.courseList.add(pythonCourse); // (500+300) - (1400+1200) = -1800 - assertEquals(-1800, commandFunction.showProfit()); + assertEquals(-1800, commandFunction.showProfitTest()); } } \ No newline at end of file From ffd02496cb8ba3b94b9097fcc36bfaa00d0f50ee Mon Sep 17 00:00:00 2001 From: alvaro Date: Wed, 26 Feb 2025 18:28:53 +0100 Subject: [PATCH 18/29] preparing merge --- .../java/com/ironhack/CommandFunction.java | 211 +++++------------- .../src/main/java/com/ironhack/Main.java | 12 +- .../src/main/java/com/ironhack/School.java | 71 ------ .../main/java/com/ironhack/ServerMenu.java | 112 ++++++++++ 4 files changed, 175 insertions(+), 231 deletions(-) delete mode 100644 homework02/src/main/java/com/ironhack/School.java create mode 100644 homework02/src/main/java/com/ironhack/ServerMenu.java diff --git a/homework02/src/main/java/com/ironhack/CommandFunction.java b/homework02/src/main/java/com/ironhack/CommandFunction.java index d7823843..0695b83e 100644 --- a/homework02/src/main/java/com/ironhack/CommandFunction.java +++ b/homework02/src/main/java/com/ironhack/CommandFunction.java @@ -5,180 +5,83 @@ import java.util.Scanner; public class CommandFunction { - private School school; - public CommandFunction(School school1) { - this.school = school1; + public CommandFunction() { } - public boolean getUserCommand(){ - boolean option = true; - Scanner scanner = new Scanner(System.in); - System.out.println("\nPlease introduce a command:"); - String cmd = scanner.nextLine(); - if(cmd.equals("EXIT")){ - option = false; - }else{ - try{ - sanitize(cmd); - } catch (Exception e) { - //System.out.println("Command not recognized, please try again."); - System.out.println(e.getMessage()); - } - } - 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("Too many arguments."); - if (values.length < 2 && !values[0].equals("EXIT")) throw new IllegalArgumentException("Too few arguments."); + public void showTeachers() { + System.out.println(); - - // COMMANDO ENROLL - if(values.length == 3 && values[0].equals("ENROLL")){ - if(school.isValidStudentId(values[1]) && school.isValidCourseId(values[2])){ - executeCommandEnroll(values[1],values[2]); - }else{ - throw new IllegalArgumentException("Parameters for ENROLL are incorrect."); - } - // COMMANDO ASSIGN - }else if (values.length == 3 && values[0].equals("ASSIGN")){ - if(school.isValidTeacherId(values[1]) && school.isValidCourseId(values[2])){ - executeCommandAssign(values[1],values[2]); - }else{ - throw new IllegalArgumentException("Parameters for ASSIGN are incorrect."); + if (Main.teacherList.isEmpty()) { + System.out.println( "There are no teachers at the school."); + } else { + for (Teacher teacher : Main.teacherList) { + System.out.println(teacher.toString()); } - // COMMANDO SHOW - }else if(values[0].equals("SHOW") && values.length == 2){ - if(values[1].equals("COURSES") || values[1].equals("STUDENTS") || values[1].equals("TEACHERS") || values[1].equals("PROFIT")){ - executeCommandShow(values[1]); - }else{ - throw new IllegalArgumentException("Invalid parameters for SHOW."); - } - // COMMANDO LOOKUP - }else if(values[0].equals("LOOKUP") && values.length == 3) { - if (values[1].equals("STUDENT") && school.isValidStudentId(values[2])) { - executeCommandLookUpStudent(values[2]); - } else if (values[1].equals("COURSE") && school.isValidCourseId(values[2])) { - executeCommandLookUpCourse(values[2]); - } else if (values[1].equals("TEACHER") && school.isValidTeacherId(values[2])) { - executeCommandLookUpTeacher(values[2]); - } else { - throw new IllegalArgumentException("Invalid parameters for LOOKUP"); - } - }else{ - throw new IllegalArgumentException("Invalid Command."); } - //return sanitize; } - private void executeCommandLookUpTeacher(String teacherId) { - // GET TEACHER - for (int i = 0; i < school.getTeacherList().size(); i++){ - if(school.getTeacherList().get(i).getTeacherId().equals(teacherId)){ - // TEACHER FOUND - System.out.println(school.getTeacherList().get(i).toString()); - break; + public void lookupCourse(String courseId){ + boolean found = false; + String result = ""; + // If no students in list + if (Main.courseList.isEmpty()) { + result = "There are no courses."; + } else { + for (Course course : Main.courseList) { + if (course.getCourseId().equals(courseId)) { + // Store found student info + result = course.toString(); + found = true; + break; + } } - } - } - - private void executeCommandLookUpCourse(String courseId) { - // GET COURSE - for (int i = 0; i < school.getCourseList().size(); i++){ - if(school.getCourseList().get(i).getCourseId().equals(courseId)){ - // COURSE FOUND - System.out.println(school.getCourseList().get(i).toString()); - break; + // If not found + if (!found) { + result = "Not Found course with ID ( " + courseId + " )."; } } + System.out.println(result); } - private void executeCommandLookUpStudent(String studentId) { - // GET STUDENT - for (int i = 0; i < school.getStudentList().size(); i++){ - if(school.getStudentList().get(i).getStudentId().equals(studentId)){ - // STUDENT FOUND - System.out.println(school.getStudentList().get(i).toString()); - break; - } - } - } + public void enroll(String studentId, String courseId) { + boolean isFoundStudent = false; + boolean isFoundCourse = false; + int j=0; + int k=0; - private void executeCommandShow(String element) { - if(element.equals("STUDENTS")){ - System.out.println(school.getStudentList()); - }else if(element.equals("TEACHERS")){ - System.out.println(school.getTeacherList()); - }else if(element.equals("COURSES")){ - System.out.println(school.getCourseList()); - }else{ // PROFIT - double a = 0; - double b = 0; - for(Course c: school.getCourseList()){ - a+=c.getMoney_earned(); + if (Main.studentList.isEmpty() || Main.courseList.isEmpty()) { + System.out.println("Something went wrong."); + } else { + for (int i = 0; i list = school.getTeacherList(); - List list2 = school.getCourseList(); - Teacher teacher = new Teacher("a",1); - Course course = new Course("c",1); - // GET TEACHER - for (int i = 0; i < list.size(); i++){ - if(list.get(i).getTeacherId().equals(teacherId)){ - // TEACHER FOUND - teacher = list.get(i); - break; - } - } - for(int i = 0; i < list2.size(); i++){ - if(list2.get(i).getCourseId().equals(courseId)){ - course = list2.get(i); - course.setTeacher(teacher); - list2.set(i,course); - break; + for (int i = 0; i list = school.getStudentList(); - List list2 = school.getCourseList(); - Course course = new Course("c",1); - Student student = new Student("a","a","a"); - for(int i = 0; i < list2.size(); i++){ - if(list2.get(i).getCourseId().equals(courseId)){ - course = list2.get(i); - course.setMoney_earned(course.getMoney_earned()+course.getPrice()); - list2.set(i,course); - break; - } - } - for (int i = 0; i < list.size(); i++){ - if(list.get(i).getStudentId().equals(studentId)){ - // STUDENT FOUND - student = list.get(i); - student.setCourse(course); - list.set(i,student); - break; + if (!isFoundStudent) { + System.out.println("Not Found student with ID ( " + studentId + " )."); + } else if (!isFoundCourse) { + System.out.println("Not Found course with ID ( " + courseId + " )."); + }else{ + Course c = Main.courseList.get(k); + Student s = Main.studentList.get(j); + s.setCourse(c); + Main.studentList.set(j,s); + System.out.println(Main.studentList.get(j).toString()); } } - school.setCourseList(list2); - school.setStudentList(list); } - private void executeCommand(String args) { - System.out.println("The following command will be executed: " + args); - } + } diff --git a/homework02/src/main/java/com/ironhack/Main.java b/homework02/src/main/java/com/ironhack/Main.java index 17e10728..b63c0ff9 100644 --- a/homework02/src/main/java/com/ironhack/Main.java +++ b/homework02/src/main/java/com/ironhack/Main.java @@ -7,6 +7,10 @@ public class Main { public static int global_int = 0; public static int global_course_id = 0; public static int global_student_id = 0; + public static List teacherList = new ArrayList<>(); + public static List studentList = new ArrayList<>(); + public static List courseList = new ArrayList<>(); + public static void main(String[] args) { //Menu @@ -24,9 +28,6 @@ public static void main(String[] args) { System.out.println(johny.getStudentId()); System.out.println(janey.getStudentId()); - List teacherList = new ArrayList<>(); - List studentList = new ArrayList<>(); - List courseList = new ArrayList<>(); teacherList.add(john); teacherList.add(jane); studentList.add(johny); @@ -34,12 +35,11 @@ public static void main(String[] args) { courseList.add(math); courseList.add(art); - School school = new School(teacherList,studentList,courseList); - CommandFunction command = new CommandFunction(school); + CommandFunction command = new CommandFunction(); boolean res = true; do{ - res = command.getUserCommand(); + res = serverMenu.getUserCommand(); }while(res); } } \ No newline at end of file diff --git a/homework02/src/main/java/com/ironhack/School.java b/homework02/src/main/java/com/ironhack/School.java deleted file mode 100644 index 829bfa5a..00000000 --- a/homework02/src/main/java/com/ironhack/School.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.ironhack; - -import java.util.ArrayList; -import java.util.List; - -public class School { - - List teacherList = new ArrayList<>(); - List studentList = new ArrayList<>(); - List courseList = new ArrayList<>(); - - public School(List teacherList, List studentList, List courseList) { - this.teacherList = teacherList; - this.studentList = studentList; - this.courseList = courseList; - } - - public boolean isValidStudentId(String value) { - boolean result = false; - for(Student s: getStudentList()){ - if(s.getStudentId().equals(value)){ - result = true; - } - } - return result; - } - - public boolean isValidCourseId(String value) { - boolean result = false; - for(Course c: getCourseList()){ - if(c.getCourseId().equals(value)){ - result = true; - } - } - return result; - } - - public boolean isValidTeacherId(String value) { - boolean result = false; - for(Teacher t: getTeacherList()){ - if(t.getTeacherId().equals(value)){ - result = true; - } - } - return result; - } - - public List getTeacherList() { - return teacherList; - } - - public void setTeacherList(List teacherList) { - this.teacherList = teacherList; - } - - public List getStudentList() { - return studentList; - } - - public void setStudentList(List studentList) { - this.studentList = studentList; - } - - public List getCourseList() { - return courseList; - } - - public void setCourseList(List courseList) { - this.courseList = courseList; - } -} 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..d9030232 --- /dev/null +++ b/homework02/src/main/java/com/ironhack/ServerMenu.java @@ -0,0 +1,112 @@ +package com.ironhack; + +import java.util.Scanner; + +public class ServerMenu { + public ServerMenu() { + } + + public boolean selectCommand(){ + boolean option = true; + Scanner scanner = new Scanner(System.in); + System.out.println("\nPlease introduce a command:"); + String cmd = scanner.nextLine(); + if(cmd.equals("EXIT")){ + option = false; + }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("Too many arguments."); + if (values.length < 2 && !values[0].equals("EXIT")) throw new IllegalArgumentException("Too few arguments."); + + 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("Parameters for ENROLL are incorrect."); + } + // 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("Parameters for ASSIGN are incorrect."); + } + // COMMANDO SHOW + }else if(values[0].equals("SHOW") && values.length == 2){ + if(values[1].equals("COURSES")){ + command.showCourse(); + }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("Invalid parameters for SHOW."); + } + // 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("Invalid parameters for LOOKUP"); + } + }else{ + throw new IllegalArgumentException("Invalid Command."); + } + //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; + } +} From 5e921672ae9132fa64ab73610b0c913267fce201 Mon Sep 17 00:00:00 2001 From: alvaro Date: Wed, 26 Feb 2025 19:04:03 +0100 Subject: [PATCH 19/29] fixed some bugs --- .../src/main/java/com/ironhack/CommandFunction.java | 12 +++++++----- .../src/main/java/com/ironhack/InitialMenu.java | 6 +++--- homework02/src/main/java/com/ironhack/Main.java | 1 + .../src/main/java/com/ironhack/ServerMenu.java | 3 ++- homework02/src/main/java/com/ironhack/Teacher.java | 2 +- 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/homework02/src/main/java/com/ironhack/CommandFunction.java b/homework02/src/main/java/com/ironhack/CommandFunction.java index 215f2d3c..53d1628c 100644 --- a/homework02/src/main/java/com/ironhack/CommandFunction.java +++ b/homework02/src/main/java/com/ironhack/CommandFunction.java @@ -28,7 +28,6 @@ public void showProfit() { System.out.println("The profit is: "+ (totalMoneyCourses - totalSalaries)); } - public void showCourses() { String result = ""; // If no courses in list @@ -115,23 +114,24 @@ public void lookupStudent(String studentId) { public void lookupTeacher(String teacherId) { boolean isFound = false; - + String result = ""; if (Main.teacherList.isEmpty()) { - System.out.println("There are no students."); + System.out.println("There are no teachers."); } else { for (Teacher teacher : Main.teacherList) { if (teacher.getTeacherId().equals(teacherId)) { // Store found teacher info - teacher.toString(); + result=teacher.toString(); isFound = true; break; } } // If not found if (!isFound) { - System.out.println("Not Found student with ID ( " + teacherId + " )."); + result="Not Found teacher with ID ( " + teacherId + " )."; } } + System.out.println(result); } public void enroll(String studentId, String courseId) { @@ -165,6 +165,8 @@ public void enroll(String studentId, String courseId) { System.out.println("Not Found course with ID ( " + courseId + " )."); }else{ Course c = Main.courseList.get(k); + c.setMoneyEarned(c.getMoneyEarned()+c.getPrice()); + Main.courseList.set(k,c); Student s = Main.studentList.get(j); s.setCourse(c); Main.studentList.set(j,s); diff --git a/homework02/src/main/java/com/ironhack/InitialMenu.java b/homework02/src/main/java/com/ironhack/InitialMenu.java index b49fb06a..885a824a 100644 --- a/homework02/src/main/java/com/ironhack/InitialMenu.java +++ b/homework02/src/main/java/com/ironhack/InitialMenu.java @@ -116,7 +116,7 @@ public void createStudent(){ } - - - + public void closeScanner() { + this.scanner.close(); + } } diff --git a/homework02/src/main/java/com/ironhack/Main.java b/homework02/src/main/java/com/ironhack/Main.java index f64d246a..aed6eda9 100644 --- a/homework02/src/main/java/com/ironhack/Main.java +++ b/homework02/src/main/java/com/ironhack/Main.java @@ -24,6 +24,7 @@ public static void main(String[] args) { initialMenu.createCourse(); initialMenu.numberStudent(); initialMenu.createStudent(); + //initialMenu.closeScanner(); serverMenu.showMenu(); boolean res = true; diff --git a/homework02/src/main/java/com/ironhack/ServerMenu.java b/homework02/src/main/java/com/ironhack/ServerMenu.java index 1b97a292..ef2bbf1c 100644 --- a/homework02/src/main/java/com/ironhack/ServerMenu.java +++ b/homework02/src/main/java/com/ironhack/ServerMenu.java @@ -31,6 +31,7 @@ public boolean selectCommand(){ String cmd = scanner.nextLine(); if(cmd.equals("EXIT")){ option = false; + scanner.close(); }else{ try{ sanitize(cmd); @@ -39,7 +40,7 @@ public boolean selectCommand(){ System.out.println(e.getMessage()); } } - scanner.close(); + //scanner.close(); return option; } diff --git a/homework02/src/main/java/com/ironhack/Teacher.java b/homework02/src/main/java/com/ironhack/Teacher.java index 96ccbaef..6ca345f0 100644 --- a/homework02/src/main/java/com/ironhack/Teacher.java +++ b/homework02/src/main/java/com/ironhack/Teacher.java @@ -17,7 +17,7 @@ public Teacher(String name, double salary) { private String autoTeacherId() { // return "T-" + UUID.randomUUID().toString().substring(0, 3); // T-XXX Main.teacherId++; - return "C" + Main.teacherId; + return "T" + Main.teacherId; } public String getTeacherId() { From 1fb4d1aa6a5c5c3914fa99250cb605a52838af50 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 27 Feb 2025 11:04:22 +0100 Subject: [PATCH 20/29] Modified the closeScanner --- homework02/src/main/java/com/ironhack/InitialMenu.java | 5 ----- homework02/src/main/java/com/ironhack/Main.java | 5 ++++- homework02/src/main/java/com/ironhack/ServerMenu.java | 7 ++++++- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/homework02/src/main/java/com/ironhack/InitialMenu.java b/homework02/src/main/java/com/ironhack/InitialMenu.java index 885a824a..f65f9fef 100644 --- a/homework02/src/main/java/com/ironhack/InitialMenu.java +++ b/homework02/src/main/java/com/ironhack/InitialMenu.java @@ -114,9 +114,4 @@ public void createStudent(){ } } - - - public void closeScanner() { - this.scanner.close(); - } } diff --git a/homework02/src/main/java/com/ironhack/Main.java b/homework02/src/main/java/com/ironhack/Main.java index aed6eda9..1efa9cc4 100644 --- a/homework02/src/main/java/com/ironhack/Main.java +++ b/homework02/src/main/java/com/ironhack/Main.java @@ -24,12 +24,15 @@ public static void main(String[] args) { initialMenu.createCourse(); initialMenu.numberStudent(); initialMenu.createStudent(); - //initialMenu.closeScanner(); + 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 index ef2bbf1c..3c5aca6f 100644 --- a/homework02/src/main/java/com/ironhack/ServerMenu.java +++ b/homework02/src/main/java/com/ironhack/ServerMenu.java @@ -5,6 +5,7 @@ import java.util.Scanner; public class ServerMenu { + Scanner scanner = new Scanner(System.in); public ServerMenu() { } @@ -26,7 +27,7 @@ public void showMenu() { public boolean selectCommand(){ boolean option = true; - Scanner scanner = new Scanner(System.in); + System.out.println("\nPlease introduce a command:"); String cmd = scanner.nextLine(); if(cmd.equals("EXIT")){ @@ -128,4 +129,8 @@ public boolean isValidTeacherId(String value) { } return result; } + + public void closeScanner() { + this.scanner.close(); + } } \ No newline at end of file From 200eeb98d0ed06be975b7614e45375eff109f40b Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 27 Feb 2025 11:11:53 +0100 Subject: [PATCH 21/29] Modified exit --- homework02/src/main/java/com/ironhack/ServerMenu.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/homework02/src/main/java/com/ironhack/ServerMenu.java b/homework02/src/main/java/com/ironhack/ServerMenu.java index 3c5aca6f..5bccf23d 100644 --- a/homework02/src/main/java/com/ironhack/ServerMenu.java +++ b/homework02/src/main/java/com/ironhack/ServerMenu.java @@ -22,7 +22,8 @@ public void showMenu() { "\n- LOOKUP STUDENT [STUDENT_ID] Example: LOOKUP STUDENT S5" + "\n- SHOW TEACHERS" + "\n- LOOKUP TEACHER [TEACHER_ID] Example: LOOKUP STUDENT T10" + - "\n- SHOW PROFIT"); + "\n- SHOW PROFIT" + + "\n- EXIT"); } public boolean selectCommand(){ From 551610461e5d8f704e127a37e36f266238bdeb95 Mon Sep 17 00:00:00 2001 From: Fernando Date: Thu, 27 Feb 2025 11:14:33 +0100 Subject: [PATCH 22/29] Add Extra Commands --- .../java/com/ironhack/CommandFunction.java | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/homework02/src/main/java/com/ironhack/CommandFunction.java b/homework02/src/main/java/com/ironhack/CommandFunction.java index 53d1628c..fa0b913e 100644 --- a/homework02/src/main/java/com/ironhack/CommandFunction.java +++ b/homework02/src/main/java/com/ironhack/CommandFunction.java @@ -269,6 +269,59 @@ public double showProfitTest() { } // EXTRA COMMANDS + public void showStudentsByCourse(String courseId) { + boolean found = false; + for (Course course : Main.courseList) { + if (course.getCourseId().equals(courseId)) { + found = true; + System.out.println("Students enrolled in the course " + courseId + ":"); + for (Student student : Main.studentList) { + if (student.getCourse() != null && student.getCourse().getCourseId().equals(courseId)) { + System.out.println(student); + } + } + break; + } + } + if (!found) { + System.out.println("Course with ID " + courseId + " not found."); + } + } + + public void showMoneyEarned() { + double totalMoneyEarned = 0; + for (Course course : Main.courseList) { + totalMoneyEarned += course.getMoneyEarned(); + } + System.out.println("Total money earned from all courses: " + totalMoneyEarned); + } + + public void showMoneySpent() { + double totalMoneySpent = 0; + for (Teacher teacher : Main.teacherList) { + totalMoneySpent += teacher.getSalary(); + } + System.out.println("Total money spent on teachers' salaries: " + totalMoneySpent); + } + + public void showTeachersByCourse(String courseId) { + boolean found = false; + for (Course course : Main.courseList) { + if (course.getCourseId().equals(courseId)) { + found = true; + System.out.println("Teachers assigned to course " + courseId + ":"); + if (course.getTeacher() != null) { + System.out.println(course.getTeacher()); + } else { + System.out.println("No teacher assigned to this course."); + } + break; + } + } + if (!found) { + System.out.println("Course with ID " + courseId + " not found."); + } + } } From 8dc035435e396a462dddfe1391d9f540fc5d1d0e Mon Sep 17 00:00:00 2001 From: alvaro Date: Thu, 27 Feb 2025 11:16:02 +0100 Subject: [PATCH 23/29] added to upper case --- homework02/src/main/java/com/ironhack/ServerMenu.java | 1 + 1 file changed, 1 insertion(+) diff --git a/homework02/src/main/java/com/ironhack/ServerMenu.java b/homework02/src/main/java/com/ironhack/ServerMenu.java index 5bccf23d..32e6418f 100644 --- a/homework02/src/main/java/com/ironhack/ServerMenu.java +++ b/homework02/src/main/java/com/ironhack/ServerMenu.java @@ -31,6 +31,7 @@ public boolean selectCommand(){ System.out.println("\nPlease introduce a command:"); String cmd = scanner.nextLine(); + cmd = cmd.toUpperCase(); if(cmd.equals("EXIT")){ option = false; scanner.close(); From 80938f546b09864c119d61dd821f5dbf37052757 Mon Sep 17 00:00:00 2001 From: alvaro Date: Thu, 27 Feb 2025 19:55:05 +0100 Subject: [PATCH 24/29] added art and extra functions --- .../src/main/java/com/ironhack/Course.java | 24 ++++++++++------- .../main/java/com/ironhack/InitialMenu.java | 27 +++++++++++++++++++ .../main/java/com/ironhack/ServerMenu.java | 13 +++++++++ .../src/main/java/com/ironhack/Student.java | 21 +++++++++++---- .../src/main/java/com/ironhack/Teacher.java | 16 +++++++---- 5 files changed, 82 insertions(+), 19 deletions(-) diff --git a/homework02/src/main/java/com/ironhack/Course.java b/homework02/src/main/java/com/ironhack/Course.java index a89669de..0c82c8f8 100644 --- a/homework02/src/main/java/com/ironhack/Course.java +++ b/homework02/src/main/java/com/ironhack/Course.java @@ -1,7 +1,5 @@ package com.ironhack; -import java.util.UUID; - public class Course { private String courseId; private String name; @@ -65,12 +63,20 @@ public void setTeacher(Teacher teacher) { @Override public String toString() { - return "Course{" + - "courseId='" + courseId + '\'' + - ", name='" + name + '\'' + - ", price=" + price + - ", moneyEarned=" + moneyEarned + - ", teacher=" + teacher + - '}'; + String teacherText; + try{ + teacherText = this.teacher.getName(); + } catch (Exception e) { + teacherText = "assignment pending"; + } + + return "(\\ \n" + + "\\'\\ "+name+"\n" + + " \\'\\ __________ By: "+ teacherText +" \n" + + " / '| ()_________) Price: "+price+" $\n" + + " \\ '/ \\ ~~~~~~~~ \\ MoneyEarned: "+moneyEarned+" $ \n" + + " \\ \\ ~~~~~~ \\ \n" + + " ==). \\__________\\ ID: '"+courseId+"'\n" + + " (__) ()__________)\n"; } } diff --git a/homework02/src/main/java/com/ironhack/InitialMenu.java b/homework02/src/main/java/com/ironhack/InitialMenu.java index f65f9fef..3b7e1a72 100644 --- a/homework02/src/main/java/com/ironhack/InitialMenu.java +++ b/homework02/src/main/java/com/ironhack/InitialMenu.java @@ -10,6 +10,7 @@ public class InitialMenu { int numberStudent; public void startMenu(){ + showArt(); System.out.println("What is the school name?"); nameSchool = scanner.nextLine(); System.out.println("Ok! Welcome to the " + nameSchool +"!"); @@ -114,4 +115,30 @@ public void createStudent(){ } } + + private void showArt() { + String art = + " /\\ /\\\n" + + " ||______||\n" + + " || ^ ^ ||\n" + + " \\| | | |/\n" + + " |______|\n" + + " __ | __ |\n" + + " / \\ ________________________|_/ \\_|__\n" + + " / ^^ \\ /=========================/ ^^ \\===|\n" + + " / [] \\ /=========================/ [] \\==|\n" + + " /________\\ /=========================/________\\=|\n" + + " * | |/==========================| |=|\n" + + " *** | ^^ ^^ |---------------------------| ^^ ^^ |--\n" + + " *****| [] [] | IRON SCHOOL | [] [] | |\n" + + " *****| [] [] | _____ | [] [] | |\n" + + " ******* | /_____\\ | * | |\n" + + " *********^^ ^^ | ^^ ^^ | | | ^^ ^^ | ***| |\n" + + " ***********] [] | [] [] | | | [] [] | ===***** |\n" + + " ************* | @|__|__|@ |/ |*******|\n" + + "*************** ***********--=====--**********| *********\n" + + "***************___*********** |=====| **********|***********\n" + + " ************* ********* /=======\\ ******** | *********\n"; + System.out.println(art); + } } diff --git a/homework02/src/main/java/com/ironhack/ServerMenu.java b/homework02/src/main/java/com/ironhack/ServerMenu.java index 32e6418f..9ed9f47d 100644 --- a/homework02/src/main/java/com/ironhack/ServerMenu.java +++ b/homework02/src/main/java/com/ironhack/ServerMenu.java @@ -93,6 +93,19 @@ public void sanitize(String args){ } else { throw new IllegalArgumentException("Invalid parameters for LOOKUP"); } + //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("Invalid Command."); } diff --git a/homework02/src/main/java/com/ironhack/Student.java b/homework02/src/main/java/com/ironhack/Student.java index 870266ef..95f98525 100644 --- a/homework02/src/main/java/com/ironhack/Student.java +++ b/homework02/src/main/java/com/ironhack/Student.java @@ -80,10 +80,21 @@ public void setCourse(Course course) { @Override public String toString() { - return "The studentId " + studentId + - " with name " + name + - " lives at the address " + address + - ". The email contact is " + email + - ". Course=" + course; + String courseText; + try{ + courseText = this.course.getName(); + } catch (Exception e) { + courseText = "assignment 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/Teacher.java b/homework02/src/main/java/com/ironhack/Teacher.java index 6ca345f0..7dcccebe 100644 --- a/homework02/src/main/java/com/ironhack/Teacher.java +++ b/homework02/src/main/java/com/ironhack/Teacher.java @@ -42,10 +42,16 @@ public void setSalary(double salary) { @Override public String toString() { - return "Teacher{" + - "teacherId='" + teacherId + '\'' + - ", name='" + name + '\'' + - ", salary=" + salary + - '}'; + + return " ||=|=|=|=|=| | __..\\/ | |_| ||#|| |\n" + + " || | | | | | /\\ \\ \\\\|++|=| ||#|| | TEACHER\n" + + " ||_|_|_|_|_|__/_/\\_.___\\__|_|__||_||__| "+name+"\n" + + " |___________ /\\~()/()~//\\ ____________|\n" + + " | __ __ \\_ (_ . _/ _ _ _ _ _| Salary: "+salary+" $\n" + + " ||~~|_|..| _ \\ //\\\\ / |=|__|~|~| | | \n" + + " ||--|+|^^|| |__/\\ __ /\\__| |==|x|x| | | \n" + + " ||__|_|__|| / \\ \\ / / \\_|__|_|_|_|_| ID: '"+teacherId+"'\n" + + " |________ _/ \\/\\/\\/ \\_ _________| \n" + + " | _____ |/ \\../ \\| __ ___|\n"; } } From 2df546a51de5dd7e0903398c57580ba288751600 Mon Sep 17 00:00:00 2001 From: Fernando Date: Thu, 27 Feb 2025 20:38:10 +0100 Subject: [PATCH 25/29] Colors Styles --- .../java/com/ironhack/CommandFunction.java | 38 +++++++------ .../main/java/com/ironhack/InitialMenu.java | 57 ++++++++++++------- .../main/java/com/ironhack/ServerMenu.java | 41 ++++++------- .../src/main/java/com/ironhack/Style.java | 35 ++++++++++++ 4 files changed, 112 insertions(+), 59 deletions(-) create mode 100644 homework02/src/main/java/com/ironhack/Style.java diff --git a/homework02/src/main/java/com/ironhack/CommandFunction.java b/homework02/src/main/java/com/ironhack/CommandFunction.java index fa0b913e..25e14faf 100644 --- a/homework02/src/main/java/com/ironhack/CommandFunction.java +++ b/homework02/src/main/java/com/ironhack/CommandFunction.java @@ -24,15 +24,19 @@ public void showProfit() { totalSalaries += teacher.getSalary(); } -// System.out.println("Total Profit: " + (totalMoneyCourses - totalSalaries)); - System.out.println("The profit is: "+ (totalMoneyCourses - totalSalaries)); + 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 = "There are no courses available."; + result = Style.NOT_FOUND + "There are no courses available." + Style.RESET; } else { for (Course course : Main.courseList) { // Append course to the result string @@ -46,7 +50,7 @@ public void showStudents() { System.out.println(); if (Main.studentList.isEmpty()) { - System.out.println( "There are no students at the school."); + 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()); @@ -58,7 +62,7 @@ public void showTeachers() { System.out.println(); if (Main.teacherList.isEmpty()) { - System.out.println( "There are no teachers at the school."); + 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()); @@ -71,7 +75,7 @@ public void lookupCourse(String courseId){ String result = ""; // If no students in list if (Main.courseList.isEmpty()) { - result = "There are no courses."; + result = Style.NOT_FOUND + "There are no courses." + Style.RESET; } else { for (Course course : Main.courseList) { if (course.getCourseId().equals(courseId)) { @@ -83,7 +87,7 @@ public void lookupCourse(String courseId){ } // If not found if (!found) { - result = "Not Found course with ID ( " + courseId + " )."; + result = Style.NOT_FOUND + "Not Found course with ID ( " + courseId + " )." + Style.RESET; } } System.out.println(result); @@ -94,7 +98,7 @@ public void lookupStudent(String studentId) { String result = ""; // If no students in list if (Main.studentList.isEmpty()) { - result = "There are no students."; + result = Style.NOT_FOUND + "There are no students." + Style.RESET; } else { for (Student student : Main.studentList) { if (student.getStudentId().equals(studentId)) { @@ -106,7 +110,7 @@ public void lookupStudent(String studentId) { } // If not found if (!found) { - result = "Not Found student with ID ( " + studentId + " )."; + result = Style.NOT_FOUND + "Not Found student with ID ( " + studentId + " )." + Style.RESET; } } System.out.println(result); @@ -116,7 +120,7 @@ public void lookupTeacher(String teacherId) { boolean isFound = false; String result = ""; if (Main.teacherList.isEmpty()) { - System.out.println("There are no teachers."); + System.out.println(Style.NOT_FOUND + "There are no teachers." + Style.RESET); } else { for (Teacher teacher : Main.teacherList) { if (teacher.getTeacherId().equals(teacherId)) { @@ -128,7 +132,7 @@ public void lookupTeacher(String teacherId) { } // If not found if (!isFound) { - result="Not Found teacher with ID ( " + teacherId + " )."; + result=Style.NOT_FOUND + "Not Found teacher with ID ( " + teacherId + " )." + Style.RESET; } } System.out.println(result); @@ -141,7 +145,7 @@ public void enroll(String studentId, String courseId) { int k=0; if (Main.studentList.isEmpty() || Main.courseList.isEmpty()) { - System.out.println("Something went wrong."); + System.out.println(Style.ERROR + "Something went wrong." + Style.RESET); } else { for (int i = 0; i 3 && !values[0].equals("EXIT")) throw new IllegalArgumentException("Too many arguments."); - if (values.length < 2 && !values[0].equals("EXIT")) throw new IllegalArgumentException("Too few arguments."); + 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(); @@ -60,14 +61,14 @@ public void sanitize(String args){ if(isValidStudentId(values[1]) && isValidCourseId(values[2])){ command.enroll(values[1],values[2]); }else{ - throw new IllegalArgumentException("Parameters for ENROLL are incorrect."); + 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("Parameters for ASSIGN are incorrect."); + throw new IllegalArgumentException(Style.ERROR + "Parameters for ASSIGN are incorrect." + Style.RESET); } // COMMANDO SHOW }else if(values[0].equals("SHOW") && values.length == 2){ @@ -80,7 +81,7 @@ public void sanitize(String args){ }else if(values[1].equals("PROFIT")){ command.showProfit(); }else{ - throw new IllegalArgumentException("Invalid parameters for SHOW."); + throw new IllegalArgumentException(Style.ERROR + "Invalid parameters for SHOW." + Style.RESET); } // COMMANDO LOOKUP }else if(values[0].equals("LOOKUP") && values.length == 3) { @@ -91,10 +92,10 @@ public void sanitize(String args){ } else if (values[1].equals("TEACHER") && isValidTeacherId(values[2])) { command.lookupTeacher(values[2]); } else { - throw new IllegalArgumentException("Invalid parameters for LOOKUP"); + throw new IllegalArgumentException(Style.ERROR + "Invalid parameters for LOOKUP." + Style.RESET); } }else{ - throw new IllegalArgumentException("Invalid Command."); + throw new IllegalArgumentException(Style.ERROR + "Invalid Command." + Style.RESET); } //return sanitize; } 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"; + } +} From 3e1662696595969e9924768aefb21b43b737e8d0 Mon Sep 17 00:00:00 2001 From: Fernando Date: Thu, 27 Feb 2025 20:48:03 +0100 Subject: [PATCH 26/29] Delete duplicate question --- homework02/src/main/java/com/ironhack/InitialMenu.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homework02/src/main/java/com/ironhack/InitialMenu.java b/homework02/src/main/java/com/ironhack/InitialMenu.java index 6e9b77e9..87b30441 100644 --- a/homework02/src/main/java/com/ironhack/InitialMenu.java +++ b/homework02/src/main/java/com/ironhack/InitialMenu.java @@ -11,7 +11,7 @@ public class InitialMenu { public void startMenu(){ showArt(); - System.out.println("What is the school name?"); +// System.out.println("What is the school name?"); System.out.println(Style.BOLD + "What is the " + Style.UNDERLINE+Style.SCHOOL + "school" + Style.RESET+Style.BOLD + " name?" + Style.RESET); nameSchool = scanner.nextLine(); System.out.println(Style.SUCCESS_BG+Style.BOLD + " Ok! Welcome to the " + Style.SCHOOL + nameSchool + Style.RESET+Style.SUCCESS_BG+Style.BOLD + "! " + Style.RESET); From 77c90d9412e830350671cbf99092ebbfa1977f53 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 28 Feb 2025 08:17:25 +0100 Subject: [PATCH 27/29] New ServerMenuTest --- .../java/com/ironhack/ServerMenuTest.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 homework02/src/test/java/com/ironhack/ServerMenuTest.java 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 From 3d8836b184fd735da0de7780c3f60a58594d2c6f Mon Sep 17 00:00:00 2001 From: alvaro Date: Fri, 28 Feb 2025 08:58:58 +0100 Subject: [PATCH 28/29] some final tests --- .../java/com/ironhack/CommandFunction.java | 2 +- .../main/java/com/ironhack/ServerMenu.java | 4 ++++ .../src/main/java/com/ironhack/Student.java | 2 +- .../src/main/java/com/ironhack/Teacher.java | 4 ++++ .../com/ironhack/CommandFunctionTest.java | 23 +++++++++++++++++++ 5 files changed, 33 insertions(+), 2 deletions(-) diff --git a/homework02/src/main/java/com/ironhack/CommandFunction.java b/homework02/src/main/java/com/ironhack/CommandFunction.java index 25e14faf..6d2191b2 100644 --- a/homework02/src/main/java/com/ironhack/CommandFunction.java +++ b/homework02/src/main/java/com/ironhack/CommandFunction.java @@ -186,7 +186,7 @@ public void assign(String teacherId, String courseId) { int k=0; if (Main.teacherList.isEmpty() || Main.courseList.isEmpty()) { - System.out.println(Style.NOT_FOUND + "There are no students." + Style.RESET); + System.out.println(Style.NOT_FOUND + "There are no courses or teachers." + Style.RESET); } else { for (int i = 0; i(); } + // 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() { From 672a266076a784c116c0e251c1cc619589b7571b Mon Sep 17 00:00:00 2001 From: alvaro Date: Fri, 28 Feb 2025 09:09:11 +0100 Subject: [PATCH 29/29] validate email working --- .../main/java/com/ironhack/InitialMenu.java | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/homework02/src/main/java/com/ironhack/InitialMenu.java b/homework02/src/main/java/com/ironhack/InitialMenu.java index 87b30441..019293fa 100644 --- a/homework02/src/main/java/com/ironhack/InitialMenu.java +++ b/homework02/src/main/java/com/ironhack/InitialMenu.java @@ -114,11 +114,21 @@ public void createStudent(){ auxiliarStudent = new Student(nameStudent, addressStudent,emailStudent); //This line is for proving that email format is correct - try{ - auxiliarStudent.setEmail(emailStudent); - }catch (Exception e){ - System.out.println(e.getMessage()); - } + boolean opt = false; + do{ + try{ + auxiliarStudent.setEmail(emailStudent); + auxiliarStudent = new Student(nameStudent, addressStudent,emailStudent); + opt = false; + }catch (Exception e){ + System.out.println(e.getMessage()); + System.out.println("Please introduce a new valid email: "); + emailStudent = scanner.next(); + + opt = true; + } + }while(opt); + System.out.println(Style.SUCCESS_BG+Style.BOLD + " Ok! Student " + counter + ": " + Style.STUDENT + nameStudent + Style.RESET+Style.SUCCESS_BG+Style.BOLD +