diff --git a/StandarInput/pom.xml b/StandarInput/pom.xml new file mode 100644 index 0000000..f7786b7 --- /dev/null +++ b/StandarInput/pom.xml @@ -0,0 +1,30 @@ + + + 4.0.0 + + org.example + StandarInput + 1.0-SNAPSHOT + + + 21 + 21 + UTF-8 + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 21 + 21 + --enable-preview + + + + + + \ No newline at end of file diff --git a/StandarInput/src/main/java/Employee.java b/StandarInput/src/main/java/Employee.java new file mode 100644 index 0000000..8c64a73 --- /dev/null +++ b/StandarInput/src/main/java/Employee.java @@ -0,0 +1,67 @@ +public class Employee { + // Campos privados + private String name; + private String email; + private int age; + private double salary; + + // Constructor + public Employee(String name, String email, int age, double salary) { + this.name = name; + this.email = email; + this.age = age; + this.salary = salary; + } + + // Getters y Setters + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + if (age >= 18) { // Validación básica: edad mínima 18 + this.age = age; + } else { + System.out.println("Error: La edad debe ser al menos 18 años"); + } + } + + public double getSalary() { + return salary; + } + + public void setSalary(double salary) { + if (salary >= 0) { // Validación básica: salario no negativo + this.salary = salary; + } else { + System.out.println("Error: El salario no puede ser negativo"); + } + } + + // Método toString para representación como cadena + @Override + public String toString() { + return "Employee{" + + "name='" + name + '\'' + + ", email='" + email + '\'' + + ", age=" + age + + ", salary=" + salary + + '}'; + } +} \ No newline at end of file diff --git a/StandarInput/src/main/java/EmployeeFileWriter.java b/StandarInput/src/main/java/EmployeeFileWriter.java new file mode 100644 index 0000000..83466a7 --- /dev/null +++ b/StandarInput/src/main/java/EmployeeFileWriter.java @@ -0,0 +1,45 @@ +import java.io.FileWriter; +import java.io.PrintWriter; +import java.io.IOException; + +public class EmployeeFileWriter { + public static void main(String[] args) { + // Crear un array con 10 empleados de ejemplo + Employee[] employees = { + new Employee("Juan Pérez", "juan.perez@empresa.com", 32, 15000.0), + new Employee("María García", "maria.garcia@empresa.com", 28, 18000.0), + new Employee("Carlos López", "carlos.lopez@empresa.com", 45, 41000.0), + new Employee("Ana Martínez", "ana.martinez@empresa.com", 29, 16000.0), + new Employee("Luis Rodríguez", "luis.rodriguez@empresa.com", 38, 11000.0), + new Employee("Sofía Hernández", "sofia.hernandez@empresa.com", 31, 17000.0), + new Employee("Pedro Díaz", "pedro.diaz@empresa.com", 42, 15000.0), + new Employee("Laura Sánchez", "laura.sanchez@empresa.com", 27, 14000.0), + new Employee("Jorge Ramírez", "jorge.ramirez@empresa.com", 35, 19000.0), + new Employee("Elena Flores", "elena.flores@empresa.com", 40, 13000.0) + }; + + // Escribir los empleados en el archivo + try (FileWriter fileWriter = new FileWriter("employees.txt"); + PrintWriter printWriter = new PrintWriter(fileWriter)) { + + // Escribir encabezado + printWriter.println("Lista de Empleados"); + printWriter.println("=================="); + printWriter.println(); + + // Escribir cada empleado + for (Employee emp : employees) { + printWriter.println("Nombre: " + emp.getName()); + printWriter.println("Email: " + emp.getEmail()); + printWriter.println("Edad: " + emp.getAge()); + printWriter.println("Salario: " + emp.getSalary()); + printWriter.println("------------------"); + } + + System.out.println("Los datos de los empleados se han guardado en employees.txt"); + + } catch (IOException e) { + System.err.println("Error al escribir en el archivo: " + e.getMessage()); + } + } +} diff --git a/StandarInput/src/main/java/Intern.java b/StandarInput/src/main/java/Intern.java new file mode 100644 index 0000000..e9008a7 --- /dev/null +++ b/StandarInput/src/main/java/Intern.java @@ -0,0 +1,39 @@ +public class Intern extends Employee { + // Constante para el límite de salario + public static final double SALARY_LIMIT = 20000.0; + + // Constructor + public Intern(String name, String email, int age, double salary) { + super(name, email, age, salary); + validateSalary(salary); + } + + // Sobrescribir el setter de salario para aplicar el límite + @Override + public void setSalary(double salary) { + validateSalary(salary); + super.setSalary(salary); + } + + // Método privado para validar el salario + private void validateSalary(double salary) { + if (salary > SALARY_LIMIT) { + throw new IllegalArgumentException("El salario de un interno no puede exceder " + SALARY_LIMIT); + } + if (salary < 0) { + throw new IllegalArgumentException("El salario no puede ser negativo"); + } + } + + // Método toString específico para Intern + @Override + public String toString() { + return "Intern{" + + "name='" + super.getName() + '\'' + + ", email='" + super.getEmail() + '\'' + + ", age=" + super.getAge() + + ", salary=" + super.getSalary() + + ", salaryLimit=" + SALARY_LIMIT + + '}'; + } +} \ No newline at end of file