Trouble calling a method from an external class

Posted by Bradley Hobbs on Stack Overflow See other posts from Stack Overflow or by Bradley Hobbs
Published on 2012-11-07T22:57:14Z Indexed on 2012/11/07 22:59 UTC
Read the original article Hit count: 160

Filed under:
|

Here is my employee database program:

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 double 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
{       

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("");
}

public 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();
    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();
    Employee emp1 = new HourlyEmployee(empName, wages);
    emp.add(emp1);
    printMenu();
    question();}
    else if (wage.equals("s")){
    System.out.print("Enter annual salary: ");
    Scanner stdin5 = new Scanner(System.in);
    salary = stdin5.nextDouble();
    Employee emp1 = new SalariedEmployee(empName, salary);
    printMenu();
    question();}}
else if (input.equals("c")){
    for (int i = 0; i < emp.size(); i++){
    System.out.println("Enter number of hours worked by " + emp.get(i) + ":");
    }
    Scanner stdin = new Scanner(System.in);
    numHours = stdin.nextInt();
    System.out.println("Pay: " + emp1.computePay(numHours));
    System.out.print("Enter number of hours worked by " + empName);
        Scanner stdin2 = new Scanner(System.in);
        numHours = stdin2.nextInt();
        System.out.println("Pay: " + emp1.computePay(numHours));
    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();
}
}

}

Here is one of the class files:

 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;
}
}

And here are the errors:

P.java:108: cannot find symbol
symbol  : variable emp1
location: class P
    System.out.println("Pay: " + emp1.computePay(numHours));
                                 ^
P.java:112: cannot find symbol
symbol  : variable emp1
location: class P
        System.out.println("Pay: " + emp1.computePay(numHours));
                                     ^
2 errors

I'm trying to the get paycheck to print out but i'm having trouble with how to call the method. It should take the user inputed numHours and calculate it then print on the paycheck for each employee. Thanks!

© Stack Overflow or respective owner

Related posts about class

Related posts about methods