0

该程序旨在接收用户输入信息并将其存储在 arraylist 员工对象中。我不确定为什么会出现这些错误。有什么帮助吗?

代码:

import java.util.*;
import java.io.*;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;

public class P
{
//Instance Variables
private static String empName;
private static String wage;
private static double wages;
private static double salary;
private static int numHours;
private static double increase; 

//  static ArrayList<String> ARempName = new ArrayList<String>();
//  static ArrayList<Double> ARwages = new ArrayList<Double>();
//  static ArrayList<Double> ARsalary = new ArrayList<Double>();
static ArrayList<Employee> emp = new ArrayList<Employee>();

public static void main(String[] args) throws Exception
{       
    Employee emp1 = new Employee("", 0);    


clearScreen();
printMenu();
question();
exit();
}

public static void printArrayList(ArrayList<Employee> emp)
{
    for (int i = 0; i < emp.size(); i++){
            System.out.println(emp.get(i));
}
}

public static void clearScreen()
{
    System.out.println("\u001b[H\u001b[2J");
}

private static void exit()
{
System.exit(0);
}

private static void printMenu()
{
System.out.println("\t------------------------------------");
System.out.println("\t|Commands: n - New employee        |");
System.out.println("\t|          c - Compute paychecks   |");
System.out.println("\t|          r - Raise wages         |");
System.out.println("\t|          p - Print records       |");
System.out.println("\t|          d - Download data       |");
System.out.println("\t|          u - Upload data         |");
System.out.println("\t|          q - Quit                |");
System.out.println("\t------------------------------------");
System.out.println("");
}

private static void question()
{
System.out.print("Enter command: ");
Scanner q = new Scanner(System.in);
String input = q.nextLine();
input.replaceAll("\\s","").toLowerCase();

boolean valid = (input.equals("n") || input.equals("c") || input.equals("r") ||    input.equals("p") || input.equals("d") || input.equals("u") || input.equals("q"));

if (!valid){
    System.out.println("Command was not recognized; please try again.");
printMenu();
question();
}
else if (input.equals("n")){
    System.out.print("Enter the name of new employee: ");
    Scanner stdin = new Scanner(System.in);
    empName = stdin.nextLine();
    emp1.setName(empName);
    System.out.print("Hourly (h) or salaried (s): ");
    Scanner stdin2 = new Scanner(System.in);
    wage = stdin2.nextLine();
    wage.replaceAll("\\s","").toLowerCase(); 
    if (!(wage.equals("h") || wage.equals("s"))){
    System.out.println("Input was not h or s; please try again");
    }
    else if (wage.equals("h")){
    System.out.print("Enter hourly wage: ");
    Scanner stdin4 = new Scanner(System.in);
    wages = stdin4.nextDouble();
    emp1.setWage(wages);
    printMenu();
    question();}
    else if (wage.equals("s")){
    System.out.print("Enter annual salary: ");
    Scanner stdin5 = new Scanner(System.in);
    salary = stdin5.nextDouble();
    emp1.setWage(salary);
    printMenu();
    question();}}
else if (input.equals("c")){
    System.out.print ("Enter number of hours worked by " + empName);
    Scanner stdin = new Scanner(System.in);
    numHours = stdin.nextInt();
    System.out.println("Pay: ");
    System.out.print("Enter number of hours worked by " + empName);
        Scanner stdin2 = new Scanner(System.in);
        numHours = stdin2.nextInt();
        System.out.println("Pay: ");
    printMenu();
    question();}
else if (input.equals("r")){
    System.out.print("Enter percentage increase: ");
    Scanner stdin = new Scanner(System.in);
    increase = stdin.nextDouble();
    System.out.println("\nNew Wages");
    System.out.println("---------");
//      System.out.println(Employee.toString());
    printMenu();
    question();
    }
else if (input.equals("p")){
    printArrayList(emp);
    printMenu();
    question();
}
else if (input.equals("q")){
    exit();
}
}


public abstract class Employee {
private String name;
private double wage;

protected Employee(String name, double wage){
  this.name = name;
  this.wage = wage;
}

public String getName() {
  return name;
}

public double getWage() {
  return wage;
}

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

public void setWage(double wage) {
  this.wage = wage;
}

public void percent(double wage, double percent) {
  wage *= percent;
}
}

public class HourlyEmployee extends Employee {
//Instance Variables
public String result;

public HourlyEmployee(String name, double wage){
  super(name, wage);
}

public void computePay(double wage){
  if (numHours <= 40){
    wage *= numHours;}
  else {
    wage = numHours * (1.5 * wage);}
}

/*  public void toString(){
  System.out.println("Name: " + name);
}*/
}

public class SalariedEmployee extends Employee {
    //Instance Variables
private double salary;
private int salHours = 2080;

    public SalariedEmployee(String name, double wage){
      super(name, wage);
    }

    public void computePay(double wage){
      salary = (salHours * salary) / 52;}

public double getSalary(){
  return salary;
}

public void setSalary(double salary) {
  this.salary = salary;
}

/*     public void toString(){
      System.out.print("Name:  " name + getSalary() + "/year");
     }*/
}
}

错误:

javac P.java
P.java:24: non-static variable this cannot be referenced from a static context
    Employee emp1 = new Employee("", 0);    
                    ^
P.java:82: cannot find symbol
symbol  : variable emp1
location: class P
    emp1.setName(empName);
    ^
P.java:94: cannot find symbol
symbol  : variable emp1
location: class P
    emp1.setWage(wages);
    ^
P.java:101: cannot find symbol
symbol  : variable emp1
location: class P
    emp1.setWage(salary);
    ^
4 errors

任何能指出我正确方向的提示都将不胜感激!

4

3 回答 3

1

你不能实例化一个抽象类,emp1 也没有在本地初始化.. prolly 因为你不能将一个对象实例化为一个抽象..

      Employee emp1 = HiredEmployee("", 0) or SalaryEmployee("", 0);
于 2012-11-06T20:07:15.703 回答
0

您的代码很容易修复:

你必须更换:

public abstract class Employee {}

public static class Employee {}

抽象类不能被实例化,另外我认为你的意思是员工类是静态的。

然后,移动

final Employee emp1 = new Employee("", 0);

进入:

private static void question() {}

因为 emp1 仅在该函数中使用,否则它将不可见(它“超出范围”),或者作为替代,将其声明为全局变量。

于 2012-11-06T20:16:13.050 回答
0

非静态变量 this 不能从静态上下文中引用

Employee是一个非静态的内部类,因此它必须有一个外部类的封闭实例。因此,您无法创建Employee没有 aP

该机制在Java 教程 > 嵌套类中进行了解释

更改此行:

public abstract class Employee {

public static abstract class Employee {

它应该可以工作(您还必须更改不同的 Employee 子类型)

于 2012-11-06T20:22:28.297 回答