java实例 物流信息管理 及 map的简单使用

一.物流信息管理

import java.io.*;
import java.util.Vector;

// 读取货物处理方案
class Transactions
{
    FileReader tra_fileReader;                     // 处理方案文件
    BufferedReader tra_in;                         // 读取方案流
    String goodinformationA[] = new String[250];   // A 类物流信息
    String goodinformationR[] = new String[250];   // R 类物流信息
    String goodinformationO[] = new String[250];   // O 类物流信息
    String goodinformationD[] = new String[250];   // D 类物流信息
    int Acount = 0;                                // A 类物流信息数
    int Rcount = 0;                                // R 类物流信息数
    int Ocount = 0;                                // O 类物流信息数
    int Dcount = 0;                                // D 类物流信息数
    String inv[];                                  // 库存信息
    int invcount;                                  // 货物种类数量
    String errormessage[] = new String[1000];      // 报错信息
    int errorcount = 0;                            // 出错信息数

    // 读取Transactions.txt
    public Transactions(String inv[], int invcount)
    {
        this.inv = inv;
        this.invcount = invcount;

        try
        {
            // 读取货物处理方案信息
            tra_fileReader = new FileReader("C:\\Users\\dell\\Desktop\\2019215053 杨雨润 实验三\\Inventory.java\\Transactions.txt");
            tra_in = new BufferedReader(tra_fileReader);
            String inf;
            while((inf = tra_in.readLine()) != null)
            {
                // 给物流信息分类 这里的判断必须用 equals
                String trag[] = inf.split("\t");
                if(trag[0].equals( "A" ))
                    goodinformationA[Acount++] = inf;
                else if(trag[0].equals( "R" ))
                    goodinformationR[Rcount++] = inf;
                else if(trag[0].equals( "O" ))
                    goodinformationO[Ocount++] = inf;
                else if(trag[0].equals( "D" ))
                    goodinformationD[Dcount++] = inf;
            }

            // 关闭流
            tra_in.close();
        }
        catch (IOException TraIOE)
        {
            System.out.println(TraIOE.getMessage());
        }
    }

    // 处理事件 A,添加货物
    public void DealA()
    {
        // 获取A的信息,同时添加货物
        for(int i = 0;i < Acount;i++)
        {
            String mes[] = goodinformationA[i].split("\t");
            String itemnumber = mes[1];     // 货物编号
            int quantity = 0;               // 货物数量
            String supplier = mes[2];       // 供应商编号
            String description = mes[3];    // 货物描述

            // 添加
            inv[invcount++]  = itemnumber + "\t" + quantity + "\t" + supplier + "\t" + description;
        }
    }

    // 处理事件 R,到货记录
    public void DealR()    // 传入 库存信息 和 货物种类数量
    {
        // 对比库存信息,添加货物
        for(int i = 0;i < Rcount;i++)     // R 物流信息
        {
            String Rmes[] = goodinformationR[i].split("\t");  // 获取R物流信息的货物编号
            String Rnum = Rmes[1];                                   // 物流信息货物编号
            int Rgoodcount = Integer.valueOf(Rmes[2]);               // 进购货物数量

            for(int j = 0;j < invcount;j++)            // 库存信息
            {
                String invmess[] = inv[j].split("\t");         // 获取库存中货物的编号
                String invnum = invmess[0];                           // 库存货物编号
                int invgoodcount = Integer.valueOf(invmess[1]);       // 库存货物数量

                if(Rnum.equals(invnum))
                {
                    int newgoodcount = Rgoodcount + invgoodcount;      // 获得新货物数量

                    // 更新库存
                    inv[j] = invnum + "\t" + newgoodcount + "\t" + invmess[2] + "\t" + invmess[3];
                }
            }
        }
    }


