You chose to open this file: Employees.txt
John Smith Manufacturing 6.75 120 444
0.0
Betty White Manager 1200.0 0 111
0.0
Stan Slimy Sales 10000.0 0 332
250.0
Betty Boop Design 12.5 50 244
0.0
意味着我的所有计算都得到0并导致每周报告:
WEEKLY PAY REPORT FOR Wacky Widgets COMPANY
Employee
0
WEEKLY PAY
250
Total Payroll: $0.0
Total number of managers paid:1
Total number of Design Employees paid:1
Total number of Sales Employees paid:1
Total number of Manufacturing Employees paid:1
我需要帮助修复我的方法,这样他们就可以接受文件的整数并在计算中使用它们,因为我现在得到的所有数据都是0。如果需要,我可以提供我的读文件方法。感谢
这里是所有其他员工类型从中扩展的抽象类 [代码]
public abstract class Employee {
public abstract double calculateWeeklyPay();
private String fName;
private String lName;
private int EmpId;
private Position p;
/*
public Employee()
{
}*/
/**
* default constructor
*/
public Employee() {
// TODO Auto-generated constructor stub
/*this.getFName();
this.getLName();
this.getEmpId();*/
getFName();
getLName();
this.getEmpId();
}
/**
* @param string first name
* @param string2 last name
* @param y position
*/
public Employee(String string, String string2, String y) {
fName = string;
lName = string2;
// EmpId=string3;
if (y.equals("Design")) {
p = Position.Design;
}
if (y.equals("Sales")) {
p = Position.Sales;
}
if (y.equals("Manufacturing")) {
p = Position.Manufacturing;
}
if (y.equals("Manager")) {
p = Position.Manager;
}
// StringTokenizer str=new StringTokenizer(fName+ lName+y,": .");
}
/**
* @return first name
*/
public String getFName() {
return fName;
}
/**
* @param firstName sets the first name
*/
public void setFName(String firstName) {
this.fName = firstName;
}
/**
* @return last name
*/
public String getLName() {
return lName;
}
/**
* @param lastName
*/
public void setLName(String lastName) {
this.lName = lastName;
}
/*public String toString()
{
// String str = firstName+" "+lastName+" "+"Position: "+getPosition();
return firstName;
}*/
/**
* @return p position
*/
public Position getPosition() {
// String str = firstName+" "+lastName+" "+"Position: "+getPosition();
return p;
}
public String toString() {
String str = fName + " " + lName + " " + "Position: " + getPosition();
return str;
}//https://www.udemy.com/blog/java-abstract-class/
public int getEmpId() {
return EmpId;
}
public void setEmpId(int empId) {
this.EmpId = empId;
}
}
设计员工类
public class Design extends Employee {
public double rate;//rate for each hours
public double h;//hours worked
public Design(String fName, String lName, String pos) {
// TODO Auto-generated constructor stub
//double firstParam, int secondParam, int empNum
super(fName, lName, pos);
//toString();
calculateWeeklyPay();
}
@Override
public double calculateWeeklyPay() {
// TODO Auto-generated method stub
double weeklyPay = 0;
if (h <= 40) {
weeklyPay = h * rate;
}
if (h > 40) {
h = h * (3 / 2);
weeklyPay = h * rate;
}
//System.out.println(weeklyPay);
return weeklyPay;
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
public double getH() {
return h;
}
public void setH(double h) {
this.h = h;
}
}
销售员工
public class Sales extends Employee {
private double weekSales;
private final double pay = 250;
private final double percent = .057;
public Sales(String fName, String lName, String pos) {
// TODO Auto-generated constructor stub
super(fName, lName, pos);
this.calculateWeeklyPay();
}
@Override
public double calculateWeeklyPay() {
// TODO Auto-generated method stub
double weekPay;
weekPay = pay + percent * this.getWeekSales();
return weekPay;
}
public double getWeekSales() {
return weekSales;
}
public void setWeekSales(double weekSales) {
this.weekSales = weekSales;
}
}
我还有另外两种员工类型,但他们不需要在这里,因为如果我解决了这个问题,我可以解决其他问题。 这是我的公司课程,它读取文件a计算所有员工课程的每周工资和每周工资总额
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Company implements CompanyInterface {
//private Employee addEmp = new Employee();
// private Position addPosition = new
private ArrayList<Employee> list;
private static int numberOfCompanies;
private String CompanyName;
private int numDesign;
private int numEmployees;
private int numManufacturing;
private int numManager;
private int numSales;
/*String fName="";
String lName="";
String position="";
String first="";
String second="";
String empID="";
Scanner File;*/
//private ArrayList<Employee> list2 ;
private Employee addEmp;
/**
* @param cn is the company name
*/
public Company(String cn) {
cn = "Wacky Widgets";
list = new ArrayList<Employee>();
numDesign = 0;
numEmployees = 0;
numManufacturing = 0;
numSales = 0;
numberOfCompanies++;
setCompanyName(cn);
}
@Override
/**
* @param fName first name
* @param lName last name
* @param pos position
* @return null if everything is right
*/
public String addEmployee(String fName, String lName, String pos,
double firstParam, int secondParam, int empNum) {
String message = null;
// if (pos.equals("DESIGN")&&
numDesign == 1 && numSales != 2 && numManufacturing != 4)
if (pos.equals("Design")) {
if (numDesign == 2)//p2=Position.DESIGN;
{
message = "There are already 2 design persons\nEmployee not
added ";
} else {
addEmp = new Design(fName, lName, pos);
numDesign++;
numEmployees++;
list.add(addEmp);
}
}//else return message;
//if (pos.equals("SALES")&&numDesign<1&&numSales<2&&numManufacturing<4)
else if (pos.equals("Sales")) {
if (numSales == 1) {
message = "There is already a sales person\nEmployee not
added ";
} else {
//p2=Position.SALES;
addEmp = new Sales(fName, lName, pos);
numSales++;
numEmployees++;
list.add(addEmp);
}
}//else return message;
else if (pos.equals("Manufacturing")) {
if (numManufacturing == 4) {
message = "There are already four manufacturing persons
\nEmployee not added ";
} else {
//p2=Position.MANUFACTURING;
addEmp = new Manufacturing(fName, lName, pos);
numManufacturing++;
numEmployees++;
list.add(addEmp);
}
} else if (pos.equals("Manager")) {
if (numManager == 1) {
message = "There is already a manager person\nEmployee not
added ";
} else {
//p2=Position.MANUFACTURING;
addEmp = new Manufacturing(fName, lName, pos);
numManager++;
numEmployees++;
list.add(addEmp);
}
}
//String str=fName+" "+lName+" "+pos+" "+firstParam+" "+empNum;
System.out.println(fName + " " + lName + " " + pos + " " + firstParam + " " + secondParam + "
"+empNum);
//firstParam=Employee.
System.out.println(calculateTotalWeeklyPay());
return message;
}
/**
* Returns number of companies
*
* @return number of companies
*/
public static int getNumCompanies() {
return numberOfCompanies;
}
public void setNumCompanies(int nc) {
numberOfCompanies = nc;
}
/**
* Sets the number of employees
*
* @param ne number of employees
*/
public void setNumemployees(int ne) {
numEmployees = ne;
}
/**
* Returns the number of employees
*
* @return the number of employees
*/
public int getNumEmployees() {
return numEmployees;
}
@Override
public int getNumManager() {
return numManager;
}
/**
* sets the number of designer
*
* @param nd number of designer
*/
public void setNumDesign(int num) {
numDesign = num;
}
@Override
public int getNumDesign() {
return numDesign;
}
public void setNumsales(int num) {
numSales = num;
}
@Override
public int getNumSales() {
return numSales;
}
/**
* Sets the number of manufacturer
*
* @param nm the number of manufacturer
*/
public void setNumManufacturing(int num) {
numManufacturing = num;
}
@Override
public int getNumManufacturing() {
return numManufacturing;
}
public void setNumManager(int num) {
numManager = num;
}
@Override
public String generateWeeklyReport() {
int empID = 0;
String title = "WEEKLY PAY REPORT FOR " + getCompanyName() + " COMPANY" + "\n";
String label = "Employee\n";
String label2 = "WEEKLY PAY \n";
int payWeek = 0;
double total = 0.0;
for (Employee index : list) {
empID += index.getEmpId();
payWeek += index.calculateWeeklyPay();
}
return title + label + empID + "\n" + "\t" + label2 + payWeek + "\n" + "Total Payroll:
$ "+total+"\n "+
"Total number of managers paid:" + getNumManager() + "\n" +
"Total number of Design Employees paid:" + getNumDesign() + "\n" +
"Total number of Sales Employees paid:" + getNumSales() + "\n" +
"Total number of Manufacturing Employees paid:" + getNumManufacturing();
}
@Override
public double calculateTotalWeeklyPay() {
double total = 0;
for (int index = 0; index < list.size(); index++) {
total = list.get(index).calculateWeeklyPay();
//total.add(list.get(index).calculateWeeklyPay());
}
return total;
}
/**
* @return str the arraylist of employees and positions
*/
public String printCompany() {
// TODO Auto-generated method stub
String str = "";
for (int index = 0; index < list.size(); index++) {
str += list.get(index).getFName() + " " + list.get(index).getLName() + "
"+" Position:
"+list.get(index).getPosition()+"\n ";
}//System.out.println(str);
return str;
}
/**
* @return company name
*/
public String getCompanyName() {
return CompanyName;
}
public void setCompanyName(String companyName) {
//companyName="Wacky Widgets";
this.CompanyName = companyName;
}
public String toString() {
// int i =0;
String str = CompanyName + "";
//String str = "";
for (int i = 0; i < list.size(); i++) {
str += "\n" + list.get(i).getFName() + " " + list.get(i).getLName() + "
"+" Position:
"+list.get(i).getPosition()+"\n ";
}
return str;
}
}
答案 0 :(得分:0)
这很突出:
h = h * (3 / 2);
您在这里进行整数除法,因此您总是有效地乘以1。
将其更改为使用浮点值。
h = h * (3.0 / 2);
或者,明确说明你的乘法 - 你知道它总是一次半,所以我认为没有理由不这样做:
h = h * 1.5;