程序的主体几乎都是参照例子制作的,没有太大的区别。
我的程序目的是创造一个公司,在这个公司页面可以增加、删除员工等各项功能。
先为各位介绍一下下面控制台界面的测试情况(从上往下依次):
1.输入了一个company名字
2.用add命令增加了一个员工
3.用addBonus命令给其中一个员工增加bonus两次
4.用find命令来显示其员工的信息(问题出在这里!)按理说增加了两次bonus应该会有具体数目显示,而下面却只出现了两个0,也就是说里面根本没有被赋予值。
总体程序所需要的3个class都在下面,望大神帮我解答心中疑惑,顺便把解决办法告诉我下,十分感谢!!!
(星号以上为控制台界面)
Please enter a company name
tom
Company directory commands
add - Add a new Employee
find - Find an Employee
addBonus - Add a bonus
findHighest - Find an employee with highest salary
delete - Delete an Employee
quit - Quitadd
Please enter a name.
tom
Please enter an employee number.
1000
Please enter the salary.
2000
Please enter the address.
asd
Company directory commands
add - Add a new Employee
find - Find an Employee
addBonus - Add a bonus
findHighest - Find an employee with highest salary
delete - Delete an Employee
quit - Quit
addBonus
Please enter an employee number which you are going to add.
1000
Please enter a bonus
2000
Do you want to continue?(press " yes " to go, " No "to leave)
yes
Please enter a bonus
2000
Do you want to continue?(press " yes " to go, " No "to leave)
no
Company directory commands
add - Add a new Employee
find - Find an Employee
addBonus - Add a bonus
findHighest - Find an employee with highest salary
delete - Delete an Employee
quit - Quitfind
Please enter an employee number which you are going to find.
1000
Name: tom
Employee Number: 1000
Address: asd
Salary: 2000
List of bonus:
0
0
******************************
/**
* CPSC 1181 Assignment #2
* Instructor: Hengameh Hamavand
* <p>
* I pledge that I have completed the programming assignment independently.
* I have not copied the code from a student or any other source.
* I have not given my code to any student.
* Siny (Xiao Yang Ma), May. 21. 2012
* <p>
* This program is use for creating employee account.
* User can read, change and display information form this program.
* <p>
* @author Siny (Ma, Xiao Yang) 100182585
* @version 1
*/
public class Employee
{
/**
* Employee method: read from other method.
* read name. employee number, address, salary and bonus of a employee.
* <p>
* return Employee.
* @param newName string show a few line entered by user.
* @param newAddress string show a few line entered by user.
* @param newEmployeeNumber int show a number entered by user.
* @param newSalary double show a number entered by user.
* @param newBonus double array show a array entered by user.
* <p>
* precondition: the input string name and address, double salary, int empolyee number and double array bonus separate by a comma.
*/
public Employee(String newName, int newEmployeeNumber, String newAddress,int newSalary)
{
name = newName;
employeeNumber = newEmployeeNumber;
address = newAddress;
salary = newSalary;
}
/**
* getName method.
* <p>
* return string.
* <p>
* precondition: return name string to the method which is called it.
*/
public String getName()
{
return name;
}
/**
* getEmployeeNumber.
* <p>
* return double.
* <p>
* precondition:return employeeNumber double to the method which is called it.
*/
public int getEmployeeNumber()
{
return employeeNumber;
}
/**
* getAddress method.
* <p>
* return String.
* <p>
* precondition:return address string to the method which is called it.
*/
public String getAddress()
{
return address;
}
/**
* getSalary method.
* <p>
* return double.
* <p>
* precondition:return salary double to the method which is called it.
*/
public int getSalary()
{
return salary;
}
/**
* changeAddress method.
* <p>
* void method not return any variable.
* <p>
* precondition: the string new_address.
*/
public void changeAddress(String new_address)
{
address = new_address;
}
/**
* changeSalary method.
* <p>
* void method not return any variable.
* <p>
* precondition: the double new_salary.
*/
public void changeSalary(int new_salary)
{
salary = new_salary;
}
/**
* changeBonus method.
* <p>
* void method not return any variable.
* <p>
* precondition: the double array new_bonus.
*/
public void addBonus(int bonuses)
{
if(indexOfBonus < 10)
{
bonus[indexOfBonus] = bonuses;
indexOfBonus++;
}
}
/**
* toString method.
* <p>
* return string.
* <p>
* postcondition: return a few line string to the method which is called it.
*/
public String toString()
{
String bonusString = "";
for(int ind=0; ind < indexOfBonus; ind++)
{
bonusString = bonusString +"\n"+bonus[indexOfBonus];
}
return ("Name: "+name+"\nEmployee Number: "+employeeNumber+"\nAddress: "+address+"\nSalary: "+salary+"\nList of bonus:"+bonusString);
}
//define private variables
private String name;
private int employeeNumber;
private String address;
private int salary;
private int[] bonus = new int[10];
private int indexOfBonus = 0;
}
import java.util.ArrayList;
public class Company
{
public Company(String newCompanyName)
{
companyName = newCompanyName;
employees = new ArrayList<Employee>();
}
public void addEmployee(Employee emp)
{
employees.add(emp);
}
public Employee findEmployee(int employeeNumber)
{
for(Employee emp : employees)
{
if(emp.getEmployeeNumber() == employeeNumber)
return emp;
}
return null;
}
public String findHighest()
{
int highestInd = 0;
if(employees.size() == 0) return null;
for(int ind=0; ind < employees.size() - 1; ind++ )
{
if((employees.get(ind)).getSalary() < (employees.get(ind+1)).getSalary())
{
highestInd = ind + 1;
}
else
{
highestInd = ind;
}
ind = ind++;
}
return (employees.get(highestInd)).toString();
}
public ArrayList<Employee> getList()
{
return employees;
}
public String removeEmployee(int employeeNumber)
{
for(int ind=0; ind < employees.size(); ind++)
{
if(employeeNumber == (employees.get(ind)).getEmployeeNumber())
{
employees.remove(ind);
return ((employees.get(ind)).getName() + " is deleted.");
}
}
return "No match.";
}
private String companyName;
private ArrayList<Employee> employees;
}
import java.util.*;
public class CompanyTest
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
Company company;
company = read_input(in);
run(company, in);
} public static Company read_input(Scanner in)
{
String companyName;
System.out.println("Please enter a company name");
companyName = in.nextLine();
return new Company(companyName);
}
public static void run(Company company, Scanner in )
{
String answer, name, address, answer_1, result;
int number, employeeNumber, salary, bonuses, ind = 0;
Employee emp_1;
do
{
System.out.println("Company directory commands");
System.out.println("add - Add a new Employee");
System.out.println("find - Find an Employee");
System.out.println("addBonus - Add a bonus");
System.out.println("findHighest - Find an employee with highest salary");
System.out.println("delete - Delete an Employee");
System.out.println("quit - Quit");
System.out.println();
answer = in.next();
switch(answer)
{
case "add":
System.out.println("Please enter a name.");
name = in.next();
System.out.println("Please enter an employee number.");
employeeNumber = in.nextInt();
System.out.println("Please enter the salary.");
salary = in.nextInt();
System.out.println("Please enter the address.");
address = in.next();
emp_1 = new Employee(name, employeeNumber, address, salary);
company.addEmployee(emp_1);
break;
case "find":
System.out.println("Please enter an employee number which you are going to find.");
number = in.nextInt();
Employee emp_2 = company.findEmployee(number);
if(emp_2 == null)
System.out.println("No employee with number: "+number);
else
System.out.println(emp_2.toString());
break;
case "addBonus":
System.out.println("Please enter an employee number which you are going to add.");
number = in.nextInt();
Employee emp_3 = company.findEmployee(number);
if(emp_3 == null)
System.out.println("No employee with number: "+number);
else
do
{
System.out.println("Please enter a bonus");
bonuses = in.nextInt();
emp_3.addBonus(bonuses);
System.out.println("Do you want to continue?(press \" yes \" to go, \" No \"to leave)");
answer_1 = in.next();
}while(answer_1.equals("yes"));
break;
case "findHighest":
System.out.println("the employee who has the highest salary: \n"+company.findHighest());
break;
case "delete":
System.out.println("Please enter an employee number which you are going to delete.");
number = in.nextInt();
company.removeEmployee(number);
break;
default:
if(!answer.equals("quit"))
System.out.println("****Error! The value is invalid, try again.****\n");
}
}while(!answer.equals("quit"));
}
}
我的程序目的是创造一个公司,在这个公司页面可以增加、删除员工等各项功能。
先为各位介绍一下下面控制台界面的测试情况(从上往下依次):
1.输入了一个company名字
2.用add命令增加了一个员工
3.用addBonus命令给其中一个员工增加bonus两次
4.用find命令来显示其员工的信息(问题出在这里!)按理说增加了两次bonus应该会有具体数目显示,而下面却只出现了两个0,也就是说里面根本没有被赋予值。
总体程序所需要的3个class都在下面,望大神帮我解答心中疑惑,顺便把解决办法告诉我下,十分感谢!!!
(星号以上为控制台界面)
Please enter a company name
tom
Company directory commands
add - Add a new Employee
find - Find an Employee
addBonus - Add a bonus
findHighest - Find an employee with highest salary
delete - Delete an Employee
quit - Quitadd
Please enter a name.
tom
Please enter an employee number.
1000
Please enter the salary.
2000
Please enter the address.
asd
Company directory commands
add - Add a new Employee
find - Find an Employee
addBonus - Add a bonus
findHighest - Find an employee with highest salary
delete - Delete an Employee
quit - Quit
addBonus
Please enter an employee number which you are going to add.
1000
Please enter a bonus
2000
Do you want to continue?(press " yes " to go, " No "to leave)
yes
Please enter a bonus
2000
Do you want to continue?(press " yes " to go, " No "to leave)
no
Company directory commands
add - Add a new Employee
find - Find an Employee
addBonus - Add a bonus
findHighest - Find an employee with highest salary
delete - Delete an Employee
quit - Quitfind
Please enter an employee number which you are going to find.
1000
Name: tom
Employee Number: 1000
Address: asd
Salary: 2000
List of bonus:
0
0
******************************
/**
* CPSC 1181 Assignment #2
* Instructor: Hengameh Hamavand
* <p>
* I pledge that I have completed the programming assignment independently.
* I have not copied the code from a student or any other source.
* I have not given my code to any student.
* Siny (Xiao Yang Ma), May. 21. 2012
* <p>
* This program is use for creating employee account.
* User can read, change and display information form this program.
* <p>
* @author Siny (Ma, Xiao Yang) 100182585
* @version 1
*/
public class Employee
{
/**
* Employee method: read from other method.
* read name. employee number, address, salary and bonus of a employee.
* <p>
* return Employee.
* @param newName string show a few line entered by user.
* @param newAddress string show a few line entered by user.
* @param newEmployeeNumber int show a number entered by user.
* @param newSalary double show a number entered by user.
* @param newBonus double array show a array entered by user.
* <p>
* precondition: the input string name and address, double salary, int empolyee number and double array bonus separate by a comma.
*/
public Employee(String newName, int newEmployeeNumber, String newAddress,int newSalary)
{
name = newName;
employeeNumber = newEmployeeNumber;
address = newAddress;
salary = newSalary;
}
/**
* getName method.
* <p>
* return string.
* <p>
* precondition: return name string to the method which is called it.
*/
public String getName()
{
return name;
}
/**
* getEmployeeNumber.
* <p>
* return double.
* <p>
* precondition:return employeeNumber double to the method which is called it.
*/
public int getEmployeeNumber()
{
return employeeNumber;
}
/**
* getAddress method.
* <p>
* return String.
* <p>
* precondition:return address string to the method which is called it.
*/
public String getAddress()
{
return address;
}
/**
* getSalary method.
* <p>
* return double.
* <p>
* precondition:return salary double to the method which is called it.
*/
public int getSalary()
{
return salary;
}
/**
* changeAddress method.
* <p>
* void method not return any variable.
* <p>
* precondition: the string new_address.
*/
public void changeAddress(String new_address)
{
address = new_address;
}
/**
* changeSalary method.
* <p>
* void method not return any variable.
* <p>
* precondition: the double new_salary.
*/
public void changeSalary(int new_salary)
{
salary = new_salary;
}
/**
* changeBonus method.
* <p>
* void method not return any variable.
* <p>
* precondition: the double array new_bonus.
*/
public void addBonus(int bonuses)
{
if(indexOfBonus < 10)
{
bonus[indexOfBonus] = bonuses;
indexOfBonus++;
}
}
/**
* toString method.
* <p>
* return string.
* <p>
* postcondition: return a few line string to the method which is called it.
*/
public String toString()
{
String bonusString = "";
for(int ind=0; ind < indexOfBonus; ind++)
{
bonusString = bonusString +"\n"+bonus[indexOfBonus];
}
return ("Name: "+name+"\nEmployee Number: "+employeeNumber+"\nAddress: "+address+"\nSalary: "+salary+"\nList of bonus:"+bonusString);
}
//define private variables
private String name;
private int employeeNumber;
private String address;
private int salary;
private int[] bonus = new int[10];
private int indexOfBonus = 0;
}
import java.util.ArrayList;
public class Company
{
public Company(String newCompanyName)
{
companyName = newCompanyName;
employees = new ArrayList<Employee>();
}
public void addEmployee(Employee emp)
{
employees.add(emp);
}
public Employee findEmployee(int employeeNumber)
{
for(Employee emp : employees)
{
if(emp.getEmployeeNumber() == employeeNumber)
return emp;
}
return null;
}
public String findHighest()
{
int highestInd = 0;
if(employees.size() == 0) return null;
for(int ind=0; ind < employees.size() - 1; ind++ )
{
if((employees.get(ind)).getSalary() < (employees.get(ind+1)).getSalary())
{
highestInd = ind + 1;
}
else
{
highestInd = ind;
}
ind = ind++;
}
return (employees.get(highestInd)).toString();
}
public ArrayList<Employee> getList()
{
return employees;
}
public String removeEmployee(int employeeNumber)
{
for(int ind=0; ind < employees.size(); ind++)
{
if(employeeNumber == (employees.get(ind)).getEmployeeNumber())
{
employees.remove(ind);
return ((employees.get(ind)).getName() + " is deleted.");
}
}
return "No match.";
}
private String companyName;
private ArrayList<Employee> employees;
}
import java.util.*;
public class CompanyTest
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
Company company;
company = read_input(in);
run(company, in);
} public static Company read_input(Scanner in)
{
String companyName;
System.out.println("Please enter a company name");
companyName = in.nextLine();
return new Company(companyName);
}
public static void run(Company company, Scanner in )
{
String answer, name, address, answer_1, result;
int number, employeeNumber, salary, bonuses, ind = 0;
Employee emp_1;
do
{
System.out.println("Company directory commands");
System.out.println("add - Add a new Employee");
System.out.println("find - Find an Employee");
System.out.println("addBonus - Add a bonus");
System.out.println("findHighest - Find an employee with highest salary");
System.out.println("delete - Delete an Employee");
System.out.println("quit - Quit");
System.out.println();
answer = in.next();
switch(answer)
{
case "add":
System.out.println("Please enter a name.");
name = in.next();
System.out.println("Please enter an employee number.");
employeeNumber = in.nextInt();
System.out.println("Please enter the salary.");
salary = in.nextInt();
System.out.println("Please enter the address.");
address = in.next();
emp_1 = new Employee(name, employeeNumber, address, salary);
company.addEmployee(emp_1);
break;
case "find":
System.out.println("Please enter an employee number which you are going to find.");
number = in.nextInt();
Employee emp_2 = company.findEmployee(number);
if(emp_2 == null)
System.out.println("No employee with number: "+number);
else
System.out.println(emp_2.toString());
break;
case "addBonus":
System.out.println("Please enter an employee number which you are going to add.");
number = in.nextInt();
Employee emp_3 = company.findEmployee(number);
if(emp_3 == null)
System.out.println("No employee with number: "+number);
else
do
{
System.out.println("Please enter a bonus");
bonuses = in.nextInt();
emp_3.addBonus(bonuses);
System.out.println("Do you want to continue?(press \" yes \" to go, \" No \"to leave)");
answer_1 = in.next();
}while(answer_1.equals("yes"));
break;
case "findHighest":
System.out.println("the employee who has the highest salary: \n"+company.findHighest());
break;
case "delete":
System.out.println("Please enter an employee number which you are going to delete.");
number = in.nextInt();
company.removeEmployee(number);
break;
default:
if(!answer.equals("quit"))
System.out.println("****Error! The value is invalid, try again.****\n");
}
}while(!answer.equals("quit"));
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货