本文共 14061 字,大约阅读时间需要 46 分钟。
实验目的
1.掌握Java中文件的读写操作。 2.学会使用Java提供的实用类(Vector, ArrayList)来完成特定的功能。 3.掌握字符串类(String, StringBuffer)的使用。 4.掌握用面向对象的方法分析和解决复杂问题。 二.实验内容 编写一个Inventory.java完成以下功能: 1.程序首先打开并读取Inventory.txt中记录的所有库存记录,然后读取Transactions.txt,处理这个文件中包含的事务,记录发货记录到Shipping.txt,并记录错误信息到Errors.txt中。最后更新库存到另外一个文件NewInventory.txt中。 2.文件Inventory.txt和NewInventory.txt的每行包含一个存货记录,没条记录包含下面一些字段息,这些字段之间用一个tab分开(见后面的文件格式):字段 格式和含义
Item number 字符串型,货物编号 Quantity 整型,货物数量 Supplier 字符串型,供应商编号 Description 字符串型,货物描述 3.字段Items按照从小到大的顺序写入文件的。注意Item号不必连续,如Item号为752的后面可能是800。 4.文件Transactions.txt包含几个不同的处理记录(每行一条记录)。每条记录前面以一个大写字母开头,表示这条记录是什么类型的事务。在不同的大写字母后面是不同的信息格式。所有的字段也是以tab键分开的(见Transactions.txt文件格式)。 5.以’O’开头(Order的首字母)的事务表示这是一个发货订单,即某一种货物应该发给特定的客户。Item number和Quantity的格式如上面表格定义。Custom编号和上面的Supplier编号一致。处理一条定单记录(以’O’开头的事务)意味着从减少库存记录中相应货物的数量(减少的数量=发货单中的数量),记录发货信息到Shipping.txt中。注意:Inventory.txt中的quantity不应该小于0,如果对于某一种货物,库存的数量小于发货单的数量的话,系统应该停止处理发货单,并记录出错信息到Errors.txt。如果对于某一种货物有多个发货单,而且库存总量小于这些发货单的总和的话,系统应该按照发货单中的数量从小到大的有限原则满足客户。也就是说,对于某一种货物如果一个数量Quantity少的发货单没有处理之前,数量Quantity多的发货单永远不会被处理。(这种处理原则不受发货单记录在Transactions.txt的先后顺序影响) 6.以’R’开头的事务表示这是一个到货单记录,在’R’后面是Item number和它的数量Quanlity。处理一条到货单意味着增加库存中相应货物的数量(增加的数量=到货单中的数量)。注意:如果在Transactions.txt文件中,到货单出现在发货单之后,到货单中的货物数量可以用来填补发货单中的数量(可以理解成在Transactions.txt中,优先处理到货单)。 7.以’A’开头的事务表示向库存中增加一种新的货物(即这种货物以前库存中没有),在’A’后面是Item number,供应商supplier以及货物的描述description。处理一个新增货物记录意味着向库存中增加一个数量Quantity为0的新的Item。你可以假设在一个Transactions.txt中,新增货物记录总是出现在第一个到货单之前。 8.以’D’开头的事务表示从库存中删除一种货物,在’D’后面是Item号。删除操作总是在所有的事物处理之后才被处理,以保证对于可能出现的同一种货物的发货单的操作能在删除之前被正确处理。如果要删除的某种货物的库存量Quantity不为0的话,系统应该向Errors.txt记录出错信息。 9.文件Shipping.txt中的每一行代表给某一客户的发货信息。Shipping.txt中的每一行分别是客户编号、Item号、货物数量,它们之间用tab键分隔。如果发货单中有两条客户编号和Item编号一样的记录,在Shipping.txt中应该将这两条发货信息合并(即将它们的数量相加)。 10.Errors.txt文件包含未发送的发货记录和库存量大于0的删除记录。Errors.txt每一行包含Custom编号、Item编号以及发货单上的数量Quantity。对于删除操作,Custom编号为0,数量Quntity为库存中的Quantity. 11.实验测试数据: Inventory.txt 17 42 6 blue 1234 0 4 whatsit 123123 999999 98 doohicky Transactions.txt O 123123 1000 9 O 17 36 8 O 17 12 4 R 123123 1 D 1234 A 5 4 Thingy 代码:(目前还不完整,含有读取transanctions.txt和写入到shipping.txt中的) 先附资料:vector:
public static void main(String args[]) throws IOException { Vector hs = new Vector(); Integer integer1 = new Integer(1); hs.addElement(integer1); Integer integer2 = new Integer(111); hs.addElement(integer2); Iterator iterator = hs.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } }
ArrayList是最常用的List实现类,内部是通过数组实现的,它允许对元素进行快速随机访问。数组的缺点是每个元素之间不能有间隔,当数组大小不满足时需要增加存储能力,就要讲已经有数组的数据复制到新的存储空间中。当从ArrayList的中间位置插入或者删除元素时,需要对数组进行复制、移动、代价比较高。因此,它适合随机查找和遍历,不适合插入和删除。
Vector与ArrayList一样,也是通过数组实现的,不同的是它支持线程的同步,即某一时刻只有一个线程能够写Vector,避免多线程同时写而引起的不一致性,但实现同步需要很高的花费,因此,访问它比访问ArrayList慢。 vector是线程(Thread)同步(Synchronized)的,所以它也是线程安全的,而Arraylist是线程异步(ASynchronized)的,是不安全的public class Student { public static void main(String[] args) throws IOException { // Item number 字符串型,货物编号// Quantity 整型,货物数量// Supplier 字符串型,供应商编号// Description 字符串型,货物描述String ItemNumber []=new String[10];int Cliens []= new int[5];int Quantity[]= new int[10];//库存数量 int ItemFa[]= new int[10];//发货编号String Supplier[]=new String[10];String Description[]=new String[10];String opertion[]=new String[10];//操作String Matter[]=new String[10];//事务类型String TransactionsItem[]=new String[10];int numbers[]= new int[10] ; String data2[][]= new String[100][100]; HashMapham = new HashMap<>();int TotalLines=0;//一共多少行 String datas[][] = new String[100][100]; try { FileReader fr = new FileReader("D:\\Inventory.txt"); BufferedReader bffr = new BufferedReader(fr); String s = null; int i = 0; while((s=bffr.readLine())!=null){ datas[i] = s.split("\t"); System.out.println("第"+i+"行"+s); // System.out.println(datas[i][0]+","+datas[i][1]);ItemNumber[i] = datas[i][0];Quantity[i] = Integer.parseInt(datas[i][1]);Supplier[i]= datas[i][2];Description[i]= datas[i][3]; i++; } bffr.close(); fr.close(); } catch(Exception e1) { e1.printStackTrace(); } try { FileReader mfr = new FileReader("D:\\Transactions.txt"); BufferedReader mbffr = new BufferedReader(mfr); String s = null; int i = 0; while((s=mbffr.readLine())!=null){ data2[i] = s.split("\t"); // System.out.println("第"+(i+1)+"行"+s);// System.out.println(Integer.parseInt(data2[i][2]));if(data2[i][0]!=null){ Matter[i]= data2[i][0];if(Matter[i].matches("O")) { Cliens[i] = Integer.parseInt(data2[i][3]);ItemFa[i]= Integer.parseInt(data2[i][1]);}} if( data2[i][1]!=null)TransactionsItem[i] = data2[i][1];if(data2[i].length>2)numbers[i] = Integer.parseInt(data2[i][2]);if(ham.containsKey(TransactionsItem[i])) { int total = ham.get(TransactionsItem[i])+numbers[i]; ham.put(TransactionsItem[i],total);}else ham.put(TransactionsItem[i],numbers[i]);//if(Matter[i].matches("O")) System.out.print("YES");TotalLines++; i++; } mbffr.close(); mfr.close(); } catch(Exception e1) { e1.printStackTrace(); } File file2 = new File("D:\\Shipping.txt"); for(int m = 0;m < Matter.length;m++){ if(Matter[m].matches("O")){ System.out.println(Cliens[m]); try { FileWriter fw = new FileWriter(file2,true); BufferedWriter writer = new BufferedWriter(fw); writer.write(String.valueOf(Cliens[m])); writer.write("\t"); writer.write(String.valueOf(numbers[m])); writer.write("\t"); writer.write(String.valueOf(ItemFa[m])); writer.newLine();writer.close();fw.close(); } catch (Exception e){ e.printStackTrace(); } } } // for(String key:ham.keySet()) // System.out.println(ham.get(key)); for(int j = 0;j < TotalLines-1;j++){ for(int z = j+1;z < TotalLines;z++){ System.out.print(TransactionsItem[j]+":"); if(TransactionsItem[j].matches(TransactionsItem[z])) { numbers[j] = numbers[j]+numbers[z]; } // System.out.println(numbers[j]); }; }}}
用vector思路的版本:
class goods{ private String Itemnumber; private int Quatity; private String Supplier; private String Description; public String getItemnumber() { return Itemnumber; }public void update(int num){ this.Quatity = this.Quatity+num;//修改总库存的函数} public void setItemnumber(String itemnumber) { Itemnumber = itemnumber; } public int getQuatity() { return Quatity; } public void setQuatity(int quatity) { Quatity = quatity; } public String getSupplier() { return Supplier; } public void setSupplier(String supplier) { Supplier = supplier; } public String getDescription() { return Description; } public void setDescription(String description) { Description = description; }public void less(int num){ //发货时数量减少的函数 this.Quatity = this.Quatity-num;} public goods(String itemnumber, int quatity, String supplier, String description) { Itemnumber = itemnumber; Quatity = quatity; Supplier = supplier; Description = description; }}class Fagood{ private String operaion; private String Item; private int total; private String receiver; public String getOperaion() { return operaion; } public void setOperaion(String operaion) { this.operaion = operaion; } public String getItem() { return Item; } public void setItem(String item) { Item = item; } public int getTotal() { return total; } public void setTotal(int total) { this.total = total; } public String getReceiver() { return receiver; } public void setReceiver(String receiver) { this.receiver = receiver; } public Fagood(String operaion, String item, int total, String receiver) { this.operaion = operaion; Item = item; this.total = total; this.receiver = receiver; } public Fagood(String operaion, String item) { this.operaion = operaion; Item = item; } public Fagood(String operaion, String item, int total) { this.operaion = operaion; Item = item; this.total = total; }}public class Student { public static void main(String[] args) throws IOException { String datas[][] = new String[10][10]; String data2[][] = new String[10][10]; VectorGood = new Vector (0); // int Quantity[] = new int[1000000];//库存数量 Vector FaGood = new Vector (0); PrintWriter out1 = new PrintWriter("D:\\Error.txt","UTF-8"); FileReader mfr = new FileReader("D:\\Transactions.txt"); BufferedReader mbffr = new BufferedReader(mfr); FileReader fr = new FileReader("D:\\Inventory.txt"); PrintWriter out = new PrintWriter(new FileWriter("D:\\Shipping.txt",true)); out.println("客户编号 "+" "+"Item号 "+"货物数量"); BufferedReader bffr = new BufferedReader(fr); try { String s = null; int i = 0; while ((s = bffr.readLine()) != null) { datas[i] = s.split("\t"); System.out.println("第" + i + "行" + s); goods agood = new goods(datas[i][0], Integer.parseInt(datas[i][1]), datas[i][2], datas[i][3]); // System.out.println(agood.getDescription()); Good.addElement(agood);//把一个商品加入到vector数组中 i++; } bffr.close(); fr.close(); } catch (Exception e1) { e1.printStackTrace(); } System.out.println(Good.capacity());//处理D:\Transactions.txt try { String s = null; int i = 0; while ((s = mbffr.readLine()) != null) { data2[i] = s.split("\t"); // System.out.println("第"+(i+1)+"行"+s);// System.out.println(Integer.parseInt(data2[i][2])); Iterator iterator = Good.iterator(); if (data2[i][0].matches("O")) { Fagood fagood = new Fagood(data2[i][0], data2[i][1], Integer.parseInt(data2[i][2]), data2[i][3]); FaGood.add(fagood); for (int j = 0; j < Good.capacity()-1; j++) { //System.out.println(good.getQuatity()); if (Integer.parseInt(Good.get(j).getItemnumber()) == Integer.parseInt(fagood.getItem())) { int num = Good.get(j).getQuatity(); System.out.println(num); num = num - fagood.getTotal(); System.out.println("减去后"+num); if (num < 0) { //验错,如果发货的总量超过了库存则写入error.txt try { File file2 = new File("D:\\Error.txt"); FileWriter in = new FileWriter(file2, true); out1.println(String.valueOf(fagood.getItem()) + "发货库存量不足\n"); } catch (Exception ee) { ee.printStackTrace(); } } else { Good.get(j).less(fagood.getTotal()); try { File ship = new File("D:\\Shipping.txt"); System.out.println("发货一次"); FileWriter fw = new FileWriter(ship, true); out.println( fagood.getReceiver()+"\t"+fagood.getTotal()+"\t"+fagood.getItem()+"\n"); } catch (Exception e) { e.printStackTrace(); } } } } // System.out.println(Good.capacity()); } if (data2[i][0].matches("D"))//删除一种商品 { for (goods good : Good) { if (Integer.parseInt(good.getItemnumber()) == Integer.parseInt(data2[i][1])) { Good.remove(good);//删除该种商品 } } } if (data2[i][0].matches("R")) { //到货单记录,在'R'后面是Item number和它的数量Quanlity。 for (goods good : Good) { if (Integer.parseInt(good.getItemnumber()) == Integer.parseInt(data2[i][1])) { System.out.println("是R"); good.update(Integer.parseInt(data2[i][2]));//增加库存 } } } i++; } out.flush(); out.close(); out1.close(); } catch (Exception e) { e.printStackTrace(); } finally { } for (goods good : Good) { System.out.println(good.getItemnumber()+" "+good.getQuatity()+" "+good.getDescription()); } }}
为了同学们自己看,于是删掉了代码注释,有问题的私我吧~
附上结果图:
转载地址:http://otten.baihongyu.com/