Add OOP/Java/Assignments/FISAC/Q4.java
This commit is contained in:
parent
343e8e0aff
commit
f9d68be8f0
48
OOP/Java/Assignments/FISAC/Q4.java
Normal file
48
OOP/Java/Assignments/FISAC/Q4.java
Normal file
@ -0,0 +1,48 @@
|
||||
import java.util.*;
|
||||
|
||||
class Employee {
|
||||
String empName, empCode;
|
||||
int leaveBalance[] = {13, 12, 10};
|
||||
Employee(String name, String code) { empName = name; empCode = code; }
|
||||
}
|
||||
|
||||
class EmployeeNotFound extends Exception { EmployeeNotFound(String s) { super(s); } }
|
||||
class NoLeaves extends Exception { NoLeaves(String s) { super(s); } }
|
||||
|
||||
public class LeaveManagement {
|
||||
static List<Employee> employees = Collections.synchronizedList(new ArrayList<>());
|
||||
public static void main(String[] args) {
|
||||
Thread t1 = new Thread(() -> {
|
||||
employees.add(new Employee("Alice", "E1"));
|
||||
employees.add(new Employee("Bob", "E2"));
|
||||
employees.add(new Employee("Charlie", "E3"));
|
||||
employees.add(new Employee("David", "E4"));
|
||||
employees.add(new Employee("Eve", "E5"));
|
||||
});
|
||||
|
||||
Thread t2 = new Thread(() -> {
|
||||
try {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
String code = sc.next(), type = sc.next();
|
||||
int days = sc.nextInt(), idx = type.equals("Comp") ? 0 : type.equals("Casual") ? 1 : 2;
|
||||
boolean found = false;
|
||||
for (Employee e : employees) {
|
||||
if (e.empCode.equals(code)) {
|
||||
found = true;
|
||||
if (e.leaveBalance[idx] >= days) e.leaveBalance[idx] -= days;
|
||||
else throw new NoLeaves("Not enough leaves");
|
||||
}
|
||||
}
|
||||
if (!found) throw new EmployeeNotFound("Employee not found");
|
||||
} catch (Exception ex) { System.out.println(ex.getMessage()); }
|
||||
});
|
||||
|
||||
try { t1.start(); t1.join(); t2.start(); t2.join(); } catch (InterruptedException e) { e.printStackTrace(); }
|
||||
|
||||
synchronized (employees) {
|
||||
for (Employee e : employees) {
|
||||
System.out.println(e.empName + " - Casual Leaves: " + e.leaveBalance[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user