Home > Enterprise >  Employee Java Inherit and poly
Employee Java Inherit and poly

Time:04-18

It keeps asking me that "cant find main method: Employee" but my main method is in the RunEmployee class.

can you guys help me out?

import java.util.*;

class Employee{

    String name;

    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
}
class FullTimeEmployee extends Employee{

    double monthlySalary;

    public void setMonthlySalary(double monthlySalary){
        this.monthlySalary = monthlySalary;
    }
    public double getMonthlySalary(){
        return monthlySalary;
    }
}
class PartTimeEmployee extends Employee{

    double ratePerHour,wage;
    int hoursWorked;

    public void setWage(double ratePerHour, int hoursWorked, double wage){
        this.ratePerHour = ratePerHour;
        this.hoursWorked = hoursWorked;
        this.wage = wage;
    }
    public double getWage(){
        return wage = ratePerHour * hoursWorked;
    }
}
class RunEmployeeName{

    public static void main(String[]args){

        Employee zero = new Employee();
        FullTimeEmployee first = new FullTimeEmployee();
        PartTimeEmployee second = new PartTimeEmployee();


        Scanner ans = new Scanner(System.in);

        System.out.println("Enter name: ");
        zero.name = ans.nextLine();

        System.out.println("Press F for Full Time or P for Part Time");
        String ForP = ans.nextLine();

        if(ForP == "P"){

            System.out.println("Enter rate per hour and no. of hours worked separated by space");
            System.out.println("Sample: 107 .50 13");
            second.ratePerHour = ans.nextDouble();
            second.hoursWorked = ans.nextInt();
            ans.nextLine();

            System.out.println("Name: "   zero.name);
            System.out.println("Wage: "   second.wage);
        }
        else if (ForP == "F"){
            System.out.println("Enter monthly salary: ");
            first.monthlySalary = ans.nextDouble();

            System.out.println("Name: "   zero.name);
            System.out.println("Wage: "   first.monthlySalary);
        }
        else{
            System.out.println("Ivalid, input letters P and F only!");
        }
    }
}

These are the output supposed to be

Enter Name:
Uncle Dope
Press F for Full Time or P for Part Time:
P
Enter rate per hour and no. of hours worked separated by space:
Sample: 107 .50 13
108.95 72
Name: Uncle Dope
Wage: 7844.40

OR....

Enter Name:
Uncle Dope
Press F for Full Time or P for Part Time:
F
Enter monthly salary:
Sample: 415.60
382.85
Name: Uncle Dope
Montly Salary: 382.85

CodePudding user response:

here I re-write the program it will help you.

Please run the main method. :)

import java.util.Scanner;

public class Main { private static Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {

    boolean quit =false;
    while (!quit){
        menu();
        System.out.print("Enter your choice : ");
        String ForP = scanner.nextLine();
        switch (ForP) {
            case "1":
                fullTime();
                break;
            case "2":
                partTime();
                break;
            case "0":
                quit = true;
                break;
            default:
                System.out.println("enter 1 or 2 ");
        }
    }
    }
    static void menu(){
        System.out.println("------------- Wage Calculator ---------------");
        System.out.println("'1' - for Full Time");
        System.out.println("'2' - for Part Time");
        System.out.println("'0' - for Quit ");

    }
    static void partTime(){
        System.out.print("Enter name : ");
        String name = scanner.nextLine();
        System.out.print("Enter rate per hour : ");
        double rate = scanner.nextDouble();
        System.out.print("no. of hours worked : ");
        int hours = scanner.nextInt();
        PartTimeEmployee newPartEmployee = new PartTimeEmployee(name,rate,hours);
        System.out.println("monthly wage of "  newPartEmployee.getName() " : " newPartEmployee.getWage());
    }
    static void fullTime(){
        System.out.print("Enter name : ");
        String name = scanner.nextLine();
        FullTimeEmployee newFullEmployee = new FullTimeEmployee(name,5000);
        System.out.println(" wage of "  newFullEmployee.getName() " : " newFullEmployee.getMonthlySalary());
    }

}

public class Employee { String name;

public Employee(String name) {
    this.name = name;
}
public void setName(String name){
    this.name = name;
}
public String getName(){
    return name;
}

}

public class FullTimeEmployee extends Employee {
double monthlySalary;

public FullTimeEmployee(String name,double monthlySalary) {
    super(name);
    this.monthlySalary = monthlySalary;
}

public void setMonthlySalary(double monthlySalary){
    this.monthlySalary = monthlySalary;
}

public double getMonthlySalary() {
    return monthlySalary;
}

}

public class PartTimeEmployee extends Employee{
private double ratePerHour,wage;
private int hoursWorked;

public PartTimeEmployee(String name,double ratePerHour, int hoursWorked) {
    super(name);
    this.ratePerHour = ratePerHour;
    this.hoursWorked = hoursWorked;
}
public double getWage(){
    return wage = this.ratePerHour * this.hoursWorked;
}

}

CodePudding user response:

You changed my code entire structure
This is the output supposed to be

Enter Name:
Uncle Dope
Press F for Full Time or P for Part Time:
P
Enter rate per hour and no. of hours worked separated by space:
Sample: 107 .50 13
108.95 72
Name: Uncle Dope
Wage: 7844.40

OR....

Enter Name:
Uncle Dope
Press F for Full Time or P for Part Time:
F
Enter monthly salary:
Sample: 415.60
382.85
Name: Uncle Dope
Montly Salary: 382.85

  • Related