Java 小程序:实现一个购物流程的功能(2)

来源:互联网 发布:国内餐饮软件排名 编辑:程序博客网 时间:2024/04/30 15:37
/** *@ author  黄伟 *@ version V1.00 *@ date     2014/4/6 *@ time     15:08 */import java.util.Scanner;public class Shopping2 {public static void main(String[] args) {/* * 需求:用户输入登录,如果登录失败,继续重新登录,失败次数超过三次,退出系统 */int loginIndex = 0;// 定义一个变量,存放用户登录失败的次数Scanner input = new Scanner(System.in);while (true) {// 展示登录菜单System.out.println("\t\t欢迎使用我行我素购物管理系统1.0版\n");System.out.print("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");System.out.println("");System.out.println("\t\t\t1. 登录系统\n");System.out.println("\t\t\t2. 退出系统\n");System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");// 提示用户输入选项System.out.print("请输入您的选项:");int choice1 = input.nextInt();// 判断用户输入选项是否正确if (choice1 == 1) {/* 登录系统 */// 登录系统要实现的功能:提示用户输入用户名和密码System.out.print("请输入用户名:");String username = input.next();System.out.print("请输入密码:");String password = input.next();// 判断用户输入的用户名是否为admin,密码是否为1111/* 整数判断可以用==;字符串是否相等判断,用equals方法 */if ("admin".equals(username) && "1111".equals(password)) {System.out.println("登录成功!");// 登录成功之后,展示购物系统页面while (true) {System.out.println("\t\t    欢迎使用我行我素购物管理系统\n");System.out.print("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");System.out.println("");System.out.println("\t\t\t1. 客 户 信 息 管 理\n");System.out.println("\t\t\t2. 购 物 结 算\n");System.out.println("\t\t\t3. 真 情 回 馈\n");System.out.println("\t\t\t4. 返回上一级\n");System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ");//输入相应选项,实现各自功能System.out.println("请输入你的选择:");int choice2 = input.nextInt();if (choice2 == 1) {while (true) {// 输入1,展示客户信息管理System.out.println("我行我素购物管理系统 > 客户信息管理\n");System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ");System.out.println("");System.out.println("\t\t\t1. 显 示 所 有 客 户 信 息\n");System.out.println("\t\t\t2. 添 加 客 户 信 息\n");System.out.println("\t\t\t3. 修 改 客 户 信 息\n");System.out.println("\t\t\t4. 查 询 客 户 信 息\n");System.out.println("\t\t\t5. 返回上一级\n");System.out.print("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ");System.out.println("");// 根据客户信息管理页面,再实现显示客户信息功能,添加、修改、查询等功能后期完善System.out.println("请输入你的选择:");int choice3 = input.nextInt();if (choice3 == 1) {//显示所有客户信息System.out.println("客户编号\t\t客户姓名\t\t客户生日\t\t客户积分");System.out.println("1001\t\t\t小猫\t\t\t1992-7-10\t\t20000");System.out.println("1002\t\t\t小狗\t\t\t1992-6-17\t\t25000");System.out.println("1003\t\t\t小马\t\t\t1992-7-10\t\t30000");System.out.println("1004\t\t\t小狐狸\t\t\t1992-11-1\t\t10000");System.out.println("1005\t\t\t大象\t\t\t1992-11-17\t\t10000");System.out.println("请按任意键继续!");String str1 = input.next();if (str1 != "\0") {//if语句表示只要输入的字符串不是回车,继续执行循环语句continue;}}//因部分实现功能的代码出现问题,这段先注释掉,以后再修改/*else if (choice3 == 2) {System.out.println("欢迎添加客户!");System.out.println("请输入新的客户名称(2-8个字符):");// String name=input.next();System.out.println("请输入新客户的生日(格式:YYYY-MM-DD):");// int yy=input.nextInt();// int mm=input.nextInt();// int dd=input.nextInt(); * if((mm<1||mm>12)){ * System.out.println("新的客户生日输入异常,添加失败"); * System.out.println("请按任意键继续!"); String * str2 = input.next(); if(str2 != "\0"){ * continue; } }System.out.println("请输入新客户的积分:");// long score=input.nextLong();System.out.println("恭喜,客户添加成功!");System.out.println("请按任意键继续!");String str3 = input.next();if (str3 != "\0") {continue;}} else if (choice3 == 3) {System.out.println("请输入您想要修改的客户编号:");// long number=input.nextLong();System.out.println("客户编号\t\t客户姓名\t\t客户生日\t\t客户积分\n");// System.out.println(number+"\t\t"+a+"\t\t"+b+"\t\t"+c+"\n");System.out.println("请选择你要修改的内容:1. 姓名     2.生日");System.out.println("请输入您的选择:");int cho = input.nextInt();if (cho == 1) {System.out.println("请输入新的客户姓名(2-8个字符串):");String str = input.next();if (str.length() > 1&& str.length() < 9) {System.out.println("修改成功!");System.out.println("请按任意键继续!");String str0 = input.next();if (str0 != "\0") {continue;}} else {System.out.println("请输入新的客户生日(格式:YYYY-MM-DD):");// int yy1=input.nextInt();int mm1 = input.nextInt();// int dd1=input.nextInt();if ((mm1 < 1 || mm1 > 12)) {System.out.println("新的客户生日输入异常,添加失败");System.out.println("请按任意键继续!");String str4 = input.next();if (str4 != "\0") {continue;}}System.out.println("修改成功!");System.out.println("请按任意键继续!");String str5 = input.next();if (str5 != "\0") {continue;}}}} else if (choice3 == 4) {System.out.println("请输入你要查询的客户编号:");// long number2=input.nextLong();System.out.println("客户编号\t\t客户姓名\t\t客户生日\t\t客户积分\n");// System.out.println(number+"\t\t"+a+"\t\t"+b+"\t\t"+c+"\n");System.out.println("请按任意键继续!");String str6 = input.next();if (str6 != "\0") {continue;}}*/ else {break;}continue;}} // 显示购物结算页面else if (choice2 == 2) {System.out.println("欢迎进入购物系统!");System.out.println("现有商品如下:");System.out.println("商品编号\t商品名称\t\t商品价格 ");System.out.println("1\t\t云南白药\t\t¥18.00");System.out.println("2\t\t小米手机\t\t¥1998.00");System.out.println("3\t\t小黑\t\t\t¥4998.00");System.out.println("4\t\t拖鞋\t\t\t¥25.80");System.out.println("5\t\t酱油\t\t\t¥5.60 ");System.out.println("6\t\t汽车模型\t\t¥350.00");System.out.println("7\t\t坦克模型\t\t¥400.00");System.out.println("8\t\t玩具枪\t\t\t¥99.80");System.out.println("请输入你要购买的产品编号: ");int number = input.nextInt();System.out.println("请输入你要购买的产品数量: ");int count = input.nextInt();String GoodsName = "";  double price = 0;double totalPrice = 0;    // 以下的Switch语句主要实现用户购买的何种商品以及它的数量switch (number) {case 1:GoodsName = "云南白药";price = 18.00;totalPrice = count * price;break;case 2:GoodsName = "小米手机";price = 1998.00;totalPrice = count * price;break;case 3:GoodsName = "小黑";price = 4998.00;totalPrice = count * price;break;case 4:GoodsName = "拖鞋";price = 25.00;totalPrice = count * price;break;case 5:GoodsName = "酱油";price = 5.60;totalPrice = count * price;break;case 6:GoodsName = "汽车模型";price = 350.00;totalPrice = count * price;break;case 7:GoodsName = "坦克模型";price = 400.00;totalPrice = count * price;break;case 8:GoodsName = "玩具枪";price = 99.80;totalPrice = count * price;break;default:System.out.println("对不起,没有你需要的商品。");break;}//显示用户购买的商品信息以及付款情况System.out.println("尊敬的用户:小猫");System.out.println("产品名称\t产品单价\t产品数量\t产品总价");System.out.println(GoodsName+ "\t\t¥"+ price+ "\t\t"+ count+ "\t\t¥"+(float)(totalPrice));System.out.println("您购买的商品总价为:"+ (float)(totalPrice));System.out.println("按您当前的积分,您的折扣为:0.85");double pay = totalPrice * 0.85;//提示用户付款,付款不够,继续付款System.out.println("您的应付款为:"+(float)(pay));System.out.print("请付款:");while(true){double money = input.nextDouble();if (money >= pay) {System.out.println("付款成功!");System.out.println("找零:"+ (float)(money - pay));System.out.println("欢迎下次光临!");break;} else {//付款不足,执行循环结构System.out.println("您付款金额不足,请重新付款:");continue;}}System.out.println("请按任意键继续!");String str11 = input.next();if (str11 != "\0") {continue;}} // 显示真情回馈页面else if (choice2 == 3) {while (true) {System.out.println("我行我素购物管理系统 > 真情回馈\n");System.out.print("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ");System.out.println("");System.out.println("\t\t\t1. 幸 运 大 放 送\n");System.out.println("\t\t\t2. 幸 运 抽 奖\n");System.out.println("\t\t\t3. 生 日 问 候\n");System.out.println("\t\t\t4. 返 回 上 一 级\n");System.out.print("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ");System.out.println("");//按页面所示,输入相应选项System.out.println("请输入您的选择:");int cho1 = input.nextInt();if (cho1 == 1) {System.out.println("积分最高的客户是:");System.out.println("客户编号\t\t客户姓名\t\t客户生日 ");System.out.println(" ");System.out.println("恭喜以上用户,获得一个价值¥50.00的话费充值卡");System.out.println("请按任意键继续!");String str6 = input.next();if (str6 != "\0") {continue;}} else if (cho1 == 2) {System.out.println("幸运客户是:");System.out.println("客户编号\t\t客户姓名\t\t客户生日 ");System.out.println(" ");System.out.println("恭喜以上用户,获得一个价值¥13.50的黑人牙膏");System.out.println("请按任意键继续!");String str7 = input.next();if (str7 != "\0") {continue;}} else if (cho1 == 3) {System.out.println("对不起,今天没有过生日的客户!");System.out.println("请按任意键继续!");String str8 = input.next();if (str8 != "\0") {continue;}} else {break;}continue;}}else {break;}continue;}} else {loginIndex++;if (loginIndex > 2) {System.out.println("输入错误次数已经超过限制,傻逼,警察叔叔来找你了。。。");System.exit(1);}System.out.println("呀咩碟,大傻逼啊,用户名或者密码有误,请重新登录");continue;}} else if (choice1 == 2) {/* 退出系统 */System.out.println("哈大喜哇,下次光临");System.exit(1);// 系统退出} else {/* 大傻逼啊,没有这个选项 */System.out.println("呀咩碟,大傻逼啊,没有这个选项,请重新登录");continue;}}}}

关于此段代码,是对上次的补充,如果看过我上次写的代码,你会发现出现最大的不同是循环结构的语句全部换成while语句了,上次写的for语句,另外对于返回上一级的代码用到了break语句和continue语句,前者是执行跳出循环动作,而后者则是结束当前语句,从循环开始语句开始执行。

    程序中用到了很多if-else-else if 语句,应注意条件判断的依据,熟悉个中关系.

     最后,还得说声抱歉,因部分知识掌握不深,购物功能尚有部分未实现,在下次写的博客-实现购物功能(3)中全部实现,尽请各位期待。

以下是程序运行的界面:

1.登录系统,设用户名和密码分别为admin和1111 。


2 . 选择显示所有客户信息,其他功能下次再实现(抱歉啊!)


3. 选择2,实现购物结算:


4. 选择3,实现真情回馈(关于用户信息未打印出,后期改善)


5.实现返回上一级功能:


6. 返回上一级并退出系统:


1 0
原创粉丝点击