Advertisement
vvccs

E1_Employee

Nov 8th, 2023
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.11 KB | None | 0 0
  1. class Employee {
  2.     private String firstName;
  3.     private String lastName;
  4.     private double monthlySalary;
  5.  
  6.     public Employee(String firstName, String lastName, double monthlySalary) {
  7.         this.firstName = firstName;
  8.         this.lastName = lastName;
  9.         if (monthlySalary > 0) {
  10.             this.monthlySalary = monthlySalary;
  11.         } else {
  12.             this.monthlySalary = 0.0;
  13.         }
  14.     }
  15.  
  16.     public String getFirstName() {
  17.         return firstName;
  18.     }
  19.  
  20.     public void setFirstName(String firstName) {
  21.         this.firstName = firstName;
  22.     }
  23.  
  24.     public String getLastName() {
  25.         return lastName;
  26.     }
  27.  
  28.     public void setLastName(String lastName) {
  29.         this.lastName = lastName;
  30.     }
  31.  
  32.     public double getMonthlySalary() {
  33.         return monthlySalary;
  34.     }
  35.  
  36.     public void setMonthlySalary(double monthlySalary) {
  37.         if (monthlySalary > 0) {
  38.             this.monthlySalary = monthlySalary;
  39.         } else {
  40.             this.monthlySalary = 0.0;
  41.         }
  42.     }
  43.  
  44.     public double getYearlySalary() {
  45.         return monthlySalary * 12;
  46.     }
  47.  
  48.     public void giveRaise(double percent) {
  49.         double raiseAmount = monthlySalary * percent / 100;
  50.         monthlySalary += raiseAmount;
  51.     }
  52. }
  53.  
  54. public class EmployeeTest {
  55.     public static void main(String[] args) {
  56.         // Create two Employee objects
  57.         Employee employee1 = new Employee("John", "Doe", 5000.0);
  58.         Employee employee2 = new Employee("Jane", "Smith", 6000.0);
  59.  
  60.         // Display yearly salaries
  61.         System.out.println("Yearly Salary for Employee 1: " + employee1.getYearlySalary());
  62.         System.out.println("Yearly Salary for Employee 2: " + employee2.getYearlySalary());
  63.  
  64.         // Give each Employee a 10% raise
  65.         employee1.giveRaise(10);
  66.         employee2.giveRaise(10);
  67.  
  68.         // Display yearly salaries after the raise
  69.         System.out.println("Yearly Salary for Employee 1 after raise: " + employee1.getYearlySalary());
  70.         System.out.println("Yearly Salary for Employee 2 after raise: " + employee2.getYearlySalary());
  71.     }
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement