(题目需求我整理以后更新)
客户信息管理系统:
模拟和实现一个基于文本界面的《客户信息管理系统》
进一步掌握编程技巧和调试技巧,熟悉面向对象编程
主要涉及以下知识点:
类的结构的使用,属性,方法以及构造器
对象的创建与使用
类的封装性
声明和使用数组
数组的插入删除和替换
关键字的使用:this
代码如下:
package MENU;
import java.util.*;
public class CMUtility
{
public static int readMenuSelection()
{
Scanner input=new Scanner(System.in);
int num=input.nextInt();
for(;;)
{
if(num<=0&&num>5)
{
System.out.println("选择错误,请重新输入");
}else break;
}return num;
}
public static int readConfirmSelection()
{
Scanner input=new Scanner(System.in);
int num=input.nextInt();
for(;;)
{
if(num!=1&&num!=2)
{
System.out.println("选择错误,请重新输入");
}else break;
}return num;
}
public static String readString(int limit)
{
Scanner input=new Scanner(System.in);
String num=input.next();
for(;;)
{
if(num.length()>limit)
{
System.out.println("选择错误,请重新输入");
}else break;
}return num;
}
public static int readint()
{
Scanner input=new Scanner(System.in);
int num=input.nextInt();
for(;;)
{
if(num<0&&num>100)
{
System.out.println("选择错误,请重新输入");
}else break;
}return num;
}
public static String readString(int limit,String defultValue)
{
Scanner input=new Scanner(System.in);
String num=input.next();
for(;;)
{
if(num.length()>limit)
{
System.out.println("选择错误,请重新输入");
}else break;
}return num;
}
public static int readint(int defultValue)
{
Scanner input=new Scanner(System.in);
int num=input.nextInt();
for(;;)
{
if(num<0&&num>100)
{
System.out.println("选择错误,请重新输入");
}else break;
}return num;
}
}
这是工具类
package MENU;
public class Customer {
private String name;
private String sex;
private int age;
private String tel;
private String email;
public Customer() {
}
public Customer( String name, String sex, int age, String tel, String email) {
this.name = name;
this.sex = sex;
this.age = age;
this.tel = tel;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
这是customer类
package MENU;
public class CustomerList
{
private Customer[] customers;
private int total=0;
public CustomerList(int totalCustomer)//totalCustomer记录客户的个数
{
customers=new Customer[10];
}
public boolean addCustomer(Customer customer)
{
if(total>=customers.length)
{
return false;
}else
{
customers[total++]=customer;
return true;
}
}
public boolean replaceCustomer(int index,Customer cust)
{
if(index<0||index>=total)
{
return false;
}
customers[index]=cust;
return true;
}
public boolean deleteCustomer(int index)
{
if(index<0||index>=total)
{
return false;
}
for(int i=index;i<total;i++)
{
customers[i]=customers[i+1];
}
customers[total-1]=null;
total--;
return true;
}
public Customer[] getAllCustomer()
{
Customer[] custs=new Customer[total];
for(int i=0;i<total;i++)
{
custs[i]=customers[i];
}
return custs;
}
public Customer getCustomer(int index)
{
if(total>=customers.length)
{
return null;
}
return customers[index];
}
public int getTotal()
{
return total;
}
}
这是customerlist类
package MENU;
import java.util.Scanner;
public class CustomerView {
private CustomerList customerlist=new CustomerList(10);
public void enterMainMenu()
{
boolean isFlag=true;
while(isFlag)
{
System.out.println("*****************客户信息管理系统**************************");
System.out.println(" 1.添加客户 ");
System.out.println(" 2.修改客户 ");
System.out.println(" 3.删除客户 ");
System.out.println(" 4.客户列表 ");
System.out.println(" 5.退出 ");
Scanner input=new Scanner(System.in);
System.out.println("请选择(1-5):");
int menu=CMUtility.readMenuSelection();
switch(menu)
{
case 1:
addNewCustomer();
break;
case 2:
modifyCustomer();
break;
case 3:
deleteCustomer();
break;
case 4:
listAllCustomer();
break;
case 5:
System.out.println("确认是否退出(1/2):");
int isExit=CMUtility.readConfirmSelection();
if(isExit==1)
{
isFlag=false;
}
}
}
}
//添加客户的操作
private void addNewCustomer()
{
System.out.println("---------------------------添加客户-------------------------------------------------");
System.out.println("姓名;");
String name=CMUtility.readString(10);
System.out.println("性别;");
String sex=CMUtility.readString(1);
System.out.println("年龄;");
int age=CMUtility.readint();
System.out.println("电话;");
String tel=CMUtility.readString(11);
System.out.println("邮箱;");
String email=CMUtility.readString(15);
Customer customer=new Customer(name,sex,age,tel,email);
boolean issSuccess=customerlist.addCustomer(customer);
if(issSuccess)
{
System.out.println("---------------------------增加完成-----------------------------------------");
}else
{
System.out.println("-----------------------客户目录已满,添加失败-----------------------------------------");
}
}
private void modifyCustomer()
{
System.out.println("---------------------------修改客户-------------------------------------------------");
Customer cust;
int num;
for(;;)
{
System.out.println("请选择待修改客户的编号(-1退出):");
num=CMUtility.readint();
if(num==-1)
{
return;
}
cust=customerlist.getCustomer(num-1);
if (cust==null)
{
System.out.println("未找到指定客户");
}else
{
break;
}
}
System.out.print("姓名("+cust.getName()+"):");
String name=CMUtility.readString(10,cust.getName());
System.out.print("性别("+cust.getSex()+"):");
String sex=CMUtility.readString(10,cust.getName());
System.out.print("年龄("+cust.getAge()+"):");
int age=CMUtility.readint(cust.getAge());
System.out.print("电话("+cust.getTel()+"):");
String tel=CMUtility.readString(10,cust.getTel());
System.out.print("邮箱("+cust.getEmail()+"):");
String email=CMUtility.readString(10,cust.getEmail());
Customer newCust=new Customer(name,sex,age,tel,email);
customerlist.replaceCustomer(num-1, newCust);
boolean issRepalaced=customerlist.replaceCustomer(num-1, newCust);
if(issRepalaced)
{
System.out.println("--------------------------修改完成-----------------------------------------");
}else
{
System.out.println("--------------------------修改失败-----------------------------------------");
}
}//删除客户的操作
private void deleteCustomer()
{
System.out.println("---------------------------删除客户-------------------------------------------------");
Customer cust;
int num;
for(;;)
{
System.out.println("请选择待删除客户的编号(-1退出):");
num=CMUtility.readint();
if(num==-1)
{
return;
}
cust=customerlist.getCustomer(num-1);
if (cust==null)
{
System.out.println("未找到指定客户");
}else
{
break;
}
}
System.out.println("是否确定删除(1/2)");
int isDelete=CMUtility.readConfirmSelection();
if(isDelete==1)
{
customerlist.deleteCustomer(num-1);
}else
{
return;
}
boolean deleteSuccess=customerlist.deleteCustomer(num-1);
if(deleteSuccess)
{
System.out.println("--------------------------删除完成-----------------------------------------");
}else
{
System.out.println("--------------------------删除失败-----------------------------------------");
}return;
}
private void listAllCustomer()
{
System.out.println("---------------------------客户列表-------------------------------------------------");
int num=customerlist.getTotal();
if(num==0)
{
System.out.println("当前没有客户记录");
}else
{
System.out.println("编号\t姓名\t性别\t年龄\t\t电话\t\t邮箱");
Customer[] custs=customerlist.getAllCustomer();
System.out.println();
for(int i=0;i<custs.length;i++)
{
Customer cust=custs[i];
System.out.println((i+1)+"\t"+cust.getName()+"\t"+cust.getSex()+"/t"+cust.getAge()+"\t\t"+cust.getTel()+"\t\t"+cust.getEmail());
}
}
System.out.println("--------------------------客户列表完成--------------------------------------------");
}
public static void main(String[] args)
{
CustomerView view=new CustomerView();
view.enterMainMenu();
}
}
这是customerview类
运行结果如下: