Java大作业--快递柜管理系统

import java.io.*;
import java.util.ArrayList;
import java.util.Objects;
import java.util.Random;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

public class Main {
    public static void main(String[] args) throws IOException, InterruptedException {
        System.out.println("********************************************************");
        System.out.println("姓名:");
        System.out.println("班级:2020级电子信息工程技术C班");
        System.out.println("学号:");
        System.out.println("********************************************************");
        System.out.println("管理员初始密码为:8f48aeg45");
        System.out.println("********************************************************");
        Scanner input = new Scanner(System.in);
        BoxList box_obj = new BoxList();
        for (;;)
        {
            System.out.print("1、以用户身份登录\n2、以管理员身份登录\n3、退出\n请选择以哪个身体登录:");
            int e_sele = input.nextInt();
            if (e_sele == 1){
                User user = new User();
                user.take_courier(box_obj);
            }
            if (e_sele == 2){
                Admin ad_user = new Admin();
                System.out.print("输入密码:");
                if (!ad_user.pass.equals(input.next())){
                    System.out.println("密码不正确");
                    continue;
                }
                for (;;){
                    System.out.print("1、显示柜子信息\n2、开柜门\n3、取快递\n4、存快递\n5、修改密码\n6、返回普通用户模式\n请选择你需要的功能:");
                    int s = input.nextInt();
                    if (s == 1){
                        ad_user.show(box_obj);
                    }
                    if (s == 2){
                        ad_user.open_door(box_obj);
                    }
                    if (s == 3){
                        ad_user.take_courier(box_obj);
                    }
                    if (s == 4){
                        ad_user.append(box_obj);
                    }
                    if (s == 5){
                        ad_user.change_pass();
                    }
                    if (s == 6){
                        break;
                    }
                }
            }
            if (e_sele == 3){
                box_obj.save();
                System.out.println("系统退出");
                break;
            }
            else {
                System.out.println("输入有误");
            }
        }
    }
}

class Box{
    protected String number;
    protected String pass;
    protected boolean state;
    protected boolean door;   //true open      false close

    public String toString(){
        String str = new String();
        str += number;str += " ";
        str += (door)?"1":"0";str += " ";
        str += (state)?"1":"0";str += " ";
        str += pass;str += "\n";
        return str;
    }

    public void show(){
        if (state)
            System.out.println("柜子编号:"+number+"\t取货码:"+pass+"\t"+((door)?"门开":"门关"));
        else
            System.out.println("柜子编号:"+number+"\t无快递");
    }
}

class FileFlush{
    public void clear(String path) throws IOException {
        final File file = new File(path);
        try {
            FileWriter fileWriter = new FileWriter(file);
            fileWriter.write("");
            fileWriter.flush();
            fileWriter.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

class BoxList extends FileFlush{
    ArrayList<Box> list;
    File list_txt;

    public BoxList() throws IOException {   //构造函数先读取出文件中的列表
        list_txt = new File(".\\list.txt");
        if (!list_txt.exists()) {
            System.out.println("打开失败");
            return;
        }
        // 编号   门状态    是否有快递    密码(取件码)
        BufferedReader list_txt_line_read = new BufferedReader(new FileReader(list_txt));
        String str = null;
        list = new ArrayList<>();
        while ((str = list_txt_line_read.readLine()) != null){
            Box t = new Box();
            String[] strList = str.split(" ");
            t.number = strList[0];
            t.door = Objects.equals(strList[1], "1");
            t.state = Objects.equals(strList[2], "1");
            t.pass = strList[3];
            list.add(t);
        }
    }

    public void save() throws IOException {
        File file = new File(".\\list.txt");
        try {
            FileWriter fileWriter = new FileWriter(file);
            for (Box b:list)
                fileWriter.write(b.toString());
            fileWriter.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    //true 存在   false 不存在
    public boolean check_pass(String pa){   //检查取件码是否已经存在
        for (Box b:list)
            if (b.pass.equals(pa)) return true;
        return false;
    }

    public int index(String pa) {   //通过取件码查询数组索引
        int ind = 0;
        for (Box b:list){
            if (b.pass.equals(pa) || b.number.equals(pa)) return ind;
            ind ++;
        }
        return 200;
    }

    public void show(){
        for (Box b:list) b.show();
    }
}

class User extends FileFlush{

    void open_door(BoxList boxlist, int ind){
        if (!boxlist.list.get(ind).door) {
            System.out.println("柜门开启!!!!!");
            boxlist.list.get(ind).door = true;
        }else
            System.out.println("柜门已经处于开启状态");
    }

    void close_door(BoxList boxlist, int ind){
        if (boxlist.list.get(ind).door) {
            System.out.println("柜门关闭!!!!!");
            boxlist.list.get(ind).door = false;
        }else
            System.out.println("柜门已经处于关闭状态");
    }
    public void take_courier(BoxList boxlist) throws InterruptedException {
        Scanner input = new Scanner(System.in);
        System.out.print("输入取件码:");
        String in = input.next();
        if (!boxlist.check_pass(in)){
            System.out.println("取件码不存在");
            return;
        }else{
            int ind = boxlist.index(in);
            open_door(boxlist, ind);
            delay(5);
            boxlist.list.get(ind).state = false;
            close_door(boxlist, ind);
        }
    }

    public void delay(int t) throws InterruptedException {
        for(int i=0;i<t;i++){
            System.out.println("还剩"+(t-i)+"秒关闭");
            TimeUnit.MILLISECONDS.sleep(1000);
        }
    }
}

class Admin extends User{
    public String pass;
    public Admin() throws IOException {
        File pass_file = new File(".\\pass.txt");
        BufferedReader pass_read = new BufferedReader(new FileReader(pass_file));
        pass = pass_read.readLine();
    }

    public void change_pass() throws IOException {
        System.out.print("输入新密码:");
        clear(".\\pass.txt");
        Scanner input = new Scanner(System.in);
        String new_pass = input.next();
        File pass_file = new File(".\\pass.txt");
        try {
            FileWriter fileWriter = new FileWriter(pass_file);
            fileWriter.write(new_pass);
            fileWriter.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    private int input_number(BoxList boxlist){
        Scanner input = new Scanner(System.in);
        System.out.print("输入柜门编号:");
        int ind = input.nextInt();
        if (ind > boxlist.list.size()+1 || ind <= 0) return -1;
        ind -= 2;
        return ind;
    }

    public void open_door(BoxList boxlist) throws InterruptedException {
        int ind = input_number(boxlist);
        if (ind == -1) return;
        open_door(boxlist, ind);
        delay(5);
        close_door(boxlist, ind);
    }

    public void show(BoxList boxlist){
        boxlist.show();
    }

    public void append(BoxList boxlist){
        int ind = input_number(boxlist);
        if (ind == -1) return;
        if (boxlist.list.get(ind).state){
            System.out.println("已有快递不可添加");
            return;
        }
        boxlist.list.get(ind).door = false;
        boxlist.list.get(ind).state = true;
        boxlist.list.get(ind).pass = generate_pass();
        System.out.println("添加成功");
    }

    private String generate_pass(){
        String str = new String();
        Random ran = new Random();
        for (int i=0;i<6;i++)
            str += (ran.nextInt(10));
        return str;
    }
}

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值