Pergunta

This is an Object Oriented program with two classes Catalog and Products that reads a file that contains the product name, quantity, and price, adds them to a list array, and calculate the total Price of the items. Everything works with my program except that it has a toString method that when I ran the program it prints something like this @445ea7e I think this happens because the addProducts() and getProducts(String name) on the Catalog class has to do something with the toString() and I don't know how to connect them to work.

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

    public class Catalog{



    private static int MAX_ITEMS = 10;

    /**
     * List of Products objects.
     */
    private Products[] list;
    private int nextItem;


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

    /**
     * Adds an item to the list
     */
    public void addProducts (String name, int quantity, double price){
            **//I tried this: list[nextItem] = new Products(name, quantity, price);
                //but I'm not sure if that's ok.**

     }

    /**
     * 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
     */
    public Products getProducts(String name){
        **//search a list for string equality using the name of the product and returns 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.println(toString());                  **I don't know how to call toString**
             System.out.println();
             System.out.format("Total Price = %9.2f\n", catalog.getTotalPrice());
            }
}

This is the Products class with the toString() method.

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(){
    String s=null;
    s+=getName();
    s+=s + " ";
    s+=getQuantity();
    s+=s + " ";
    s+=getPrice();
    return s;    
}

} This is the file with the Products information

Football 2 15.50

ChapStick 1 3.87

Book 4 10.00

Foi útil?

Solução

  1. You add toString() to your Catalog class as Product class :

    public class Catalog {
    private static int MAX_ITEMS = 10;
    
    /**
     * List of Products objects.
     */
    private Products[] list;
    private int nextItem;
    
    // your old code here
    
      // new code
      @Override
      public String toString() {
         return "this is a catalog";
      }        
    }
    
  2. You call catalog.toString():

public static void main(String[] args) 
     throws FileNotFoundException { 
     Catalog catalog= new Catalog(); 
     catalog.loadList(args[0]);
     System.out.println();
     //System.out.println(toString());                  **I don't know how to call toString**
     // use this line
     System.out.println(catalog.toString());  
     System.out.println();
     System.out.format("Total Price = %9.2f\n", catalog.getTotalPrice());
    }
  1. In your toString() method of Product class. you should change initialize variable from s=null; to s="";. Here is sample :
public String toString(){
    String s="";
    s+=getName();
    s+=s + " ";
    s+=getQuantity();
    s+=s + " ";
    s+=getPrice();
    return s;    
}

Hope this help :)

Outras dicas

In your toString() method (in products class), change

String s = null;

to

String s = "";

Also, you have to make a toString method for the Catalog class in order to call it.

An example toString method for catalog class can be:

public String toString(){
 String toReturn = "";
 for(Products current: list){
  toReturn+=current.toString()+"\n";
 }
 return toReturn;
}

Then if main just call, System.out.println(catalog.toString());

Try adding override to your toString method this might be the solution base on your current output.

@Override
public String toString(){
    String s=null;
    s+=getName();
    s+=s + " ";
    s+=getQuantity();
    s+=s + " ";
    s+=getPrice();
    return s;    
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top