Answer to Question #186207 in C++ for Ayush parashar

Question #186207

Create a class named 'Member' having the following members:

Data members

1 - Name

2 - Age

3 - Phone number

4 - Address

5 - Salary

It also has a method named 'printSalary' which prints the salary of the members.

Two classes 'Employee' and 'Manager' inherits the 'Member' class. The 'Employee' and 'Manager' 

classes have data members 'specialization' and 'department' respectively. Now, assign name, age, 

phone number, address and salary to an employee and a manager by making an object of both of these 

classes and print the same.


1
Expert's answer
2021-04-27T23:32:42-0400
public class Employee extends Member {
    private String specialization;

    public Employee(String name, int age, String phoneNumber, String city, int salary, String specialization) {
        super(name, age, phoneNumber, city, salary);
        this.specialization = specialization;
    }

    @Override
    public String toString() {
        return "Employee: \n" +
                " name = " + this.getName() + '\n'  +
                " age = " + this.getAge() +
                " phoneNumber = " + this.getPhoneNumber() + '\n' +
                " city = " + this.getCity() + '\n' +
                " salary = " + this.getSalary()+ "\n"+
                " specialization = " + specialization ;
    }

    public String getSpecialization() {
        return specialization;
    }

    public void setSpecialization(String specialization) {
        this.specialization = specialization;
    }
}
public class Employee extends Member {
    private String specialization;

    public Employee(String name, int age, String phoneNumber, String city, int salary, String specialization) {
        super(name, age, phoneNumber, city, salary);
        this.specialization = specialization;
    }

    @Override
    public String toString() {
        return "Employee: \n" +
                " name = " + this.getName() + '\n'  +
                " age = " + this.getAge() +
                " phoneNumber = " + this.getPhoneNumber() + '\n' +
                " city = " + this.getCity() + '\n' +
                " salary = " + this.getSalary()+ "\n"+
                " specialization = " + specialization ;
    }

    public String getSpecialization() {
        return specialization;
    }

    public void setSpecialization(String specialization) {
        this.specialization = specialization;
    }
}
public class Manager extends Member {

    private String department;

    public Manager(String name, int age, String phoneNumber, String city, int salary, String department) {
        super(name, age, phoneNumber, city, salary);
        this.department = department;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    @Override
    public String toString() {
        return "Manager: \n" +
                " name = " + this.getName() + '\n'  +
                " age = " + this.getAge() +
                " phoneNumber = " + this.getPhoneNumber() + '\n' +
                " city = " + this.getCity() + '\n' +
                " salary = " + this.getSalary()+ "\n"+
                " department = " + department;

    }
}
public class Member {
    private String name;
    private int age;
    private String phoneNumber;
    private String city;
    private int salary;



    public Member(String name, int age, String phoneNumber, String city, int salary) {
        this.name = name;
        this.age = age;
        this.phoneNumber = phoneNumber;
        this.city = city;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Member: \n" +
                " name = " + name + '\n'  +
                ", age = " + age +
                ", phoneNumber = " + phoneNumber + '\n' +
                ", city = " + city + '\n' +
                ", salary = " + salary;
    }
}
import java.util.ArrayList;

public class Main {
    private static Employee[] employees = {
            new Employee("employee1", 30, "656-659-95", "London", 3800, "Java developer"),
            new Employee("employee2", 20, "648-648-95", "Berlin", 4000, "C++ developer"),
            new Employee("employee3", 40, "656-159-95", "Tokyo", 1000, "Manager"),
            new Employee("employee4", 50, "111-222-23", "Dubai", 1500, ""),
            new Employee("employee5", 60, "656-222-95", "Atlanta", 100, "Python developer"),
            new Employee("employee6", 70, "656-167-95", "Denver", 5500, "Java developer")
    };
    private static Manager[] managers = {
            new Manager("manager6", 30, "656-659-95", "London", 3800, "Java developer"),
            new Manager("manager2", 20, "648-648-95", "Berlin", 4000, "C++ developer"),
            new Manager("manager4", 40, "656-159-95", "Tokyo", 1000, "Manager"),
            new Manager("manager9", 50, "111-222-23", "Dubai", 1500, ""),
            new Manager("manager8", 60, "656-222-95", "Atlanta", 100, "Python developer"),
            new Manager("manager3", 70, "656-167-95", "Denver", 5500, "Java developer")
    };

    private static ArrayList<Employee> searchBySpecialization(final Employee[] empl, final String specialization) {
        ArrayList<Employee> res = new ArrayList<>();
        for (Employee employee : empl) {
            if (employee.getSpecialization().equals(specialization))
                res.add(employee);
        }
        return res;
    }

    private static ArrayList<Member> searchBySalary(final Member[] members, final int minSalary, final int maxSalary) {
        ArrayList<Member> res = new ArrayList<>();
        for (Member member : members) {
            if (member.getSalary() >= minSalary && member.getSalary() <= maxSalary
                    && member.getAge() < 50)
                res.add(member);
        }
        return res;
    }

    public static void main(String[] args) {
        System.out.println("---------------EMPLOYEES---------------");
        for (Employee employee : employees) {
            System.out.println(employee);
        }
        System.out.println("---------------MANAGERS---------------");
        for (Employee employee : employees) {
            System.out.println(employee);
        }
        System.out.println("---------------All employees with specialization 'Java developer'---------------");
        ArrayList<Employee> list = searchBySpecialization(employees, "Java developer");
        for (Employee empl : list) {
            System.out.println(empl);
        }
        System.out.println("---------------All employers salary [2000; 5000]---------------");
        ArrayList<Member> listSalary = searchBySalary(employees, 2000, 5000);
        for (Member empl : listSalary) {
            System.out.println(empl);
        }
        System.out.println("---------------All managers salary [2000; 5000]---------------");
        ArrayList<Member> listSalary2 = searchBySalary(managers, 2000, 5000);
        for (Member empl : listSalary2) {
            System.out.println(empl);
        }

    }

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS