jeffbobmate
jeffbobmate

Reputation: 15

Boolean always returning as true?

So I'm working with 3 class files. A piece of my code from file 1 is calling some code from file 2 that checks to see if a product database, file3, is full. I'm expecting the databaseFull method to return a false but it seems to only return true even if there is only a single item in the database.
Can I please get some assistance as to why this is happening?
edit: upon request i have pasted all of my code

File 1 code:

public class StarberksInterface
{
Scanner console = new Scanner(System.in);

public static void main(String[] args)
{
    StarberksInterface intFace = new StarberksInterface();
    intFace.run();
}

private void run() 
{
    int option, demandRate, choice;
    double setupCost, unitCost, inventoryCost, sellPrice;
    String productName, newProductName, deleteName;
    Store store = new Store();

    do {
        System.out.println("(1) Input data for one product\n(2) Show data from one product\n(3) Show product replenishment strategy\n(4) Exit");
        option = console.nextInt();
        switch(option)
        {
            case 1: 
                System.out.println("Enter product name (3 to 10 characters): ");
                productName = console.next().toLowerCase();

                while(productName.length()>10 || productName.length()<3 ){
                    System.out.println("Product name length is not valid. Try again.");
                    productName = console.next().toLowerCase();
                }

                if (store.databaseFull()){
                    System.out.println("Database is full:\n(1) Delete a product\n(2) Menu");
                    choice = console.nextInt();
                    if (choice == 1){
                        System.out.println("Enter name of product to delete: ");
                        deleteName = console.next().toLowerCase();
                        store.deleteProduct(deleteName);
                        System.out.println("Product deleted.");
                        break;
                    }
                    else{
                        break;
                    }
                }

                if (store.productExists(productName)){
                    System.out.println("Product already exists:\n(1) Change the product name\n(2) Change product data\n(3) Menu"); 
                    choice = console.nextInt();
                    if (choice == 1){
                        System.out.println("Enter new product name: ");
                        newProductName = console.next().toLowerCase();
                        while(newProductName.length()>10 || newProductName.length()<3 ){
                            System.out.println("Product name length is not valid. Try again.");
                            newProductName = console.next().toLowerCase();
                        }
                        store.changeName(productName, newProductName);

                    }else if (choice == 2){
                        System.out.println("Enter new demand rate: ");
                        demandRate = console.nextInt();
                        demandRate = checkInput(demandRate);

                        System.out.println("Enter new setup cost: ");
                        setupCost = console.nextDouble();
                        setupCost = checkInput(setupCost);

                        System.out.println("Enter new unit cost: ");
                        unitCost = console.nextDouble();
                        unitCost = checkInput(unitCost);

                        System.out.println("Enter new inventory cost: ");
                        inventoryCost = console.nextDouble();
                        inventoryCost = checkInput(inventoryCost);

                        System.out.println("Enter new sell price: ");
                        sellPrice = console.nextDouble();
                        sellPrice = checkInput(sellPrice);

                        System.out.println("Data changed.");

                        store.changeData(productName, demandRate, setupCost, unitCost, inventoryCost, sellPrice);                     
                    } 
                    else{
                        break;
                    }
                }
                else{
                    // user inputs
                    System.out.println("Enter demand rate: ");
                    demandRate = console.nextInt();
                    demandRate = checkInput(demandRate);

                    System.out.println("Enter setup cost: ");
                    setupCost = console.nextDouble();
                    setupCost = checkInput(setupCost);

                    System.out.println("Enter unit cost: ");
                    unitCost = console.nextDouble();
                    unitCost = checkInput(unitCost);

                    System.out.println("Enter inventory cost: ");
                    inventoryCost = console.nextDouble();
                    inventoryCost = checkInput(inventoryCost);

                    System.out.println("Enter sell price: ");
                    sellPrice = console.nextDouble();
                    sellPrice = checkInput(sellPrice);

                    store.addProduct(productName, demandRate, setupCost, unitCost, inventoryCost, sellPrice);
                }
                break;

            case 2:
                System.out.println("Enter product name (3 to 10 characters): ");
                productName = console.next().toLowerCase();

                while(productName.length()>10 || productName.length()<3 ){
                    System.out.println("Product name length is not valid. Try again.");
                    productName = console.next().toLowerCase();
                }

                while (!store.productExists(productName)){
                    System.out.println("This product does not exist, try again.");
                    productName = console.next().toLowerCase();

                    while(productName.length()>10 || productName.length()<3 ){
                        System.out.println("Product name length is not valid. Try again.");
                        productName = console.next().toLowerCase();
                    }
                }

                //at this point, the product entered will exist in the store, so you will be able to show the product details
                showData(store.getProduct(productName));

                break;

            /**case 3: System.out.println
             * 
             * 
             * 
                break;**/

            case 4: break;

            default: System.out.println("invalid option");
        }
    }
    while(option!=4);
}

public void option1(){ 

}

public void option2(){

}

public void option3(){

}

public void option4(){

}

// This function checks integer inputs for negative numbers and returns a message and/or the input.
public int checkInput(int input){

    while(input < 0){
        System.out.println("Cannot input negative number. Try again.");
        input = console.nextInt();
    }

    return input;
}

// This function checks double inputs for negative numbers and returns a message and/or the input.
public double checkInput(double input){

    while(input < 0){
        System.out.println("Cannot input negative number. Try again.");
        input = console.nextDouble();
    }

    return input;
}

private static void showData(Product product)
{
  System.out.println("Name = "+product.getProductName());
  System.out.println("Demand rate = "+product.getDemandRate());
  System.out.println("Setup cost = "+product.getSetupCost());
  System.out.println("Unit cost = "+product.getUnitCost());
  System.out.println("Inventory cost = "+product.getInventoryCost());
  System.out.println("Sell price = "+product.getSellPrice());
}

}

