使用Java语言模拟超市储物箱案例

来源:互联网 发布:js array push splice 编辑:程序博客网 时间:2024/06/12 01:11

需求:有存包和取包两种操作。 系统初始化有一系列的储物格, 存包动作生一个密码, 并占用一个储物格; 取包时验证输入储物格编号以及密码,正确则清空储物格。

import java.util.Random;import java.util.Scanner;public class SupermarketStoreBox {public static int[] boxex = new int[24];static Scanner scan = new Scanner(System.in);public static void main(String[] args) {welcome();}/** * 定义一个画箱子的方法 */public static void drawBoxex() {for (int i = 0; i < 12; i++) {// 遍历第一行的12个箱子if (boxex[i] == 0) {// 判断箱子中是否有东西System.out.print(" □ ");// 打印空箱子} else {// 箱子中有东西System.out.print(" ■ ");// 打印实心箱子}}System.out.println();for (int j = 1; j <= 12; j++) {System.out.printf("%2d ", j);// 给前12个箱子编号}System.out.println();for (int x = 12; x < boxex.length; x++) {// 打印后12个箱子if (boxex[x] == 0) {// 判断箱子中是否有东西System.out.print(" □ ");} else {// 箱子中有东西System.out.print(" ■ ");}}System.out.println();for (int y = 13; y <= 24; y++) {System.out.printf("%2d ", y);// 给后12个箱子编号}System.out.println();}/** * 存包方法:判断箱子是否已存东西,如果没有存东西,提示用户箱子可以使用 并且生成密码,  * 已经存东西了,继续查找空箱子,如果没有找到空箱子, * 提示用户箱子已经满了,请稍后。 *//** * 定义一个存包的方法 */public static void put() {Random rd = new Random();for (int i = 0; i <= 23; i++) {if (boxex[i] == 0) {// 箱子是空的boxex[i] = rd.nextInt(8999) + 1000;// 生成密码System.out.println((i + 1) + "号箱子可以使用");System.out.println("这是您的密码:" + boxex[i] + ",切勿告诉他人~");break;}if (i == 23) {System.out.println("箱子已经满了,请稍后!");}}}/** * 定义一个取包的方法 */public static void get() {int count = 0;//计数器,记录密码输错次数System.out.println("请输入箱号:");int i = scan.nextInt() - 1;if (i > 23) {System.out.println("不存在该储物箱子");} else {if (boxex[i] == 0) {System.out.println("该箱子中并没有东西!");} else {while (true) {System.out.println("请输入箱子密码:");int pwd = scan.nextInt();if (boxex[i] == pwd) {System.out.println((i + 1) + "号箱子打开,请取走您的贵重物品!");boxex[i] = 0;break;} else {count++;System.out.println("密码错误,请重新输入!");if (count >= 3) {System.out.println("密码输错3次,储物箱被锁定,请联系前台服务人员解决。");return;}}}}}}/** * 欢迎界面 */public static void welcome() {System.out.println("\t------------储物箱管理系统------------\n\n");System.out.println("\t  ╱╲╱╲ ╰★╮【储】╭★╯  ╱╲╱╲      \n");System.out.println("\t   ╲欢╲╱ ╰☆╮【物】╭☆╯  ╲╱欢╱      \n");System.out.println("\t   ╱╲迎╲  ╰★╮【管】╭★╯  ╱迎╱╲      \n");System.out.println("\t  ╱╲╱╲ ╰☆╮【理】╭☆╯    ╱╲╱╲   \n\n");operate();}/** * 操作方法 */public static void operate() {while (true) {drawBoxex();System.out.println("\t--------------------------------------\n");System.out.println("\t     1、存包     2、取包     0、退出");System.out.println("请选择操作:");int select = scan.nextInt();switch (select) {case 1:put();break;case 2:get();break;case 0:exit();break;default:System.out.println("输入错误");break;}}}/** * 退出方法 */public static void exit() {System.out.println("欢迎下次再来,Bye Bye!!!");System.exit(0);}}


0 0
原创粉丝点击