1.使用静态调用一些类方法和属性以简化数据在类之间的传递;
2.在所有需要用户输入数据的地方都做了有效性验证,但有些地方不太完美;
3.更容易理解,代码复用率提高,代码量比原版的少了许多,但功能不少,反而有所增强。
下面是七个类的代码:
----------------------------------------------------------------------------------------------------
package com.wxws.sms;
public class CustManagement {
/**
* @param 客户管理类
* @author 刘祺 2007-7-9
*/
/* 显示所有客户 */
public void show() {
System.out.println("我行我素购物管理系统 > 客户信息管理 > 显示客户信息\n");
CustListTitle();
/* 遍历所有客户 */
for (int i = 0; Data.notEndOfUserList(i); i++) {
System.out.println(Data.custNo[i] + "\t|" + Data.custBirth[i]
+ "\t|" + Data.custScore[i]);
}
Data.getInputString("按N返回:");
}
/* 添加客户 */
public void add() {
System.out.println("我行我素购物管理系统 > 客户信息管理 > 添加客户信息\n");
/* 输入会员信息 */
boolean isWrong;
/* 输入会员号 */
int custNo;
do {
custNo = Data.getInputInt("请输入会员号(<四位整数>):");
isWrong = custNo < 1000 || custNo > 9999;
if (isWrong) {
System.out.print("输入错误!");
}
} while (isWrong);
/* 输入会员生日 */
String custBirth;
do {
custBirth = Data.getInputString("请输入会员生日(月/日<用两位数表示>):");
isWrong = !Data.chickBirth(custBirth);
if (isWrong) {
System.out.print("输入错误!");
}
} while (isWrong);
/* 输入会员积分 */
int custScore;
do {
custScore = Data.getInputInt("请输入积分:");
isWrong = custScore < 0;
if (isWrong) {
System.out.print("输入错误!");
}
} while (isWrong);
/* 打印录入的数据 */
CustListTitle();
System.out.println(custNo + "\t|" + custBirth + "\t|" + custScore);
if (Data.getInputString("按:\"Y\"保存以上会员信息").equalsIgnoreCase("y")) {
/* 插入数据库 */
for (int i = 0; i < Data.custNo.length; i++) {
if (Data.custNo[i] == 0) {
Data.custNo[i] = custNo;
Data.custBirth[i] = custBirth;
Data.custScore[i] = custScore;
break;
}
}
}
}
/* 修改客户信息 */
public void modify() {
System.out.println("我行我素购物管理系统 > 客户信息管理 > 修改客户信息\n");
int i = searchCust();
if (i >= 0) {
boolean isWrong;
switch (Data.getInputInt("\n1.修改会员生日\n2.修改会员积分\n0.取消,不修改\n\n请选择,输入数字:")) {
case 1:
String custBirth;
do {
custBirth = Data.getInputString("请输入修改后的生日(月/日<用两位数表示>):");
isWrong = !Data.chickBirth(custBirth);
if (isWrong) {
System.out.print("输入错误!");
}
} while (isWrong);
Data.custBirth[i] = custBirth;
System.out.println("生日信息已更改!");
break;
case 2:
int custScore;
do{
custScore = Data.getInputInt("请输入修改后的会员积分:");
isWrong = custScore < 0;
if (isWrong) {
System.out.print("输入错误!");
}
} while (isWrong);
Data.custScore[i] = custScore;
System.out.println("会员积分已更改!");
break;
}
}
}
/* 查询客户信息 */
public void search() {
System.out.println("我行我素购物管理系统 > 客户信息管理 > 查询客户信息\n");
while (searchCust() > 0)
;
}
/* 查询客户方法 */
public static int searchCust() {
int custNo;
while (true) {
custNo = Data.getInputInt("请输入会员号(<四位整数>输入0取消):");
if (custNo > 999 && custNo < 10000) {
for (int i = 0; Data.notEndOfUserList(i); i++) {
if (Data.custNo[i] == custNo) {
CustListTitle();
System.out
.println(Data.custNo[i] + "\t|"
+ Data.custBirth[i] + "\t|"
+ Data.custScore[i]);
return i;
}
}
System.out.println("会员号" + custNo + "不存在");
} else if (custNo == 0) {
return -1;
} else {
System.out.print("输入错误!");
}
}
}
private static void CustListTitle(){
System.out.println("会员号\t|生日\t|积分\n--------+-------+--------");
}
}
----------------------------------------------------------------------------------------------------
package com.wxws.sms;
import java.util.Scanner;
public class Data {
static Scanner input = new Scanner(System.in);
/* 商品信息 */
static String[] goodsName = new String[256];// 商品名称
static double[] goodsPrice = new double[256]; // 商品价格
/* 会员信息 */
static int[] custNo = new int[256];// 会员号
static String[] custBirth = new String[256]; // 会员生日
static int[] custScore = new int[256]; // 会员积分
public static void initial() {
/* 为客户信息赋初值 */
custNo[0] = 1623; // 客户1
custBirth[0] = "06/26";
custScore[0] = 5000;
custNo[1] = 1545; // 客户2
custBirth[1] = "08/08";
custScore[1] = 2200;
custNo[2] = 8848; // 客户3
custBirth[2] = "11/26";
custScore[2] = 4416;
custNo[3] = 9673; // 客户4
custBirth[3] = "02/08";
custScore[3] = 2200;
custNo[4] = 1106; // 客户5
custBirth[4] = "06/26";
custScore[4] = 8000;
custNo[5] = 3319; // 客户6
custBirth[5] = "01/08";
custScore[5] = 3300;
custNo[0] = 1623; // 客户7
custBirth[0] = "06/26";
custScore[0] = 5000;
custNo[1] = 1545;// 客户8
custBirth[1] = "04/08";
custScore[1] = 2200;
/* 为商品信息赋初值 */
goodsName[0] = "addidas运动鞋"; // 商品1
goodsPrice[0] = 880;
goodsName[1] = "addidasT恤"; // 商品2
goodsPrice[1] = 420.78;
goodsName[2] = "Nike运动鞋"; // 商品4
goodsPrice[2] = 900;
}
public static boolean notEndOfUserList(int index) {
if (index < custNo.length && custNo[index] != 0) {
return true;
} else {
return false;
}
}
/* 获取用户输入的字符串并效验 */
public static String getInputString(String prompt) {
try {
System.out.print(prompt);
return input.next();
} catch (Exception e) {
return null;
}
}
/* 获取用户输入的整数并效验 */
public static int getInputInt(String prompt) {
try {
System.out.print(prompt);
return input.nextInt();
} catch (Exception e) {
return -1;
}
}
public static boolean chickBirth(String birth) {
if (birth.length() != 5 || birth.indexOf('/') != 2) { // 字符串长度5,第三字符为'/',否则报错
return false;
}
for (int i = 0; i < birth.length(); i++) { // 检查第1、2、4、5字符是否为数字
if (i == 2) {
continue; // 跳过第三位不检查
} else if (birth.charAt(i) - '0' < 0 || birth.charAt(i) - '0' > 9) { // 检查是否为数字
return false;
}
}
int month = (birth.charAt(0) - '0') * 10 + (birth.charAt(1) - '0'); // 提取字符串中的月份为整数
int day = (birth.charAt(3) - '0') * 10 + (birth.charAt(4) - '0'); // 提取字符串中的日为整数
return (month >= 1 && month <= 12 && day >= 1 && day <= 31)
&& !((month == 4 || month == 6 || month == 9 || month == 11) && day > 30)
&& !(month == 2 && day > 29); // 判断日期合法性
}
}
----------------------------------------------------------------------------------------------------
package com.wxws.sms;
public class GiftManagement {
/* 模拟幸运抽奖 */
public void sendLuckyCust() {
System.out.println("我行我素购物管理系统 > 幸运抽奖\n");
do {
int custNo = Data.getInputInt("\n请输入4位会员号:"); // 录入会员号
int random = (int) (Math.random() * 10); // 生成0-9的随机整数
int baiwei = (int) custNo / 100 % 10; // 获取会员号的百位数
if (baiwei == random) { // 会员号与随机数比较
System.out.println(custNo + "是幸运客户,获赠精美MP3一个。");
} else {
System.out.println(custNo + "\t谢谢您的支持!");
}
} while (!(Data.getInputString("是否继续(Y/N):").equalsIgnoreCase("n")));
}
public void sendBirthCust() {
System.out.println("我行我素购物管理系统>生日问候\n");
String date;
boolean isWrong;
do {
date = Data.getInputString("请输入今天的日期(月/日<用两位表示>):");
isWrong = !Data.chickBirth(date);
if (isWrong) {
System.out.print("输入错误!");
}
} while (isWrong);
String msg = "";
for (int i = 0; Data.notEndOfUserList(i); i++) {
if (Data.custBirth[i].equals(date)) {
msg += Data.custNo[i];
msg += "\t";
}
}
if (msg.length() == 0) {
System.out.println("今天没有过生日的会员!");
} else {
System.out.println("今天过生日的会员:" + msg + "\n恭喜!获赠MP3一个!");
}
Data.getInputString("按N返回:");
}
public void sendGoldenCust() {
System.out.println("我行我素购物管理系统 > 幸运大放送\n\n");
int max = 0;
// 假定积分各不相同
for (int i = 0; Data.notEndOfUserList(i); i++) {
// 求最大积分的客户
if (Data.custScore[i] > Data.custScore[max]) {
max = i;
}
}
System.out.println("具有最高积分的会员是: " + Data.custNo[max] + "\t"
+ Data.custBirth[max] + "\t" + Data.custScore[max]);
System.out.println("获赠价值¥12000的苹果笔记本电脑一台");
Data.getInputString("按N返回:");
}
}
----------------------------------------------------------------------------------------------------
package com.wxws.sms;
public class Manager {
static String username = "accp"; // 管理员用户名
static String password = "0000"; // 管理员密码
public String toString() {
return "管理员信息是: " + username + "\t" + password;
}
/* 用户名密码效验 */
public static boolean PswVerify() {
for (int i = 2; i >= 0; i--) { // 三次输入用户名、密码的机会
String name = Data.getInputString("请输入用户名:");
String psw = Data.getInputString("请输入密码:");
System.out.println();
if (name.equals(username) && psw.equals(password)) {
return true;
} else {
System.out.println("用户名或密码错误,请重试。\n(还" + i + "次机会)");
}
}
System.out.println("您没有权限进入系统");
return false;
}
}
----------------------------------------------------------------------------------------------------
package com.wxws.sms;
public class Menu {
/**
* @param 菜单类
*/
/* 登录菜单 */
public int showLoginMenu() {
System.out.println("\n\n\t\t\t欢迎使用我行我素购物管理系统1.0版\n\n");
System.out.println("\t\t\t\t1.登 陆 系 统\n\n");
System.out.println("\t\t\t\t2.退 出\n\n");
printLine();
System.out.print("请选择,输入数字:");
return getChoice();
}
/* 主菜单 */
public void showMainMenu() {
while (true) {
System.out.println("\n\n\t\t\t欢迎使用我行我素购物管理系统1.0版\n");
printLine();
System.out.println("\t\t\t\t1.客 户 信 息 管 理\n");
System.out.println("\t\t\t\t2.购 物 结 算\n");
System.out.println("\t\t\t\t3.真 情 回 馈\n");
System.out.println("\t\t\t\t4.注 销\n");
printLine();
System.out.print("请选择,输入数字:");
switch (getChoice()) {
case 1:
showCustMMenu(); // 执行显示客户信息菜单
break;
case 2:
Pay.calcPay();// System.out.println("执行购物结算");
break;
case 3:
showSendGiftMenu(); // 执行真情回馈菜单
break;
case 4:
return;
default:
System.out.println("输入错误!");
}
}
}
/* 客户信息管理 */
public void showCustMMenu() {
while (true) {
System.out.println("\n我行我素购物管理系统 > 客户信息管理\n");
printLine();
System.out.println("\t\t\t\t1.显 示 所 有 客 户 信 息\n");
System.out.println("\t\t\t\t2.添 加 客 户 信 息\n");
System.out.println("\t\t\t\t3.修 改 客 户 信 息\n");
System.out.println("\t\t\t\t4.查 询 客 户 信 息\n");
printLine();
System.out.print("请选择,输入数字或按0返回上一级菜单:");
CustManagement custManagement = new CustManagement();
switch (getChoice()) {
case 1:
custManagement.show(); // 执行显示所有客户信息
break;
case 2:
custManagement.add(); // 执行添加客户信息
break;
case 3:
custManagement.modify(); // 执行修改客户信息
break;
case 4:
custManagement.search(); // 执行查询客户信息
break;
case 0:
return;
default:
System.out.println("输入错误!");
}
}
}
/* 真情回馈 */
public void showSendGiftMenu() {
while (true) {
System.out.println("\n我行我素购物管理系统 > 真情回馈\n");
printLine();
System.out.println("\t\t\t\t1.幸 运 大 放 送\n");
System.out.println("\t\t\t\t2.幸 运 抽 奖\n");
System.out.println("\t\t\t\t3.生 日 问 候\n");
printLine();
System.out.print("请选择,输入数字或按0返回上一级菜单:");
GiftManagement giftManagement = new GiftManagement();
switch (getChoice()) {
case 1:
giftManagement.sendGoldenCust(); // 执行幸运大放送
break;
case 2:
giftManagement.sendLuckyCust(); // 执行幸运抽奖
break;
case 3:
giftManagement.sendBirthCust(); // 执行生日问候
break;
case 0:
return;
default:
System.out.println("输入错误!");
}
}
}
/* 打印横线 */
private void printLine() {
System.out
.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
}
/* 获取用户输入的整数并效验 */
private static int getChoice() {
try {
return Data.input.nextInt();
} catch (Exception e) {
return -1;
}
}
}
----------------------------------------------------------------------------------------------------
package com.wxws.sms;
public class Pay {
/**
* @param 商品价格查询
* @author 刘祺 2009-7-9
*/
public static void calcPay() {
do {
// 在屏幕上打出菜单
System.out.println("\n我行我素购物管理系统 > 购物结算\n");
System.out.println("******************************");
for (int i = 0; i < Data.goodsName.length
&& Data.goodsName[i] != null; i++) {
System.out.print(i + 1 + ".");
System.out.println(Data.goodsName[i]);
}
System.out.println("******************************");
double total = 0; // 购物总金额
StringBuffer goodList = new StringBuffer(
"\n********************消费清单********************\n物品\t\t单价\t 个数\t金额\n");
while (true) {
int goodsNo; // 商品编号
int count = 0; // 商品数量
double sum;
goodsNo = Data.getInputInt("\n请输入商品编号(输入0结束):");
if (goodsNo == 0) {
break;
} else if (goodsNo < 0 || goodsNo > Data.goodsName.length
|| Data.goodsName[goodsNo - 1] == null) {
System.out.println("输入错误");
continue;
} else {
goodsNo -= 1;
count = Data.getInputInt("单价¥" + Data.goodsPrice[goodsNo]
+ "\t请输入购买数量:");
if (count <= 0) {
System.out.println("输入错误");
continue;
} else {
sum = Data.goodsPrice[goodsNo] * 100 * count / 100;
System.out.println(Data.goodsName[goodsNo] + "\t单价¥"
+ Data.goodsPrice[goodsNo] + "\t总价:¥" + sum);
goodList.append(Data.goodsName[goodsNo] + "\t¥"
+ Data.goodsPrice[goodsNo] + "\t " + count
+ "\t¥" + sum + "\n");
total += sum;
}
}
}
System.out.print("\n");
// 结算
if (total == 0) {
System.out.println("什么都没买。");
} else {
int custIndex = CustManagement.searchCust(); // 查找会员
/* 计算折扣 */
double discount = 1; // 折扣
if (custIndex < 0) {
discount = 1;
} else if (Data.custScore[custIndex] >= 8000) {
discount = 0.6;
} else if (Data.custScore[custIndex] >= 4000) {
discount = 0.7;
} else if (Data.custScore[custIndex] >= 2000) {
discount = 0.8;
} else if (Data.custScore[custIndex] >= 1000) {
discount = 0.9;
}
System.out.println("折扣:\t" + discount + "折\n");
/* 算总账 */
double finalPay = ((int) ((total * discount * 100) + 0.5)) / 100; // 打折后须付款
double payment = Data.getInputInt("应付款总计:¥" + finalPay
+ "\n请输入实际付款金额(输入0退出结算):"); // 输入实付金额
if (payment == 0) {
return;
}
while (payment < finalPay) {
payment = Data.getInputInt("输入错误\t应付款总计:¥" + finalPay
+ "\n请重新输入实际付款金额:"); // 输入实付金额
}
double retunMoney = payment - finalPay; // 找钱
int score = (int) finalPay / 100 * 3; // 计算积分
/* 打印账单 */
System.out.println(goodList);
System.out.println("折 扣:\t" + discount + "折");
System.out.println("金额总计:\t¥" + finalPay);
System.out.println("实际交付:\t¥" + payment);
System.out.println("找 钱:\t¥" + retunMoney);
System.out.println("本次消费所获积分:\t" + score);
if (custIndex >= 0) {
Data.custScore[custIndex] += score; // 保存积分
}
}
} while (!Data.getInputString("\n是否继续购物(Y/N)?").equalsIgnoreCase("n"));
}
}
----------------------------------------------------------------------------------------------------
package com.wxws.sms;
public class StartSMS {
/**
* @param 程序入口
*/
public static void main(String[] args) {
Menu menu = new Menu();
Data.initial();
while (true) {
switch (menu.showLoginMenu()) { // 调用登录菜单并判断返回选择
case 1:
/* 用户名、密码验证 */
if (Manager.PswVerify()) {
menu.showMainMenu();
}
break;
case 2:
System.out.println("谢谢您的使用!"); // 注销
return;
default:
System.out.println("输入错误!"); // 无效返回值
}
}
}
}
----------------------------------------------------------------------------------------------------
顶一下
(6)
85.7%
踩一下
(1)
14.3%

评论加载中....