Skip to content

Solution lab 2 #11

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 3 commits 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
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/lab-java-standard-input-and-classes.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.
Binary file not shown.
46 changes: 46 additions & 0 deletions src/Employee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
public class Employee {
private String name;
private String email;
private int age;
private double salary;


public Employee(String name, String email, int age, double salary){
setName(name);
setEmail(email);
setAge(age);
setSalary(salary);
}

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) {
this.age = age;
}

public double getSalary() {
return salary;
}

public void setSalary(double salary) {
this.salary = salary;
}
}
15 changes: 15 additions & 0 deletions src/Intern.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public class Intern extends Employee{
private static final double salaryLimit = 20000;

public Intern(String name, String email, int age, int salary){
super(name, email, age, salary);
validateSalary();
}

private void validateSalary(){
if(getSalary() > salaryLimit){
setSalary(salaryLimit);
System.out.println("Intern salary cannot exceed 20000, Setting salary to 20000");
}
}
}
41 changes: 41 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;

public class Main {
public static void main(String[] args) throws IOException {
String[] names = {"Carlos", "Pepe", "Luis", "Andrea" , "Julia", "Anselmo","Pedro", "Iria", "Jose Manuel", "Lidia" };
String[] domainNameServers = {"@yahoo.es", "@proton.me", "@corporate.es"};
Integer[] ages = {18, 19, 23, 43, 50};
Integer[] mockSalaries = {22000, 19000, 30000, 150000, 40000};
Boolean[] isIntern = {true, false};

FileWriter writer = new FileWriter("result/employees.txt", false);

Random random = new Random();
for(String name : names){
String email = name + generateRandomValues(domainNameServers);
int age = generateRandomValues(ages);
int salary = generateRandomValues(mockSalaries);
boolean isEmployeeIntern = generateRandomValues(isIntern);

if(isEmployeeIntern){
Intern internEmployee = new Intern(name, email, age, salary);
writer.write(internEmployee.getName() + " " + internEmployee.getEmail() + " " + internEmployee.getAge() + " " + internEmployee.getSalary() + "\n") ;

}else{
Employee employee = new Employee(name, email, age, salary);
writer.write(employee.getName() + " " + employee.getEmail() + " " + employee.getAge() + " " + employee.getSalary() + "\n" );
}

}
writer.close();
}

public static <T> T generateRandomValues(T[] data){
Random random = new Random();
int index = random.nextInt(data.length);
return data[index];
}
}