+1 (315) 557-6473 

Create A Program to Implement Classes and Inheritances in C++ Assignment Solution.


Instructions

Objective
Write a C++ assignment where you are required to create a program to implement classes and inheritances in C++ language. This assignment will test your understanding of object-oriented programming concepts and your ability to design and use classes effectively. In this assignment, you will be tasked with creating a hierarchy of classes that demonstrate inheritance, encapsulation, and polymorphism. Make sure to carefully plan your class structure and use appropriate access specifiers to showcase the principles of object-oriented programming.

Requirements and Specifications

Program to implement classes inheritances in C++

Source Code

import java.io.File;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

import java.util.stream.Collectors;

public class Hospital {

    private static final String DEFAULT_FILENAME = "Employee.txt";

    private final String filename;

    private final List employees;

    public Hospital(String filename) throws IOException {

        this.filename = filename;

        try(Scanner scanner = new Scanner(new File(filename))) {

            employees = new ArrayList<>();

            while(scanner.hasNextLine()) {

                String[] parts = scanner.nextLine().split("\\s+");

                String name = parts[1];

                int blazerId = Integer.parseInt(parts[2]);

                String role = parts[0];

                switch (role) {

                    case "E":

                        addEmployee(new HospitalEmployee(blazerId, name));

                        break;

                    case "D":

                        addEmployee(new Doctor(blazerId, name, parts[3]));

                        break;

                    case "N":

                        addEmployee(new Nurse(blazerId, name, Integer.parseInt(parts[3])));

                        break;

                    case "A":

                        addEmployee(new Administrator(blazerId, name, parts[3]));

                        break;

                    case "S":

                        addEmployee(new Surgeon(blazerId, name, parts[3], "Y".equalsIgnoreCase(parts[4])));

                        break;

                    case "R":

                        addEmployee(new Receptionist(blazerId, name, parts[3], "Y".equalsIgnoreCase(parts[4])));

                        break;

                    case "J":

                        addEmployee(new Janitor(blazerId, name, parts[3], "Y".equalsIgnoreCase(parts[4])));

                        break;

                    default:

                        throw new IllegalArgumentException();

                }

            }

        }

    }

    public void addEmployee(Employee employee) {

        employees.add(employee);

    }

    public boolean removeEmployee(int blazerId, String role) {

        Employee employee = employees.stream().filter(e -> e.getBlazerId() == blazerId && e.getRole().equals(role)).findAny().orElse(null);

        if (employee == null) {

            return false;

        }

        employees.remove(employee);

        return true;

    }

    public void displayList() {

        System.out.println("The UAB Hospital System has the following employees:");

        System.out.println();

        System.out.println("Total Number of employees = " + employees.size());

        System.out.println();

        List hospitalEmployees = employees.stream().filter(e -> "Hospital Employee".equals(e.getRole())).collect(Collectors.toList());

        System.out.println("Hospital Employees: " + hospitalEmployees.size());

        for (Employee e : hospitalEmployees) {

            System.out.println(e);

        }

        System.out.println();

        List doctors = employees.stream().filter(e -> "Doctor".equals(e.getRole())).collect(Collectors.toList());

        System.out.println("Doctors: " + doctors.size());

        for (Employee e : doctors) {

            System.out.println(e);

        }

        System.out.println();

        List surgeons = employees.stream().filter(e -> "Surgeon".equals(e.getRole())).collect(Collectors.toList());

        System.out.println("Surgeons: " + surgeons.size());

        for (Employee e : surgeons) {

            System.out.println(e);

        }

        System.out.println();

        List nurses = employees.stream().filter(e -> "Nurse".equals(e.getRole())).collect(Collectors.toList());

        System.out.println("Nurses: " + nurses.size());

        for (Employee e : nurses) {

            System.out.println(e);

        }

        System.out.println();

        List administrators = employees.stream().filter(e -> "Administrator".equals(e.getRole())).collect(Collectors.toList());

        System.out.println("Administrators: " + administrators.size());

        for (Employee e : administrators) {

            System.out.println(e);

        }

        System.out.println();

        List receptionists = employees.stream().filter(e -> "Receptionist".equals(e.getRole())).collect(Collectors.toList());

        System.out.println("Receptionists: " + receptionists.size());

        for (Employee e : receptionists) {

            System.out.println(e);

        }

        System.out.println();

        List janitors = employees.stream().filter(e -> "Janitor".equals(e.getRole())).collect(Collectors.toList());

        System.out.println("Janitors: " + janitors.size());

        for (Employee e : janitors) {

            System.out.println(e);

        }

        System.out.println();

    }

