对象类实现客户姓名添加和显示和修改
public class CustomerBiz {
private String[] names =new String[30];
/**
* 添加名称
* @param name2
/
public void addNames(String name){
//循环数组,添加姓名
for (int i = 0; i < names.length; i++) {
//可以添加
if(names[i]==null){
names[i]=name;
break;
}
}
}
/*
* 显示客户名称
/
public void showNames(){
System.out.println("");
System.out.println("\t客户列表");
for (String name : names) {
if (name != null) {
System.out.println(name+"\t");
}
}
System.out.println("\n");
}
//找到要替换名称的下标
int index=-1;
public int findOldName(String oldName){
for (int i = 0; i < names.length; i++) {
if(oldName.equals(names[i])){
index=i;
}
}
return index;
}
/*
* 修改名称的方法
* @param oldName
* @param newName
* @return
/
public boolean editNames(String oldName, String newName){
int result = findOldName(oldName);//下标给result
if (result == -1) {//不存在
return false;
}
//修改
names[index]=newName;
System.out.println("*********修改结构***********");
System.out.println("找到并修改成功");
return true;
}
}
public class testCustomer {
public static void main(String[] args) {
// TODO 客户姓名添加和显示
/*CustomerBiz biz = new CustomerBiz();
Scanner input = new Scanner(System.in);
do {
System.out.print("请输入客户名称:");
String name = input.next();
//添加
biz.addNames(name);
System.out.print("是否继续添加>y/n:");
String goTo = input.next();
if ("n".equals(goTo)) {
break;
}
} while (true);
//显示姓名
biz.showNames();*/
//TODO 修改客户姓名
CustomerBiz biz = new CustomerBiz();
Scanner input = new Scanner(System.in);
do {
for (int i = 0; i < 5; i++) {
System.out.print("请输入客户名称:");
String name = input.next();
//添加
biz.addNames(name);
}
biz.showNames();
System.out.print("请输入要修改的客户名称:");
String oldName = input.next();
System.out.print("请输入新的客户名称:");
String newName = input.next();
biz.editNames(oldName,newName);
//显示姓名
biz.showNames();
} while (true);
}
}