1

I am doing an Object Oriented program which contains a class Catalog and a class Products. Catalog has a method which suppose to search a list of products with a specific name that are read from a file. Everything works but the getProducts is not working.

This is the Catalog class with the getProducts(String name)

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());
            }
        }

This is Products class

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;
    }

This is the file

    Football 2 15.50
    Football-Jersey 2 30.95
    ChapStick 1 3.87
    Book 4 10.00
    Book-Journal 1 5.00
Jason C
  • 38,729
  • 14
  • 126
  • 182
Ruben
  • 61
  • 2
  • 7

2 Answers2

0

Item is an Object, so you can try to get the name by using dot like this

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

or

 if(item.getName.equalsIgnoreCase(name))
newuser
  • 8,338
  • 2
  • 25
  • 33
0

You have:

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

Notice that item is a Products but you are comparing it to the String, name. You would want to compare the product's name, e.g.:

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

You can also use String.equalsIgnoreCase() if names are case-insensitive, possibly using trim() first if leading/trailing whitespace is an issue, too.

Jason C
  • 38,729
  • 14
  • 126
  • 182
  • and should I return item; I think item.getName() was the problem thank you! – Ruben Mar 06 '14 at 02:27
  • @Ruben Returning the item would make the most sense. A fundamental principal of OOP (and in general) is thinking about what you want to do conceptually (e.g. "I would like to search a catalog for a *product* given *its name*") and then echoing that in code (e.g. `Product getProduct (String name)` in `Catalog`, which searches for a product whose name [`item.getName()`] matches what you are looking for [`item.getName().equals(name)`] and returns the product [`return item`] for later use). – Jason C Mar 06 '14 at 02:38
  • (^ I hope that last comment made sense. Sorry if it did not.) – Jason C Mar 06 '14 at 02:41
  • 1
    Thanks a lot you not only answered my question but you made me comprehend more of OOP :) – Ruben Mar 06 '14 at 02:45