Challenge: Employee Pay

Lab Java Intermediate 20 points

Write a class Employee with a name, a base salary and a method int pay() that returns the salary. Write Manager extends Employee that also has a bonus, uses super(...) in its constructor, and overrides pay() to return salary + bonus.

Read n lines. Each is either E name salary or M name salary bonus. Print <name> earns <pay> for each, then Total: <sum>.

Input

First n, then n lines as described.

Output

n lines, then the total.

Example

Input

3
E Ravi 30000
M Asha 50000 10000
E Meera 32000

Output

Ravi earns 30000
Asha earns 60000
Meera earns 32000
Total: 122000
Sample input available Sample output available
Editor
Java

Hints

The first line of the Manager constructor is super(name, salary);

Only read the bonus when the type is M.

Solution explanation

An Employee[] array can hold managers too. Each object's own pay() is used.

Show solution
import java.util.Scanner;

class Employee {
    String name;
    int salary;

    Employee(String name, int salary) {
        this.name = name;
        this.salary = salary;
    }

    int pay() { return salary; }
}

class Manager extends Employee {
    int bonus;

    Manager(String name, int salary, int bonus) {
        super(name, salary);
        this.bonus = bonus;
    }

    @Override
    int pay() { return salary + bonus; }
}

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Employee[] staff = new Employee[n];
        for (int i = 0; i < n; i++) {
            String type = sc.next();
            String name = sc.next();
            int salary = sc.nextInt();
            staff[i] = type.equals("M") ? new Manager(name, salary, sc.nextInt()) : new Employee(name, salary);
        }
        int total = 0;
        for (Employee e : staff) {
            System.out.println(e.name + " earns " + e.pay());
            total += e.pay();
        }
        System.out.println("Total: " + total);
    }
}
Output
Answer
HOME LABS COMMUNITY DASHBOARD