-1

I'm trying to write a program for a class that creates a payroll stub. I've looked at work online and I'm trying to create my own version, but I keep getting errors revolving around what my main() is, and it wont run correctly.

I've tried reorganizing the code, creating a new file, and renaming things, but I still get the following exception:

"Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    No enclosing instance of type Employee is accessible. Must qualify the allocation with an enclosing instance of type Employee (e.g. x.new A() where x is an instance of Employee).
at Employee.main(Employee.java:8)

I expected the ability to input these values in my code:

import java.util.Scanner;
import java.util.*;

public class Employee{
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        PayStub data = new PayStub();

        System.out.println("Enter name: ");
        data.setName(input.next());

        System.out.println("Enter address: ");
        data.setAddress(input.next());

        System.out.println("Enter SSN: ");
        data.setSSN((input.next()));

        System.out.println("Number of hours: ");
        data.setHoursWorked(Double.parseDouble(input.next()));

        System.out.println("Enter hourly rate: ");
        data.setHourlyPayRate(Double.parseDouble(input.next()));

        input.close();
    }
}

public class PayStub {
    private String name;
    private double hoursWorked;
    private double hourlyRate;
    private double fedTaxRate;
    private double stateTaxRate;
    private String address;
    private String phone;
    private String SSN;

    public PayStub() {

    }
    public PayStub(String n, double w, double p) {
        this.name = n;
        this.hoursWorked = w;
        this.hourlyRate = p;
    }

    public double grossPay(double hr, double hw) {
        double grossPay = (hourlyRate * hoursWorked);
        return grossPay;
    }

    public void setFedTax(double fedTaxRate) {
        this.fedTaxRate = fedTaxRate;
    }
    public double getFedTax() {
        return fedTaxRate;
    }

    public void setStateTax(double stateTaxRate) {
        this.stateTaxRate = stateTaxRate;
    }

    public double getStateTax() {
        return stateTaxRate;
    }

    public String getName() {
        return name;
    }

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

    public double getHoursWorked() {
        return hoursWorked;
    }

    public void setHoursWorked(double hoursWorked) {
        this.hoursWorked = hoursWorked;
    }

    public double getHourlyPayRate() {
        return hourlyRate;
    }

    public void setHourlyPayRate(double hourlyRate) {
        this.hourlyRate = hourlyRate;
    }

    public String getAddress(){
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPhone(){
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getSSN() {
        return SSN;
    }

    public void setSSN(String SSN) {
        this.SSN = SSN;
    }
}
Micah Zoltu
  • 6,764
  • 5
  • 44
  • 72
  • 2
    *"What is causing the error I am getting"* + *"Unresolved compilation problem"* = A compilation error prevents the code from running. Fix the compilation error before trying to run the code. Look at the output of the **compiler** to see the compiler error in full. Then fix it. – Andreas Jul 22 '19 at 20:42

1 Answers1

0

The easiest fix for this is to move the public class PayStub to a file called PayStub.java.

You could declare PayStub as a static class, making it a nested class of Employee, but there would be no reason to do that as PayStub is not a type of employee, but it something an employee has. It is something a contractor might have, etc... So you want to split it up into its own file as it is a reusable class.

Dan
  • 7,286
  • 6
  • 49
  • 114