模拟通过文本对电商客户进行管理,熟悉面向对象编程,主要涉及以下知识点:
类的结构使用:属性,方法,构造器
对象的创建和使用
类的封装性声明和数组使用
数组插入删除及修改
实现的目标如下:

总共通过三个类来完成,Customer(客户类),CustomerList(客户管理类),CustomerView(界面管理类)
客户类的实现
/**
* @Author:cgn
* @Description 客户类:设定属性 姓名,性别,年龄,电话,邮箱
* 添加无参及有参构造器
* @date
*/
public class Customer {
private String name;
private char sex;
private int age;
private String tel;
private String email;
public Customer() {
}
public Customer(String name, char 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 char getSex() {
return sex;
}
public void setSex(char 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;
}
}
客户管理类的实现:
/**
* @Author:cgn
* @Description Customr管理类
* @date
*/
public class CustomerList {
private Customer[] customers;
private int total = 0;
/**
*创建对象时,同时定义数组
* @param totalCustomer
*/
public CustomerList(int totalCustomer){
customers = new Customer[totalCustomer];
}
public int getTotal(){
return total;
}
//添加客户
public boolean addCustomer(Customer customer){
boolean flag = false;
if(this.getTotal() >= customers.length){
return false;
}else{
customers[total] = customer;
this.total++;
System.out.println("添加成功");
return true;
}
}
//修改客户信息
public boolean replaceCustomer(int index,Customer cust){
if(index < 0 || index >= this.getTotal()){
System.out.println("输入的用户不存在");
return false;
}else{
customers[index] = cust;
System.out.println("修改客户信息成功");
return true;
}
}
//删除客户信息
public boolean deleteCustomer(int index){
boolean flag = false;
if(index < 0 || index >= this.getTotal()){
System.out.println("输入的用户不存在");
flag = false;
}else{
for(int i = index;index < this.getTotal();index++){
customers[index] =customers[index +1];
this.total--;
System.out.println("删除成功");
flag = true;
}
}
return flag;
}
//查询客户列表
public Customer[] getCustomers(){
return customers;
}
//查询索引是index的客户信息
public Customer getCustomer(int index){
if(index < 0 || index >= this.getTotal()) {
System.out.println("查询的信息不存在");
return null;
}else{
return customers[index];
}
}
}
界面显示类(框架搭起来,需要补充实现的方法)
import java.util.Scanner;
/**
* @Author:cgn
* @Description
* @date
*/
public class CustomerView {
public static void main(String[] args) {
}
//创建一个最大包含20个客户的数组
CustomerList customerList = new CustomerList(20);
//进入主界面的方法
public void enterMainMenu(){
Scanner sc = new Scanner(System.in);
boolean flag = true;
do{
System.out.println("**********拼电商客户管理系统************");
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();
System.out.println("请输入(1-5)");
char input = sc.next().charAt(0);//没有校验,需要添加方法或类进行处理
switch (input){
case '1':
break;
case '2':
break;
case '3':
break;
case '4':
break;
case '5':
break;
default:
break;
}
}while(flag);
sc.close();
}
}
828

被折叠的 条评论
为什么被折叠?