    // 处理O事件,发货
    public void DealO()     // 传入 库存信息,货物种类数量,方便后面进行对比
    {
        // 按照发货数量进行排序,获得发货顺序
        String reg = "\t";
        String shoppingmessage[] = new String[1000];         // 储存订单信息
        int shoppingmessagecount = 0;

        // 排序获得发货顺序
        for(int i = 0; i < Ocount;i++)
        {
            for(int j = 0;j < Ocount-1;j++)
            {
                String z;   // 中转
                String n1[] = goodinformationO[j].split(reg);      // 截取数字
                String n2[] = goodinformationO[j + 1].split(reg);

                int num1 = Integer.valueOf(n1[2]);             // 获得第一个货物数量
                int num2 = Integer.valueOf(n2[2]);

                if(num1 > num2)
                {
                    z = goodinformationO[j];
                    goodinformationO[j] = goodinformationO[j + 1];
                    goodinformationO[j + 1] = z;
                }
            }
        }

        // 与库存对比进行发货
        for(int i = 0;i < Ocount;i++)          // 需要发送的货物
        {
            String Omes[] = goodinformationO[i].split(reg);
            String Onum = Omes[1];                              // 货物编号
            int Ogoodcount = Integer.valueOf(Omes[2]);          // 发货数量

            for(int j = 0;j < invcount;j++)    // 库存的货物
            {
                String invmess[] = inv[j].split("\t");         // 获取库存中货物信息
                String invnum = invmess[0];                           // 库存货物编号
                int invgoodcount = Integer.valueOf(invmess[1]);       // 库存货物数量

                if(Onum.equals(invnum))                                // 匹配成功
                {
                    if(invgoodcount < Ogoodcount)              // 判断报错
                    {
                        // 发送报错信息 “ 几号货物暂时缺货多少件,买家是谁”
                        int shortage = Ogoodcount - invgoodcount;         // 短缺货物数
                        // 发送信息
                        errormessage[errorcount++] = (invmess[0] + " 号货物暂时缺货 " + shortage + " 件,买家是 " + Omes[3] + "\t" + "\n");
                    }
                    else                // 发货创建shopping.txt,
                    {
                        invgoodcount = invgoodcount - Ogoodcount;                  // 新货物数
                        inv[j] = invmess[0] + "\t" + invgoodcount + "\t" + invmess[2] + "\t" + invmess[3] ;   // 更新库存

                        // 填写shopping.txt订单, 货号 发货数 买家。
                        shoppingmessage[shoppingmessagecount++] = Onum + "\t" + Ogoodcount + "\t" + Omes[3] + "\n";
                    }
                }
            }
        }

        // 审查shopping.txt订单,合并同类型
        try
        {
            // 获取订单信息
            Vector<String> shopmessage = new Vector<>();   // 新订单信息
            for(int i = 0;i < shoppingmessagecount;i++)
                shopmessage.add(shoppingmessage[i]) ;

            // 查找同类型订单
            for(int i = 0; i < shopmessage.size() - 1; i++)
            {
                for(int j = i + 1; j < shopmessage.size(); j++)
                {
                    String shop1[] = ( shopmessage.get(i).split("\t") );
                    String shop2[] = ( shopmessage.get(j).split("\t") );

                    String goodnum1 = shop1[0];                  // 货号
                    String goodnum2 = shop2[0];
                    int goodcount1 = Integer.valueOf(shop1[1]);  // 货物数量
                    int goodcount2 = Integer.valueOf(shop2[1]);
                    String goodbuyer1 = shop1[2];                // 买家
                    String goodbuyer2 = shop2[2];

                    // 匹配同类型成功
                    if( (goodnum1.equals(goodnum2)) && (goodbuyer1.equals(goodbuyer2)) )
                    {
                        int newgoodcount = goodcount1 + goodcount2;
                        String newmessag = goodnum1 + "\t" + newgoodcount + "\t" + goodbuyer1;
                        shopmessage.remove(i);
                        shopmessage.remove(i);   // 后面的j补齐到i上
                        shopmessage.add(0, newmessag);
                        j--;
                    }
                }
            }

            // 重新填写订单
            FileWriter shop_filewrite = new FileWriter("C:\\Users\\dell\\Desktop\\2019215053 杨雨润 实验三\\Inventory.java\\Shopping.txt");
            BufferedWriter shop_out = new BufferedWriter(shop_filewrite);

            String s = "";
            for(int i = 0; i < shopmessage.size();i++)
            {
                s = s + shopmessage.get(i);
            }
            shop_out.write(s);

            // 关闭流
            shop_out.close();

        }
        catch (IOException SIOE)
        {
            System.out.println(SIOE.getMessage());
        }
    }

