java动手动脑-2024/9/27

复盘初试代码,解决了直接查询进度报错问题
增加了代码健壮性
PlanInformation

package Exam1;

public class PlanInformation {
    private int id; //表示日报流水号,依次加一
    private String planid; //表示产品生产批次号(例如:2312-110,有8 位字符组成,前四位表示年月,后三位表示序号)
    private String planname; //表示产品名称(例如:“表体”)
    private String process; //表示当前工序名称。(例如:“10.00 .射蜡”数字表示工序号,字符表示工序名称)
    private String nextprocess; //表示下一接收工序名称。(例如:“11.00 .修蜡”含义同上)
    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; //表示该日报记录的状态,0表示初始计划,1表示日报提交,2表示日报确认

    public PlanInformation() {
    }

    public PlanInformation(int id, String planid, String planname, String process, String nextprocess, String operator, String recipient, int plannumber, int innumber, int outnumber, int missnumber, int badnumber, int inspectednumber, int statement) {
        this.id = id;
        this.planid = planid;
        this.planname = planname;
        this.process = process;
        this.nextprocess = nextprocess;
        this.operator = operator;
        this.recipient = recipient;
        this.plannumber = plannumber;
        this.innumber = innumber;
        this.outnumber = outnumber;
        this.missnumber = missnumber;
        this.badnumber = badnumber;
        this.inspectednumber = inspectednumber;
        this.statement = statement;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getPlanid() {
        return planid;
    }

    public void setPlanid(String planid) {
        this.planid = planid;
    }

    public String getPlanname() {
        return planname;
    }

    public void setPlanname(String planname) {
        this.planname = planname;
    }

    public String getProcess() {
        return process;
    }

    public void setProcess(String process) {
        this.process = process;
    }

    public String getNextprocess() {
        return nextprocess;
    }

    public void setNextprocess(String nextprocess) {
        this.nextprocess = nextprocess;
    }

    public String getOperator() {
        return operator;
    }

    public void setOperator(String operator) {
        this.operator = operator;
    }

    public String getRecipient() {
        return recipient;
    }

    public void setRecipient(String recipient) {
        this.recipient = recipient;
    }

    public int getPlannumber() {
        return plannumber;
    }

    public void setPlannumber(int plannumber) {
        this.plannumber = plannumber;
    }

    public int getInnumber() {
        return innumber;
    }

    public void setInnumber(int innumber) {
        this.innumber = innumber;
    }

    public int getOutnumber() {
        return outnumber;
    }

    public void setOutnumber(int outnumber) {
        this.outnumber = outnumber;
    }

    public int getMissnumber() {
        return missnumber;
    }

    public void setMissnumber(int missnumber) {
        this.missnumber = missnumber;
    }

    public int getBadnumber() {
        return badnumber;
    }

    public void setBadnumber(int badnumber) {
        this.badnumber = badnumber;
    }

    public int getInspectednumber() {
        return inspectednumber;
    }

    public void setInspectednumber(int inspectednumber) {
        this.inspectednumber = inspectednumber;
    }

    public int getStatement() {
        return statement;
    }

    public void setStatement(int statement) {
        this.statement = statement;
    }

    /*public String toString() {
        return "PlanInformation{id = " + id + ", planid = " + planid + ", planname = " + planname + ", process = " + process + ", nextprocess = " + nextprocess + ", operator = " + operator + ", recipient = " + recipient + ", plannumber = " + plannumber + ", innumber = " + innumber + ", outnumber = " + outnumber + ", missnumber = " + missnumber + ", badnumber = " + badnumber + ", inspectednumber = " + inspectednumber + ", statement = " + statement + "}";
    }*/
}

PlanManagement

package Exam1;

import java.util.ArrayList;
import java.util.Scanner;

public class PlanManagement {

    static ArrayList<PlanInformation> planArr = new ArrayList<>();
    static String regex1 = "\\d{4}[-]\\d{3}";
    static Scanner sc = new Scanner(System.in);
    static String[] operatorName = {"柳泽羽","藤艺哲","郝一诺","杨松铎","刘宇恒","陶攀印","阎琪文","郝正一","刘静一"};
    static String[] work = {"射蜡","修蜡","面层","封浆","熔化","切割","精磨","调型","检验"};

