0% found this document useful (0 votes)
6 views9 pages

Java AOOP: Employee & Voting System

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)
6 views9 pages

Java AOOP: Employee & Voting System

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

Advanced Object Oriented Programming-AOOP(A)

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

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

import [Link];

import [Link];

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) {

[Link] = id;

[Link] = name;

[Link] = salary;

[Link] = 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) {

[Link] = name;

public void setSalary(double salary) {

[Link] = salary;

public void setBenefits(String benefits) {

[Link] = 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) {

[Link] = id;

[Link] = name;

[Link] = salary;

[Link] = 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) {

[Link] = name;

public void setSalary(double salary) {

[Link] = salary;

}
public void setContractDuration(int contractDuration) {

[Link] = contractDuration;

class EmployeeManager {

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

public void addEmployee(Employee employee) {

[Link]([Link](), employee);

public void removeEmployee(String id) {

[Link](id);

public Employee getEmployee(String id) {

return [Link](id);

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

Employee employee = [Link](id);

if (employee != null) {

[Link](name);

[Link](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);

[Link](permanentEmployee);

[Link](contractEmployee);

[Link]("1", "John Doe", 55000, "Life Insurance", null);

[Link]("2", "Jane Smith", 45000, null, 18);

[Link]("1");

Employee retrieved = [Link]("2");

if (retrieved != null) {

[Link]([Link]());

} else {

[Link]("Employee not found.");

Output:
Question-2:
package Problem2;

import [Link];

import [Link];

import [Link];

import [Link];

class Candidate {

private String name;

private int votes;

public Candidate(String name) {

[Link] = name;

[Link] = 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 ([Link](name)) {

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

[Link](name, new Candidate(name));

public void vote(String voterId, String candidateName) {

if ([Link](voterId)) {

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

Candidate candidate = [Link](candidateName);

if (candidate == null) {

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

[Link]();

[Link](voterId);
}

public int getVotes(String candidateName) {

Candidate candidate = [Link](candidateName);

if (candidate == null) {

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

return [Link]();

public class Main {

public static void main(String[] args) {

VotingSystem system = new VotingSystem();

[Link]("Alice");

[Link]("Bob");

[Link]("Voter1", "Alice");

[Link]("Voter2", "Bob");

[Link]("Voter3", "Alice");

[Link]("Votes for Alice: " + [Link]("Alice"));

[Link]("Votes for Bob: " + [Link]("Bob"));

try {

[Link]("Voter1", "Bob");

} catch (IllegalArgumentException e) {

[Link]([Link]());

}
try {

[Link]("Voter4", "Charlie");

} catch (IllegalArgumentException e) {

[Link]([Link]());

try {

[Link]("Votes for Charlie: " + [Link]("Charlie"));

} catch (IllegalArgumentException e) {

[Link]([Link]());

Output:

Common questions

Powered by AI

The VotingSystem imposes constraints by using exceptions to prevent duplicate candidate names and duplicate votes from the same voter. Programmatically, these constraints are checked using the containsKey method in the case of candidates, and contains in the case of voters. If these checks detect a violation, IllegalArgumentExceptions are thrown, preventing the invalid operations from proceeding .

To redesign the VotingSystem to allow a voter to change their vote, you would need to store which candidate a voter has voted for. This could be achieved by using a map with voter IDs as keys and candidate names as values. When a vote is changed, the system would decrement the vote count of the original candidate and increment the count for the new candidate. Considerations would include ensuring the vote change integrity, preventing rapid switching (which could lead to ballot stuffing tactics), and transaction-keeping for error recovery .

The VotingSystem class uses a HashSet to maintain a unique set of voters. The add operation of HashSet does not allow duplicate entries, ensuring that a voter can only be added once. This is important to prevent duplicate votes by the same voter, maintaining the integrity of the election process and ensuring a fair result .

The VotingSystem class uses IllegalArgumentExceptions for error handling. These exceptions are thrown when a voter attempts to vote more than once, or when voting for an unregistered candidate. This mechanism prevents invalid operations from damaging the system’s integrity and provides clear feedback to the application about what went wrong, ensuring robustness against common input errors .

The Employee interface lays out common methods such as getId, getName, and getSalary, which are implemented by both PermanentEmployee and ContractEmployee. This implementation enables polymorphism, allowing the EmployeeManager to interact with these objects through the common Employee type without knowing their specific classes. Inheritance is used to extend the common interface, adding specific features like benefits or contract duration to the subclasses, enhancing code reuse and flexibility .

Encapsulation is demonstrated via private fields for employee attributes such as id, name, and salary in both PermanentEmployee and ContractEmployee classes. Access to these fields is controlled through public getter and setter methods, ensuring that any changes to the state of the objects are validated and managed appropriately, thereby encapsulating the data and enforcing constraints on how it can be accessed or modified .

The updateEmployee method enhances flexibility by accommodating the different attributes of PermanentEmployee and ContractEmployee through type checking. It can adjust not only the common attributes like name and salary but also type-specific attributes such as benefits and contract duration using conditional logic. This allows for a unified interface to manage updates while catering to the diverse needs of different employee types .

The EmployeeManager class updates Employee objects by checking the instance type. When updateEmployee is called, it first retrieves the Employee object. The type of employee is checked using instanceof; if it's a PermanentEmployee, the method updates the benefits using setBenefits. If it's a ContractEmployee, it updates the contract duration using setContractDuration. This allows for selective data update based on specific characteristics of the subclass .

In the PermanentEmployee class, benefits are handled by a specific field (benefits), with access and modification through the getBenefits and setBenefits methods. In contrast, the ContractEmployee class manages contract duration through a similar mechanism, utilizing a dedicated field (contractDuration) and accessor methods getContractDuration and setContractDuration. These differences underscore how each class extends the Employee interface to include features relevant to its employment type .

Not using defensive programming techniques in updateEmployee could lead to potential issues such as updating non-existent employee records, setting null or invalid values in employee attributes, and type safety issues when casting derived class objects. Without checks and validations, the method could inadvertently corrupt the data integrity within the EmployeeManager, causing runtime exceptions or logical bugs that are hard to trace and fix. This vulnerability can lead to data inconsistency and potential system crashes .

You might also like