Skip to content

Commit solucion #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
30 changes: 30 additions & 0 deletions StandarInput/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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>org.example</groupId>
<artifactId>StandarInput</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>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>21</source>
<target>21</target>
<compilerArgs>--enable-preview</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>

</project>
67 changes: 67 additions & 0 deletions StandarInput/src/main/java/Employee.java
Original file line number Diff line number Diff line change
@@ -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 +
'}';
}
}
45 changes: 45 additions & 0 deletions StandarInput/src/main/java/EmployeeFileWriter.java
Original file line number Diff line number Diff line change
@@ -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", "[email protected]", 32, 15000.0),
new Employee("María García", "[email protected]", 28, 18000.0),
new Employee("Carlos López", "[email protected]", 45, 41000.0),
new Employee("Ana Martínez", "[email protected]", 29, 16000.0),
new Employee("Luis Rodríguez", "[email protected]", 38, 11000.0),
new Employee("Sofía Hernández", "[email protected]", 31, 17000.0),
new Employee("Pedro Díaz", "[email protected]", 42, 15000.0),
new Employee("Laura Sánchez", "[email protected]", 27, 14000.0),
new Employee("Jorge Ramírez", "[email protected]", 35, 19000.0),
new Employee("Elena Flores", "[email protected]", 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());
}
}
}
39 changes: 39 additions & 0 deletions StandarInput/src/main/java/Intern.java
Original file line number Diff line number Diff line change
@@ -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 +
'}';
}
}