2015-09-16 1 views
-1

Я пытаюсь, чтобы эта программа добавила объект «Продукт» в мой связанный список в ProductList. »(Как только я получу то, что я могу сделать остальные) м не уверен, если я сделал LinkedList в моем главном праве, или если я даже нужен, но я просто не могу показаться, чтобы выяснить, как это сделать, и любая помощь будет оценена. Спасибо!Я мог бы использовать некоторые рекомендации с LinkedLists в java. В частности, добавление объекта в связанный список в другом классе

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 + "\n\nEnter 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 
     } 











    } 
} 
+0

В чем смысл вашего класса ProductList? Кажется, у вас просто есть переменная экземпляра 'LinkedList' и методы, которые выполняют только ту же функцию, что и' LinkedList'? Почему бы просто не сделать «LinkedList» объектов «Product» в главном, а не «LinkedList» вашего 'ProductList'? –

+0

Я должен сделать это таким образом, исходя из моих указаний. Я также думаю, что это не имеет никакого смысла и совершенно бессмысленно, но я должен иметь его там. – brandin

ответ

0

Вы дон Не нужно ProductList как собственный класс. Просто используйте LinkedList<Product> mainList = new LinkedList<Product>() в своем классе.

Если у вас есть требование ввести ProductList, вы должны с ним обращаться как fo llows:

public class ProductList extends LinkedList<Product> { 
    // only put required constructors here 
    // all methods from LinkedList are inherited automatically with data type Product 
} 

В main Instantiate, как ProductList mainList = new ProductList(); и использования в качестве mainList.add(new Product());