Java 这个公司级OOP程序有什么问题?
关键是要编写一个程序,通过搜索员工的id号、打印所有员工的所有信息以及打印单个员工的信息来查找员工。我需要这方面的帮助(搜索),单独打印一名员工是否正确?谢谢 类别员工代码:Java 这个公司级OOP程序有什么问题?,java,class,oop,object,Java,Class,Oop,Object,关键是要编写一个程序,通过搜索员工的id号、打印所有员工的所有信息以及打印单个员工的信息来查找员工。我需要这方面的帮助(搜索),单独打印一名员工是否正确?谢谢 类别员工代码: public class Employee { private String name; private int id; private int salary; private boolean bonus; public Employee(String n, int i, int s, b
public class Employee {
private String name;
private int id;
private int salary;
private boolean bonus;
public Employee(String n, int i, int s, boolean b) {
name = n;
id = i;
salary = s;
bonus = b;
}
public void computeSalary(int s, boolean b) {
if (b == true)
salary += 2000;
}
public void printInfo() {
System.out.print("Name: "+name+" ID: "+id+" Salary: "+salary+" Bonus: "+bonus+" ");
}
类别EmployeeApp类别代码:
import java.util.Scanner;
public class EmployeeApp {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the name: ");
String n = in.nextLine();
System.out.print("Enter the ID: ");
int g = in.nextInt();
System.out.print("Enter the salary: ");
int s = in.nextInt(); in.nextLine();
System.out.print("true or false for bonus? ");
boolean b = in.nextBoolean();
Employee e = new Employee(n, g, s, b);
e.computeSalary(s, b);
e.printInfo();
}
}
public class Company{
private Employee[] e = new Employee[4];
public void printAllEmployees() {
for(int i = 0; i < e.length; i++)
e[i].printInfo();
}
public Employee searchEmployee(int i) {
Employee temp = null;
for (int j = 0; j < e.length; j++) {
if (Employee.id == i)
temp = Employee;
}
return temp;
}
public void printAnEmployee(Employee e) {
e.printInfo();
}
}
import java.util.Scanner;
公共类EmployeeApp{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
System.out.print(“输入名称:”);
字符串n=in.nextLine();
系统输出打印(“输入ID:”);
int g=in.nextInt();
系统输出打印(“输入工资:”);
int s=in.nextInt();in.nextLine();
系统输出打印(“奖金是真还是假?”);
布尔b=in.nextBoolean();
员工e=新员工(n、g、s、b);
e、 计算语法(s,b);
e、 printInfo();
}
}
公营公司{
私人员工[]e=新员工[4];
公共无效打印所有员工(){
for(int i=0;i
将员工放在一个可以进行简单搜索的结构中,如下所示:
private Map<Integer, Employee> e = new HashMap<Integer, Employee>();
public void printAllEmployees() {
for( Integer key : e.keySet() )
System.out.println(e.get( key ));
}
public Employee searchEmployee(int i) {
return e.get(i);
}
private-Map e=newhashmap();
公共无效打印所有员工(){
for(整数键:e.keySet())
System.out.println(e.get(key));
}
公共雇员搜索雇员(int i){
返回e.get(i);
}
我不知道您给我们的代码是如何为用户工作的,因为您的类中有一个小问题。
首先,在员工中使用setter和getter是一个很好的实践,因为为了安全起见,封装对象是很好的,所以如何构造它们请参阅本教程
第二,在EmployeeApp类中
当你问这个的时候
System.out.print("true or false for bonus? ");
boolean b = in.nextBoolean();
不清楚用户必须输入什么类型的输入,所以最好将第一行更改为
System.out.print("true or false for bonus? \nplease enter true or false");
第三,最好将您的公司类放在不同的文件中,公司类中的一些小问题如下
注意:Employee是一个类,e是一个Employee类型的数组
public Employee searchEmployee(int i) {
Employee temp = null;
for (int j = 0; j < e.length; j++) {
if (**Employee.id** == i)
**temp = Employee;**
}
return temp;
}
公共雇员搜索雇员(int i){
员工临时工=空;
对于(int j=0;j
改为
public Employee searchEmployee(int i) {
Employee temp = null;
for (int j = 0; j < e.length; j++) {
if (e[i].getId() == i) {
temp = e[i];
}
}
return temp;
}
公共雇员搜索雇员(int i){
员工临时工=空;
对于(int j=0;j
最后,如果需要添加更多员工,则需要将其放入while循环中。因此,虽然循环将是好的,例如用户推X.< /P>我不确定,但是在文件中间有一个导入看起来很奇怪。这是一个文件还是多个文件?如果是单个文件,请将其移到“import java.util.Scanner”的顶部,它看起来不像是在
computeSalary()方法中使用的。也许你可以摆脱那个争论?这看起来像是家庭作业,这样可以吗?它是多重的,我只是把前者拿出来作为参考。我为Employee类删减了一些代码,所以没有那么长,我只需要公司类的帮助。他说他需要搜索方面的帮助