0% found this document useful (0 votes)
2 views

Skill Week 5

The document contains Java code implementing two systems: an Employee Management System and a Voting System. The Employee Management System allows for the management of permanent and contract employees, including adding, updating, and removing employees, while the Voting System enables the addition of candidates and voting functionality with checks for duplicate votes. Both systems demonstrate the use of object-oriented programming principles such as interfaces, classes, and encapsulation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Skill Week 5

The document contains Java code implementing two systems: an Employee Management System and a Voting System. The Employee Management System allows for the management of permanent and contract employees, including adding, updating, and removing employees, while the Voting System enables the addition of candidates and voting functionality with checks for duplicate votes. Both systems demonstrate the use of object-oriented programming principles such as interfaces, classes, and encapsulation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Advanced Object Oriented Programming-AOOP(A)

Course Code-23CS2103A
Name: K. Madhu mitha
ID Number: 2300090088

Skill-Week-5
Question-1:
package Problem1;

import java.util.HashMap;

import java.util.Map;

interface Employee {

String getId();

String getName();

double getSalary();

void setName(String name);

void setSalary(double salary);

class PermanentEmployee implements Employee {

private String id;

private String name;

private double salary;

private String benefits;

public PermanentEmployee(String id, String name, double salary, String benefits) {

this.id = id;

this.name = name;

this.salary = salary;

this.benefits = benefits;

}
public String getId() {

return id;

public String getName() {

return name;

public double getSalary() {

return salary;

public String getBenefits() {

return benefits;

public void setName(String name) {

this.name = name;

public void setSalary(double salary) {

this.salary = salary;

public void setBenefits(String benefits) {

this.benefits = benefits;

class ContractEmployee implements Employee {

private String id;

private String name;

private double salary;


private int contractDuration;

public ContractEmployee(String id, String name, double salary, int contractDuration) {

this.id = id;

this.name = name;

this.salary = salary;

this.contractDuration = contractDuration;

public String getId() {

return id;

public String getName() {

return name;

public double getSalary() {

return salary;

public int getContractDuration() {

return contractDuration;

public void setName(String name) {

this.name = name;

public void setSalary(double salary) {

this.salary = salary;

}
public void setContractDuration(int contractDuration) {

this.contractDuration = contractDuration;

class EmployeeManager {

private Map<String, Employee> employees = new HashMap<>();

public void addEmployee(Employee employee) {

employees.put(employee.getId(), employee);

public void removeEmployee(String id) {

employees.remove(id);

public Employee getEmployee(String id) {

return employees.get(id);

public void updateEmployee(String id, String name, double salary, String benefits, Integer contractDuration) {

Employee employee = employees.get(id);

if (employee != null) {

employee.setName(name);

employee.setSalary(salary);

if (employee instanceof PermanentEmployee) {

((PermanentEmployee) employee).setBenefits(benefits);

} else if (employee instanceof ContractEmployee) {

((ContractEmployee) employee).setContractDuration(contractDuration);

}
public class Main {

public static void main(String[] args) {

EmployeeManager manager = new EmployeeManager();

PermanentEmployee permanentEmployee = new PermanentEmployee("1", "John", 50000, "Health


Insurance");

ContractEmployee contractEmployee = new ContractEmployee("2", "Jane", 40000, 12);

manager.addEmployee(permanentEmployee);

manager.addEmployee(contractEmployee);

manager.updateEmployee("1", "John Doe", 55000, "Life Insurance", null);

manager.updateEmployee("2", "Jane Smith", 45000, null, 18);

manager.removeEmployee("1");

Employee retrieved = manager.getEmployee("2");

if (retrieved != null) {

System.out.println(retrieved.getName());

} else {

System.out.println("Employee not found.");

Output:
Question-2:
package Problem2;

import java.util.HashMap;

import java.util.HashSet;

import java.util.Map;

import java.util.Set;

class Candidate {

private String name;

private int votes;

public Candidate(String name) {

this.name = name;

this.votes = 0;

public String getName() {

return name;

}
public int getVotes() {

return votes;

public void incrementVotes() {

votes++;

class VotingSystem {

private Map<String, Candidate> candidates = new HashMap<>();

private Set<String> voters = new HashSet<>();

public void addCandidate(String name) {

if (candidates.containsKey(name)) {

throw new IllegalArgumentException("Candidate already exists.");

candidates.put(name, new Candidate(name));

public void vote(String voterId, String candidateName) {

if (voters.contains(voterId)) {

throw new IllegalArgumentException("Voter has already voted.");

Candidate candidate = candidates.get(candidateName);

if (candidate == null) {

throw new IllegalArgumentException("Candidate does not exist.");

candidate.incrementVotes();

voters.add(voterId);
}

public int getVotes(String candidateName) {

Candidate candidate = candidates.get(candidateName);

if (candidate == null) {

throw new IllegalArgumentException("Candidate does not exist.");

return candidate.getVotes();

public class Main {

public static void main(String[] args) {

VotingSystem system = new VotingSystem();

system.addCandidate("Alice");

system.addCandidate("Bob");

system.vote("Voter1", "Alice");

system.vote("Voter2", "Bob");

system.vote("Voter3", "Alice");

System.out.println("Votes for Alice: " + system.getVotes("Alice"));

System.out.println("Votes for Bob: " + system.getVotes("Bob"));

try {

system.vote("Voter1", "Bob");

} catch (IllegalArgumentException e) {

System.out.println(e.getMessage());

}
try {

system.vote("Voter4", "Charlie");

} catch (IllegalArgumentException e) {

System.out.println(e.getMessage());

try {

System.out.println("Votes for Charlie: " + system.getVotes("Charlie"));

} catch (IllegalArgumentException e) {

System.out.println(e.getMessage());

Output:

You might also like