为这学期Java的课程设计而准备,一个简易的小东西
尚硅谷java项目二
package work2;
public class CustomerView {
private CustomerList customerList=new CustomerList(10);
// public CustomerView(){
// Customer customer=new Customer("王涛",'男',12,"10086","@qq.com");
// customerList.addCustomer(customer);
// }
public void enterMainMenu(){//主菜单
char c;
while(true){
System.out.println("\n---------------------客户信息管理软件------------------------\n");
System.out.println(" 1添加客户");
System.out.println(" 2修改客户");
System.out.println(" 3删除客户");
System.out.println(" 4客户列表");
System.out.println(" 5退 出\n");
System.out.print(" 请选择(1-5):");
c=CMUtility.readMenSelection();
if(c=='1'){
addNewCustomer();
} else if(c=='2'){
modifyCustomer();
} else if(c=='3') {
deleteCustomer();
} else if(c=='4') {
listAllCustomer();
} else if(c=='5') {
System.out.print("确认是否退出(Y/N):");
if(work.CMUtility.readConfirmSelection()=='Y') break;
}
}
}
private void addNewCustomer(){//添加客户信息
System.out.println("\n-----------------------添加客户信息-------------------------\n");
System.out.print("姓名:");
String name=CMUtility.readString(10);
System.out.print("性别:");
char gender=CMUtility.readChar();
System.out.print("年龄:");
int age=CMUtility.readInt();
System.out.print("电话:");
String phone=CMUtility.readString(13);
System.out.print("邮箱:");
String email=CMUtility.readString(30);
Customer customer=new Customer(name,gender,age,phone,email);
boolean isSuccess=customerList.addCustomer(customer);
if(isSuccess) System.out.println("-------------------------添加完成--------------------------");
else System.out.println("-----------------------客户目录已满-------------------------");
}
private void modifyCustomer(){//修改客户信息,不修改的可以回车跳过
System.out.println("------------------------修改客户---------------------------");
int number;
Customer cust;
while(true){
System.out.print("请选择待修改客户编号(-1退出):");
number= CMUtility.readInt();
if(number==-1) return;
cust = customerList.getCustomer(number);
if(cust==null) System.out.println("无法找到指定客户!");
else break;
}
System.out.print("姓名("+cust.getName()+"):");
String name=CMUtility.readString(10,cust.getName());
System.out.print("性别("+cust.getGender()+"):");
char gender=CMUtility.readChar(cust.getGender());
System.out.print("年龄("+cust.getAge()+"):");
int age=CMUtility.readInt(cust.getAge());
System.out.print("电话("+cust.getPhone()+"):");
String phone=CMUtility.readString(13,cust.getPhone());
System.out.print("邮箱("+cust.getEmail()+"):");
String email=CMUtility.readString(30,cust.getEmail());
Customer customer=new Customer(name,gender,age,phone,email);
boolean isRepalaced=customerList.replaveCustomer(number,customer);
if (isRepalaced) System.out.println("-------------------------修改成功!-------------------------");
}
private void deleteCustomer(){//删除客户
System.out.println("------------------------删除客户---------------------------");
int number;
while(true){
System.out.print("请输入待删除客户编号(-1)退出:");
number=CMUtility.readInt();
if(number==-1) return;
Customer customer=customerList.getCustomer(number);
if(customer==null) System.out.println("无法找到指定客户!");
else break;
}
System.out.print("确认是否删除(Y/N):");
char c=CMUtility.readConfirmSelection();
if(c=='Y'){
if(customerList.deteleCustomer(number)){
System.out.println("------------------------删除成功!--------------------------");
}
} else return;
}
private void listAllCustomer(){//输出客户信息
System.out.println("------------------------客户列表---------------------------");
if(customerList.getTotal()==0) System.out.println("没有客户信息");
else{
System.out.println("编号\t\t姓名\t\t性别\t\t年龄\t\t电话\t\t\t邮箱");
Customer[] custs=customerList.getCustomers();
for(int i=0;i<customerList.getTotal();i++)
System.out.println((i+1)+"\t\t"+custs[i].getName()+"\t\t"+custs[i].getGender()+"\t\t"+custs[i].getAge()+"\t\t"+custs[i].getPhone()+"\t\t"+custs[i].getEmail());
}
System.out.println("-----------------------客户列表完成-------------------------");
}
public static void main(String[] args) {
CustomerView view=new CustomerView();
view.enterMainMenu();
}
}
package work2;
public class CustomerList {
private Customer[] customers;//存客户信息
private int total=0;//客户个数
public CustomerList(int totalCustomer){//创建存储客户信息的空间
customers=new Customer[totalCustomer];
}
public boolean addCustomer(Customer customer){//添加客户信息
if(customers.length <= total) return false;
customers[total++]=customer;
return true;
}
public boolean replaveCustomer(int index,Customer cust){//将客户信息覆盖到指定位置
if(index < 1 && index > total) return false;
customers[index-1]=cust;
return true;
}
public boolean deteleCustomer(int index){//删除指定客户信息
if(index < 1 && index > total) return false;
for (int i = index ;i<customers.length; i++)
customers[i-1]=customers[i];
customers[total--]=null;
return true;
}
public Customer[] getCustomers(){//返回所有客户信息
Customer[] custs=new Customer[total+1];
for (int i=0;i < total; i++)
custs[i]=customers[i];
return custs;
}
public Customer getCustomer(int index){//返回指定客户的信息
if(index < 1 || index > total) return null;
Customer cust=new Customer();
System.out.println(index);
cust=customers[index-1];
return cust;
}
public int getTotal(){//返回客户个数
return total;
}
}
package work2;
public class Customer {
private String name;//封装,减小访问权限,只有这一个类的可以字节访问
private char gender;
private int age;
private String phone;
private String email;
public String getName() {
return name;
}
public char getGender() {
return gender;
}
public int getAge() {
return age;
}
public String getPhone() {
return phone;
}
public String getEmail() {
return email;
}
public void setName(String name) {
this.name = name;
}
public void setGender(char gender) {
this.gender = gender;
}
public void setAge(int age) {
this.age = age;
}
public void setPhone(String phone) {
this.phone = phone;
}
public void setEmail(String email) {
this.email = email;
}
public Customer() {//构造器
}
public Customer(String name, char gender, int age, String phone, String email) {//构造器
this.name = name;
this.gender = gender;
this.age = age;
this.phone = phone;
this.email = email;
}
}
package work2;
import java.util.*;//把包导入库中
public class CMUtility {
private static Scanner scanner=new Scanner(System.in);
public static char readMenSelection(){//返回一个数字为1~5的字符
char c;
while(true){
String str = readKeyBoard(1,false);
c=str.charAt(0);
if(c<'1'||c>'5'){
System.out.print("输入错误,请重新输入!");
} else break;
}
return c;
}
public static char readChar(){//返回字符串中第一个字符
String str = readKeyBoard(1,false);
return str.charAt(0);
}
public static char readChar(char defaultValue){//返回字符串中第一个字符或原本的字符
String str=readKeyBoard(1,true);
return (str.length()==0)?defaultValue:str.charAt(0);
}
public static int readInt(){//返回一个两位数的数字
int n;
while(true){
String str=readKeyBoard(2,false);//返回值不可为空
try{
n=Integer.parseInt(str);
break;
} catch (NumberFormatException e) {
System.out.print("数字输入错误,请重新输入:");
}
}
return n;
}
public static int readInt(int defaultValue){//返回一个两位数的数字或原本的数字
int n;
while(true){
String str=readKeyBoard(2,true);//返回值可以为空
if(str.length()==0) return defaultValue;
try{
n=Integer.parseInt(str);
break;
} catch (NumberFormatException e) {
System.out.print("数字输入错误,请重新输入:");
}
}
return n;
}
public static String readString(int limit){//返回一个长度为Limit的字符串
return readKeyBoard(limit,false);
}
public static String readString(int limit,String defaultValue){//返回一个长度为Limit的字符串或原本字符串
String str=readKeyBoard(limit,true);
return (str.length()==0)?defaultValue:str;
}
public static char readConfirmSelection(){//返回字符'Y'或者'N'
char c;
while(true){
String str=readKeyBoard(1,false).toUpperCase();
if(str.charAt(0) == 'Y' || str.charAt(0) == 'N'){
c=str.charAt(0);
break;
} else System.out.print("输入错误,请重新输入:");
}
return c;
}
private static String readKeyBoard(int limit,boolean blankReturn){//返回一个长度为Limit的字符串,字符串可能为空
String line="";
while(scanner.hasNextLine()){
line=scanner.nextLine();
if((line.length()==0)){
if(blankReturn) return line;//如果blankReturn为true则返回值可以为空
else continue;
}
if(line.length()<1||line.length()>limit){
System.out.print("输入长度(不大于"+limit+")输入错误,请重新输入:");
continue;
}
break;
}
return line;
}
}