您的位置:首页 > 编程语言 > Java开发

JAVA 语言客户信息管理系统解题报告

2015-03-23 00:18 381 查看
客户信息管理软件解题分析


一,实验目的:

1,模拟实现一个基于文本界面的《客户信息管理软件》

2,进一步掌握编程技巧和调试技巧,熟悉面向对象编程

3,主要涉及以下知识点:

类和对象(属性、方法及构造器)

类的封装

引用数组

数组的插入、删除和替换

对象的聚集处理

多对象协同工作

二,需求分析:

模拟实现基于文本界面的《客户信息管理软件》。

该软件能够实现对客户对象的插入、修改和删除(用数组实现),并能够打印客户明细表。

三,软件设计结构:

该软件由以下三个模块组成:

CustomerView为主模块,负责菜单的显示和处理用户操作

CustomerList为Customer对象的管理模块,内部用数组管理一组Customer对象,并提供相应的添加、修改、删除和获取方法,供CustomerView调用

Customer为实体对象,用来封装客户信息

四,实验具体内容:

见p2.ppt.

五,实验代码:

见打包程序.

六,实验后收获:

本次实验室建立在java语言对象的实验基础上进行的实验,刚拿到需求分析的时候,不知道如何下手,主要是类类型的定义和类类型的引用,类数组—存的是类的对象.比如本次实验中 Customer类是对象的属性的封装,CustomerList类是类Customer类的封装

在CustomerList类中 定义了一个数组customers[];用来存储对象,然后通过CustomerList 来操纵数组的改变,比如增删改.

此次实验三个类之间的关系是 CustomerView 是一个主菜单 来显示操纵后的结果,其中定义了CustomerList类的对象,然后通过它的对象来操纵数组customers[];然后再通过类Customer来改变对象的属性,最终达到该软件的功能.

此次实验个人不足的是没有具体理解对象,类的作用,类类型,以及逻辑思维的严谨和分析能力不足.

要引用类中的方法就要创建对象才能调用.

这里写代码片
//导入输入和输出语句类
import java.util.*;

