Java库存项目

时间:2014-06-08 00:51:25

标签: java project editing inventory

问题是: 项目5.类库存,ProductItem,ProductShoe,ProductPants,ProductShirt。类库存是ProductItem实例的集合。 ProductItem类是一个基类,具有私有成员唯一的整数产品ID和字符串产品项描述。类ProductShoe,ProductPants和ProductShirt继承自ProductItem,这些类中的每一个都应具有私有成员,每个产品至少反映三个属性(数量是其中之一)以及相应的setter和getter函数。主应用程序使用击键(控制台应用程序)与用户交互,允许他/她列出产品项目,创建/显示/编辑产品项目。 代码如下:

    public class ProductItem {

    private int productid;
    private int productquantity;
    private String productdesc;


    //constructor : sets product id,quantity and description to 0 if no parameters given
    public ProductItem()
    {
        productid=0;
        productquantity=0;
        productdesc="No description given";
    }

    //overload constructor : sets product id,quantity and description to whatever parameters given
    public ProductItem(int id, int quantity, String desc)
    {
        productid=id;
        productquantity=quantity;
        productdesc=desc;
    }

    //sets a new id for productid
    public void setid (int newid)
    {productid=newid;}

    //sets a new id for productquantity
    public void setquantity (int newquantity)
    {productquantity=newquantity;}

    //sets a new id for productdesc
    public void setdesc (String newdesc)
    {productdesc=newdesc;}

    //get function that returns productid
    public int getid()
    {return productid;}

    //get function that returns productquantity
    public int getquantity()
    {return productquantity;}

    //get function that returns productdesc
    public String getdesc()
    {return productdesc;}

    //method to output ProductId
    public void outputid()
    {System.out.println("Product Id: " + productid);}

    //method to output ProductQuantity
    public void outputquantity()
    {System.out.println("Product Quantity: " + productquantity);}

    //method to output ProductDesc
    public void outputdesc()
    {System.out.println("Product Description: " + productdesc);}

    }

__________________________________________________________________

import java.util.Scanner;
//
public class Inventory extends ProductItem {

    //Show item
        public void printitem()
        {
            super.outputid();
            super.outputquantity();
            super.outputdesc();
        }

    //create item
        public ProductItem itemcreation()
        {
            ProductItem newitem = new ProductItem();
            Scanner keyboard = new Scanner (System.in);
            int newid;
            System.out.println("Enter new value for ID: ");
            super.setid(newid= keyboard.nextInt());
            int newquantity;
            System.out.println("Enter new value for Quantity: ");
            super.setquantity(newquantity= keyboard.nextInt());
            String newdesc;
            System.out.println("Enter new value for Description: ");
            super.setdesc(newdesc= keyboard.nextLine());
            return newitem;
        }

    //edit item
        public void itemediting ()
        {

        }

我无法想象我的生活如何让用户编辑ProductItem的特定对象,例如ProductShit // ProductShoes // ProductPants,甚至更难创建自己创建的项目。任何反馈都将非常感谢,这是我的成绩的25%

1 个答案:

答案 0 :(得分:0)

以下是解决方案的概要

start application
get user input
if user wants to create, create item and add the item to inventory
else if user wants to see all items, show all items
else if user wants to edit item, edit item
else if user wants to exist, exit application

对于实际代码,我建议使用ProductItems的ArrayList,即ArrayList<ProductItem> inventory=...,并让用户传入参数。就像用户键入create shirt param1 param2 param3一样,然后将使用给定参数创建衬衫并将其添加到库存中,即inventory.add(new ProductShirt(param1,param2,param3));

查看String.split方法,了解有关如何完成此操作的更多信息