使用IO完善快递管理系统
还记得之前的快递管理吗?我们将数据存储在集合中,在程序被关闭后,存储的数据就丢失了。下面我们来学习 IO,学习了 IO 以后,就可以使用 IO 技术将快递数据存储到文件中了。文件存储快递信息后,可以在每次启动应用时读取文件中的内容,从而实现程序数据的一直存在。 通过简单的思考与分析,以下是我的代码与思路。
欢迎与再见:
public void welcome() {
System.out.println("欢迎使用dbg快递柜");
}
public void bye() {
System.out.println("欢迎您的下次使用");
}
各种操作界面
初始菜单
//初始菜单
public int menu() {
System.out.println("请根据提示输入序号");
System.out.println("1、管理员登录");
System.out.println("2、普通用户登录");
String text = input.nextLine();
int num = -1;
try {
num = Integer.parseInt(text);
} catch (NumberFormatException e) {
}
if (num < 0 || num > 2) {
System.out.println("输入错误请重新输入");
return menu();
}
return num;
}
管理员操作界面
//管理员操作界面
public int aMenu() {
System.out.println("请根据提示输入序号");
System.out.println("1、快递录入");
System.out.println("2、删除快递");
System.out.println("3、查看所有快递");
System.out.println("0、返回上一级");
String text = input.nextLine();
int num = -1;
try {
num = Integer.parseInt(text);
} catch (NumberFormatException e) {
}
if (num < 0 || num > 3) {
System.out.println("输入错误请重新输入");
return aMenu();
}
return num;
}
用户操作界面
//用户操作界面
public int uMenu() {
System.out.println("请根据提示输入序号");
System.out.println("1、取出快递");
System.out.println("0、返回上一级");
String text = input.nextLine();
int num = -1;
try {
num = Integer.parseInt(text);
} catch (NumberFormatException e) {
}
if (num < 0 || num > 1) {
System.out.println("输入错误请重新输入");
return uMenu();
}
return num;
}
创建快递相关信息并将其录入
//创建快递柜坐标 //录入快递信息
public int size = 1;//总快递数
private Random random = new Random();//随机数
public boolean add() throws IOException {
System.out.println("请根据提示输入快递信息");
while (true) {
System.out.println("快递单号:");
String number = input.nextLine();
System.out.println("快递公司:");
String company = input.nextLine();
//随机生成两个下标
int x = random.nextInt(10);
int y = random.nextInt(10);
int code = code(); //获取随机取件码
if (x == 0 || y == 0) { //随即单号不能为0
continue;
}
if (findByNumber(number) == false || findByAxis(x, y) == false) { //判断单号与位置不重复
if (Full() == true) { //判断快递是否存满
System.out.println("快递已满");
} else {
String d = String.valueOf(x); //将获取的数值转换为String方便存储
String e = String.valueOf(y);
String f = String.valueOf(code);
Properties p = new Properties();
FileReader r = new FileReader("d://express.properties"); //读取文件夹
p.load(r); //写入p
if (p.get("Size") != null) {
size = (Integer.parseInt((String) p.get("Size"))) + 1; //快递数++
}
FileWriter fw = new FileWriter("d://express.properties");
String g = String.valueOf(size);
p.put("Number" + size, number); //分别存储数据
p.put("Company" + size, company);
p.put("xAxis" + size, d);
p.put("yAxis" + size, e);
p.put("Code" + size, f);
p.put("Size", g);
p.store(fw, "");
fw.close(); //关闭流
return false;
}
} else {
System.out.println("您输入的单号已存在,请重新输入");
continue;//结束本次循环重新输入。
}
}
}
查询快递信息
//查询所有快递 //显示快递信息
public void printAll() throws IOException {
FileReader r = new FileReader("d://express.properties");
Properties p = new Properties();
p.load(r); //读取文档信息
if (Empty() != true) { //判断是否为空
for (int i = 1; i <= Integer.parseInt((String) p.get("Size")); i++) { //遍历输出
System.out.println("第" + (p.get("xAxis" + i)) + "排, 第" + (p.get("yAxis" + i)) + "列快递,");
System.out.println("快递信息如下");
System.out.println("快递公司:" + p.get("Company" + i) + ", 快递单号:" + p.get("Number" + i) + ", 取件码:" + p.get("Code" + i));
}
} else {
System.out.println("无快递");
}
}
判断快递是否存在
public boolean Empty() throws IOException { //判断是否为空
FileReader r = new FileReader("d://express.properties");
Properties p = new Properties();
p.load(r);
if (p.get("Size") != null) { //文件为空
if (Integer.parseInt((String) p.get("Size")) == 0) { //快递数为0
return true;
}
return false;
}
return true;
}
判断快递是否存满
public boolean Full() throws IOException { //判断是否存满
FileReader r = new FileReader("d://express.properties");
Properties p = new Properties();
p.load(r);
if (p.get("Size") != null) { //文件数据不为空
if (Integer.parseInt((String) p.get("Size")) == 0) { //快递存在
return false;
}
if (p.get("Size").equals(100)) { //数值100
return true;
}
}
return false;
}
生成取件码
//生成一个不重复的取件码
private int code() throws IOException {
while (true) {
//随机100000-999999
int code = random.nextInt(900000) + 100000;
if (findByCode(code) == false) { //生成的取件码不重复
return code;
}
System.out.println("取件码:" + code);
}
}
根据坐标遍历查询
//根据坐标遍历查询
public boolean findByAxis(int x, int y) throws IOException {
FileReader r = new FileReader("d://express.properties");
Properties p = new Properties();
p.load(r);
for (int i = 1; i <= ((int) p.get("Size")); i++) { //遍历输出
if (p.get("xAxis" + i).equals(x) && p.get("yAxis" + i).equals(y)) { //x,y重复坐标存在
return false;
}
}
return true;
}
根据取件码查询快递
//根据取件码查询快递
public boolean findByCode(int code) throws IOException {
FileReader r = new FileReader("d://express.properties");
Properties p = new Properties();
p.load(r);
if (Empty() == false) {
for (int i = 1; i <= Integer.parseInt((String) p.get("Size")); i++) { //遍历输出
int c = Integer.parseInt((String) p.get("Code" + i));
if (c == code) { //取件码重复
return true;
}
}
}
return false;
}
根据单号查询快递
public boolean findByNumber(String number) throws IOException {
FileReader r = new FileReader("d://express.properties");
Properties p = new Properties();
p.load(r);
if (Empty() == false) {
if (p.get("Size") != null) {
for (int i = 1; i <= Integer.parseInt((String) p.get("Size")); i++) { //遍历输出
if (p.get("Number" + i).equals(number)) { //单号重复
return true;
}
}
}
}
return false;
}
根据单号操作快递
public String operate() {
System.out.println("请输入要操作的快递信息");
System.out.println("请输入快递单号");
String number = input.nextLine();
return number;//返回单号
}
用取件码取出快递
//根据取件码遍历数组并删除
public void uCode() throws IOException {
System.out.println("请输入取件码");
int code = Integer.parseInt(input.nextLine());
if (findByCode(code) == true) { //取件码存在
//遍历取出快递
FileReader r = new FileReader("d://express.properties");
Properties p = new Properties();
p.load(r);
for (int i = 1; i <= Integer.parseInt((String) p.get("Size")); i++) { //遍历输出
int c = Integer.parseInt((String) p.get("Code" + i));
if (c == code) {
delete(i);
System.out.println("快递以取出");
break;
}
}
} else {
System.out.println("快递不存在");
}
}
管理员删除单号相关快递信息
//根据单号查找并删除
public boolean deleteNumber() throws IOException {
String number = operate();
if (findByNumber(number) == true) { //单号存在
FileReader r = new FileReader("d://express.properties");
Properties p = new Properties();
p.load(r);
if (Empty() == false) {
for (int i = 1; i <= Integer.parseInt((String) p.get("Size")); i++) { //遍历删除
if (p.get("Number" + i).equals(number)) {
if (sure() == 1) {
delete(i);
System.out.println("快递已删除");
}
break;
}
}
}
} else {
System.out.println("查无此快递");
return false;
}
return true;
}
询问是否删除快递信息
public int sure() {
System.out.println("请确认是否删除");
System.out.println("1、确认删除");
System.out.println("0、取消");
int sure = Integer.parseInt(input.nextLine());
return sure;
}
删除快递
//删除快递的方法
public void delete(int i) throws IOException {
FileReader r = new FileReader("d://express.properties");
Properties p = new Properties();
p.load(r);
String g = String.valueOf((Integer.parseInt((String) p.get("Size")) - 1));
p.remove("Code" + i);//删除快递
p.remove("Number" + i);
p.remove("Company" + i);
p.remove("xAxis" + i);
p.remove("yAxis" + i);
p.remove("Code" + i);
p.put("Size", g);
FileWriter fw = new FileWriter("d://express.properties");
p.store(fw, "");
fw.close();
}
}
PS:
在将快递信息存入文件时,如果不对键进行处理那么会出现相同的键存在不同的值的情况。针对这种问题,我是将size信息与原来的键相结合,每存入一个快递,键就会发生改变,这样的方式还存在很大的缺陷。