    public static void main(String[] args) {
        while(true){
            showMenu();
            int choose = sc.nextInt();
            switch (choose){
                case 1 -> generatePlan();
                case 2 -> submitPlan();
                case 3 -> confirmPlan();
                case 4 -> statistics();
                default -> System.out.println("该选项不存在");
            }
        }

    }

    //生成计划
    public static void generatePlan(){
        headMenu();
        String planid,planname;
        int plannumber;
        while(true){
            System.out.print("产品批次:");
            planid = sc.next();
            System.out.print("产品名称:");
            planname = sc.next();
            System.out.print("计划数量:");
            plannumber = sc.nextInt();
            System.out.println("***********************************************************");
            if(planid.matches(regex1)){
                headMenu();
                System.out.print("产品批次:");
                System.out.println(planid);
                System.out.print("产品名称:");
                System.out.println(planname);
                System.out.print("计划数量:");
                System.out.println(plannumber);
                System.out.println("该产品入库操作已完成,是否提交(Y/N)");
                System.out.println("***********************************************************");
                String s = sc.next();
                if(s.equalsIgnoreCase("Y")){
                    PlanInformation p = new PlanInformation();
                    p.setOperator("");
                    p.setRecipient("");
                    p.setPlannumber(0);
                    p.setPlanid(planid);
                    p.setPlanname(planname);
                    p.setPlannumber(plannumber);
                    p.setStatement(0);
                    planArr.add(p);
                    System.out.println("入库成功!");
                }
                break;
            }
            else {
                System.out.println("录入错误,请重新录入信息");
                headMenu();
            }
        }
    }

    //提交日报
    public static void submitPlan(){
        headMenu();
        System.out.println("请输入8位产品批次号:");
        String planId = sc.next();
        System.out.println("***********************************************************");
        int index = isExist(planId);
        if(index != -1){
            headMenu();
            if(planArr.get(index).getStatement() == 0){
                String process,nextprocess;
                int innumber,outnumber,missnumber,badnumber,inspectednumber;
                innumber = planArr.get(index).getPlannumber();
                System.out.print("1、产品批次:");
                System.out.println(planArr.get(index).getPlanid());
                System.out.print("2、产品名称:");
                System.out.println(planArr.get(index).getPlanname());
                System.out.print("3、计划数量:");
                System.out.println(planArr.get(index).getPlannumber());
                System.out.print("4、当前工序:");
                process = sc.next();
                System.out.print("5、下一工序:");
                nextprocess = sc.next();
                System.out.print("6、操作员:");
                System.out.println(operatorName[seekOperator(process) - 1]);
                System.out.print("7、接收员:");
                System.out.println(operatorName[seekOperator(nextprocess) - 1]);
                System.out.print("8、上一道工序转入数量:");
                System.out.println(innumber);
                while (true) {
                    System.out.print("9、转出总数:");
                    outnumber = sc.nextInt();
                    System.out.print("10、丢失数量:");
                    missnumber = sc.nextInt();
                    System.out.print("11、废品数量:");
                    badnumber = sc.nextInt();
                    System.out.print("12、待检验数量:");
                    inspectednumber = sc.nextInt();
                    if(innumber == missnumber + badnumber + inspectednumber + outnumber){
                        System.out.println("是否将修改信息保存提交(Y/N)");
                        String s = sc.next();
                        if(s.equalsIgnoreCase("Y")){
                            planArr.get(index).setProcess(process);
                            planArr.get(index).setNextprocess(nextprocess);
                            planArr.get(index).setInnumber(innumber);
                            planArr.get(index).setOutnumber(outnumber);
                            planArr.get(index).setMissnumber(missnumber);
                            planArr.get(index).setOperator(operatorName[seekOperator(process) - 1]);
                            planArr.get(index).setRecipient(operatorName[seekOperator(nextprocess) - 1]);
                            planArr.get(index).setBadnumber(badnumber);
                            planArr.get(index).setInspectednumber(inspectednumber);
                            planArr.get(index).setStatement(1);
                            System.out.println("提交成功!");
                        }
                        break;
                    }
                    else {
                        System.out.println("录入错误信息,请重新输入三个数量");
                    }
                }

            }
            else if(planArr.get(index).getStatement() == 2){
                System.out.print("1、产品批次:");
                System.out.println(planArr.get(index).getPlanid());
                System.out.print("2、产品名称:");
                System.out.println(planArr.get(index).getPlanname());
                System.out.print("3、计划数量:");
                System.out.println(planArr.get(index).getPlannumber());
                System.out.print("4、当前工序:");
                System.out.println(planArr.get(index).getProcess());
                System.out.print("5、下一工序:");
                System.out.println(planArr.get(index).getNextprocess());
                System.out.print("6、操作员:");
                System.out.println(planArr.get(index).getOperator());
                System.out.print("7、接收员:");
                System.out.println(planArr.get(index).getRecipient());
                System.out.print("8、上一道工序转入数量:");
                System.out.println(planArr.get(index).getInnumber());
                while (true) {
                    int outnumber,missnumber,badnumber,inspectednumber;
                    System.out.print("9、转出总数:");
                    outnumber = sc.nextInt();
                    System.out.print("10、丢失数量:");
                    missnumber = sc.nextInt();
                    System.out.print("11、废品数量:");
                    badnumber = sc.nextInt();
                    System.out.print("12、待检验数量:");
                    inspectednumber = sc.nextInt();
                    if(planArr.get(index).getInnumber() == outnumber + missnumber + badnumber + inspectednumber){
                        System.out.println("是否将修改信息保存提交(Y/N)");
                        String s = sc.next();
                        if(s.equalsIgnoreCase("Y")){
                            planArr.get(index).setOutnumber(outnumber);
                            planArr.get(index).setMissnumber(missnumber);
                            planArr.get(index).setBadnumber(badnumber);
                            planArr.get(index).setInspectednumber(inspectednumber);
                            planArr.get(index).setStatement(1);
                            System.out.println("提交成功!");
                        }
                        break;
                    }
                    else {
                        System.out.println("录入错误信息,请重新输入三个数量");
                    }
                }
            }

        }
        else {
            System.out.println("库中没有该产品计划");
        }
        System.out.println("***********************************************************");
    }