    // 处理D事件,删除货物
    public void DealD()
    {
        // 匹配货物编号
        for (int i = 0; i < Dcount; i++)          // 需要删除的货物
         {
             String Dmes[] = goodinformationD[i].split("\t");
             String Dnum = Dmes[1];                                      // 删除货物编号

             for (int j = 0; j < invcount; j++)    // 库存的货物
              {
                  if(inv[j] != null)
                  {
                      String invmess[] = inv[j].split("\t");         // 获取库存中货物信息
                      String invnum = invmess[0];                           // 库存货物编号
                      int invgoodcount = Integer.valueOf(invmess[1]);       // 库存货物数量

                      // 匹配编号
                      if( Dnum.equals(invnum) &&  invgoodcount == 0)         // 匹配上而且库存为0
                       {
                           inv[j] = null;   // 删除货物
                       }
                      else if( Dnum.equals(invnum) &&  invgoodcount != 0 )   // 匹配上库存不为0,报错
                       {
                           errormessage[errorcount++] = (invnum + " 号货物库存不为0,不能被删除" + "\n");
                       }
                  }
              }
         }
    }

    // 排序输出
    public void invsort()
    {
        // 获得没有null的数组
        String newinv[] = new String[1000];
        int newinvcount = 0;

        for(int i = 0; i < invcount;i++)
        {
            if(inv[i] != null)
                newinv[newinvcount++] = inv[i];
        }

        // 排序
        for(int i = 0; i < newinvcount;i++)
        {
            for(int j = 0;j < newinvcount-1;j++)
            {
                String z;   // 中转
                String n1[] = newinv[j].split("\t");      // 截取数字
                String n2[] = newinv[j + 1].split("\t");

                int num1 = Integer.valueOf(n1[0]);             // 获得第一个货物的编号
                int num2 = Integer.valueOf(n2[0]);

                if(num1 > num2)
                {
                    z = newinv[j];
                    newinv[j] = newinv[j + 1];
                    newinv[j + 1] = z;
                }
            }
        }

        try
        {
            // 打开文件
            FileWriter newinv_filewrite = new FileWriter("C:\\Users\\dell\\Desktop\\2019215053 杨雨润 实验三\\Inventory.java\\NewInventory.txt");
            BufferedWriter newinv_out = new BufferedWriter(newinv_filewrite);

            // 填写单号
            String s = "";
            for(int i = 0; i < newinvcount;i++)
            {
                s = s + newinv[i] + "\n";
            }
            newinv_out.write(s);

            // 关闭流
            newinv_out.close();
        }
        catch (IOException NIOE)
        {
            System.out.println(NIOE.getMessage());
        }
    }

    // 输出报错信息
    public void showerror()
    {
        String s = "";
        for(int i = 0; i < errorcount;i++)
            s = s + errormessage[i];

        try
        {
            // 打开 ERROR.txt 文件
            FileWriter error_filewrite = new FileWriter("C:\\Users\\dell\\Desktop\\2019215053 杨雨润 实验三\\Inventory.java\\Errors.txt");
            BufferedWriter error_out = new BufferedWriter(error_filewrite);
            error_out.write(s);

            // 关闭流
            error_out.close();
        }
        catch (IOException EIOE)
        {
            System.out.println(EIOE.getMessage());
        }
    }
}

public class Inventory
{
    public static void main(String args[])
    {
        String inventory[] = new String[1000];      // 库存信息
        int inv_count = 0;                          // 货物种类数量

        try
        {
            // 读取 Inventory.txt 库存
            FileReader inv_fileReader = new FileReader("C:\\Users\\dell\\Desktop\\2019215053 杨雨润 实验三\\Inventory.java\\Inventory.txt");
            BufferedReader inv_in = new BufferedReader(inv_fileReader);

            // 获取库存信息
            String message;
            while((message = inv_in.readLine()) != null)
                inventory[inv_count++] = message;

            // 读取Transactions.txt
            Transactions transactions = new Transactions(inventory, inv_count);

            // 处理A事件,添加货物
            transactions.DealA();

            // 处理R事件,到货
            transactions.DealR();

            // 处理O事件,发货
            transactions.DealO();

            // 处理D事件,删除
            transactions.DealD();

            // 填写新库存
            transactions.invsort();

            // 填写报错信息
            transactions.showerror();

            // 关闭库存流
            inv_in.close();

        }
        catch (IOException IOE)
        {
            System.out.println(IOE.getMessage());
        }
    }
}

二.map的简单使用

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class test
{
    public static void main(String args[])throws IOException
    {
        Map<String, String> map = new HashMap< String, String>();
        map.put("17","40");
        System.out.println(map.get("17"));
        
        map.put("17", "52");                 // 设置17对应的数值
        System.out.println(map.get("17"));   // 获取17对应的数值

    }
}

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值