-1

私はちょうど今、Java で OOP をやっているところですが、ちょっとした問題にぶつかりました。2 つの問題があります。最大の問題は、オブジェクトが正しく表示されないことです。私のemployee.javaクラスには、従業員/労働者を表示するための次のコードがあります。

public String getInfo() {
        return firstName + " " + lastName + "\n" + address + "\n" + city + 
                                            "\n" + wageRate + "\n" + hoursWorked;
    }

現在、employeeTest.javaほとんどの処理と更新を行っているところで、個別のオブジェクト用に次のものが作成されています。

//instantiate 4 employee objects
        employees worker1 = new employees();
        employees worker2 = new employees();
        employees worker3 = new employees();
        employees worker4 = new employees();

その後、次のコードを使用した直後に従業員を更新して表示しようとしました (テスト上の理由から不完全です)。

    //declare variables to update employees
    String newFirstName1, newFirstName2, newFirstName3, newFirstName4;
    String newLastName1, newLastName2, newLastName3, newLastName4;
    String newAddress1, newAddress2, newAddress3, newAddress4;
    String newCity1, newCity2, newCity3, newCity4;
    int newHoursWorked1, newHoursWorked2, newHoursWorked3, newHoursWorked4;
    int newWageRate1, newWageRate2, newWageRate3, newWageRate4;

    //update the employee (in progress)
    System.out.println("Please enter Employee 1's first name:");
    newFirstName1 = conIn.nextLine();
    System.out.println("Please enter Employee 1's last name:");
    newLastName1 = conIn.nextLine();
    System.out.println("Please enter Employee 1's address:");
    newAddress1 = conIn.nextLine();
    System.out.println("Please enter Employee 1's city:");
    newCity1 = conIn.nextLine();
    System.out.println("Please enter Employee 1's hours worked:");
    newHoursWorked1 = conIn.nextInt();
    System.out.println("Please enter Employee 1's wage rate:");
    newWageRate1 = conIn.nextInt();
    worker1.setFirstName(newFirstName1);
    worker1.setLastName(newLastName1);

    //Display the employees
    System.out.println("Employee 1");
    System.out.println("----------------------");
    System.out.println(worker1.getInfo());
    System.out.println("Employee 2");
    System.out.println("----------------------");
    System.out.println(worker2.getInfo());
    System.out.println("Employee 3");
    System.out.println("----------------------");
    System.out.println(worker3.getInfo());
    System.out.println("Employee 4");
    System.out.println("----------------------");
    System.out.println(worker4.getInfo());

ただし、これはすべてのワーカーを更新します。元。ファースト ネームに「John」と入力するworker1-4と、すべてファースト ネーム「John」で更新されます。私は更新したいだけworker1なので、それが機能することを知っています。

2 つ目の問題は、コードの実行およびテスト中に次のコードがスキップされることです。

    //change the company name
    String newCompanyName;
    System.out.println("Please enter a new company name:");
    newCompanyName = conIn.nextLine();
    employees.setCompanyName(newCompanyName);

編集 3

employeeTest.java以下のコード

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package employeerionmurphy;
import employeerionmurphy.employees.*;
import java.util.*;

/**
 *
 * @author Owner
 */
public class employeeTest {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        //declare scanner
        Scanner conIn = new Scanner(System.in);

        //instantiate 4 employee objects
        employees worker1 = new employees();
        employees worker2 = new employees();
        employees worker3 = new employees();
        employees worker4 = new employees();

        //Display the employees
        System.out.println("Employee 1");
        System.out.println("----------------------");
        System.out.println(worker1.getInfo());
        System.out.println("Employee 2");
        System.out.println("----------------------");
        System.out.println(worker2.getInfo());
        System.out.println("Employee 3");
        System.out.println("----------------------");
        System.out.println(worker3.getInfo());
        System.out.println("Employee 4");
        System.out.println("----------------------");
        System.out.println(worker4.getInfo());

