Skip to content

Lab2 #22

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

Lab2 #22

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
54 changes: 54 additions & 0 deletions LabTwo/Employee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
public class Employee {
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
public String getName() {
return name;
}

public String getEmail() {
return email;
}

public int getAge() {
return age;
}

public double getSalary() {
return salary;
}

// Setters
public void setName(String name) {
this.name = name;
}

public void setEmail(String email) {
this.email = email;
}

public void setAge(int age) {
this.age = age;
}

public void setSalary(double salary) {
this.salary = salary;
}

// toString method for printing employee details
@Override
public String toString() {
return "Name: " + name + ", Email: " + email + ", Age: " + age + ", Salary: " + salary;
}
}
18 changes: 18 additions & 0 deletions LabTwo/Intern.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class Intern extends Employee {

private static final double MAX_SALARY = 20000;

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

if (salary > MAX_SALARY) {
System.out.println("Input is INVALID! Limit reached for intern " + getName() + ". Setting salary to the maximum allowed.");
setSalary(MAX_SALARY); // Enforce maximum salary
}
}

@Override
public String toString() {
return "- Intern {Name = '" + getName() + "', E-mail = '" + getEmail() + "', Age = " + getAge() + ", Salary = " + getSalary() + "}";
}
}
33 changes: 33 additions & 0 deletions LabTwo/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

import java.io.FileWriter;
import java.io.IOException;

public class Main {
public static void main(String[] args) throws IOException {
Employee[] employees = new Employee[10];


employees[0] = new Employee("Alice Johnson", "[email protected]", 28, 50000);
employees[1] = new Employee("Bob Smith", "[email protected]", 35, 60000);
employees[2] = new Intern("Charlie Brown", "[email protected]", 22, 15000);
employees[3] = new Employee("David Wilson", "[email protected]", 40, 70000);
employees[4] = new Intern("Eve Thompson", "[email protected]", 21, 18000);
employees[5] = new Employee("Frank Harris", "[email protected]", 45, 55000);
employees[6] = new Intern("Grace Lee", "[email protected]", 23, 20000);
employees[7] = new Employee("Hannah White", "[email protected]", 32, 62000);
employees[8] = new Employee("Ian Scott", "[email protected]", 29, 48000);
employees[9] = new Intern("Jack Taylor", "[email protected]", 24, 17000);


writeEmployeeDetailsToFile(employees);
}

private static void writeEmployeeDetailsToFile(Employee[] employees) throws IOException {
try (FileWriter writer = new FileWriter("employees.txt", true)) {
for (Employee employee : employees) {
writer.write(employee.toString() + "\n"); // Writing employee data with a newline
}
System.out.println("Employee details appended to employees.txt successfully.");
}
}
}