    //确认日报
    public static void confirmPlan(){
        headMenu();
        System.out.println("请输入八位的产品批次:");
        String planid = sc.next();
        System.out.println("***********************************************************");
        int index = isExist(planid);
        if(index != -1 && planArr.get(index).getStatement() == 1){
            headMenu();
            System.out.print("1、产品批次:");
            System.out.println(planArr.get(index).getPlanid());
            System.out.print("2、产品名称:");
            System.out.println(planArr.get(index).getPlanname());
            System.out.print("3、计划数量:");
            System.out.println(planArr.get(index).getPlannumber());
            System.out.print("4、当前工序:");
            System.out.println(planArr.get(index).getProcess());
            System.out.print("5、下一工序:");
            System.out.println(planArr.get(index).getNextprocess());
            System.out.print("6、操作员:");
            System.out.println(planArr.get(index).getOperator());
            System.out.print("7、接收员:");
            System.out.println(planArr.get(index).getRecipient());
            System.out.print("8、上一道工序转入数量:");
            System.out.println(planArr.get(index).getInnumber());
            System.out.print("9、转出总数:");
            System.out.println(planArr.get(index).getOutnumber());
            System.out.print("10、丢失数量:");
            System.out.println(planArr.get(index).getMissnumber());
            System.out.print("11、废品数量:");
            System.out.println(planArr.get(index).getBadnumber());
            System.out.print("12、待检验数量:");
            System.out.println(planArr.get(index).getInspectednumber());
            System.out.println("***********************************************************");
            int i = seekOperator(planArr.get(index).getProcess());
            int j = seekOperator(planArr.get(index).getNextprocess());
                    System.out.println("是否确认结果(Y/N)");
                    String s = sc.next();
                    if(s.equalsIgnoreCase("Y")){
                        int outnumber = planArr.get(index).getOutnumber();
                        int missnumber = planArr.get(index).getMissnumber();
                        int badnumber = planArr.get(index).getBadnumber();
                        int inspectednumber = planArr.get(index).getInspectednumber();
                       planArr.get(index).setInnumber(outnumber - missnumber - badnumber - inspectednumber);
                        planArr.get(index).setProcess(work[i]);
                        planArr.get(index).setNextprocess(work[j]);
                        planArr.get(index).setOperator(operatorName[i]);
                        planArr.get(index).setRecipient(operatorName[j]);
                       planArr.get(index).setStatement(2);
                        System.out.println("确认成功!");
                        headMenu();
                        System.out.print("1、产品批次:");
                        System.out.println(planArr.get(index).getPlanid());
                        System.out.print("2、产品名称:");
                        System.out.println(planArr.get(index).getPlanname());
                        System.out.print("3、计划数量:");
                        System.out.println(planArr.get(index).getPlannumber());
                        System.out.print("4、当前工序:");
                        System.out.println(planArr.get(index).getProcess());
                        System.out.print("5、下一工序:");
                        System.out.println(planArr.get(index).getNextprocess());
                        System.out.print("6、操作员:");
                        System.out.println(planArr.get(index).getOperator());
                        System.out.print("7、接收员:");
                        System.out.println(planArr.get(index).getRecipient());
                        System.out.print("8、上一道工序转入数量:");
                        System.out.println(planArr.get(index).getInnumber());
                    }
                }
        else {
            System.out.println("库中没有该产品批次");
            confirmPlan();
        }

    }