        //spacer for user readability
        System.out.println("");
        System.out.println("--------------------------------------------");
        System.out.println("");

        //declare variables to update employees
        String newFirstName1, newFirstName2, newFirstName3, newFirstName4;
        String newLastName1, newLastName2, newLastName3, newLastName4;
        String newAddress1, newAddress2, newAddress3, newAddress4;
        String newCity1, newCity2, newCity3, newCity4;
        int newHoursWorked1, newHoursWorked2, newHoursWorked3, newHoursWorked4;
        int newWageRate1, newWageRate2, newWageRate3, newWageRate4;

        //update the employee (in progress)
        System.out.println("Please enter Employee 1's first name:");
        newFirstName1 = conIn.nextLine();
        System.out.println("Please enter Employee 1's last name:");
        newLastName1 = conIn.nextLine();
        System.out.println("Please enter Employee 1's address:");
        newAddress1 = conIn.nextLine();
        System.out.println("Please enter Employee 1's city:");
        newCity1 = conIn.nextLine();
        System.out.println("Please enter Employee 1's hours worked:");
        newHoursWorked1 = conIn.nextInt();
        System.out.println("Please enter Employee 1's wage rate:");
        newWageRate1 = conIn.nextInt();
        worker1.setFirstName(newFirstName1);
        worker1.setLastName(newLastName1);

        //spacer for user readability
        System.out.println("");
        System.out.println("--------------------------------------------");
        System.out.println("");

        //upgrade the wage rate of the employee that has a wage rate of zero
        //(in progress)


        //spacer for user readability
        System.out.println("");
        System.out.println("--------------------------------------------");
        System.out.println("");

        //display employees
        System.out.println("Employee 1");
        System.out.println("----------------------");
        System.out.println(worker1.getInfo());
        System.out.println("Employee 2");
        System.out.println("----------------------");
        System.out.println(worker2.getInfo());
        System.out.println("Employee 3");
        System.out.println("----------------------");
        System.out.println(worker3.getInfo());
        System.out.println("Employee 4");
        System.out.println("----------------------");
        System.out.println(worker4.getInfo());

        //spacer for user readability
        System.out.println("");
        System.out.println("--------------------------------------------");
        System.out.println("");

        //display the employees name and gross pay
        System.out.println(employees.getTotalPay());

        //spacer for user readability
        System.out.println("");
        System.out.println("--------------------------------------------");
        System.out.println("");

        //change the company name
        String newCompanyName;
        System.out.println("Please enter a new company name:");
        newCompanyName = conIn.nextLine();
        employees.setCompanyName(newCompanyName);

        //spacer for user readability
        System.out.println("");
        System.out.println("--------------------------------------------");
        System.out.println("");

        //display the employees
        System.out.println("Employee 1");
        System.out.println("----------------------");
        System.out.println(worker1.getInfo());
        System.out.println("Employee 2");
        System.out.println("----------------------");
        System.out.println(worker2.getInfo());
        System.out.println("Employee 3");
        System.out.println("----------------------");
        System.out.println(worker3.getInfo());
        System.out.println("Employee 4");
        System.out.println("----------------------");
        System.out.println(worker4.getInfo());

        //spacer for user readability
        System.out.println("");
        System.out.println("--------------------------------------------");
        System.out.println("Built by Rion Murphy");

    }
}

employee.java以下のコード

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package employeerionmurphy;

import java.util.*;
/**
 *
 * @author Owner
 */
public class employees {
    //declare variables
    static String firstName;
    static String lastName;
    static String address;
    static String city;
    static int hoursWorked;
    static int wageRate;
    final double OVERTIME = 40.00;

    public static String companyName = "Grand Company";

    //declare a constructor to initalize all fields to null
    public employees() {
    }

