Skip to content

Implemented lab-java-standard-input-and-classes by Sergi Vaqué #13

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 6 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
30 changes: 30 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
.idea

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
20 changes: 20 additions & 0 deletions employees.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Employee{name='Employee1', email='[email protected]', age=50, salary=43430.66}
Employee{name='Employee2', email='[email protected]', age=31, salary=31490.61}
Employee{name='Employee3', email='[email protected]', age=36, salary=74666.22}
Employee{name='Employee4', email='[email protected]', age=37, salary=35533.95}
Employee{name='Employee5', email='[email protected]', age=44, salary=22315.23}
Employee{name='Employee6', email='[email protected]', age=49, salary=55973.04}
Employee{name='Employee7', email='[email protected]', age=57, salary=56885.26}
Employee{name='Intern1', email='[email protected]', age=20, salary=14040.34}
Employee{name='Intern2', email='[email protected]', age=21, salary=14195.74}
Employee{name='Intern3', email='[email protected]', age=21, salary=13430.48}
Employee{name='Employee1', email='[email protected]', age=49, salary=28949.15}
Employee{name='Employee2', email='[email protected]', age=16, salary=29809.21}
Employee{name='Employee3', email='[email protected]', age=44, salary=35288.17}
Employee{name='Employee4', email='[email protected]', age=30, salary=63541.62}
Employee{name='Employee5', email='[email protected]', age=54, salary=28934.29}
Employee{name='Employee6', email='[email protected]', age=49, salary=22186.85}
Employee{name='Employee7', email='[email protected]', age=25, salary=37881.57}
Employee{name='Intern1', email='[email protected]', age=19, salary=13909.72}
Employee{name='Intern2', email='[email protected]', age=17, salary=18110.1}
Employee{name='Intern3', email='[email protected]', age=17, salary=20000.0}
58 changes: 58 additions & 0 deletions src/Employee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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) {
this.name = name;
this.email = email;
this.age = age;
setSalary(salary);
System.out.println(this);
}

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
;
}

@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", email='" + email + '\'' +
", age=" + age +
", salary=" + salary +
'}';
}
}
19 changes: 19 additions & 0 deletions src/Intern.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public class Intern extends Employee {

private static final double SALARY_LIMIT = 20000;

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

@Override
public void setSalary(double salary) {
if (salary > SALARY_LIMIT) {
System.out.printf("The maximum salary for an intern is %f%n", SALARY_LIMIT);
super.setSalary(SALARY_LIMIT);
} else {
super.setSalary(salary);
}
}

}
39 changes: 39 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import java.io.FileWriter;
import java.io.IOException;
import java.util.stream.IntStream;

public class Main {

static FileWriter writer;

public static void main(String[] args) throws IOException {
// Open file to be written in
writer = new FileWriter("employees.txt", true);

// Creating employees
IntStream employees = IntStream.range(1, 8);
employees.forEach(e -> {
saveToFile(new Employee("Employee" + e, "employee" + e + "@hotmail.com", (int) getRandomNumericValueInARange(16, 65), getRandomNumericValueInARange(18000, 80000)));
});

IntStream interns = IntStream.range(1, 4);
interns.forEach(i -> {
saveToFile(new Intern("Intern" + i, "intern" + i + "@hotmail.com", (int) getRandomNumericValueInARange(16, 25), getRandomNumericValueInARange(12000, 30000)));
});

// Close buffers
writer.close();
}

private static void saveToFile(Employee employee) {
try {
writer.write(employee.toString() + "\n");
} catch (IOException e) {
System.out.println(e.getMessage());
}
}

public static double getRandomNumericValueInARange(int min, int max) {
return Math.round(((Math.random() * (max - min)) + min) * 100.0) / 100.0;
}
}