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.