Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Employee {
- private String firstName;
- private String lastName;
- private double monthlySalary;
- public Employee(String firstName, String lastName, double monthlySalary) {
- this.firstName = firstName;
- this.lastName = lastName;
- if (monthlySalary > 0) {
- this.monthlySalary = monthlySalary;
- } else {
- this.monthlySalary = 0.0;
- }
- }
- public String getFirstName() {
- return firstName;
- }
- public void setFirstName(String firstName) {
- this.firstName = firstName;
- }
- public String getLastName() {
- return lastName;
- }
- public void setLastName(String lastName) {
- this.lastName = lastName;
- }
- public double getMonthlySalary() {
- return monthlySalary;
- }
- public void setMonthlySalary(double monthlySalary) {
- if (monthlySalary > 0) {
- this.monthlySalary = monthlySalary;
- } else {
- this.monthlySalary = 0.0;
- }
- }
- public double getYearlySalary() {
- return monthlySalary * 12;
- }
- public void giveRaise(double percent) {
- double raiseAmount = monthlySalary * percent / 100;
- monthlySalary += raiseAmount;
- }
- }
- public class EmployeeTest {
- public static void main(String[] args) {
- // Create two Employee objects
- Employee employee1 = new Employee("John", "Doe", 5000.0);
- Employee employee2 = new Employee("Jane", "Smith", 6000.0);
- // Display yearly salaries
- System.out.println("Yearly Salary for Employee 1: " + employee1.getYearlySalary());
- System.out.println("Yearly Salary for Employee 2: " + employee2.getYearlySalary());
- // Give each Employee a 10% raise
- employee1.giveRaise(10);
- employee2.giveRaise(10);
- // Display yearly salaries after the raise
- System.out.println("Yearly Salary for Employee 1 after raise: " + employee1.getYearlySalary());
- System.out.println("Yearly Salary for Employee 2 after raise: " + employee2.getYearlySalary());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement