用物料清单创建类结构的最佳方法是什么?

时间:2019-04-04 05:14:57

标签: java deserialization

对于一个学期项目(使用Java),我必须进行一些材料流程优化。整个任务的起点将是物料清单。用户应该能够通过文件(xml或yaml)提供它。现在我的问题是,如何从该物料清单中自动构建具有依赖关系的类对象?

到目前为止,我发现了一个称为简单(http://simple.sourceforge.net/)的序列化/反序列化框架,但是我不确定这是否是正确的方法。

很高兴为您提供任何建议。

1 个答案:

答案 0 :(得分:2)

  

用户应该能够通过文件(xml或yaml)提供它。

首先,您必须知道以上文件的结构。了解了结构之后,您便可以构建您的类了。我添加了一个示例结构,以及如何将其转换为Java类。请参阅下面的内容。

示例文件结构

<Product>
    <Name>Widget</Name>
    <Description>
        This widget is the highest quality widget. 
    </Description>
    <Price>5.50</Price>
    <Quantity>5</Quantity>
</Product>
<Product>
    <Name>Glass</Name>
    <Description>
        This Glass is the highest quality glass. 
    </Description>
    <Price>6.50</Price>
    <Quantity>1</Quantity>
</Product>
<Product>
    <Name>Wood</Name>
    <Description>
        This wood is the highest quality wood. 
    </Description>
    <Price>70</Price>
    <Quantity>100</Quantity>
</Product>

从中您可以看到必须有一个Product类来容纳每个产品。然后,您可以像下面的代码一样制作Product类。

注意:由于OP是新手,因此我有意将所有属性设置为public
产品类别

public class Product {
    public String name; // holder of the name of the product
    public String description; // holder of the description of the product
    public double price; // holder of the price of the product, the data type is double since it can contain decimal values
    public int quantity; // holder of the quantity of the product, the data type is int since a product can't usually have decimal values as quantity
}

您还可以创建一个包含物料清单的类。

物料清单类:

public class Bill {
     public List<Product> productList = new ArrayList<>(); // this will hold all the product
     public double totalCost; // this will be the total of the bill of materials
}

然后,您将以任何想要的方式读取文件。但是我在下面做了一个伪代码。

读取文件中的伪代码:

Declare a list that will hold all Products inside file
Declare a variable that will hold the total cost of the bill and set it to 0
iterate each Product in file {
    Declare a new Product variable that will contain a single Product of file
    Set the Product's name
    Set the Product's description
    Set the Product's price
    Set the Product's quantity
    Add this Product to the product list that was declare above
    Add the Product's price to the current total
}
Declare a new Bill variable that will contain the product list and the total cost
Set the Bill's productList to be the product list that was declare above
Set the Bill's totalCost to be the total cost of the bill that was declare above

现在,这是如何从材料明细表创建类结构的要点。 只需从我的答案中获取想法,然后根据您的需要进行编辑即可。希望我能帮上忙。

编辑:关于OP的评论
您可以这样操作:

<ProductA>
    <Name>HyBrid</Name>
    <Description>
        This HyBrid is the highest quality HyBrid. 
    </Description>
    <Price>50000</Price>
    <Quantity>5</Quantity>
    <ProductBList>
        <Product>
            <Name>WidgetA</Name>
            <Description>
                This widgetA is the highest quality widgetA. 
            </Description>
            <Price>5.50</Price>
            <Quantity>5</Quantity>
        </Product>
        <Product>
            <Name>WidgetB</Name>
            <Description>
                This widgetB is the highest quality widgetB. 
            </Description>
            <Price>5.50</Price>
            <Quantity>5</Quantity>
        </Product>
    </ProductBList>
    <ProductCList>
        <Product>
            <Name>WidgetA</Name>
            <Description>
                This widgetA is the highest quality widgetA. 
            </Description>
            <Price>5.50</Price>
            <Quantity>5</Quantity>
        </Product>
        <Product>
            <Name>WidgetB</Name>
            <Description>
                This widgetB is the highest quality widgetB. 
            </Description>
            <Price>5.50</Price>
            <Quantity>5</Quantity>
        </Product>
        <Product>
            <Name>WidgetC</Name>
            <Description>
                This widgetC is the highest quality widgetC. 
            </Description>
            <Price>5.50</Price>
            <Quantity>5</Quantity>
        </Product>
    </ProductCList>
</ProductA>

然后您可以拥有一个这样的课程:

public class ProductA {
    public String name; // holder of the name of the product
    public String description; // holder of the description of the product
    public double price; // holder of the price of the product, the data type is double since it can contain decimal values
    public int quantity; // holder of the quantity of the product, the data type is int since a product can't usually have decimal values as quantity
    public List<Product> productBList = new ArrayList<>(); // holder for all productB
    public List<Product> productCList = new ArrayList<>(); // holder for all productC
}

请注意,此方法不会限制productBList和productCList的内容。因此,如果要限制它,只需使用一个数组即可。另外,在生成类之前,您还必须首先检查文件是否包含有效的xml。