Although a beginner myself, this is what I came up with:
import java.util.Scanner; public class RunEmployee { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter name: "); String name = scanner.nextLine(); // Getting the name of the Employee System.out.println("Press F for Full Time or P for Part Time"); char employeeType = scanner.next().charAt(0); // Getting the Employee type // controlling the switch block with employeeType switch (employeeType) { case 'F' -> { // for full-time employee System.out.println("Enter Monthly Salary:"); FullTimeEmployee fullTimeEmployee = new FullTimeEmployee(name, scanner.nextDouble()); // instantiating FullTimeEmployee Object System.out.println("Name: " + fullTimeEmployee.getName()); System.out.println("Monthly Salary: " + fullTimeEmployee.getMonthlySalary()); } case 'P' -> { // for part-time employee System.out.println("Enter rate per hour and no. of hours worked separated by space:"); System.out.println("Sample: 107.50 13"); PartTimeEmployee partTimeEmployee = new PartTimeEmployee(name, scanner.nextDouble(), scanner.nextInt()); // instantiating PartTimeEmployee Object System.out.println("Name: " + partTimeEmployee.getName()); System.out.printf("Wage: %.2f", partTimeEmployee.getWage()); } default -> { System.out.println("Wrong Choice!!"); } } } } class Employee { private String name; Employee(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } } class FullTimeEmployee extends Employee { private double monthlySalary; FullTimeEmployee(String name, double monthlySalary) { super(name); this.monthlySalary = monthlySalary; } public double getMonthlySalary() { return monthlySalary; } public void setMonthlySalary(double monthlySalary) { this.monthlySalary = monthlySalary; } } class PartTimeEmployee extends Employee { private double wage; PartTimeEmployee(String name, double rph, int hWorked) { super(name); wage = rph * hWorked; } public double getWage() { return wage; } public void setWage(double rph, int hWorked) { this.wage = rph * hWorked; } }
Here are the things I improved and why you should do it:-
String inputForP; Scanner s = new Scanner(System.in); Employee employee1 = new Employee(); System.out.println("Enter name: "); employee1.setName(s.nextLine()); System.out.println("Press F for Full Time or P for Part Time"); inputForP = s.nextLine();
The String variable inputForP should be more descriptive about its use, so a better identifier is recommended such as employeeType and since use are using it to store just one character i.e., P or F, it type should be char as there is no point in using String here.
You also instantiated Employee Class here but it is totally redundant as FullTimeEmployee and PartTimeEmployee are subclasses of Employee class and they inherit all properties and methods of Employee class, so if you want to store the name you can just use setName() method in FullTimeEmployee/PartTimeEmployee class. So here, the imporved version of this block of code:
Scanner scanner = new Scanner(System.in); System.out.println("Enter name: "); String name = scanner.nextLine(); // Getting the name of the Employee System.out.println("Press F for Full Time or P for Part Time"); char employeeType = scanner.next().charAt(0); // Getting the Employee type
Next you used if-else-statement when it would have been better to switch block with employeeType as its controller
switch (employeeType) { case 'F' -> { // for full-time employee System.out.println("Enter Monthly Salary:"); FullTimeEmployee fullTimeEmployee = new FullTimeEmployee(name, scanner.nextDouble()); // instantiating FullTimeEmployee Object System.out.println("Name: " + fullTimeEmployee.getName()); System.out.println("Monthly Salary: " + fullTimeEmployee.getMonthlySalary()); } case 'P' -> { // for part-time employee System.out.println("Enter rate per hour and no. of hours worked separated by space:"); System.out.println("Sample: 107.50 13"); PartTimeEmployee partTimeEmployee = new PartTimeEmployee(name, scanner.nextDouble(), scanner.nextInt()); // instantiating PartTimeEmployee Object System.out.println("Name: " + partTimeEmployee.getName()); System.out.printf("Wage: %.2f", partTimeEmployee.getWage()); } default -> { System.out.println("Wrong Choice!!"); } }