2
\$\begingroup\$

We were tasked to do this assignment where there is the base class employee and two derived class part-time and full-time employees. Here is what I came up with:

import java.util.Scanner; class RunEmployee { public static void main(String[] args){ 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(); if(inputForP.equals("F")){ System.out.println("Enter Monthly Salary:"); FullTimeEmployee fte1 = new FullTimeEmployee(s.nextDouble()); System.out.println("Name: " + employee1.getName()); System.out.println("Monthly Salary: " + fte1.getMonthlySalary()); }else if (inputForP.equals("P")){ System.out.println("Enter rate per hour and no. of hours worked seperated by space :"); System.out.println("Sample : 107.50 13"); PartTimeEmployee pte1 = new PartTimeEmployee(s.nextDouble(), s.nextInt()); System.out.println("Name: " + employee1.getName()); System.out.printf("Wage: %.2f" , pte1.getWage()); }else{ System.out.println("Invalid"); } } } class Employee{ private String name; public void setName(String newName){ name = newName; } public String getName(){ return name; } } class FullTimeEmployee extends Employee { public double monthlySalary; Scanner s = new Scanner(System.in); FullTimeEmployee(double newMonthlySalary){ setMonthlySalary(newMonthlySalary); } public void setMonthlySalary(double newMonthlySalary){ monthlySalary = newMonthlySalary; } public double getMonthlySalary(){ return monthlySalary; } } class PartTimeEmployee extends Employee{ public double wage; Scanner s = new Scanner(System.in); PartTimeEmployee(double rph, int hWorked){ setWage(rph, hWorked); } public void setWage(double rph, int hWorked){ wage = rph * hWorked; } public double getWage(){ return wage; } } 

I wonder in what ways I can improve my code to make it look cleaner and look like it is written by a professional. I am only a beginner, and this is the best I can come up with.

\$\endgroup\$
3
  • 1
    \$\begingroup\$I wouldn't represent the full/part-time-ness of an Employee via inheritance. It's something that may change over time.\$\endgroup\$
    – Andy
    CommentedMar 19, 2022 at 6:18
  • \$\begingroup\$What happens if the name is blank? OR the values entered are not numbers?\$\endgroup\$
    – Mr R
    CommentedMar 19, 2022 at 8:48
  • \$\begingroup\$What exactly are the requirements of your assignment? It's hard for us to tell if you did the task well if we don't know what the task is.\$\endgroup\$CommentedMar 19, 2022 at 14:05

1 Answer 1

2
\$\begingroup\$

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!!"); } } 
\$\endgroup\$
0

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.