我可以使用java中的LinkedLists的一些指导.特别是向另一个类中的链表添加对象



我想让这个程序添加一个"产品"对象到我的链表上ProductList。"(一旦我知道我可以做其他的)我不确定我是否做了LinkedList在我的主要权利,或者如果我甚至需要一个,但我只是不知道如何做到这一点,任何帮助将不胜感激。谢谢你!

public class Product{
    //Private variable names
    private String prodID;
    private String prodName;
    private int qty;
    private float unitCost;
    //Default Constructor
    public Product(){
        this.prodID = "";
        this.prodName = "";
        this.qty = 0;
        this.unitCost = 0.0f;
    }
    //Constructor with arguments
    public Product(String tmpProdID, String tmpProdName, int tmpQty, float tmpUnitCost){
        this.prodID   =   tmpProdID;
        this.prodName =   tmpProdName;
        this.qty      =   tmpQty;
        this.unitCost =   tmpUnitCost;
    }
    public void setProdID(String tmpProdID){
        this.prodID = tmpProdID;
    }
    public void setProdName(String tmpProdName){
        this.prodName = tmpProdName;
    }
    public void setProdQty(int tmpQty){
        this.qty = tmpQty;
    }
    public void setUnitCost(float tmpUnitCost){
        this.unitCost = tmpUnitCost;
    }
    public String getProdID(){
        return this.prodID;
    }
    public String getProdName(){
        return this.prodName;
    }
    public int getQty(){
        return this.qty;
    }
    public float getUnitCost(){
        return this.unitCost;
    }
    public String toString(){
        return "Prod ID: " + prodID + ". Prod name: " + prodName + ". Prod qty: " + qty + ". Prod unit cost: " + unitCost;
    }
}
import java.util.LinkedList;
public class ProductList{
    //Creating a linked list of products
    private LinkedList<Product> prodList = new LinkedList<Product>();
    public void addProduct(Product tmpProduct){
        prodList.add(tmpProduct);
    }
    public void addProduct(int tmpIndex, Product tmpProduct){
        prodList.add(tmpIndex, tmpProduct);
    }
    public void addFirst(Product tmpProduct){
        prodList.addFirst(tmpProduct);
    }
    public void clear(){
        prodList.clear();
    }
    public boolean contains(Product tmpProduct){
        return prodList.contains(tmpProduct);
    }
    public Product get(int tmpInt){
        return prodList.get(tmpInt);
    }
    public int indexOf(Product tmpProduct){
        return prodList.indexOf(tmpProduct);
    }
    public void remove(Product tmpProduct){
        prodList.remove(tmpProduct);
    }
    public String printAllProducts(){
        return "Something";
    }
}
import java.util.ArrayList;
import java.util.Scanner;
import java.util.LinkedList;
public class TestProdList {
    public static void main(String[] args) {
        LinkedList<ProductList> mainList = new LinkedList<ProductList>();
        Scanner input = new Scanner(System.in);
        //Creating the switch variable.
        int choice = 0;
        //Creating a menu driven program.
        String menu = "nWhich would you like to do?" +
                        "n1 - Add a Product."+
                        "n2 - Find a Product by Name." +
                        "n3 - Find the Product at a Particular Position." +
                        "n4 - Remove a product." +
                        "n5 - Clear the list of Prodcuts" +
                        "n6 - Print the entire inventory of Products" +
                        "n7 - Quit";
        System.out.println(menu + "nnEnter your selection: ");
        choice = input.nextInt();
        input.nextLine();
        while (choice != 7){
            switch(choice){
            case 1: 
                String prodID;
                String prodName;
                int qty;
                float unitCost;
                System.out.println("Please enter the product id: ");
                prodID = input.nextLine();
                System.out.println("Please enter the product name: ");
                prodName = input.nextLine();
                System.out.println("Please enter a quantity: ");
                qty = input.nextInt();
                input.nextLine();
                System.out.println("Please enter a unit cost: ");
                unitCost = input.nextFloat();
                input.nextLine();   
                mainList.add(new prodList(prodID,prodName,qty,unitCost));
                break;
            case 2:
                break;
            case 3:
                break;
            case 4:
                break;
            case 5:
                break;
            case 6:
                break;
            case 7:
                break;
            default: 
                System.out.println("n~~~Invalid Selection~~~n");
                break;
            } //End of Switch statement 
        }





    }
}

您不需要将ProductList作为自己的类。只要在你的主类中使用LinkedList<Product> mainList = new LinkedList<Product>()

如果您需要引入ProductList,您应该这样做:

public class ProductList extends LinkedList<Product> {
    // only put required constructors here
    // all methods from LinkedList are inherited automatically with data type Product
}
main实例化为ProductList mainList = new ProductList();并使用为mainList.add(new Product());

相关内容

  • 没有找到相关文章

最新更新