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

这篇博客介绍了Java小程序中购物流程的改进,包括管理员密码修改功能的增强,添加了三次尝试限制,以及实现了商品的重复购买功能。用户需输入正确凭证才能修改密码,并且重复购买功能提供了更便捷的购物体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
 *@ author  黄伟
 *@ version V1.00
 *@ date     2014/4/9
 *@ purpose  实现购物功能
 */
import java.util.Scanner;  //导入java.util包中的Scanner类
import java.util.Random; //导入java.util包中的Random类
import java.util.Date;      //导入java.util包中的Date类
public class Shopping3 {
	public static void main(String[] args){	
	
		//声明,定义10个用户,包括用户名和密码
		String []  username = new String []{"name0","name1","name2","name3","name4","name5"
																,"name6","name7","name8","name9"};
		String [] password = new String []{"pass0","pass1","pass2","pass3","pass4","pass5"
																,"pass6","pass7","pass8","pass9"};
		
		// 定义数组,存放客户信息
		int  []  custNo = new  int[10];               //创建一个存放10个整型数据的客户编号的数组
		String [] custName = new  String[10];	//创建一个存放10个字符串型数据的客户姓名的数组
		String [] custBirth = new String[10];		//创建一个存放10个字符串数据的客户生日的数组
		int [] custScore = new int[10];				//创建一个存放10个整型数据的客户积分的数组

		//记录客户信息
		for(int cn=0;cn<custNo.length;cn++){ //客户编号由循环产生,从1001到1010
			custNo[cn]=cn+1001;
		}
		custName[0]="小猫";
		custBirth[0]="1992-7-10";		//第一个客户的信息
		custScore[0]=20000;

		custName[1]="小狗";
		custBirth[1]="1992-6-17";		//第二个客户的信息		
		custScore[1]=25000;

		custName[2]="小马";
		custBirth[2]="1992-7-10";		//第三个客户的信息
		custScore[2]=30000;

		custName[3]="小狐狸";
		custBirth[3]="1992-11-1";		//第四个客户的信息
		custScore[3]=10000;

		custName[4]="大象";
		custBirth[4]="1992-11-17";		//第五个客户的信息
		custScore[4]=10000;

		
		// 展示登录菜单
		N1:
		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("\t\t\t3. 退 出\n");
			System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ");
			// 提示用户输入选项
			int Logindex = 0;   //记录登录系统失败的次数
			int f=2;   //记录还有几次登录系统的机会
			int m=2 ; //记录重新输入密码的机会
			int h = 0; //记录重复购买商品的次数
			
			System.out.print("请输入您的选择:");
			Scanner input = new Scanner(System.in);
			int choice1 = input.nextInt();

			// 判断用户的输入选项,产生相应的结果
			if (choice1 == 1) {/* 登录系统 */
				// 登录系统要实现的功能:提示用户输入用户名和密码
				N2:
				while(true){
					System.out.println("请输入用户名:");
					String name = input.next();
					System.out.println("请输入密码:");
					String pass = input.next();

					// 判断用户输入的用户名是否和密码对应上,只有两者均输入正确,才会显示登录成功
					/* 整数判断可以用==;字符串是否相等判断,用equals方法 */
					for(int i=0;i<username.length;i++){
						if (name.equals(username[i]) && pass.equals(password[i])) {
							System.out.println("登录成功!");
										
							// 登录成功之后,展示购物系统页面
							N3:
							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) {
									N4:
									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.print("客户编号\t客户姓名\t\t客户生日\t\t客户积分\n");
											for(int j=0;j<custName.length;j++){
												if(custName[j]==null){  //判断输出的客户姓名为null时,终止循环
													break;
												}
												System.out.println(custNo[j]+"\t\t"+custName[j]+"\t\t\t"+
																								custBirth[j]+"\t\t"+custScore[j]);
											}
											System.out.println("请按任意键继续!");
											String str0 = input.next();
											if (str0 != "\0") {//if语句表示只要输入的字符串不是回车,继续执行循环语句
												continue;
											}
										}else if (choice3 == 2) {
											System.out.println("欢迎添加客户!");
											System.out.println("请输入新的客户名称(2-8个字符):");
											String  newName=input.next();
											System.out.println("请输入新客户的生日(格式:YYYY-MM-DD):");
											String  newBirth = input.next ();
											System.out.println("请输入新客户的积分:");
											int  newScore=input.nextInt();
											int index = -1; //寻找插入数组位置
											for(int a=0;a<custName.length;a++){
												if(custName[a]==null){ 
													//判断原数组中第一个为null值的位置,将新添加客户插入此位置
													index=a;
													break;
												}
											}
											// 将新添加客户信息存入原数组的第一个空位置
											custName[index]=newName;  
											custBirth[index]=newBirth;
											custScore[index]=newScore;
											System.out.println("恭喜,客户添加成功!");
											System.out.println("请按任意键继续!");
											String str1=input.next();
											if(str1 !="\0"){
												continue;
											}
										}else if (choice3 == 3) {
											System.out.println("请输入您想要修改的客户编号:");
											int num1=input.nextInt();
											System.out.println("客户编号\t客户姓名\t客户生日\t客户积分");
											for(int k=0;k<custNo.length;k++){
												if(num1==custNo[k]){
													System.out.println(num1+"\t\t"+custName[k]+"\t\t"+
																								custBirth[k]+"\t\t"+custScore[k]+"\n");
													System.out.println("请选择你要修改的内容:1. 姓名     2.生日	");
													System.out.println("请输入您的选择:");
													int ch1= input.nextInt();
													if (ch1 == 1) {
														System.out.println("请输入新的客户姓名(2-8个字符串):");
														String str = input.next();
														custName[k]=str;
														System.out.println("修改成功!");
														System.out.println("请按任意键继续!");
														String str2 = input.next();
														if(str2 != "\0"){
															continue  N4;
														}
													}else {
														System.out.println("请输入新的客户生日(格式:YYYY-MM-DD):");
														String birth = input.next();
														custBirth[k] = birth;
														System.out.println("修改成功!");
														System.out.println("请按任意键继续!");
														String str3 = input.next();
														if (str3 != "\0") {
															continue N4;
														} //if
													} //else
												} //if
											}  //for
										}else if (choice3 == 4) {
											System.out.println("请输入你要查询的客户编号:");
											int num2=input.nextInt();
											System.out.println("客户编号\t客户姓名\t客户生日\t\t客户积分\n");
											for(int p=0;p<custNo.length;p++){
												if(num2==custNo[p]){
													System.out.println(num2+"\t\t"+custName[p]+"\t\t"+
																									custBirth[p]+"\t\t"+custScore[p]+"\n");
												}
											}		
											System.out.println("请按任意键继续!");
											String str4 = input.next();
											if (str4 !="\0") {
												continue;
											}
										}else  if (choice3 == 5) {
											continue N3;
										}else{
											System.out.println("没有此选项!");
											System.out.println("请按任意键继续!");
											String str = input.next();
											if (str != "\0") {
												continue;
											} //if
										} //else
									}  //N4
								}else if (choice2 == 2) {
									System.out.println("欢迎进入购物系统!");
									System.out.println("请输入你的客户编号(1001-1010):");
									int  cNo = input.nextInt();
									int [] goodsNo = new int[]{1,2,3,4,5,6,7,8};
									String  [] goodsName = new String[]{"云南白药","小米手机","小黑","拖鞋",
																									"酱油","汽车模型","坦克模型","玩具枪"};
									double []goodsPrice = new double[]{18.00,1998.00,4998.00,25.80,5.60,
																															350.00,400.00,99.80};
									System.out.println("现有商品如下:");
									System.out.println("商品编号\t商品名称\t\t商品价格 ");
									for(int r=0;r<goodsNo.length;r++){
										System.out.println(goodsNo[r]+"\t\t"+goodsName[r]+"\t\t    ¥"+goodsPrice[r]);
									}
									int []gNos = new int[8]; //定义一个存放重复购买时输入的商品编号的整形数组
									int []gCounts = new int [8];//定义一个存放重复购买时输入的商品数量的整形数组
									N:
									while(true){
										System.out.println("请输入你要购买的产品编号: ");
										int gNo= input.nextInt();
										gNos[h] =gNo; //将输入的商品编号存入数组中
										if(!(gNo>0&&gNo<9)){
												System.out.println("您输入的商品编号不存在,请重新选择购买商品!");
												continue N ;
										}
										System.out.println("请输入你要购买的产品数量: ");
										int count = input.nextInt();
										gCounts[h] = count; //将输入的商品数量存入数组中
										System.out.println("是否继续购买?(输入y继续购买,输入其他结束购买)");
										String tr=input.next();
										if("y".equals(tr)){
											h++;
											/*for(int v1=0;v1<h;v1++){
												for(int v2=1;v2<=h;v2++){
													if(gNos[v2]==gNos[v1]){
														gCounts[v1] += gCounts[v2];
													}
												}
											}	*/
											
											continue;
										} //if
										
										//显示用户购买的商品信息以及付款情况
										for(int n=0 ;n<custNo.length;n++){ //循环语句查找所在客户编号的姓名
											if(cNo == custNo[n]){ //判断输入的客户编号与存入数组的编号是否相等
												System.out.println("尊敬的用户:" + custName[n]);
												break;
											}
										} 
										double totalPrice = 0;
										
									    System.out.println("产品名称\t产品单价\t 购买数量\t\t产品总价");
										
										for(int g=0;g<=h;g++){
											for(int r=0;r<goodsNo.length;r++){
												//查找gNos[]数组中的值与商品编号数组中相同,一致做输出
												if(gNos[g]==goodsNo[r]){ 
													System.out.println(goodsName[r]	+ "\t    ¥"	+ goodsPrice[r]+ "\t\t"	+ 
																			gCounts[g]+ "\t\t    ¥"+(float)(gCounts[g]*goodsPrice[r]));
													totalPrice += gCounts[g]*goodsPrice[r];
												}
											}
										}
										System.out.println();
										System.out.println("您购买的商品总价为: ¥"+(float)totalPrice);
										System.out.println("按您当前的积分,您的折扣为:0.85");							
										double pay = totalPrice * 0.85;	
										
										//提示用户付款,付款不够,继续付款
										System.out.println("您的应付款为: ¥"+(float)(pay));					
										System.out.print("请付款: ¥");
										N5:
										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 str6 = input.next();
										if (str6 != "\0") {
											continue N3;
										} //if
									} //N
								}else if (choice2 == 3) { //选项打开真情回馈页面
									N6:
									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 ch2 = input.nextInt();
										if (ch2 == 1) {
											int max=custScore[0]; //用“擂台法”求积分最高者,首先令第一个元素积分为最高
											int t =0;//存入最大积分的客户所在下标
											for(int c=1;c<custNo.length;c++){
												if(custScore[c]>max){ //比较max和其他元素的大小
													 //将新的较大积分元素存入max中,下次继续比较,直到找到最大积分者
													max = custScore[c];
													t = c;//将最大积分的下标存入t中
												}
											}
											System.out.println("积分最高的客户是:"+custName[t]);
											System.out.println("客户编号\t客户姓名\t客户生日\t\t客户积分 ");
											System.out.println(custNo[t]+"\t\t"+custName[t]+"\t\t"+
																								custBirth[t]+"\t\t"+custScore[t]+"\n");
											Random rand0 =new Random(); //设置随机变量
											int  randnum0 = rand0.nextInt(5);//随机分配0到5 之间的任意整数
											switch(randnum0){//设置五个奖品,随机抽取
												case 0:
													System.out.println("恭喜以上用户,获得一张价值¥50.00的话费充值卡");
													break;
												case 1:
													System.out.println("恭喜以上用户,获得一箱价值¥32.00的旺仔牛奶");
													break;
												case 2:
													System.out.println("恭喜以上用户,获得一瓶价值¥18.00的茶杯");
													break;
												case 3:
													System.out.println("恭喜以上用户,获得一盒价值¥7.80的脸盆");
													break;
												case 4:
													System.out.println("恭喜以上用户,获得一盒价值¥3.50的抽纸");
													break;
												default:
													break;
											}
											System.out.println("请按任意键继续!");
											String str7 = input.next();
											if (str7 != "\0") {
												continue;
											}
										}else if (ch2 == 2) {
											Random rand1 =new Random(); //产生随机数
											//随机数范围是从0到6之间的任意整数,添加了一名客户
											int randnum1 =rand1.nextInt(7);
											System.out.println("幸运客户是:"+custName[randnum1]);
											System.out.println("客户编号\t\t客户姓名\t\t客户生日 ");
											System.out.println(custNo[randnum1]+"\t\t\t"+custName[randnum1]
																												+"\t\t\t"+custBirth[randnum1]);
											Random rand2 =new Random(); 
											int  randnum2 = rand2.nextInt(7);
											switch(randnum2){ //设定五个奖品
												case 0:
													System.out.println("恭喜以上用户,获得一个价值¥30.00的暖瓶");
													break;
												case 1:
													System.out.println("恭喜以上用户,获得一袋价值¥22.00的立白洗衣粉");
													break;
												case 2:
													System.out.println("恭喜以上用户,获得一支价值¥13.50的黑人牙膏");
													break;
												case 3:
													System.out.println("恭喜以上用户,获得一瓶价值¥7.80的海天酱油");
													break;
												case 4:
													System.out.println("恭喜以上用户,获得一盒价值¥3.50的抽纸");
													break;
												default:
													System.out.println("很可惜,没有中奖,谢谢惠顾");
													break;
											}
											System.out.println("请按任意键继续!");
											String str8 = input.next();
											if (str8 != "\0") {
												continue;
											}
										}else if (ch2 == 3) {//日期还不太会用,直接输出,后期改善
											System.out.println("对不起,今天没有过生日的客户!");
											System.out.println("请按任意键继续!");
											String str9 = input.next();
											if (str9 != "\0") {
												continue;
											}
										}else if(ch2 == 4){
											continue N3;
										}else{
											System.out.println("没有此选项!");
											System.out.println("请按任意键继续!");
											String str = input.next();
											if (str != "\0") {
												continue;
											} //if
										} //else
									} // N6
								}else if(choice2 ==4){
									continue  N1;
								}else{
									System.out.println("没有此选项!");
									System.out.println("请按任意键继续!");
									String str = input.next();
									if (str != "\0") {
										continue;
									} //if
								} //else
							}  //N3
						}  //if
					}  //for
					if(f!=0){
						System.out.println("登录失败,请重新登陆. 你还有"+ f +"次登录机会");
						f--;
					}
					Logindex++;
					if(Logindex > 2){
						System.out.println("你登录系统已失败3次,请下次再来!");
						break N1;
					} //if
				} //N2
			}else if(choice1 == 2){
				N7:
				while(true){
					System.out.println("请输入用户名: ");
					String uname = input.next();
					System.out.println("请输入原密码: ");
					String  opassword = input.next();
					for(int e=0;e <username.length;e++){
						if(uname.equals(username[e])&&opassword.equals(password[e])){
							N8:
							while(true){
								System.out.println("请输入新密码: ");
								String upassword = input.next();
								System.out.println("请确认新密码: ");
								String upassword1 = input.next();
								if(upassword1.equals(upassword)){
									System.out.println("修改密码成功!");
									password[e] = upassword1;
									System.out.println("请按任意键继续!");
									String str = input.next();
									if (str != "\0"){
										continue N1;
									}
								}else{
									if(m==0){
										System.out.println("对不起,您的机会已经用完,你无权修改密码!");
										System.out.println("请按任意键继续!");
										String str = input.next();
										if (str != "\0"){
										continue N1;
										}
									}
									System.out.println("您两次输入密码不一致,你还有"+m+"次机会重新输入!");
									m--;
									continue N8;
								} //else
							} //N8
						} //if
					} //for
					if(f==0){
						System.out.println("你已输错3次,你无权操作!");
						System.out.println("请按任意键继续!");
						String str = input.next();
						if (str != "\0"){
							continue N1;
						}
					}
					System.out.println("你输入的用户名或密码错误,你还有"+ f +"次机会重新输入");
					f--;
					continue  N7;
				} //N7
			}else if (choice1 ==3) {/* 退出系统 */
				System.out.println("系统正在退出中......");
				System.exit(1);// 系统退出
			}else {/*没有这个选项 */
				System.out.println("没有这个选项,请重新登录");
				continue;
			} //else
		}  //N1 
	}  // main
} // Shopping3



	
			
	


			

	

对于此程序,有几点说明:

1.对于修改管理员密码,做了相应的补充,其余的没多大变化。

2.增加了重复购买商品的功能。

增加代码部分的实现界面如下:

1.修改管理员密码。(增加了判断功能,只有3次机会)


2.只有输入合适的用户名和密码才能修改。修改密码也是3次机会,输错无权修改。



3. 重复购买功能的运行界面。



评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值