    //declare a constructor to hold the actual values in the variables.
    public employees(String newFirstName, String newLastName, 
            String newAddress, String newCity, int newHoursWorked, int newWageRate) {
        firstName = newFirstName;
        lastName = newLastName;
        address = newAddress;
        city = newCity;
        hoursWorked = newHoursWorked;
        wageRate = newWageRate;

    }

    //accessors for the variables
    public String getFirstName() {
        return firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public String getAddress() {
        return address;
    }
    public String getCity() {
        return city;
    }
    public int getHoursWorked() {
        return hoursWorked;
    }
    public int getWageRate() {
        return wageRate;
    }

    //mutators for hoursWorked and wageRate
    public void setHoursWorked(int newHoursWorked) {
        hoursWorked = newHoursWorked;
    }
    public void setWageRate(int newWageRate) {
        wageRate = newWageRate;
    }

    //mutators for the string variables
    public void setFirstName(String newFirstName) {
        firstName = newFirstName;
    }
    public void setLastName(String newLastName) {
        lastName = newLastName;
    }
    public void setAddress(String newAddress) {
        address = newAddress;
    }
    public void setCity(String newCity) {
        city = newCity;
    }

    //company name mutator
    public String getCompanyName() {
        return companyName;
    }
    public static void setCompanyName(String newCompanyName) {
        companyName = newCompanyName;
    }

    //printing out employee information
    public String getInfo() {
        return firstName + " " + lastName + "\n" + address + "\n" + city + "\n" + wageRate + "\n" + hoursWorked;
    }

    //printing out employee name and totalPay
    public static String getTotalPay() {
        //calculate total pay
        double totalPay;
        int overtimeHours;
        if (hoursWorked > 40) {
            overtimeHours = hoursWorked - 40;
            totalPay = (overtimeHours * (wageRate * 1.5)) + (40 * wageRate);
        } else {
            totalPay = hoursWorked * wageRate;
        }
        //return the name and the total pay of employee
        return firstName + " " + lastName + "\n" + totalPay;
    }

    //console operations done below
    public static void main(String[] args) {
        // TODO code application logic here

        //Prompt user for all data (use previous mutators to set hoursWorked and wageRate
        Scanner conIn = new Scanner(System.in);
        System.out.println("Please enter your first name: ");
        firstName = conIn.nextLine();
        System.out.println("Please enter your last name: ");
        lastName = conIn.next();


//        Errors detected in the below line of code, will repair at a later date
//        
        System.out.println("Please enter your address: ");
        address = conIn.nextLine();       
        System.out.println("Please enter your city: ");
        city = conIn.nextLine();


        System.out.println("Please enter your hours worked: ");
        hoursWorked = conIn.nextInt();
        System.out.println("Please enter your wage rate: ");
        wageRate = conIn.nextInt();
        System.out.println("");

        //spacer for user readability
        System.out.println("");
        System.out.println("--------------------------------------------");
        System.out.println("");

        //print the fields to the console (except the constant)
        System.out.println("First Name: " + firstName);
        System.out.println("Last Name: " + lastName);
        System.out.println("Address: " + address);
        System.out.println("City: " + city);
        System.out.println("Hours Worked: " + hoursWorked);
        System.out.println("Wage Rate: " + wageRate);
        System.out.println("Company Name: " + companyName);

        //spacer for user readability
        System.out.println("");
        System.out.println("--------------------------------------------");
        System.out.println("");

        //calculate and return gross pay (time and a half over 40 hours)
        double totalPay;
        int overtimeHours;
        if (hoursWorked > 40) {
            overtimeHours = hoursWorked - 40;
            totalPay = (overtimeHours * (wageRate * 1.5)) + (40 * wageRate);
        } else {
            totalPay = hoursWorked * wageRate;
        }
        System.out.println("The total pay for " + hoursWorked + " hours is " + totalPay);

        //spacer for user readability
        System.out.println("");
        System.out.println("--------------------------------------------");
        System.out.println("");
    }
}
4

1 に答える 1