File 2 code:

public class Store
{
private Product product1;
private Product product2;
private Product product3;

public Store(){
    product1 = null;
    product2 = null;
    product3 = null;
}

public void addProduct(String name, int demand, double setup, double unit, double inventory, double sell ){
    if(product1 == null){
        product1 = new Product();
        product1.setProductName(name);
        product1.setDemandRate(demand);
        product1.setSetupCost(setup);
        product1.setUnitCost(unit);
        product1.setInventoryCost(inventory);
        product1.setSellPrice(sell);
    }

    if(product2 == null){
        product2 = new Product();
        product2.setProductName(name);
        product2.setDemandRate(demand);
        product2.setSetupCost(setup);
        product2.setUnitCost(unit);
        product2.setInventoryCost(inventory);
        product2.setSellPrice(sell);
    }

    if(product3 == null){
        product3 = new Product();
        product3.setProductName(name);
        product3.setDemandRate(demand);
        product3.setSetupCost(setup);
        product3.setUnitCost(unit);
        product3.setInventoryCost(inventory);
        product3.setSellPrice(sell);
    }
}

public void changeName(String oldName, String newName){
    if(product1 != null){
        if(product1.getProductName().equals(oldName))
            product1.setProductName(newName);
    }

    if(product2 != null){
        if(product2.getProductName().equals(oldName))
            product2.setProductName(newName);
    }

    if(product3 != null){
        if(product3.getProductName().equals(oldName))
            product3.setProductName(newName);
    }  
}

public void changeData(String name, int demand, double setup, double unit, double inventory, double sell ){
    if(product1 != null){
        if(product1.getProductName().equals(name))
            product1.setDemandRate(demand);
            product1.setSetupCost(setup);
            product1.setUnitCost(unit);
            product1.setInventoryCost(inventory);
            product1.setSellPrice(sell);
    }

    if(product2 != null){
        if(product2.getProductName().equals(name))
            product2.setDemandRate(demand);
            product2.setSetupCost(setup);
            product2.setUnitCost(unit);
            product2.setInventoryCost(inventory);
            product2.setSellPrice(sell);
    }

    if(product3 != null){
        if(product3.getProductName().equals(name))
            product3.setDemandRate(demand);
            product3.setSetupCost(setup);
            product3.setUnitCost(unit);
            product3.setInventoryCost(inventory);
            product3.setSellPrice(sell);
    }  
}

public boolean productExists(String name){
    if (product1 != null){
        if (name.equals(product1.getProductName()))
            return true;
    }

    if (product2 != null){
        if (name.equals(product2.getProductName()))
            return true;
    }

    if (product3 != null){
        if (name.equals(product3.getProductName()))
            return true;
    }

    return false; 
}

public Product getProduct(String name){
    if (product1 != null){
        if (name.equals(product1.getProductName()))
            return product1;
    }

    if (product2 != null){
        if (name.equals(product2.getProductName()))
            return product2;
    }

    if (product3 != null){
        if (name.equals(product3.getProductName()))
            return product3;
    }

    return null; 
}

public boolean databaseFull(){
    return (product1 != null && product2 != null && product3 != null);      
}

public void deleteProduct(String name){
    if (name.equals(product1.getProductName())){
        product1 = null;
    }

    if (name.equals(product2.getProductName())){
        product2 = null;
    } 

    if (name.equals(product3.getProductName()))
        product3 = null;
} 

}

File 3 code:

public class Product
{
private String name;
private int demand;
private double setup, unit, inventory, sell;

public Product()
{
  name = "NO NAME";
  demand = 0;
  setup = 0;
  unit = 0;
  inventory = 0;
  sell = 0;
}

public void setProductName(String productName)
{
    name = productName;
}

public void setDemandRate(int demandRate)
{
    demand = demandRate;
}

public void setSetupCost(Double setupCost)
{
    setup = setupCost;
}

public void setUnitCost(Double unitCost)
{
    unit = unitCost;
}

public void setInventoryCost(Double inventoryCost)
{
    inventory = inventoryCost;
}

public void setSellPrice(Double sellPrice)
{
    sell = sellPrice;
}

public String getProductName()
{
  return name;
}

public int getDemandRate()
{
  return demand;
}

public double getSetupCost()
{
  return setup;
}

public double getUnitCost()
{
  return unit;
}

public double getInventoryCost()
{
  return inventory;
}

public double getSellPrice()
{
  return sell;
}

}

Upvotes: 1

Views: 209

Answers (1)

Guillaume Barr&#233;
Guillaume Barr&#233;

Reputation: 4218

The first time you pass into the addProduct() method your are setting product1, product2 and product3 with new Product().

public void addProduct(String name, int demand, double setup, double unit, double inventory, double sell ){
    if(product1 == null){
        product1 = new Product();
        //...
    }

    if(product2 == null){
        product2 = new Product();
        //..
    }

    if(product3 == null){
        product3 = new Product();
        //..
    }
}

So later because all those 3 attributes are referencing valid Product instances :

product1 != null returns true

product2 != null returns true

product3 != null returns true

Then

(product1 != null && product2 != null && product3 != null) will return true because you always create 3 products in your store. ( BTW you are creating 3 times the same )

Try this :

public void addProduct(String name, int demand, double setup, double unit, double inventory, double sell ){
    if(product1 == null){
        product1 = new Product();
        //...
    }else if(product2 == null){
        product2 = new Product();
      //...
    }else if(product3 == null){
        product3 = new Product();
      //...
    }
}

Upvotes: 1

Related Questions