    public void updateDatabase() throws IOException {

        try(PrintWriter printWriter = new PrintWriter(filename)) {

            for(Employee e : employees) {

                printWriter.println(e.toDBString());

            }

        }

        System.out.println("Database updated");

    }

    private static void printHeader() {

        System.out.println("**********************************");

        System.out.println("Welcome to the UAB Employee System");

        System.out.println("*********************************");

    }

    private static int getChoice(Scanner scanner) {

        while(true) {

            System.out.println("Menu:");

            System.out.println("1. Display the employee list");

            System.out.println("2. Add employee");

            System.out.println("3. Update the database");

            System.out.println("4. Remove the employee");

            System.out.println("5. Exit");

            System.out.print("Your choice: ");

            try {

                int choice = Integer.parseInt(scanner.nextLine());

                if (choice < 0 || choice > 5) {

                    throw new IllegalArgumentException();

                }

                return choice;

            }

            catch (Exception e) {

                System.out.println("Invalid input");

            }

            System.out.println();

        }

    }

    public static void main(String[] args) throws IOException {

        Hospital hospital = new Hospital(DEFAULT_FILENAME);

        try(Scanner scanner = new Scanner(System.in)) {

            printHeader();

            boolean isOver = false;

            while(!isOver) {

                int choice = getChoice(scanner);

                switch (choice) {

                    case 1:

                        hospital.displayList();

                        break;

                    case 2:

                        try {

                            System.out.print("Please, enter blazer ID: ");

                            int blazerId = Integer.parseInt(scanner.nextLine().trim());

                            System.out.print("Please, enter name: ");

                            String name = scanner.nextLine().trim();

                            System.out.print("Please, enter role (E, D, N, A, S, R, J): ");

                            String role = scanner.nextLine().trim();

                            switch (role) {

                                case "E":

                                    hospital.addEmployee(new HospitalEmployee(blazerId, name));

                                    break;

                                case "D":

                                    System.out.print("Please, enter specialty: ");

                                    String specialty = scanner.nextLine().trim();

                                    hospital.addEmployee(new Doctor(blazerId, name, specialty));

                                    break;

                                case "N":

                                    System.out.print("Please, enter number of patients: ");

                                    int patients = Integer.parseInt(scanner.nextLine().trim());

                                    hospital.addEmployee(new Nurse(blazerId, name, patients));

                                    break;

                                case "A":

                                    System.out.print("Please, enter department: ");

                                    String department = scanner.nextLine().trim();

                                    hospital.addEmployee(new Administrator(blazerId, name, department));

                                    break;

                                case "S":

                                    System.out.print("Please, enter department: ");

                                    department = scanner.nextLine().trim();

                                    System.out.print("Please, enter operating (Y/N): ");

                                    boolean operating = "Y".equalsIgnoreCase(scanner.nextLine().trim());

                                    hospital.addEmployee(new Surgeon(blazerId, name, department, operating));

                                    break;

                                case "R":

                                    System.out.print("Please, enter department: ");

                                    department = scanner.nextLine().trim();

                                    System.out.print("Please, enter answering (Y/N): ");

                                    boolean answering = "Y".equalsIgnoreCase(scanner.nextLine().trim());

                                    hospital.addEmployee(new Receptionist(blazerId, name, department, answering));

                                    break;

                                case "J":

                                    System.out.print("Please, enter department: ");

                                    department = scanner.nextLine().trim();

                                    System.out.print("Please, enter sweeping (Y/N): ");

                                    boolean sweeping = "Y".equalsIgnoreCase(scanner.nextLine().trim());

                                    hospital.addEmployee(new Janitor(blazerId, name, department, sweeping));

                                    break;

                                default:

                                    throw new IllegalArgumentException();

                            }

                        }

                        catch (Exception e) {

                            System.out.println("Invalid input");

                        }

                        break;

                    case 3:

                        hospital.updateDatabase();

                        break;

                    case 4:

                        try {

                            System.out.print("Please, enter blazer ID to delete: ");

                            int blazerIdToDelete = Integer.parseInt(scanner.nextLine().trim());

                            System.out.print("Please, enter role to delete: ");

                            String roleToDelete = scanner.nextLine().trim();

                            if (hospital.removeEmployee(blazerIdToDelete, roleToDelete)) {

                                System.out.println("Employee with blazer ID " + blazerIdToDelete + " was deleted");

                            } else {

                                System.out.println("Employee with given blazer ID and role not found");

                            }

                        }

                        catch (NumberFormatException e) {

                            System.out.println("Invalid blazer ID");

                        }

                        break;

                    case 5:

                        isOver = true;

                        break;

                    default:

                        throw new IllegalStateException();

                }

                System.out.println();

            }

            System.out.println("******************************************************************");

        }

    }

}