From 7fef6ff03d02044850357c954e9fa9cf92dcfa95 Mon Sep 17 00:00:00 2001 From: raymondchishamba Date: Sat, 7 Sep 2024 14:20:35 +0200 Subject: [PATCH] Lab2 --- LabTwo/Employee.java | 54 ++++++++++++++++++++++++++++++++++++++++++++ LabTwo/Intern.java | 18 +++++++++++++++ LabTwo/Main.java | 33 +++++++++++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 LabTwo/Employee.java create mode 100644 LabTwo/Intern.java create mode 100644 LabTwo/Main.java diff --git a/LabTwo/Employee.java b/LabTwo/Employee.java new file mode 100644 index 0000000..4603a6e --- /dev/null +++ b/LabTwo/Employee.java @@ -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; + } +} diff --git a/LabTwo/Intern.java b/LabTwo/Intern.java new file mode 100644 index 0000000..cd18bcb --- /dev/null +++ b/LabTwo/Intern.java @@ -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() + "}"; + } +} diff --git a/LabTwo/Main.java b/LabTwo/Main.java new file mode 100644 index 0000000..da5c3b2 --- /dev/null +++ b/LabTwo/Main.java @@ -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", "alice.johnson@letstry.com", 28, 50000); + employees[1] = new Employee("Bob Smith", "bob.smith@eletstry.com", 35, 60000); + employees[2] = new Intern("Charlie Brown", "charlie.brown@letstry.com", 22, 15000); + employees[3] = new Employee("David Wilson", "david.wilson@letstry.com", 40, 70000); + employees[4] = new Intern("Eve Thompson", "eve.thompson@vletstry.com", 21, 18000); + employees[5] = new Employee("Frank Harris", "frank.harris@letstry.com", 45, 55000); + employees[6] = new Intern("Grace Lee", "grace.lee@letstry.com", 23, 20000); + employees[7] = new Employee("Hannah White", "hannah.white@letstry.com", 32, 62000); + employees[8] = new Employee("Ian Scott", "ian.scott@letstry.com", 29, 48000); + employees[9] = new Intern("Jack Taylor", "jack.taylor@letstry.com", 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."); + } + } +} \ No newline at end of file