Search code examples
javajsonmethodsoverloading

Program not printing out all information


I am creating a store front application in Java. I have my main driver program with 5 different classes it is using. I cannot get it to print out my inventory I have listed in my JSON file. What am I doing wrong or what am I missing here? My FileService class is at the bottom from where the driver is calling the method. I think that is where my problem is.

package app;

import java.io.IOException;
import java.util.*;

public class StoreFront {
    
    static InventoryManager inventoryManager = new InventoryManager();
    static ShoppingCart shoppingCart = new ShoppingCart();
    static Scanner scnr = new Scanner(System.in);
//  static FileService fileService = new FileService();
    
    private static void viewProducts() { 
        inventoryManager.displaySalableProducts();
    }
    
    private static void purchaseProducts() {
        System.out.print("Enter the name of the product you want to purchase: ");
        scnr.nextLine(); // Consume newline character
        String productName = scnr.nextLine();

        SalableProduct productToPurchase = inventoryManager.findProductByName(productName);
        if (productToPurchase == null) {
            System.out.println("Product not found.");
        } else {
            if (productToPurchase.getQty() > 0) {
                shoppingCart.addItem(productToPurchase);
                productToPurchase.setQty(productToPurchase.getQty() - 1);
                System.out.println("Product purchased successfully.");
            } else {
                System.out.println("Product is out of stock.");
            }
        }
        
    }
    
    private static void returnProducts() {
        shoppingCart.returnProduct(null);
    }


    public static void main(String[] args) throws IOException{

/*      
 * old method of manually adding inventory
        inventoryManager.addProduct(new Food("Goji Berry", "bright orange-red berry that comes from a shrub native to Asia", 8.32, 496));
        inventoryManager.addProduct(new Potion("Elixir", "Potion that grants eternal life", 3469.00, 4));
        inventoryManager.addProduct(new Helmet("Kabuto", "Ancient Japanese war helemt", 999.99, 43));
        inventoryManager.addProduct(new Tomahawk("Polished Tomahawk", "Benjamin Martins Tomahawk", 567.00, 1));
        inventoryManager.addProduct(new Shield("Heater Shield", "Used in the Middle Ages by knights and soldiers", 269.00, 57));
        inventoryManager.addProduct(new Sword("Katana", "single-edged sword used by Japanese samurai", 4420.00, 21));
*/      
        inventoryManager.initializeInventoryFromFile("inventory.json");
        System.out.println("Welcome to the Game Store \n");
        System.out.println("Please select an option from the menu");
        System.out.println("***************************************");
        System.out.println("************MAIN MENU ******************\n");
        while (true) {
            System.out.println("Enter one of the following:\n\n"+
            "'1' : View Products\n"+
            "'2' : Purchase Products\n"+
            "'3' : Return Products\n"+
            "'4' : Exit");
            
            int menuChoice = scnr.nextInt();
            
            switch(menuChoice) {
            case 1:
                viewProducts();
                break;
            case 2:
                purchaseProducts();
                break;
            case 3:
                returnProducts();
                break;
            case 4:
                System.out.println("Thank you for coming by. Please come again.");
                System.exit(0);
                break;
                
            } //scnr.close();
        }
    }
}

File Service Class method

   public static ArrayList<SalableProduct> loadProductsFromFile(String filename) {

        
            ArrayList<SalableProduct> products = new ArrayList<SalableProduct>();
            try {
                File file = new File(filename);
                Scanner scnr = new Scanner(file);
                ObjectMapper objectMapper = new ObjectMapper();
                
                while (scnr.hasNext()) {
                    String json = scnr.nextLine();
                    SalableProduct product = objectMapper.readValue(json, SalableProduct.class);
                    products.add(product);
                }
                scnr.close();
            } catch (IOException e) {
                e.printStackTrace();
            } return products;
        }

I have tried running my program in debug mode and watch all the steps closely but have not had any luck. It works when I hardcode/manually enter the inventory but hasn't since I implemented the JSON file and methods to call it.

Here is the SalableProduct Class

    public class SalableProduct {
        /**
         * placeholder for items to sell in store
         */
        private String name;
        private String description;
        private double price;
        private int qty;
    
        public String getName() {
            return name;
        }
    
    
        public SalableProduct(String name, String description, double price, int qty) {
            super();
            this.name = name;
            this.description = description;
            this.price = price;
            this.qty = qty;
        }
    
        public String getDescription() {
            return description;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
    
        public void setDescription(String description) {
            this.description = description;
        }
    
        public void setPrice(double price) {
            this.price = price;
        }
    
        public double getPrice() {
            return price;
        }
    
        public int getQty() {
            return qty;
        }
        
        public void setQty(int qty) {
            this.qty = qty;
        }
    
        public SalableProduct() {
            name = "";
            description = "";
            price = 0;
            qty = 0;
        }
    }
Here is my JSON file
    {"name":"Goji Berry","description":"bright orange-red berry that comes from a shrub native to Asia","price":8.32,"qty":496}
    {"name":"Elixir","description":"Potion that grants eternal life","price":3469.00,"qty":4}
    {"name":"Kabuto","description":"Ancient Japanese war helmet","price":999.99,"qty":43}
    {"name":"Polished Tomahawk","description":"Benjamin Martins Tomahawk","price":567.00,"qty":1}
    {"name":"Heater Shield","description":"Used in the Middle Ages by knights and soldiers","price":269.00,"qty":57}
    {"name":"Katana","description":"single-edged sword used by Japanese samurai","price":4469.00,"qty":21}

Here is the displaySalable products

        for (SalableProduct product : products) {
            System.out.println(product.getName() + " - " + product.getDescription() + " - Price: $" + product.getPrice()
                    + " - Quantity: " + product.getQty());
        }
    }

Solution

  • My issue was residing in my Product class, I had previously had and was using SalableProduct as my super class but Product needed to be on its own. I also had to add generics to all of my classes. So now my code is this.

    package app;
    
    public class Product <T extends SalableProduct> {
    
        public Product(String name, String description, double price, int qty) {    
        }
    }
    

    I also added this line to my StoreFront class in the main method.

    inventoryManager.initializeInventoryFromFile("inventory.json");
    

    here is my method for initializeInventoryFromFile method in my InventoryManager class.

        public void initializeInventoryFromFile(String filename) {
             ArrayList<T> products = (ArrayList<T>) FileService.loadProductsFromFile("inventory.json");
    
            for (T product : products) {
                addProduct( product);
                
            }
        }