日期:2014-05-20 浏览次数:20875 次
public class AddressList {
private People[] peoples;
private int top;
private java.util.Scanner in = new java.util.Scanner(System.in);
public AddressList(int size) {
peoples = new People[size];
}
public boolean addPeople(People p) {
if (top < peoples.length) {
peoples[top++] = p;
return true;
}
return false;
}
public String toString() {
if (top == 0) return "无记录";
StringBuffer result = new StringBuffer();
result.append("编号\t姓名\t性别\t电话\t地址\n");
for (int i = 0; i < top; i++)
result.append(peoples[i].toString() + '\n');
return result.toString();
}
public void prompt() {
System.out.println("1.查询\n2.增加\n3.修改\n4.删除\n5.退出");
System.out.println("请输入操作:");
switch(in.nextInt()) {
case 1: // 查询
System.out.println(this);
prompt();
break;
case 2: // 添加
in.nextLine();
if (addPeople(new People(in.nextLine(),
in.nextLine(),
(in.nextLine().equals("男") ? People.Sex.MALE : People.Sex.FEMALE),
in.nextLine(),
in.nextLine()))) System.out.println("添加成功");
else System.out.println("添加失败");
prompt();
break;
case 3: // 查询
break;
case 4: // 删除
break;
case 5: // 退出
break;
}
}
public static void main(String[] args) {
AddressList al = new AddressList(5);
al.prompt();
}
}
class People {
public enum Sex { MALE, FEMALE }
private String num;
private String name;
private Sex sex;
private String phone;
private String address;
public People(String n1, String n2, Sex s, String p, String a) {
num = n1;
name = n2;
sex = s;
phone = p;
address = a;
}
public String getnum() {
return num;
}
public String getname() {
return name;
}
public Sex getsex() {
return sex;
}
public String getphone() {
return phone;
}
public String getaddress() {
return address;
}
public String toString() {
return num + '\t' + name + '\t' + (sex==Sex.MALE?'男':'女') + '\t' + phone + '\t'+ address;
}
}