考试改进代码

import java.util.Scanner;
import java.util.regex.Pattern;

class PlanInformation {
private static int idCounter = 1; // 用于生成唯一的id
private int id; // 日报流水号
private String planid; // 产品生产批次号
private String planname; // 产品名称
private String process; // 当前工序名称
private String nextprocess; // 下一工序名称
private String operator; // 当前工序操作者
private String recipient; // 下一道工序的接受者
private int plannumber; // 要求产品数量
private int innumber; // 上一道工序接收的合格产品总数
private int outnumber; // 当前工序的产品转出总数
private int missnumber; // 当前工序的产品丢失数量
private int badnumber; // 当前工序的产品废品数量
private int inspectednumber; // 当前工序的产品待检验数量
private int statement; // 日报记录状态

public PlanInformation(String planid, String planname, int plannumber) {
    this.id = idCounter++;
    this.planid = planid;
    this.planname = planname;
    this.plannumber = plannumber;
    this.statement = 0; // 初始状态
}

// Getters and Setters
public int getId() { return id; }
public String getPlanid() { return planid; }
public String getPlanname() { return planname; }
public int getPlannumber() { return plannumber; }
public int getInnnumber() { return innumber; }
public int getOutnumber() { return outnumber; }
public int getMissnumber() { return missnumber; }
public int getBadnumber() { return badnumber; }
public int getInspectednumber() { return inspectednumber; }
public int getStatement() { return statement; }

public void setInnnumber(int innumber) { this.innumber = innumber; }
public void setOutnumber(int outnumber) { this.outnumber = outnumber; }
public void setMissnumber(int missnumber) { this.missnumber = missnumber; }
public void setBadnumber(int badnumber) { this.badnumber = badnumber; }
public void setInspectednumber(int inspectednumber) { this.inspectednumber = inspectednumber; }
public void setStatement(int statement) { this.statement = statement; }
public void setProcess(String process) { this.process = process; }
public void setNextprocess(String nextprocess) { this.nextprocess = nextprocess; }
public void setOperator(String operator) { this.operator = operator; }
public void setRecipient(String recipient) { this.recipient = recipient; }

}

