Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
b08f840
Inicio
franciscofarrando Apr 14, 2025
df023d8
git ignore
franciscofarrando Apr 14, 2025
9972f15
clase Course OK con getters setters y const
franciscofarrando Apr 14, 2025
ceef0ff
course OK
franciscofarrando Apr 14, 2025
aaf5c33
const vacio creado
franciscofarrando Apr 14, 2025
290144e
teacher class
Irenevn16 Apr 14, 2025
8d8195c
Class Student created
GabiCase Apr 14, 2025
3ea805c
dev branch created
franciscofarrando Apr 15, 2025
ce73cff
Merge remote-tracking branch 'origin/main' into student-branch
GabiCase Apr 15, 2025
9444ecc
Merge branch 'irene' into dev
Irenevn16 Apr 15, 2025
798fdbc
Merge branch 'dev' of https://github.com/franciscofarrando/homework-j…
GabiCase Apr 15, 2025
e1e8847
Merge branch 'dev' into student-branch
GabiCase Apr 15, 2025
376c536
comienzo scanner
Irenevn16 Apr 15, 2025
548f9df
Agregar CommandHandler y cambio en student
GabiCase Apr 15, 2025
c6785e5
command2 iniciado
franciscofarrando Apr 15, 2025
77d683d
Corrección de findCourseById
GabiCase Apr 15, 2025
579a460
course con set Id
franciscofarrando Apr 15, 2025
0625ae3
scanner borrador
Irenevn16 Apr 15, 2025
290c426
Merge branch 'irene' into dev
Irenevn16 Apr 15, 2025
74fdab8
Corrected StudentId creation, created test
GabiCase Apr 15, 2025
4a0cd0a
Merge dev
GabiCase Apr 15, 2025
341ccd1
dev to francisco
franciscofarrando Apr 15, 2025
a66cdc6
id course
franciscofarrando Apr 15, 2025
36ba82b
while
Irenevn16 Apr 15, 2025
4ee06f2
command handler ok?
franciscofarrando Apr 15, 2025
50c4e43
command fixed
franciscofarrando Apr 15, 2025
a86e19f
guardando
Irenevn16 Apr 21, 2025
05ccd09
cambios menu
Irenevn16 Apr 21, 2025
ddf90e8
Merge branch 'irene' into dev
Irenevn16 Apr 21, 2025
b506217
merge
Irenevn16 Apr 21, 2025
06357d3
command handler
GabiCase Apr 21, 2025
3efbdfd
Merge branch 'student-branch' into dev
GabiCase Apr 21, 2025
9f5d085
dev merge francisco
franciscofarrando Apr 21, 2025
6f24c67
saving
Irenevn16 Apr 21, 2025
233d733
Merge branch 'dev' into irene
Irenevn16 Apr 21, 2025
6aaa430
solucionado el merge
Irenevn16 Apr 21, 2025
bebdcee
WIP: cambios antes de mergear dev
GabiCase Apr 21, 2025
331edea
Merge branch 'dev' into student-branch
GabiCase Apr 21, 2025
99cbc6e
CommandHandler fixed
GabiCase Apr 21, 2025
c5a01ed
Better formats
GabiCase Apr 21, 2025
d1e346b
final
franciscofarrando Apr 22, 2025
42e46eb
finalizado
franciscofarrando Apr 22, 2025
a2bdd48
merge student y francisco to dev
franciscofarrando Apr 22, 2025
c99f126
retoques
Irenevn16 Apr 22, 2025
00dec7b
excepciones
Irenevn16 Apr 22, 2025
021d216
v3
franciscofarrando Apr 22, 2025
4bc1c07
fail
franciscofarrando Apr 22, 2025
81d1fab
menu fixed
franciscofarrando Apr 22, 2025
fb32ece
min failure fixed
franciscofarrando Apr 22, 2025
7f910a1
dev merged to main
franciscofarrando Apr 23, 2025
b4a4a02
espanol a ingles
franciscofarrando Apr 23, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.idea/*
31 changes: 31 additions & 0 deletions homework-java/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>homework-java</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.2</version>
</dependency>

</dependencies>


</project>
189 changes: 189 additions & 0 deletions homework-java/src/main/java/com/example/CommandHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package com.example;
import java.util.*;

public class CommandHandler {
private List<Student> students;
private List<Teacher> teachers;
private List<Course> courses;

final String ROJO = "\u001B[31m";
final String RESET = "\u001B[0m";

public CommandHandler(List<Student> students, List<Course> courses, List<Teacher> teachers) {
this.students = students;
this.courses = courses;
this.teachers = teachers;
}

public boolean executeCommand(String wholeCommand){
String[] commandPart = wholeCommand.split(" ");
String command = commandPart[0];
switch (command){
case "ENROLL":
enrollStudent(commandPart[1],commandPart[2]);
return true;
case "ASSIGN":
assignTeacher(commandPart[1],commandPart[2]);
return true;
case "SHOW":
//System.out.println(command);
handleShow(commandPart);
return true;
case "LOOKUP":
lookUp(commandPart);
return true;
default:
System.out.println("❌ Not a command.");
return false;
}
}

public Student findStudentById(String student_id){
Student student = null;
for(Student s: students){
if(s.getStudentId().equals(student_id)){
student = s;
return student;
}
}
return null;
}

public Course findCourseById(String course_id){
Course course = null;
for(Course c: courses){
if(c.getCourseId().equals(course_id)){
course = c;
return course;
}
}
return null;
}

public Teacher findTeacherById(String teacher_id){
Teacher teacher = null;
for(Teacher t: teachers){
if(t.getTeacherId().equals(teacher_id)){
teacher = t;
return teacher;
}
}
return null;
}

private void lookUp(String[] commandPart) {
switch (commandPart[1]){
case "COURSE":
Course course = findCourseById(commandPart[2]);
if (course != null)
System.out.println(course.toString());
else
System.out.println("Course not found.");
break;
case "STUDENT":
Student student = findStudentById(commandPart[2]);
if (student != null)
System.out.println(student.toString());
else
System.out.println("Student not found.");
break;
case "TEACHER":
Teacher teacher = findTeacherById(commandPart[2]);
if (teacher != null)
System.out.println(teacher.toString());
else
System.out.println("Teacher not found.");
break;
}
}

private void enrollStudent(String student_id, String course_id) {
Student student = findStudentById(student_id);
Course course = findCourseById(course_id);

if (student != null && course!= null) {
student.setCourse(course);
updateMoneyEarned(course);
System.out.println(student.toString());
} else {
System.out.println("Couldn't assign the course");
}

}

public void assignTeacher(String teacher_id, String course_id){
Teacher teacher = findTeacherById(teacher_id);
Course course = findCourseById(course_id);

if (teacher != null && course!=null){
course.setTeacher(teacher);
System.out.println(course.toString());
}else {
System.out.println("Couldn't assign the course");
}
}

private void updateMoneyEarned(Course course){
double total = course.getMoney_earned() + course.getPrice();
course.setMoney_earned(total);
}

private void handleShow(String[] command) {
//System.out.println(Arrays.toString(command) + "del handle");
switch (command[1].toUpperCase()){
case "COURSES":
showCourses();
break;
case "STUDENTS":
showStudents();
break;
case "TEACHERS":
showTeachers();
break;
case "PROFIT":
showProfit();
break;
}
}

private void showStudents() {
//System.out.println("llego a show students");
StringBuilder studentsString = new StringBuilder();
for (Student student: students){
studentsString.append(student.toString()).append("\n");
}
// System.out.println("Student usando el toString() es:");
System.out.println(studentsString);
}

private void showTeachers() {
StringBuilder teachersString = new StringBuilder();
for (Teacher teacher: teachers){
teachersString.append(teacher.toString()).append("\n");
}
System.out.println(teachersString);
}

private void showProfit() {
double totalEarned = courses.stream().mapToDouble(Course::getMoney_earned).sum();
double totalSalaries = teachers.stream().mapToDouble(Teacher::getSalary).sum();


System.out.println(ROJO + "===== FINANCIAL REPORT =====" + RESET);
System.out.println("Total income from courses: $" + totalEarned);
System.out.println("Total expenses (teachers' salaries): $" + totalSalaries);
System.out.println("------------------------------");
System.out.printf("Net profit: $%.2f%n", (totalEarned - totalSalaries));
System.out.println(ROJO +"==============================" + RESET);
}

private void showCourses() {
StringBuilder coursesString = new StringBuilder();
for (Course course: courses){
coursesString.append(course.toString()).append("\n");
}
System.out.println(coursesString);
}


}
96 changes: 96 additions & 0 deletions homework-java/src/main/java/com/example/Course.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.example;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.UUID;

public class Course {
private static final Set<String> usedIds = new HashSet<>();

private String courseId;
private String name;
private double price;
private double money_earned;
private Teacher teacher;

//constructor de name y price
public Course(String name, double price) {
this.courseId = generateUniqueCourseId();
this.name = name;
this.price = price;
}
//Constructor vacio con id

public Course() {
this.courseId = generateUniqueCourseId();
}

private String generateUniqueCourseId() {
String id;
Random random = new Random();
do {
int number = random.nextInt(900) + 100;
id = "C" + number;
} while (usedIds.contains(id));

usedIds.add(id);
return id;
}

//getters y setters

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}

public String getCourseId() {
return courseId;
}

public void setCourseId(String courseId) {
this.courseId = courseId;
}

public double getMoney_earned() {
return money_earned;
}

public void setMoney_earned(double money_earned) {
this.money_earned = money_earned;
}

public Teacher getTeacher() {
return teacher;
}

public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}


@Override
public String toString() {
return String.format(
"📚 Course Info:\n" +
"🔢 ID : %s\n" +
"📖 Name : %s\n" +
"💵 Price : $%.2f\n" +
"💰 Money Earned : $%.2f\n" +
"👨‍🏫 Teacher : %s",
courseId, name, price, money_earned, teacher
);
}
}
10 changes: 10 additions & 0 deletions homework-java/src/main/java/com/example/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example;

//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
public static void main(String[] args) {

//comentario
}
}
Loading