2016-04-11 14 views
1

Also ich arbeite mit 3 Klassen-Dateien. Ein Teil meines Codes aus Datei 1 ruft einen Code aus Datei 2 auf, der überprüft, ob die Produktdatenbank file3 voll ist. Ich erwarte, dass die Methode "databaseFull" ein false zurückgibt, aber es scheint nur wahr zurückzugeben, selbst wenn nur ein einzelnes Element in der Datenbank vorhanden ist.
Kann ich bitte etwas Hilfe bekommen, warum dies geschieht?
bearbeiten: auf Anfrage Ich habe alle meine CodeBoolean immer als wahr zurück?

eingefügt

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

}

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

}

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

}

+1

Debuggen Sie helfen könnten. Wir haben keine Ahnung von Ihrer DB-Seite. –

+1

Was sind product1-2-3 ?? Veröffentlichen Sie die ganze Klasse. –

+0

Ihre Store-Klasse kompiliert nicht, daher bezweifle ich sehr, dass die Methode true zurückgibt – Stultuske

Antwort

1

Sie zum ersten Mal in die addProduct() Methode übergeben Ihre product1, product2 und product3 mit new Product() setzen.

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 später, weil diese alle drei Attribute verweisen auf gültige Produkt Instanzen:

product1 != null kehrt true

product2 != null kehrt true

product3 != null kehrt true

Dann

(product1 != null && product2 != null && product3 != null) wird wahr zurück, weil Sie immer 3 Produkte in Ihrem Shop erstellen. (BTW Sie erstellen 3 mal das gleiche)

Versuchen Sie folgendes:

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(); 
     //... 
    } 
} 
+0

Ich bin mir nicht ganz sicher, was du sagst. Du meinst, dass alle 3 Produkte initialisiert wurden und sie somit nicht mehr null sind? Würde ich die if-Anweisungen von addProduct ändern, um dieses Problem zu beheben? Bedeutet das auch, dass alle 3 von ihnen die Werte der Benutzereingabe annehmen? – jeffbobmate

+0

Yep sehe mein Update ... – Nirekin

+0

ausgezeichnet! Alles scheint jetzt zu funktionieren. vielen Dank. – jeffbobmate

Verwandte Themen