public class PlanManagement {
private PlanInformation[] items = new PlanInformation[5];
private int itemCount = 0; // 实际存储的商品数量
private Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {
    PlanManagement manager = new PlanManagement();
    manager.showMainMenu();
}

public void showMainMenu() {
    while (true) {
        printHeader();
        System.out.println("1、生成计划");
        System.out.println("2、提交日报");
        System.out.println("3、确认日报");
        System.out.println("4、统计进度");
        System.out.print("请输入操作选项:");
        int choice = scanner.nextInt();
        scanner.nextLine(); // Consume newline

        switch (choice) {
            case 1:
                generatePlan();
                break;
            case 2:
                submitReport();
                break;
            case 3:
                confirmReport();
                break;
            case 4:
                displayProgress();
                break;
            default:
                System.out.println("错误:该选项不存在。");
        }
    }
}

private void printHeader() {
    System.out.println("***********************************************************");
    System.out.println("石家庄铁道大学无限23软件开发有限公司");
    System.out.println("Mes系统2023版");
    System.out.println("***********************************************************");
}

private void generatePlan() {
    printHeader();
    System.out.print("产品批次:");
    String planid = scanner.nextLine();
    while (!isValidPlanId(planid)) {
        System.out.println("录入错误");
        System.out.print("产品批次:");
        planid = scanner.nextLine();
    }

    System.out.print("产品名称:");
    String planname = scanner.nextLine();

    System.out.print("计划数量:");
    int plannumber = scanner.nextInt();
    scanner.nextLine(); // Consume newline

    if (itemCount < items.length) {
        items[itemCount++] = new PlanInformation(planid, planname, plannumber);
        System.out.println("该产品入库操作已完成,是否提交(Y/N)");
        char choice = scanner.nextLine().charAt(0);
        if (choice == 'Y' || choice == 'y') {
            System.out.println("产品信息已保存。");
        } else {
            itemCount--; // 取消入库
            System.out.println("入库操作已取消。");
        }
    } else {
        System.out.println("仓库已满,无法存放更多商品。");
    }
}

private void submitReport() {
    printHeader();
    System.out.print("请输入产品批次号:");
    String planid = scanner.nextLine();

    PlanInformation item = findItemByPlanId(planid);
    if (item == null) {
        System.out.println("库中没有该产品计划。");
        return;
    }

    if (item.getStatement() == 0) {
        System.out.println("产品批次:" + item.getPlanid());
        System.out.println("产品名称:" + item.getPlanname());
        System.out.println("计划数量:" + item.getPlannumber());

        // 录入其他信息
        System.out.print("当前工序:");
        String process = scanner.nextLine();
        item.setProcess(process);

        System.out.print("下一工序:");
        String nextprocess = scanner.nextLine();
        item.setNextprocess(nextprocess);

        System.out.print("操作员:");
        String operator = scanner.nextLine();
        item.setOperator(operator);

        System.out.print("接收员:");
        String recipient = scanner.nextLine();
        item.setRecipient(recipient);

        // 录入数量信息
        System.out.print("上一道工序转入数量:");
        int innumber = scanner.nextInt();
        item.setInnnumber(innumber);

        System.out.print("转出总数:");
        int outnumber = scanner.nextInt();
        item.setOutnumber(outnumber);

        System.out.print("丢失数量:");
        int missnumber = scanner.nextInt();
        item.setMissnumber(missnumber);

        System.out.print("废品数量:");
        int badnumber = scanner.nextInt();
        item.setBadnumber(badnumber);

        System.out.print("待检验数量:");
        int inspectednumber = scanner.nextInt();
        item.setInspectednumber(inspectednumber);

        // 计算上一道工序转入数量
        if (innumber != (outnumber + missnumber + badnumber + inspectednumber)) {
            System.out.println("录入错误,上一道工序转入数量不符合规则。");
            return;
        }

        item.setStatement(1); // 设置状态为1
        System.out.println("日报提交成功。");
    } else {
        System.out.println("该日报已提交,无法再次提交。");
    }
}

private void confirmReport() {
    printHeader();
    System.out.print("请输入产品批次:");
    String planid = scanner.nextLine();

    PlanInformation item = findItemByPlanId(planid);
    if (item == null) {
        System.out.println("库中没有该产品批次。");
        return;
    }

    if (item.getStatement() == 1) {
        System.out.println("产品批次:" + item.getPlanid());
        System.out.println("产品名称:" + item.getPlanname());
        System.out.println("计划数量:" + item.getPlannumber());
        System.out.println("当前工序:" + item.getProcess());
        System.out.println("下一工序:" + item.getNextprocess());
        System.out.println("操作员:" + item.getOperator());
        System.out.println("接收员:" + item.getRecipient());
        System.out.println("上一道工序转入数量:" + item.getInnnumber());
        System.out.println("转出总数:" + item.getOutnumber());
        System.out.println("丢失数量:" + item.getMissnumber());
        System.out.println("废品数量:" + item.getBadnumber());
        System.out.println("待检验数量:" + item.getInspectednumber());

        System.out.print("确认结果(Y/N):");
        char choice = scanner.nextLine().charAt(0);
        if (choice == 'Y' || choice == 'y') {
            item.setStatement(2); // 设置为确认状态
            System.out.println("日报确认成功。");
        } else {
            System.out.println("确认操作已取消。");
        }
    } else {
        System.out.println("该日报未提交,无法确认。");
    }
}

private void displayProgress() {
    printHeader();
    System.out.println("进度统计:");
    for (int i = 0; i < itemCount; i++) {
        PlanInformation item = items[i];
        int completedQuantity = item.getOutnumber(); // 以最后一道工序转出数量为准
        int remainingQuantity = item.getPlannumber() - completedQuantity;

        System.out.println("产品批次:" + item.getPlanid());
        System.out.println("产品名称:" + item.getPlanname());
        System.out.println("计划数量:" + item.getPlannumber());
        System.out.println("完成数量:" + completedQuantity);
        System.out.println("剩余数量:" + remainingQuantity);
        System.out.println("**********************************************************");
    }
}

private PlanInformation findItemByPlanId(String planid) {
    for (PlanInformation item : items) {
        if (item != null && item.getPlanid().equals(planid)) {
            return item;
        }
    }
    return null;
}

private boolean isValidPlanId(String planid) {
    return Pattern.matches("^\\d{4}-\\d{3}$", planid);
}

}

posted @ 2024-09-28 10:08  Jacksoner  阅读(3)  评论(0编辑  收藏  举报