Repository navigation
Expand file tree
/
Copy pathEmployeeTest.java
More file actions
69 lines (55 loc) · 1.95 KB
/
Copy pathEmployeeTest.java
File metadata and controls
69 lines (55 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.Scanner;
class Employee {
private String name;
private String jobTitle;
private double salary;
// Constructor
public Employee(String name, String jobTitle, double salary) {
this.name = name;
this.jobTitle = jobTitle;
this.salary = salary;
}
// Method to display employee details
public void displayDetails() {
System.out.println("Employee Name: " + name);
System.out.println("Job Title: " + jobTitle);
System.out.println("Salary: " + salary);
}
// Method to calculate yearly salary
public double calculateYearlySalary() {
return salary * 12;
}
// Method to update salary
public void updateSalary(double newSalary) {
if (newSalary > 0) {
this.salary = newSalary;
System.out.println("Salary updated successfully to " + newSalary);
} else {
System.out.println("Invalid salary. Update failed.");
}
}
}
public class EmployeeTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Taking input from user
System.out.print("Enter employee name: ");
String name = sc.nextLine();
System.out.print("Enter job title: ");
String jobTitle = sc.nextLine();
System.out.print("Enter monthly salary: ");
double salary = sc.nextDouble();
// Create employee object
Employee emp = new Employee(name, jobTitle, salary);
// Display details
emp.displayDetails();
// Calculate yearly salary
System.out.println("Yearly Salary: " + emp.calculateYearlySalary());
// Update salary
System.out.print("Enter new salary to update: ");
double newSalary = sc.nextDouble();
emp.updateSalary(newSalary);
// Show updated details
emp.displayDetails();
}
}