货物进销管理系统
貨物進(jìn)銷(xiāo)管理系統(tǒng)
一.實(shí)驗(yàn)?zāi)康?/strong>
??? 1.掌握J(rèn)ava中文件的讀寫(xiě)操作。
??? 2.學(xué)會(huì)使用Java提供的實(shí)用類(lèi)(Vector, ArrayList)來(lái)完成特定的功能。
??? 3.掌握字符串類(lèi)(String, StringBuffer)的使用。
??? 4.掌握用面向?qū)ο蟮姆椒ǚ治龊徒鉀Q復(fù)雜問(wèn)題。
二.實(shí)驗(yàn)內(nèi)容
??? 編寫(xiě)一個(gè)Inventory.java完成以下功能(沒(méi)有學(xué)過(guò)Java文件處理之前,各位同學(xué)可以使用硬編碼將數(shù)據(jù)放進(jìn)兩個(gè)Vector變量里。等學(xué)過(guò)Java文件處理之后,再補(bǔ)充數(shù)據(jù)文件讀取部分):
??? 1.程序首先打開(kāi)并讀取Inventory.txt中記錄的所有庫(kù)存記錄,然后讀取Transactions.txt,處理這個(gè)文件中包含的事務(wù),記錄發(fā)貨記錄到Shipping.txt,并記錄錯(cuò)誤信息到Errors.txt中。最后更新庫(kù)存到另外一個(gè)文件NewInventory.txt中。
??? 2.文件Inventory.txt和NewInventory.txt的每行包含一個(gè)存貨記錄,沒(méi)條記錄包含下面一些字段息,這些字段之間用一個(gè)tab分開(kāi)(見(jiàn)后面的文件格式):
??????
| 字段 | 格式和含義 |
| Item number | 字符串型,貨物編號(hào) |
| Quantity | 整型,貨物數(shù)量 |
| Supplier | 字符串型,供應(yīng)商編號(hào) |
| Description | 字符串型,貨物描述 |
??? 3.字段Items按照從小到大的順序?qū)懭胛募摹W⒁釯tem號(hào)不必連續(xù),如Item號(hào)為752的后面可能是800。
??? 4.文件Transactions.txt包含幾個(gè)不同的處理記錄(每行一條記錄)。每條記錄前面以一個(gè)大寫(xiě)字母開(kāi)頭,表示這條記錄是什么類(lèi)型的事務(wù)。在不同的大寫(xiě)字母后面是不同的信息格式。所有的字段也是以tab鍵分開(kāi)的(見(jiàn)Transactions.txt文件格式)。
5.以'O'開(kāi)頭的事務(wù)表示這是一個(gè)發(fā)貨訂單,即某一種貨物應(yīng)該發(fā)給特定的客戶(hù)。Item number和Quantity的格式如上面表格定義。Custom編號(hào)和上面的Supplier編號(hào)一致。處理一條定單記錄(以'O'開(kāi)頭的事務(wù))意味著從減少庫(kù)存記錄中相應(yīng)貨物的數(shù)量(減少的數(shù)量=發(fā)貨單中的數(shù)量),記錄發(fā)貨信息到Shipping.txt中。注意:Inventory.txt中的quantity不應(yīng)該小于0,如果對(duì)于某一種貨物,庫(kù)存的數(shù)量小于發(fā)貨單的數(shù)量的話(huà),系統(tǒng)應(yīng)該停止處理發(fā)貨單,并記錄出錯(cuò)信息到Errors.txt。如果對(duì)于某一種貨物有多個(gè)發(fā)貨單,而且?guī)齑婵偭啃∮谶@些發(fā)貨單的總和的話(huà),系統(tǒng)應(yīng)該按照發(fā)貨單中的數(shù)量從小到大的有限原則滿(mǎn)足客戶(hù)。也就是說(shuō),對(duì)于某一種貨物如果一個(gè)數(shù)量Quantity少的發(fā)貨單沒(méi)有處理之前,數(shù)量Quantity多的發(fā)貨單永遠(yuǎn)不會(huì)被處理。(這種處理原則不受發(fā)貨單記錄在Transactions.txt的先后順序影響)
6.以'R'開(kāi)頭的事務(wù)表示這是一個(gè)到貨單記錄,在'R'后面是Item number和它的數(shù)量Quanlity。處理一條到貨單意味著增加庫(kù)存中相應(yīng)貨物的數(shù)量(增加的數(shù)量=到貨單中的數(shù)量)。注意:如果在Transactions.txt文件中,到貨單出現(xiàn)在發(fā)貨單之后,到貨單中的貨物數(shù)量可以用來(lái)填補(bǔ)發(fā)貨單中的數(shù)量(可以理解成在Transactions.txt中,優(yōu)先處理到貨單)。
7.以'A'開(kāi)頭的事務(wù)表示向庫(kù)存中增加一種新的貨物(即這種貨物以前庫(kù)存中沒(méi)有),在'A'后面是Item number,供應(yīng)商supplier以及貨物的描述description。處理一個(gè)新增貨物記錄意味著向庫(kù)存中增加一個(gè)數(shù)量Quantity為0的新的Item。你可以假設(shè)在一個(gè)Transactions.txt中,新增貨物記錄總是出現(xiàn)在第一個(gè)到貨單之前。
8.以'D'開(kāi)頭的事務(wù)表示從庫(kù)存中刪除一種貨物,在'D'后面是Item號(hào)。刪除操作總是在所有的事物處理之后才被處理,以保證對(duì)于可能出現(xiàn)的同一種貨物的發(fā)貨單的操作能在刪除之前被正確處理。如果要?jiǎng)h除的某種貨物的庫(kù)存量Quantity不為0的話(huà),系統(tǒng)應(yīng)該向Errors.txt記錄出錯(cuò)信息。
9.文件Shipping.txt中的每一行代表給某一客戶(hù)的發(fā)貨信息。Shipping.txt中的每一行分別是客戶(hù)編號(hào)、Item號(hào)、貨物數(shù)量,它們之間用tab鍵分隔。如果發(fā)貨單中有兩條客戶(hù)編號(hào)和Item編號(hào)一樣的記錄,在Shipping.txt中應(yīng)該將這兩條發(fā)貨信息合并(即將它們的數(shù)量相加)。
10.Errors.txt文件包含未發(fā)送的發(fā)貨記錄和庫(kù)存量大于0的刪除記錄。Errors.txt每一行包含Custom編號(hào)、Item編號(hào)以及發(fā)貨單上的數(shù)量Quantity。對(duì)于刪除操作,Custom編號(hào)為0,數(shù)量Quntity為庫(kù)存中的Quantity.
11.實(shí)驗(yàn)測(cè)試數(shù)據(jù):
Inventory.txt
Transactions.txt
?接下來(lái)上傳本人的代碼:
package inventory;import java.io.*; import java.util.*;class Goods{ //商品類(lèi)String Item; //貨物編號(hào)int Quantity; //貨物數(shù)量String Supplier; //供應(yīng)商編號(hào)String Description; //貨物描述 }class Thing{ //事件類(lèi)String thing_type; //事件編號(hào) String Item; //貨物編號(hào)int Quantity; //貨物數(shù)量String Supplier; //供應(yīng)商編號(hào)String Description; //貨物描述 }public class Inventory {static Vector<Goods> v_goods=new Vector<Goods>(); //創(chuàng)建Goods對(duì)象數(shù)組static Vector<Thing> O_thing=new Vector<Thing>(); //創(chuàng)建Thing的到貨(O)事件對(duì)象數(shù)組static Vector<Thing> R_thing=new Vector<Thing>(); //創(chuàng)建Thing的到貨(R)事件對(duì)象數(shù)組static Vector<Thing> D_thing=new Vector<Thing>(); //創(chuàng)建Thing的到貨(D)事件對(duì)象數(shù)組static Vector<Thing> A_thing=new Vector<Thing>(); //創(chuàng)建Thing的到貨(A)事件對(duì)象數(shù)組static RandomAccessFile out_Shipping=null; static RandomAccessFile out_Errors=null;static RandomAccessFile in=null;static RandomAccessFile out_NewInventory=null;static void read_Inventory() { //讀取庫(kù)存記錄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--;}//將發(fā)貨事件進(jìn)行排序后插入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;//按商品編號(hào)從小到大插入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)) { //找到對(duì)應(yīng)的商品位置v_goods.elementAt(j).Quantity+=R_thing.elementAt(i).Quantity;}}}}static void handle_O_thing() { //處理發(fā)貨事件Vector<Goods> S_goods=new Vector<Goods>(); //發(fā)貨單 數(shù)組try {out_Shipping=new RandomAccessFile("Shipping.txt","rw"); //發(fā)貨信息文件out_Errors=new RandomAccessFile("Errors.txt","rw"); //錯(cuò)誤信息文件} 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)) { //找到對(duì)應(yīng)的商品位置if(v_goods.elementAt(j).Quantity>=O_thing.elementAt(i).Quantity) {//判斷商品數(shù)量是否夠//數(shù)量夠v_goods.elementAt(j).Quantity-=O_thing.elementAt(i).Quantity; //庫(kù)存量減去發(fā)貨的數(shù)量boolean b=false; //標(biāo)志 判斷此次發(fā)貨信息是否被記錄//先將發(fā)貨信息存入發(fā)貨單數(shù)組for(int k=0;k<S_goods.size();k++) { //判斷是否有客戶(hù)和物品一樣的發(fā)貨信息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 { //商品數(shù)量不夠,將錯(cuò)誤信息寫(xiě)入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 自動(dòng)生成的 catch 塊e.printStackTrace();}}}}}//寫(xiě)入發(fā)貨單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 自動(dòng)生成的 catch 塊e.printStackTrace();}try {out_Shipping.close(); } catch (IOException e) {// TODO 自動(dòng)生成的 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) //貨物數(shù)量不為0,記錄錯(cuò)誤到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 自動(dòng)生成的 catch 塊e.printStackTrace();}v_goods.removeElement(v_goods.elementAt(j)); //刪除元素}}}try {out_Errors.close(); //關(guān)閉} catch (IOException e) {// TODO 自動(dòng)生成的 catch 塊e.printStackTrace();}}static void w_NewInventory() { //將貨物信息讀入NewInventory.txttry {out_NewInventory=new RandomAccessFile("NewInventory.txt","rw");} catch (FileNotFoundException e) {// TODO 自動(dòng)生成的 catch 塊e.printStackTrace();}for(int i=0;i<v_goods.size();i++)try { //寫(xiě)入貨物信息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 自動(dòng)生成的 catch 塊e.printStackTrace();}try {out_NewInventory.close();} catch (IOException e) {// TODO 自動(dòng)生成的 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(); //處理發(fā)貨事件handle_D_thing(); //處理刪除商品事件w_NewInventory(); //商品信息寫(xiě)入NewInventory.txt} }?
總結(jié)
- 上一篇: uni-app开发环境配置及混合开发流程
- 下一篇: 视频教程-初级学习ArcGIS Engi