Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Let\'s say i have a program that has four defrent classes first one for a manage

ID: 3548118 • Letter: L

Question

Let's say i have a program that has four defrent classes
first one for a manager

second for an employee

third for sales person

fourth for a customer  

each class of them has deferent information
first name , last name
salary , allowance  ..

I need to use a scanner function to print the information
like if a user insert the manger first name which i use in class the program will print the information for a manger ..
if he inter the customer name which i use in class the program will print the customer information ..

i don't want the program print the information that user inter ..

Explanation / Answer

Hi,


Please find the program below. It gets the input from user, checks whether it is a valid manager, Employee or Customer name and print the data accordingly. The values are hardcoded in the constructor.

Please find below the program. Hope it helps.


import java.util.Scanner;

public class PrintInfo {

    void scanner() {
        Person manager = new Manager("ManagerFN", "MMM", 100000f, 100f);
        Person employee = new Employee("EmployeeFN", "EEE", 1000f, 100f);
        Person customer = new Customer("CustomerCN", "CCC", 10000f, 100f);

        System.out.println("Please eneter the firstName");

        Scanner sc = new Scanner(System.in);
        String name = sc.next();
        if (name.equalsIgnoreCase(manager.firstName)) {
            System.out.println("Manager Data:: FirstName: " + manager.firstName
                    + " LastName:" + manager.lastName + " Salary:" + manager.salary
                    + " Allowance:" + manager.allowance);

        } else if (name.equalsIgnoreCase(employee.firstName)) {
            System.out.println("Employee Data:: FirstName: "
                    + employee.firstName + " LastName:" + employee.lastName
                    + " Salary:" + employee.salary + " Allowance:"
                    + employee.allowance);

        } else if (name.equalsIgnoreCase(customer.firstName)) {
            System.out.println("Customer Data:: FirstName: "
                    + customer.firstName + " LastName:" + customer.lastName
                    + " Salary:" + customer.salary + " Allowance:"
                    + customer.allowance);

        }
        else
        {
            System.out.println("No record found");
        }

    }

    public static void main(String[] args) {
        PrintInfo info = new PrintInfo();
        info.scanner();
    }
}

class Person {
    String firstName;
    String lastName;
    float salary;
    float allowance;

    public Person() {
        // TODO Auto-generated constructor stub
    }

    public Person(String firstName, String lastName, float salary,
            float allowance) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.salary = salary;
        this.allowance = allowance;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public float getSalary() {
        return salary;
    }

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

    public float getAllowance() {
        return allowance;
    }

    public void setAllowance(float allowance) {
        this.allowance = allowance;
    }
}

class Manager extends Person {
    public Manager(String firstName, String lastName, float salary,
            float allowance) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.salary = salary;
        this.allowance = allowance;
    }

}

class Employee extends Person {
    public Employee(String firstName, String lastName, float salary,
            float allowance) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.salary = salary;
        this.allowance = allowance;
    }

}

class Customer extends Person {
    public Customer(String firstName, String lastName, float salary,
            float allowance) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.salary = salary;
        this.allowance = allowance;
    }

}

Thanks,

Sri