2014-03-06 60 views
1

我正在做一個面向對象的程序,其中包含一個類目錄和一個類產品。目錄有一個方法,它假定搜索從文件中讀取的具有特定名稱的產品列表。一切正常,但getProducts不起作用。搜索具有特定名稱的項目列表

這是目錄類用的getProducts(字符串名稱)

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.* 

public class Catalog{ 



    private static int MAX_ITEMS = 10; 

    private Products[] list; 

    private int nextItem; 


    /** 
    * Default constructor 
    */ 
    public Catalog(){ 
     list = new Products[MAX_ITEMS]; 
     nextItem = 0; 
    } 

    /** 
    * Reads items from an input file. 
    */ 
    public void loadList(String fileName) 
      throws FileNotFoundException { 
     if ((fileName != null) && (!fileName.equals(""))) { 
      Scanner input = new Scanner(new File(fileName)); 
      String newLine = null; 
      String name = null; 
      int quantity = 0; 
      double price = 0.0; 


      while (input.hasNextLine() && nextItem < MAX_ITEMS) { 
       if (input.hasNext()) { 
        name = input.next(); 
       } else { 
        System.err.println("ERROR Not a String"); 
        System.exit(2); 
       } 
       if (input.hasNextInt()) { 
        quantity = input.nextInt(); 
       } else { 
        System.err.println("ERROR Not an integer"); 
        System.exit(2); 
       } 
       if (input.hasNextDouble()) { 
        price = input.nextDouble(); 
       } else { 
        System.err.println("ERROR Not a double"); 
        System.exit(2); 
       } 
       list[nextItem] = new Products(name, quantity, price); 
       newLine = input.nextLine(); 
       nextItem += 1; 
      } 
     } 
     return; 
    } 

    /** 
    * Calculate the total cost of products. 
    */ 
    public double getTotalPrice(){ 
     double total = 0.0; 
     for(int i=0; i < nextItem; i++){ 
      Products products = list[i]; 
      total+=products.getTotalPrice(); 
     } 
     return total; 
    } 

    /** 
    * Search Catalog items with a product name and returns it to caller 
    */ 
    public Products getProducts(String name){ 
     **//search a list for string equality using the name of the product and returns it to the caller** 
     for(int i=0; i<nextItem; i++){ 
      Products item = list[i]; 
      if(item.equals(name)){ 
       return item; //What is suppose to be returned or how to 
       //return it to the caller 
      } 

      public static void main(String[] args) 
        throws FileNotFoundException { 
       Catalog catalog= new Catalog(); 
       catalog.loadList(args[0]); 
       System.out.println(); 
       System.out.format("Total Price = %9.2f\n", catalog.getTotalPrice()); 
      } 
     } 

這是產品

public class Products { 

    private String name; 
    private int quantity; 
    private double price; 

    /** 
    * Constructor. 
    */ 
    public Products(String name, int quantity, double price){ 
     this.name = name; 
     this.quantity = quantity; 
     this.price = price; 
    } 
    /** 
    * Gets name of the product. 
    */ 
    public String getName(){ 
     return this.name; 
    } 
    /** 
    * Gets the quantity of products. 
    */ 
    public int getQuantity(){ 
     return this.quantity; 
    } 
    /** 
    * Gets the cost per product. 
    */ 
    public double getPrice(){ 
     return price; 
    } 
    /** 
    * set quantity of the products. 
    */ 
    public void setQuantity(int quantity){ 
     this.quantity=quantity; 
    } 
    /** 
    * Calculate the total price. 
    */ 
    public double getTotalPrice(){ 
     return quantity * price; 
    } 
    /** 
    * Returns a spaced-separated list of the attributes. 
    */ 
    public String toString(){ 
     toStr=""; 
     toStr= toStr + getName(); 
     return toStr; 
    } 

這是文件

 
    Football 2 15.50 
    Football-Jersey 2 30.95 
    ChapStick 1 3.87 
    Book 4 10.00 
    Book-Journal 1 5.00 
+0

您有任何問題嗎? –

+0

搜索時發生了什麼? – gks

+0

getProducts方法不讓我返回字符串相等(名稱)給調用者。 – Ruben

回答

0

您有:

public Products getProducts(String name){ 
    ... 
    for(int i=0; i<nextItem; i++){ 
     ... 
     Products item = list[i]; 
     if(item.equals(name)) { 
      ... 

注意itemProducts但你比較它的Stringname。你會要比較產品的,例如:

 if(item.getName().equals(name)) { 

您還可以使用String.equalsIgnoreCase()如果名稱是區分大小寫的,可能使用trim()第一,如果前/後空格是一個問題了。

+0

並且我應該退貨;我認爲item.getName()是問題謝謝你! – Ruben

+0

@Ruben返回該項目將是最有意義的。 OOP(一般來說)的一個基本原則是思考你想要在概念上做什麼(例如,「我想搜索*產品的目錄*給出*它的名稱*」),然後在代碼中回顯(例如'在'Catalog'中搜索名稱爲['item.getName()']的產品getProduct(String name)'[item.getName()。equals(name)']並返回產品['退貨產品]供以後使用)。 –

+0

(^希望最後一條評論有意義,對不起)。 –

0

項目是一個對象,所以你可以嘗試使用這樣的點來獲取名稱

if(item.getName().equals(name)) 

if(item.getName.equalsIgnoreCase(name)) 
+0

檢查字符串的默認方法是空的字符串是空的或更多的使用此getter更有意義上下文,因爲它存在。 –

+1

您需要調用getter,即'item.getName()'(使用'()')。 – ajp15243

+0

賈森C非常感謝... – newuser

相關問題