java写快递柜管理系统

一、概要

这里是用java写的一个快递柜管理系统,分为寄存和取出两个操作,寄存的时候快递柜有大、中、小之分,需要自己选择。成功的寄存之后,会生成一个6位数的取件码。可以凭借这个取件码完成取件(也就是快递员把快递放到快递柜里面之后,给你发一个取件码让你去取件)。

备注:在这我附加了一个功能,就是把每一次的操作都记录在了一个文件里面,用的是BufferedWriter类。

二、实现

话不多少,直接上代码,你们复制粘贴到你们写的类里面,点击运行就OK了。

package com.exp;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Random;
import java.util.Scanner;

public class Cabinet {
	public static int[] location = new int[36];
	static Scanner scan = new Scanner(System.in);

	public static void main(String[] args) {
		welcome();

	}

	public static void write(String text) {
		try {
			File file = new File("D:/CabinetOpeWrite.txt");
			Writer fw = new FileWriter(file, true);
			// 创建一个字符输出流对象
			BufferedWriter pw = new BufferedWriter(fw);
			// 输出文字到文件中
			pw.write(text);
			// 刷新流
			pw.flush();
			// 关闭流
			pw.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 定义一个画格子的方法
	 */
	public static void drawlocation() {
		System.out.println("大格子:");
		for (int i = 0; i < 12; i++) {// 遍历第一行的12个格子
			if (location[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();

		System.out.println("中格子:");
		for (int x = 12; x < 24; x++) {// 打印中间12个格子
			if (location[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();

		System.out.println("小格子:");
		for (int x = 24; x < 36; x++) {// 打印后12个格子
			if (location[x] == 0) {// 判断格子中是否有东西
				System.out.print(" □ ");
			} else {// 格子中有东西
				System.out.print(" ■ ");
			}
		}
		System.out.println();
		for (int y = 25; y <= 36; y++) {
			System.out.printf(" %2d ", y);// 给后12个格子编号
		}
		System.out.println();
	}

	/**
	 * 定义一个放快递的方法
	 */
	public static void deposit() {
		Random rd = new Random();

		System.out.println("请输入格子大小(1、大格子,2、中格子,3、小格子):");
		write("您正在存快递,请选择格子大小!(1、大格子,2、中格子,3、小格子)\r\n");
		int size = scan.nextInt();
		if (size == 1) {
			System.out.println("您选择了大格子:");
			write("您选择了大格子\r\n");
			for (int i = 0; i <= 11; i++) {
				if (location[i] == 0) {// 格子是空的
					location[i] = rd.nextInt(999999) + 100000;// 生成密码
					System.out.println();
					System.out.println("这是您的密码:" + location[i] + ",切勿告诉他人~");
					write((i + 1) + "号格子可以使用,这是您的密码:" + location[i] + ",切勿告诉他人~\r\n");
					break;
				}
				if (i == 11) {
					System.out.println("格子已经满了,请稍后!");
					write("格子已经满了,请稍后!\r\n");
				}
			}
		} else if (size == 2) {
			System.out.println("您选择了中格子:");
			write("您选择了中格子\r\n");
			for (int i = 12; i <= 23; i++) {
				if (location[i] == 0) {// 格子是空的
					location[i] = rd.nextInt(999999) + 100000;// 生成密码
					System.out.println((i + 1) + "号格子可以使用");
					System.out.println("这是您的密码:" + location[i] + ",切勿告诉他人~");
					write((i + 1) + "号格子可以使用,这是您的密码:" + location[i] + ",切勿告诉他人~\r\n");
					break;
				}
				if (i == 23) {
					System.out.println("格子已经满了,请稍后!");
					write("格子已经满了,请稍后!\r\n");
				}
			}
		} else if (size == 3) {
			System.out.println("您选择了小格子:");
			write("您选择了小格子\r\n");
			for (int i = 24; i <= 35; i++) {
				if (location[i] == 0) {// 格子是空的
					location[i] = rd.nextInt(999999) + 100000;// 生成密码
					System.out.println((i + 1) + "号格子可以使用");
					System.out.println("这是您的密码:" + location[i] + ",切勿告诉他人~");
					write((i + 1) + "号格子可以使用,这是您的密码:" + location[i] + ",切勿告诉他人~\r\n");
					break;
				}
				if (i == 35) {
					System.out.println("格子已经满了,请稍后!");
					write("格子已经满了,请稍后!\r\n");
				}
			}
		} else {
			System.out.println("您的输入有误,请重新运行系统!");
			write("您的输入有误,请重新运行系统!\r\n");
			System.exit(0);
		}
	}

	/**
	 * 定义一个取快递的方法
	 */
	public static void pickUp() {
		int count = 0;// 计数器,记录密码输错次数
		System.out.println("请输入箱号:");
		write("您正在取快递,请输入箱号!\r\n");
		int i = scan.nextInt() - 1;
		if (i > 35) {
			System.out.println("不存在该储物格子");
			write("不存在该储物格子\r\n");
		} else {
			if (location[i] == 0) {
				System.out.println("该格子中并没有东西!");
				write("该格子中并没有东西!\r\n");
			} else {
				while (true) {
					System.out.println("请输入格子密码:");
					write("请输入格子密码:\r\n");
					int pwd = scan.nextInt();
					if (location[i] == pwd) {
						System.out.println((i + 1) + "号格子打开,请取走您的贵重物品!");
						write((i + 1) + "号格子打开,请取走您的贵重物品!\r\n");
						location[i] = 0;
						break;
					} else {
						count++;
						System.out.println("密码错误,请重新输入!");
						write("密码错误,请重新输入!\r\n");
						if (count >= 3) {
							System.out.println("密码输错3次,储物箱被锁定,请联系前台服务人员解决。");
							write("密码输错3次,储物箱被锁定,请联系前台服务人员解决。\r\n");
							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");
		write("欢迎光临快递柜管理系统!\r\n");
		operate();
	}

	/**
	 * 操作方法
	 */
	public static void operate() {
		while (true) {
			drawlocation();
			System.out.println("\t--------------------------------------\n");
			System.out.println("\t     1、存包     2、取包     0、退出");
			System.out.println("请选择操作:");
			int select = scan.nextInt();
			write("1、存包     2、取包     0、退出,您选择了" + select + "。\r\n");
			switch (select) {
			case 1:
				deposit();
				break;
			case 2:
				pickUp();
				break;
			case 0:
				exit();
				break;
			default:
				System.out.println("输入错误");
				write("输入错误\r\n");
				break;
			}
		}
	}

	/**
	 * 退出方法
	 */
	public static void exit() {
		System.out.println("欢迎下次再来,Bye Bye!!!");
		write("欢迎下次再来,Bye Bye!!!\r\n");
		System.exit(0);

	}

}

三、运行

在这里插入图片描述
运行之后你会在控制台看到这样的操作提示,根据提示操作就可以了。

备注:记录你自己每次操作的文件的目录在D盘下CabinetOpeWrite.txt这个文件里面。这个目录代码里面有,名称和地址都可以修改。

发布了31 篇原创文章 · 获赞 13 · 访问量 7929

猜你喜欢

转载自blog.csdn.net/weixin_42322648/article/details/103575005