    //统计进度
    public static void statistics(){
        for (int i = 0; i < planArr.size(); i++) {
            headMenu();
            System.out.print("1、产品批次:");
            System.out.println(planArr.get(i).getPlanid());
            System.out.print("2、产品名称:");
            System.out.println(planArr.get(i).getPlanname());
            System.out.print("3、计划数量:");
            System.out.println(planArr.get(i).getPlannumber());
            System.out.print("4.完成数量: ");
            if(planArr.get(i).getOperator().equals("检验")){
                System.out.println(planArr.get(i).getInnumber());
                System.out.print("5.剩余数量: ");
                System.out.println(planArr.get(i).getPlannumber() - planArr.get(i).getInnumber());
            }
            else {
                System.out.println(0);
                System.out.print("5.剩余数量: ");
                System.out.println(planArr.get(i).getPlannumber());
            }
            System.out.println("***********************************************************");
        }
    }

    //展示菜单
    public static void showMenu(){
        headMenu();
        System.out.println("                          1、生成计划");
        System.out.println("                          2、提交日报");
        System.out.println("                          3、确认日报");
        System.out.println("                          4、统计进度");
        System.out.println("***********************************************************");
    }

    //菜单头部
    public static void headMenu(){
        System.out.println("***********************************************************");
        System.out.println("              石家庄铁道大学无限23软件开发有限公司");
        System.out.println("                       Mes系统2023版");
        System.out.println("***********************************************************");
    }

    //判断产品批次号是否存在,返回位置
    public static int isExist(String id){
        for (int i = 0; i < planArr.size(); i++) {
            if(id.equals(planArr.get(i).getPlanid())){
                return i;
            }
        }
        return -1;
    }

    public static int seekOperator(String process){
        return process.charAt(0) - '0';
    }


}

枚举类

public class EnumTest {

    public static void main(String[] args) {
        Size s=Size.SMALL;
        Size t=Size.LARGE;
        //s和t引用同一个对象?
        System.out.println(s==t);  //
        //是原始数据类型吗?
        System.out.println(s.getClass().isPrimitive());
        //从字符串中转换
        Size u=Size.valueOf("SMALL");
        System.out.println(s==u);  //true
        //列出它的所有值
        for(Size value:Size.values()){
            System.out.println(value);
        }
    }

}
enum Size{SMALL,MEDIUM,LARGE};

输出

false
false
true
SMALL
MEDIUM
LARGE

结论:

