Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is a project I am working on. The point of the program is for a user to search for and rate products in a store (Like Yelp but for the grocery store). Product Class is abstract and the two classes, Food and NonFood both extend Product.
The Food Class objects CAN, but don't have to, contain a recipe.


In the TestProject Class I am having a few issues.

1) When I add a product to the Food class and enter type in a recipe, it automatically says "This product does not contain a recipe" even though I entered one.
2) When I add a product, then rate it, then search it, the product's rating does not calculate at all and the recipe file is null.
3) if I search for a product that does not exist, the console does not print out the message I added in the switch case else if statement that states "The product you entered does not exist"

import java.util.ArrayList;

public abstract class Product implements Ratable, Comparable<Product> 
{
   private String name;
   protected double averageRating;
   private String image;
   ArrayList<Double> ratings = new ArrayList<Double>();

  
   public Product(String name, double averageRating, String image) 
   {
       super();
       this.averageRating = averageRating;
       this.name = name;
       this.image = image;
   }
   public Product(String name, String image)
   {
	   this.name = name;
	   this.image = image;
   }

   /**
   * @return the name
   */
   public String getName()
   {
       return name;
   }

   /**
   * @return the rating
   */
   public double calculateRating(ArrayList<Double> ratings) 
   {
	   double sum =0;
	   if(ratings.size()>0)
	   {
		   for(int index =0; index < ratings.size(); index ++)
		   {
			   sum+=averageRating;
		   }
		   averageRating = sum/ratings.size();
	   }
	   
       return averageRating;
   }
   public double getRating()
   {
	   return calculateRating(ratings);
   }

   /**
   * @return the image
   */
   public String getImage() 
   {
       return image;
   }

   /**
   * @param name the name to set
   */
   public void setName(String name) 
   {
       this.name = name;
   }

   /**
   * @param rating the rating to set
   */
   public void setRating(double averageRating) 
   {
       this.averageRating = averageRating;
   }

   /**
   * @param image the image to set
   */
   public void setImage(String image)
   {
       this.image = image;
	   
   }


   public String toString() 
   {
       return "Product [name=" + getName() + ", rating=" + getRating() + ", image=" + getImage() + "]";
   }
  
  
}


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

public class Food extends Product 
{
   private String recipeFile;

   public Food(String name, double rating, String image) 
   {
       super(name, rating, image);
   }
   public Food(String name, String image)
   {
	   super(name, image);
   }

   @Override
   public int compareTo(Product object) 
   {
       if (this.getRating() > object.getRating()) 
       {
           return 1;
       } else if (this.getRating() < object.getRating()) 
       {
           return 1;
       } else {
           return 0;
       }
   }

   /**
   * @return the recipeFile
   */
   public String getRecipeFile() 
   {
       return recipeFile;
   }

   /**
   * @param recipeFile the recipeFile to set
   * @throws FileNotFoundException
   */
   public void setRecipeFile(String recipeFile) throws FileNotFoundException 
   {
       File file = new File(recipeFile);
       if (!file.exists())
       {
    	   throw new FileNotFoundException("This product does not contain a recipe");
       }
       this.recipeFile=recipeFile;
      
   }

   @Override
   public String toString() 
   {
       return "Product Name=" + getName() + ", Product Rating=" + getRating()
               + ", Image=" + getImage() + ", Recipes =" + getRecipeFile();
   }
@Override
public double calculateRating(ArrayList<Double> ratings)
{
	return averageRating;
	
}

  

}

import java.util.ArrayList;

public class NonFood extends Product 
{

   public NonFood(String name, int rating, String image) 
   {
       super(name, rating, image);
   }
       
   public NonFood(String name, String image)
   {
	   super(name, image);
   }

   @Override
   public int compareTo(Product object) 
   {
       if (this.getRating() > object.getRating()) 
       {
           return 1;
       } 
       else if (this.getRating() < object.getRating()) 
       {
           return 1;
       } 
       else 
       {
           return 0;
       }
   }

@Override
public double calculateRating(ArrayList<Double> ratings)
{
	return averageRating;
	
}

}

import java.util.ArrayList;

public interface Ratable 
{
   public double calculateRating(ArrayList<Double> ratings);

   public double getRating();

}


