这个员工管理系统,我用了四个类(InterFace,Management,Workers,Test)和一个接口(Stackable)完成,要求能新建、查询、删除、修改和保存功能,并要将数据导入到内存完成这些功能。我用了ArrayList,但是发线根本无法正确添加数据,我的代码如下:package js.njau.lizzy;import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class Interface {//界面类 private Stackable st;
private ArrayList al=new ArrayList();

public Interface(Stackable st)
{
this.st=st;
//System.out.println("jfjf");
try {
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("e:/workersinfo.ser"));
al=(ArrayList)ois.readObject();//读入内存

} catch (FileNotFoundException e) {

//e.printStackTrace();
System.out.println("jhfkjdshf");
} catch (IOException e) {

//e.printStackTrace();
//System.out.println("存储文件为空!");
} catch (Exception e) {

//e.printStackTrace();
}
//new Management().readal(al);
}

public void face()
{
System.out.println("======主界面======");
System.out.println("1.新建员工");
System.out.println("2.查询员工");
System.out.println("3.删除员工");
System.out.println("4.修改员工");
System.out.println("5.保存员工");
System.out.println("注意:新建、删除、修改员工后一定要保存!否则数据无效!!!");

//new Management().ManagementRead();
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();//x为选项的标号
switch(x)//得到选项,确认方法调用
{
case 1: st.newSet(al);System.out.println(al);this.face();break;
case 2: st.check(al);this.face();break;
case 3: st.delete(al);this.face();break;
case 4: st.motify(al);this.face();break;
case 5: st.sate(al);break;
}//switch(x)

}//face()
}//管理类
package js.njau.lizzy;import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;public class Management implements Stackable {
public void newSet(ArrayList al)  //新建
{
System.out.println("请依次输入员工的信息,工号,姓名,性别,薪水,备注");
al.add(new Workers());
System.out.println(al);//这里应该可以输出所有的数据包括新加的,但是输出的是:[js.njau.lizzy.Workers@530daa, js.njau.lizzy.Workers@a62fc3],类似于我的包名的东西!!!


}//newSet()
public void check(ArrayList al)//查询
{

System.out.println("请输入你要查询的员工工号:");
Scanner sc=new Scanner(System.in);
String a=sc.next();//a=员工工号
//System.out.println(a);
for(int m=0;m<al.size();m++)//判断是否存在所要查询的员工工号
{
if(al.get(m).toString().indexOf(a)!=-1);//员工工号的索引位置
{
String b=al.get(m).toString();//b为员工的字符串
int tt=a.indexOf("]",a.indexOf(a));//tt为输出员工资料的末尾
System.out.println(b.substring(b.indexOf(a), tt));
}//if
//System.out.println("hfhf");
}//if
}//check()
public void delete(ArrayList al)//删除
{
System.out.println("请输入删除员工的工号:");
Scanner sc=new Scanner(System.in);
String a=sc.next();//a=员工工号
for(int m=0;m<al.size();m++)
{
if(al.get(m).toString().indexOf(a)==-1)//确定是否是输入的员工
{
System.out.println("输入错误!");
this.delete(al);//重新再输入删除员工的工号
}//if
else
{
al.remove(m);//删除员工
}//else
}//for(m)
}//delete()
public void motify(ArrayList al)//修改
{
System.out.println("请输入删除员工的工号:");
Scanner sc=new Scanner(System.in);
String a=sc.next();//a=员工工号
for(int m=0;m<al.size();m++)
{
if(al.get(m).toString().indexOf(a)==-1)
{
System.out.println("输入错误!");
this.motify(al);
}//if
else
{
al.remove(m);
al.add(new Workers());
}//else
}//for(m)

}//motify()
public void sate(ArrayList al)//保存
{
//System.out.println(al);
try {

ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("e:/workersinfo.ser"));
oos.writeObject(al);
System.out.println(al);
oos.flush();
oos.close();
} catch (FileNotFoundException e) {
// TODO 自动生成 catch 块
//e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}//sate() /*protected  void readal(ArrayList al)
{
try {
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("e:/workersinfo.ser"));
//System.out.println("alf");
al=(ArrayList)ois.readObject();//读入内存

} catch (FileNotFoundException e) {

//e.printStackTrace();
System.out.println("jhfkjdshf");
} catch (IOException e) {

//e.printStackTrace();
//System.out.println("存储文件为空!");
} catch (Exception e) {

//e.printStackTrace();
}
}//redal()*/

}
//员工类
package js.njau.lizzy;import java.io.*;
import java.util.Scanner;public class Workers implements Serializable{
private String number;//工号
private String name;//姓名
private String sex;//性别
private String salary;//工资
private String other;//备注
public Workers(String number, String name, String sex, String salary, String other) {
super();
this.number = number;
this.name = name;
this.sex = sex;
this.salary = salary;
this.other = other;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getOther() {
return other;
}
public void setOther(String other) {
this.other = other;
}
public String getSalary() {
return salary;
}
public void setSalary(String salary) {
this.salary = salary;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Workers()
{
Scanner sc=new Scanner(System.in);
this.number=sc.next();
this.name=sc.next();
this.sex=sc.next();
this.salary=sc.next();
this.other=sc.next();
}

}//接口
package js.njau.lizzy;import java.util.ArrayList;public interface Stackable { public void newSet(ArrayList al);//新建
public void check(ArrayList al);//查询
public void delete(ArrayList al);//删除
public void motify(ArrayList al);//修改
public void sate(ArrayList al);//保存
}
//测试类
package js.njau.lizzy;public class Test {
public static void main(String[] args)  {
// TODO Auto-generated method stub
 
 Stackable st=new Management();
new Interface(st).face();
}
}

解决方案 »

  1.   

    如果LZ说的是你点红色的部分的话
    那我可以告诉你的
    System.out.print()
    这个方法当打印的不是一个String对象时,会自动调用对象的 toString()方法
    而ArrayList中也有一个toString() 方法
    它又会调用 存在它里面的 对象的toString() 方法
    LZ要做的可以有两种办法
    1:遍历 ArrayList ,一个个输出其中对象的各个属性值
    2:重载Workers的toString()方法,把这个对象的各个属性值输出来
      

  2.   

    楼上的说的对,因为workers这个类是自己建的,当你试图输出该类是,打印出来的是这个类的地址,可以在workers类中覆盖toString方法的
      

  3.   

    在workers加一个函数就可以了啊
    public String toString()
    {
       return ""+number+" "+name+" "+sex+" "+salary+" "+other; 
    }