java货物进销管理系统_java实验 货物进销管理系统

该实验旨在通过Java实现货物进销管理系统,包括读写文件、处理库存、交易记录等功能。系统能处理发货订单、到货单、新增货物、删除货物等事务,涉及文件Inventory.txt、Transactions.txt、Shipping.txt和Errors.txt的读写操作。实验要求根据事务类型更新库存,记录发货和错误信息,并按特定规则处理事务。
摘要由CSDN通过智能技术生成

实验二货物进销管理系统

一.实验目的

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

提示:tab键分隔,在字符串处理的时候采用’\t’进行处理

三.思考题

1. 对整个实验进行总结,写出实验心得。

import javafx.util.Pair;

import java.io.*;

import java.util.*;

public class Inventory {

public String itemNumber;

public int quantity;

public String supplier;

public String description;

public static void main(String[] args) throws IOException {

String s = null;

ListinventoryList = new ArrayList<>();

MapitemToIndex = new HashMap<>();

BufferedReader inventoryReader = new BufferedReader(new FileReader("src/Inventory.txt"));

while((s = inventoryReader.readLine()) != null){

String[] split = s.split("\t");

inventoryList.add(new Inventory(split[0], Integer.parseInt(split[1]), split[2], split[3]));

itemToIndex.put(split[0], inventoryList.size() - 1);

}

inventoryReader.close();

List> transactionsList = new ArrayList<>();

for(int i = 0; i < 4; i++){ transactionsList.add(new ArrayList<>()); }

BufferedReader transactionsReader = new BufferedReader(new FileReader("src/Transactions.txt"));

while((s = transactionsReader.readLine()) != null){

switch(s.charAt(0)){

case 'O':

transactionsList.get(0).add(s);

break;

case 'R':

transactionsList.get(1).add(s);

break;

case 'A':

transactionsList.get(2).add(s);

break;

case 'D':

transactionsList.get(3).add(s);

break;

default:

break;

}

}

transactionsReader.close();

for(String temp : transactionsList.get(2)){

String[] split = temp.split("\t");

inventoryList.add(new Inventory(split[1], 0, split[2], split[3]));

itemToIndex.put(split[1], inventoryList.size() - 1);

}

for(String temp : transactionsList.get(1)){

String[] split = temp.split("\t");

inventoryList.get(itemToIndex.get(split[1])).quantity += Integer.parseInt(split[2]);

}

Collections.sort(transactionsList.get(0), new SortByString());

Map> shippingList = new HashMap<>();

BufferedWriter errorWriter = new BufferedWriter(new FileWriter("src/Error.txt"));

for(String temp : transactionsList.get(0)){

String[] split = temp.split("\t");

if(Integer.parseInt(split[2]) <= inventoryList.get(itemToIndex.get(split[1])).quantity){

inventoryList.get(itemToIndex.get(split[1])).quantity -= Integer.parseInt(split[2]);

if(shippingList.containsKey(split[3]) && shippingList.get(split[3]).getKey() == split[1]){

shippingList.put(split[3], new Pair(split[1],

Integer.parseInt(split[2]) + shippingList.get(split[3]).getValue()));

}else{

shippingList.put(split[3], new Pair(split[1], Integer.parseInt(split[2])));

}

}else{

errorWriter.write(split[3] + "\t" + split[1] + "\t" + split[2]);

errorWriter.newLine();

}

}

for(String temp : transactionsList.get(3)){

String[] split = temp.split("\t");

if(inventoryList.get(itemToIndex.get(split[1])).quantity > 0){

errorWriter.write("0" + "\t" + itemToIndex.get(split[1]) + "\t"

+ inventoryList.get(itemToIndex.get(split[1])).quantity);

errorWriter.newLine();

}

inventoryList.remove((int)itemToIndex.get(split[1]));

}

errorWriter.close();

BufferedWriter shippingWriter = new BufferedWriter(new FileWriter("src/Shipping.txt"));

for (Map.Entry> entry : shippingList.entrySet()) {

shippingWriter.write(entry.getKey() + "\t"

+ entry.getValue().getKey() + "\t" + entry.getValue().getValue());

shippingWriter.newLine();

}

shippingWriter.close();

BufferedWriter newInventoryWriter = new BufferedWriter(new FileWriter("src/NewInventory.txt"));

for(Inventory inventory : inventoryList){

newInventoryWriter.write(inventory.itemNumber + "\t" + inventory.quantity

+ "\t" + inventory.supplier + "\t" + inventory.description);

newInventoryWriter.newLine();

}

newInventoryWriter.close();

}

public Inventory(String itemNumber, int quantity, String supplier, String description) {

this.itemNumber = itemNumber;

this.quantity = quantity;

this.supplier = supplier;

this.description = description;

}

}

class SortByString implements Comparator {

public int compare(Object o1, Object o2) {

String[] s1 = ((String)o1).split("\t");

String[] s2 = ((String)o2).split("\t");

return Integer.parseInt(s1[2]) > Integer.parseInt(s2[2]) ? 1 : -1;

}

}

//大吉大利,今晚AC

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值