import java.io.FileNotFoundException;
	import java.util.ArrayList;
	import java.util.Collections;
	import java.util.List;
	import java.util.Random;
	import java.util.Scanner;

	public class TestProject 
	{

	   public static void main(String[] args) 
	   {
	       List<Product> products = new ArrayList<Product>();
	       Scanner scan = new Scanner(System.in);
	       boolean isExit = false;
	       while (!isExit) {
	           System.out.println("Please make your selection from the following options");
	           System.out.println("Press 1 : Search for a Product");
	           System.out.println("Press 2 : Rate a Product");
	           System.out.println("Press 3 : Compare with other products");
	           System.out.println("Press 4 : Add Product");
	           System.out.println("Press 5 : Exit");
	           System.out.print("Please select appropriate option : ");
	           int option = scan.nextInt();
	           int rateOption=0;
	           ArrayList<Double> ratings = new ArrayList<Double>();
	           double input = 0;
	           
	           scan.nextLine();
	           switch (option) 
	           {
	           case 1:
	               System.out.print("Please enter Prodcut name :");
	               String productName = scan.nextLine();
	               for (Product product : products) 
	               {
	                   if (product.getName().equals(productName))
	                   {
	                       System.out.println(product.toString());
	                   }
	                   else if(product.getName().equals(null))
	                   {
	                	   System.out.println("The product you entered does not exist");
	                   }
	                   
	               }
	               break;
	           case 2:
	               System.out.print("Please enter Product name :");
	               String prodName = scan.nextLine();
	               for (Product product : products) 
	               {
	                   if (product.getName().equals(prodName))
	                   {
	               
	                	   System.out.println("How would you rate this product?");
	                	   System.out.println("Enter (1) if you would not recommend");
	                	   System.out.println("Enter (2) if you found the product to be satisfactory");
	                	   System.out.println("Enter (3) if you would recommend");
	                   }
	                   rateOption = scan.nextInt();
	                   switch (rateOption)
	                   {
	                	   case 1:
	                		   input = 1.0;
	                		   ratings.add(input);
	                		   break;
	                	   case 2:
	                		   input = 2.0;
	                		   ratings.add(input);
	                		   break;
	                	   case 3:
	                		   input = 3.0;
	                		   ratings.add(input);
	                		   break;
	                		   default: System.out.println("You entered an invalid option");
	                		   break;
	                   }   
	                   
	               }
	               break;
	           case 3:
	               System.out.println("Products printed in sorted order");
	               Collections.sort(products);
	               for (Product product : products) 
	               {
	                   System.out.println(product.toString());
	               }

	               break;
	           case 4:
	               System.out.print("Which product you want to add? (F)ood or (N)onFood : ");
	               String type = scan.nextLine();
	               System.out.print("Please enter product Name : ");
	               String proName = scan.nextLine();
	               System.out.print("Please enter image url : ");
	               String image = scan.nextLine();
	               Product product = null;
	               if (type.equals("F")) 
	               {
	                   System.out.print("Please enter recipe file :");
	                   String recipeFile = scan.nextLine();
	                   product = new Food(proName, image);
	                   try 
	                   {
	                       ((Food) product).setRecipeFile(recipeFile);
	                   } 
	                   catch (FileNotFoundException e) 
	                   {
	                       System.out.println(e.getMessage());
	                   }
	               } 
	               else 
	               {
	                   product = new NonFood(proName, image);
//	                   
	               }
	               products.add(product);
	               break;
	           case 5:
	               isExit = true;
	               break;
	           default:
	               isExit = true;
	               break;

	           }
	           System.out.println();

	       }
	       scan.close();
	   }

	}


What I have tried:

I have tried changing the calculateRatings method
Posted
Updated 5-May-21 10:17am

1 solution

In your search (case 1) for a product you have the following else clause:
Java
else if(product.getName().equals(null))
{
   System.out.println("The product you entered does not exist");
}

But if the product does not exist you will never find a product with a null name. you need to set a flag or some variable when you find a product. Then at the end of the loop if that variable is null, you can then write the message.

You are building a list of ratings, but the ratings should be members of the product class.

there are probably other things but there is too much code here to analyse completely. I suggest you try some debugging to find other issues.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900