Skip to content

Solved Lab #10

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
39 changes: 39 additions & 0 deletions src/Employee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

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(getSalary());
}
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;
}

}
18 changes: 18 additions & 0 deletions src/Intern.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Intern extends Employee{
private static final double MAX_SALARY = 20000;
public Intern(String name, String email, int age, double salary){
super(name, email, age, salary);
validateSalary();
}

@Override
public void setSalary(double salary) {
super.setSalary(salary);
validateSalary();
}
private void validateSalary(){
if(getSalary() > MAX_SALARY){
setSalary(MAX_SALARY);
}
}
}
25 changes: 25 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;


public class Main{
public static void main(String[] args){
Employee[] employees = new Employee[10];
for(int i = 0; i<10; i++){
employees[i] = new Employee("Employee" + (i + 1), "employee" + (i + 1) + "@company.com", 25 + i, 50000 + i * 1000);
}
try(PrintWriter writer = new PrintWriter(new FileWriter("employees.txt"))){
for(Employee employee : employees){
writer.println("Name"+ employee.getName());
writer.println("Email"+ employee.getEmail());
writer.println("Age"+ employee.getAge());
writer.println("Salary"+ employee.getSalary());
writer.println();
}
System.out.println("Employee properties written to employees.txt");
} catch(IOException e){
e.printStackTrace();
}
}
}