public class CMUtility {
private static Scanner scanner = new Scanner(System.in);

public static char readMenuSelection() {
char c;
for (; ; ) {
String str = readKeyBoard(1, false);
c = str.charAt(0);
if (c != '1' && c != '2' &&
c != '3' && c != '4' && 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;
for (; ; ) {
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;
for (; ; ) {
String str = readKeyBoard(2, true);
if (str.equals("")) {
return defaultValue;
}

try {
n = Integer.parseInt(str);
break;
} catch (NumberFormatException e) {
System.out.print("数字输入错误,请重新输入:");
}
}
return n;
}

public static String readString(int limit) {
return readKeyBoard(limit, false);
}

public static String readString(int limit, String defaultValue) {
String str = readKeyBoard(limit, true);
return str.equals("")? defaultValue : str;
}

public static char readConfirmSelection() {
char c;
for (; ; ) {
String str = readKeyBoard(1, false).toUpperCase();
c = str.charAt(0);
if (c == 'Y' || c == 'N') {
break;
} else {
System.out.print("选择错误,请重新输入:");
}
}
return c;
}

private static String readKeyBoard(int limit, boolean blankReturn) {
String line = "";

while (scanner.hasNextLine()) {
line = scanner.nextLine();
if (line.length() == 0) {
if (blankReturn) return line;
else continue;
}

if (line.length() < 1 || line.length() > limit) {
System.out.print("输入长度(不大于" + limit + ")错误,请重新输入:");
continue;
}
break;
}

return line;
}
}
//主菜单视图
class CustomerView
{

private CustomerList cl = new CustomerList(10);

public void enterMainMenu()
{
boolean flag = true;
do{

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("                  请选择(1-5):_");

char key = CMUtility.readMenuSelection();

switch (key)
{
case '1':
//添加客户
addNewCustomer();

break;
case '2':
//修改客户
modifyCustomer();

break;
case '3':
//删除客户
deleteCustomer();
break;
case '4':
//客户列表
listAllCustomers();
break;
case '5':
//退出
System.out.print("确认是否退出(Y/N):");

char c = CMUtility.readConfirmSelection();
if(c == 'Y')
{
flag = false;
}

break;
}

}while(flag);

}

private void addNewCustomer()
{
System.out.println("---------------------添加客户---------------------");
System.out.print("姓名:");
String name = CMUtility.readString(5);
System.out.print("性别:");
char gender = CMUtility.readChar();
System.out.print("年龄:");
int age = CMUtility.readInt();
System.out.print("电话:");
String phone = CMUtility.readString(10);
System.out.print("邮箱:");
String email = CMUtility.readString(40);

Customer cu = new Customer(name , gender, age, phone, email);

boolean flag = cl.addCustomer(cu);

if (flag)
{
System.out.println("---------------------添加完成---------------------");
}
else
System.out.println("---------------------添加失败---------------------");

}

private void modifyCustomer()
{
System.out.println("---------------------修改客户---------------------");

Customer cust = null;
int num = 0;
for (; ; )
{

System.out.print("请选择待修改客户编号(-1退出):");
num = CMUtility.readInt();

if(num == -1){
return;
}

cust = cl.getCustomer(num-1);

if(cust == null)
{
System.out.println("对不起,找不到该用户!");
}else
{
break;
}

}

System.out.println("姓名:("+cust.getName()+")");
String name = CMUtility.readString(10,cust.getName());

System.out.println("性别:("+cust.getGender()+")");
char gender = CMUtility.readChar(cust.getGender());

System.out.println("年龄:("+cust.getAge()+")");
int age = CMUtility.readInt(cust.getAge());

System.out.println("电话:("+cust.getPhone()+")");
String phone = CMUtility.readString(15, cust.getPhone());

System.out.println("邮箱:("+cust.getEmail()+")");
String email = CMUtility.readString(40,cust.getEmail());

cust = new Customer(name, gender, age, phone, email);

boolean flag = cl.replaceCustomer(num-1, cust);
if(flag){
System.out.println("---------------------修改完成---------------------");
}else{
System.out.println("---------------------找不到该用户---------------------");
}

}

private void deleteCustomer()
{
System.out.println("---------------------删除客户---------------------");
Customer cust = null;
int num = 0;
for(;;){
System.out.print("请选择待修改客户编号(-1退出):");
num = CMUtility.readInt();

if(num == -1){
return;
}
cust = cl.getCustomer(num-1);

if(cust == null){
System.out.println("对不起,找不到该用户!");
}else{
break;
}
}

System.out.print("确认是否删除(Y/N):");
char c = CMUtility.readConfirmSelection();
if(c == 'N'){
return;
}

boolean flag = cl.deleteCustomer(num-1);
if(flag){
System.out.println("---------------------删除完成---------------------");
}else{
System.out.println("---------------------找不到该用户---------------------");
}

}

private void listAllCustomers()
{
System.out.println("---------------------客户列表---------------------");
Customer[] customers = cl.getAllCustomers();
if(customers.length == 0)
{
System.out.println("没有客户!");
}else
{
System.out.println("编号\t姓名\t性别\t年龄\t电话\t邮箱");
}

for(int i = 0; i < customers.length; i++){
System.out.println((i+1) + "\t" + customers[i].getDetails());
}
System.out.println("---------------------客户列表完成---------------------");
}

public static void main(String[] args)
{
CustomerView cv = new CustomerView();

cv.enterMainMenu();
}
}
//客户数组类 定义了一组对象数组
class CustomerList
{
private Customer customers[];
private int total = 0;

public CustomerList(int totalCustomer)
{
customers = new Customer[totalCustomer];

}

public boolean addCustomer(Customer customer)
{
if (total < 0 || 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;
}else

for (int i = index+1;i < total ;i++ )
{
customers[i-1] = customers[i];
}
customers[--total] = null;
return true;
}

public Customer[] getAllCustomers() {
Customer[] cust = new Customer[total];

for(int i = 0; i < total; i++){
cust[i] = customers[i];
}

return cust;
}

public Customer getCustomer(int index) {
if(index < 0 || index >= total){
return null;
}

return customers[index];

}

}

//客户类
class Customer
{

private String name;
private char gender;
private int age;
private String phone;
private String 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;

}

public void setName(String name)
{
this.name = name;
}

public String getName()
{
return name;

}

public void setGender(char gender)
{

this.gender = gender;
}

public char getGender()
{
return gender;

}

public void setAge(int age)
{
this.age = age;

}

public int getAge()
{
return age;

}

public void setPhone(String phone)
{

this.phone = phone;
}

public String getPhone()
{
return phone;
}
public void setEmail(String email)
{

this.email = email;
}

public String getEmail()
{
return email;
}

public String getDetails()
{
return name + "\t" + gender + "\t" + age + "\t" + phone + "\t" + email;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: