货物进销管理系统
貨物進銷管理系統
一.實驗目的
??? 1.掌握Java中文件的讀寫操作。
??? 2.學會使用Java提供的實用類(Vector, ArrayList)來完成特定的功能。
??? 3.掌握字符串類(String, StringBuffer)的使用。
??? 4.掌握用面向對象的方法分析和解決復雜問題。
二.實驗內容
??? 編寫一個Inventory.java完成以下功能(沒有學過Java文件處理之前,各位同學可以使用硬編碼將數據放進兩個Vector變量里。等學過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'開頭的事務表示這是一個發貨訂單,即某一種貨物應該發給特定的客戶。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
Transactions.txt
?接下來上傳本人的代碼:
package inventory;import java.io.*; import java.util.*;class Goods{ //商品類String Item; //貨物編號int Quantity; //貨物數量String Supplier; //供應商編號String Description; //貨物描述 }class Thing{ //事件類String thing_type; //事件編號 String Item; //貨物編號int Quantity; //貨物數量String Supplier; //供應商編號String Description; //貨物描述 }public class Inventory {static Vector<Goods> v_goods=new Vector<Goods>(); //創建Goods對象數組static Vector<Thing> O_thing=new Vector<Thing>(); //創建Thing的到貨(O)事件對象數組static Vector<Thing> R_thing=new Vector<Thing>(); //創建Thing的到貨(R)事件對象數組static Vector<Thing> D_thing=new Vector<Thing>(); //創建Thing的到貨(D)事件對象數組static Vector<Thing> A_thing=new Vector<Thing>(); //創建Thing的到貨(A)事件對象數組static RandomAccessFile out_Shipping=null; static RandomAccessFile out_Errors=null;static RandomAccessFile in=null;static RandomAccessFile out_NewInventory=null;static void read_Inventory() { //讀取庫存記錄try {in=new RandomAccessFile("Inventory.txt","r");String str1=null;while((str1=in.readLine())!=null) {String str2[]=str1.split("\\s+"); //分解字符串Goods goods_temp=new Goods();goods_temp.Item=str2[0];goods_temp.Quantity=Integer.parseInt(str2[1]);goods_temp.Supplier=str2[2];goods_temp.Description=str2[3];v_goods.addElement(goods_temp);}in.close();}catch(IOException e) {System.out.println(e);}}static void read_Transactions() { //讀取事件信息try {in=new RandomAccessFile("Transactions.txt","r");String str1=null;while((str1=in.readLine())!=null) {String str2[]=str1.split("\\s+"); //分解字符串Thing thing_temp=new Thing();if(str2[0].equals("O")) {thing_temp.Item=str2[1];thing_temp.Quantity=Integer.parseInt(str2[2]);thing_temp.Supplier=str2[3];int O_thing_size=O_thing.size();int O_thing_index=O_thing_size-1;if(O_thing_size==0)O_thing.addElement(thing_temp);else {while(O_thing_index>0) { if((O_thing.elementAt(O_thing_index).Quantity)>(thing_temp.Quantity))O_thing_index--;}//將發貨事件進行排序后插入O_thing.insertElementAt(thing_temp, O_thing_index);}}else if(str2[0].equals("R")) {thing_temp.Item=str2[1];thing_temp.Quantity=Integer.parseInt(str2[2]);R_thing.addElement(thing_temp);}else if(str2[0].equals("D")) {thing_temp.Item=str2[1];D_thing.addElement(thing_temp);}else {thing_temp.Item=str2[1];thing_temp.Supplier=str2[2];thing_temp.Description=str2[3];A_thing.addElement(thing_temp);}}in.close(); }catch(IOException e) {System.out.println(e);}}static void handle_A_thing() { //處理新添貨物事件for(int i=0;i<A_thing.size();i++) {Goods goods_temp=new Goods();goods_temp.Item=A_thing.elementAt(i).Item;goods_temp.Quantity=0;goods_temp.Supplier=A_thing.elementAt(i).Supplier;goods_temp.Description=A_thing.elementAt(i).Description;//按商品編號從小到大插入for(int j=0;j<v_goods.size();j++) {if(Integer.parseInt(goods_temp.Item)<Integer.parseInt(v_goods.elementAt(j).Item)) {v_goods.insertElementAt(goods_temp, j);break;}}}}static void handle_R_thing() { //處理到貨事件for(int i=0;i<R_thing.size();i++) {for(int j=0;j<v_goods.size();j++) {if(R_thing.elementAt(i).Item.equals(v_goods.elementAt(j).Item)) { //找到對應的商品位置v_goods.elementAt(j).Quantity+=R_thing.elementAt(i).Quantity;}}}}static void handle_O_thing() { //處理發貨事件Vector<Goods> S_goods=new Vector<Goods>(); //發貨單 數組try {out_Shipping=new RandomAccessFile("Shipping.txt","rw"); //發貨信息文件out_Errors=new RandomAccessFile("Errors.txt","rw"); //錯誤信息文件} catch(IOException e) {System.out.println(e);}for(int i=0;i<O_thing.size();i++) {for(int j=0;j<v_goods.size();j++) {if(O_thing.elementAt(i).Item.equals(v_goods.elementAt(j).Item)) { //找到對應的商品位置if(v_goods.elementAt(j).Quantity>=O_thing.elementAt(i).Quantity) {//判斷商品數量是否夠//數量夠v_goods.elementAt(j).Quantity-=O_thing.elementAt(i).Quantity; //庫存量減去發貨的數量boolean b=false; //標志 判斷此次發貨信息是否被記錄//先將發貨信息存入發貨單數組for(int k=0;k<S_goods.size();k++) { //判斷是否有客戶和物品一樣的發貨信息if((O_thing.elementAt(i).Supplier.equals(S_goods.elementAt(k).Supplier))&&(O_thing.elementAt(i).Item.equals(S_goods.elementAt(k).Item))) {S_goods.elementAt(k).Quantity+=O_thing.elementAt(i).Quantity; //相同信息合并b=true;}}if(!b) {Goods goods_temp=new Goods();goods_temp.Item=O_thing.elementAt(i).Item;goods_temp.Quantity=O_thing.elementAt(i).Quantity;goods_temp.Supplier=O_thing.elementAt(i).Supplier;S_goods.addElement(goods_temp);}}else { //商品數量不夠,將錯誤信息寫入Errors.txttry {out_Errors.writeBytes(O_thing.elementAt(i).Supplier+"\t"+O_thing.elementAt(i).Item+"\t"+O_thing.elementAt(i).Quantity+"\n");} catch (IOException e) {// TODO 自動生成的 catch 塊e.printStackTrace();}}}}}//寫入發貨單try {for(int i=0;i<S_goods.size();i++) {out_Shipping.writeBytes(S_goods.elementAt(i).Supplier+"\t"+S_goods.elementAt(i).Item+"\t"+S_goods.elementAt(i).Quantity+"\n");}} catch (IOException e) {// TODO 自動生成的 catch 塊e.printStackTrace();}try {out_Shipping.close(); } catch (IOException e) {// TODO 自動生成的 catch 塊e.printStackTrace();}}static void handle_D_thing() { //刪除商品for(int i=0;i<D_thing.size();i++) {for(int j=0;j<v_goods.size();j++) {if(D_thing.elementAt(i).Item.equals(v_goods.elementAt(j).Item)) {if(v_goods.elementAt(j).Quantity!=0) //貨物數量不為0,記錄錯誤到Errors.txttry {out_Errors.writeBytes(D_thing.elementAt(i).Supplier+"\t"+D_thing.elementAt(i).Item+"\t"+D_thing.elementAt(i).Quantity+"\n");} catch (IOException e) {// TODO 自動生成的 catch 塊e.printStackTrace();}v_goods.removeElement(v_goods.elementAt(j)); //刪除元素}}}try {out_Errors.close(); //關閉} catch (IOException e) {// TODO 自動生成的 catch 塊e.printStackTrace();}}static void w_NewInventory() { //將貨物信息讀入NewInventory.txttry {out_NewInventory=new RandomAccessFile("NewInventory.txt","rw");} catch (FileNotFoundException e) {// TODO 自動生成的 catch 塊e.printStackTrace();}for(int i=0;i<v_goods.size();i++)try { //寫入貨物信息out_NewInventory.writeBytes(v_goods.elementAt(i).Item+"\t"+v_goods.elementAt(i).Quantity+"\t"+v_goods.elementAt(i).Supplier+"\t"+v_goods.elementAt(i).Description+"\n");} catch (IOException e) {// TODO 自動生成的 catch 塊e.printStackTrace();}try {out_NewInventory.close();} catch (IOException e) {// TODO 自動生成的 catch 塊e.printStackTrace();}}public static void main(String args[]) {read_Inventory(); //讀取Inventory.txt(商品信息)read_Transactions(); //讀取Transactions.txt(事件信息)handle_A_thing(); //處理添加新商品事件handle_R_thing(); //處理到貨事件handle_O_thing(); //處理發貨事件handle_D_thing(); //處理刪除商品事件w_NewInventory(); //商品信息寫入NewInventory.txt} }?
總結
- 上一篇: uni-app开发环境配置及混合开发流程
- 下一篇: 视频教程-初级学习ArcGIS Engi