  1. 枚举类型的每个枚举值都是一个单例对象,不同的枚举值代表不同的对象。
  2. 枚举类型不是原始数据类型,它是一种特殊的类类型。
  3. Size.valueOf("SMALL")方法通过传入字符串 "SMALL" 来获取对应的枚举值。
  4. Size.values()方法返回一个包含所有枚举值的数组。

原码,反码,补码

正数的原码 = 反码 = 补码
复数的补码 = 反码 + 1
计算机中的存储和计算都是以补码的形式进行的

public class a02Test2 {
    public static void main(String[] args) {
        int a = 100;
        int b = -50;
        System.out.println(a >> 1);
        System.out.println(b >> 1);
    }
}

输出

50
-25

java中的数是用二进制表示的


package TestCode3;

public class TestDouble {

    public static void main(String args[]) {
        System.out.println("0.05 + 0.01 = " + (0.05 + 0.01));
        System.out.println("1.0 - 0.42 = " + (1.0 - 0.42));
        System.out.println("4.015 * 100 = " + (4.015 * 100));
        System.out.println("123.3 / 100 = " + (123.3 / 100));
    }
}

输出

0.05 + 0.01 = 0.060000000000000005
1.0 - 0.42 = 0.5800000000000001
4.015 * 100 = 401.49999999999994
123.3 / 100 = 1.2329999999999999

import java.math.BigDecimal;

public class TestBigDecimal
{
    public static void main(String[] args)
    {
        BigDecimal f1 = new BigDecimal("0.05");
        BigDecimal f2 = BigDecimal.valueOf(0.01);
        BigDecimal f3 = new BigDecimal(0.05);
        System.out.println("下面使用String作为BigDecimal构造器参数的计算结果:");
        System.out.println("0.05 + 0.01 = " + f1.add(f2));
        System.out.println("0.05 - 0.01 = " + f1.subtract(f2));
        System.out.println("0.05 * 0.01 = " + f1.multiply(f2));
        System.out.println("0.05 / 0.01 = " + f1.divide(f2));
        System.out.println("下面使用double作为BigDecimal构造器参数的计算结果:");
        System.out.println("0.05 + 0.01 = " + f3.add(f2));
        System.out.println("0.05 - 0.01 = " + f3.subtract(f2));
        System.out.println("0.05 * 0.01 = " + f3.multiply(f2));
        System.out.println("0.05 / 0.01 = " + f3.divide(f2));
    }
}

输出

下面使用String作为BigDecimal构造器参数的计算结果:
0.05 + 0.01 = 0.06
0.05 - 0.01 = 0.04
0.05 * 0.01 = 0.0005
0.05 / 0.01 = 5
下面使用double作为BigDecimal构造器参数的计算结果:
0.05 + 0.01 = 0.06000000000000000277555756156289135105907917022705078125
0.05 - 0.01 = 0.04000000000000000277555756156289135105907917022705078125
0.05 * 0.01 = 0.0005000000000000000277555756156289135105907917022705078125
0.05 / 0.01 = 5.000000000000000277555756156289135105907917022705078125

double 类型是采用二进制的科学计数法来存储数值的。具体来说:

  1. 它用 1 位表示符号位,11 位表示指数位,52 位表示尾数位。
  2. 这种存储方式决定了它不能精确表示所有的十进制小数。例如,十进制的 0.1 在二进制中是一个无限循环小数,只能用近似值来表示。

package TestCode3;

public class Test1 {
    public static void main(String[] args) {
        int X=100;
        int Y=200;
        System.out.println("X+Y="+X+Y);
        System.out.println(X+Y+"=X+Y");
    }
}

输出

X+Y=100200
300=X+Y
字符串拼接-从前往后


生成100以内随机算式

import java.util.Random;

public class randomMath {
    public static void main(String[] args) {
        Random ch = new Random();
        Random r = new Random();
        Character[] chArr = {'+','-','*','/'};

        for (int i = 0; i < 30; i++) {
            int first = r.nextInt(100);
            int second = r.nextInt(99)+1;
            char c = chArr[ch.nextInt(4)];
            System.out.print(first);
            System.out.print(c);
            System.out.println(second + " = ");
        }
    }
}
posted @ 2024-09-27 22:34  XYu1230  阅读(9)  评论(0编辑  收藏  举报