6 Replies - 365 Views - Last Post: 30 July 2013 - 07:26 PM Rate Topic: -----

#1 IceDragon81  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 20-July 13

Inventory 3 program not showing desired output.

Posted 28 July 2013 - 10:31 PM

Hello, I am having some issues with the program not listing all of the needed output. It is not showing the Manufacturer Name and the value of the inventory with the restocking fee. Am I missing something? Do I have too much in the program. Thank you in advance for the assistance.

package inventory3;

import java.text.DecimalFormat;

public class Inventory3 {
    
    
    public static void main(String[] args) {
         //create products
      
    Product one = new Product("Jack", 68048, 46, 89.99,"Pittsburg");
    Product two = new Product("Compressor", 68501, 73, 119.99, "Central Pneumatic");
    Product three = new Product("Flyswatter", 40122, 129, 9.99,"Pittsburg");
    Product four = new Product("2000lb Winch", 68142, 15, 59.99, "Badlands");
        
    
    // enter products into inventory
    
    Inventory inventory = new Inventory();
    inventory.add(one);
    inventory.add(two);
    inventory.add(three);
    inventory.add(four);
    
    inventory.bubbleSort();
    
    
    
    for(int i=0; i<inventory.getCount();i++)
    {
    Product product = inventory.get(i);
    System.out.println(product);
}
    
    DecimalFormat df= new DecimalFormat("$#,##0.00");
    String value = df.format(inventory.getInvValue());
    System.out.println("\n Value of the entire inventory: " + value);
}
}




class Product{
    
    
    private String ItemName;
    private int ItemNumber;
    private int Onhand;
    private double ItemCost;
    
    
    public Product(String ItemName, int ItemNumber, int Onhand, double ItemCost,String ManufacturerName){
        this.ItemName= ItemName;
        this.ItemNumber= ItemNumber;
        this.Onhand= Onhand;
        this.ItemCost= ItemCost;
    }
      
     public String getItemName(){
         return ItemName;
     }
     
     public int getItemNumber(){
         return ItemNumber;
     }
     
     public int getOnhand(){
         return Onhand;
     }
     
     public double getItemCost(){
         return ItemCost;
     }
     public double getInvValue(){
         return Onhand * ItemCost;
     }
     
     
     public void setItemName(String ItemName){
         this.ItemName= ItemName;
     }
     public void setItemNumber(int ItemNumber){
         this.ItemNumber= ItemNumber;
     }
     public void setOnhand(int Onhand){
         this.Onhand= Onhand;
     }
     public void setItemCost(double ItemCost){
         this.ItemCost= ItemCost;
     }
     
     @Override
     public String toString(){
         String NameStr= "Item Name" + String.format("%-20s|",ItemName);
         String NumStr= "Item Number" + String.format("%-4s|",ItemNumber);
         String QuantStr= "On Hand" + String.format("%-4d|",Onhand);
         String CostStr= "Item Cost" + String.format("$%6.2f|", ItemCost);
         String ValueStr= "Inventory Cost" + String.format("$%8.2f",getInvValue());
         return NameStr + NumStr + QuantStr + CostStr + ValueStr;
         
     }

    void showProduct() {
        throw new UnsupportedOperationException("Not supported yet."); 
    }
}

  class Inventory {
      
      private Product products[];
      private int count;
      
      public Inventory(){
          products = new Product[100];
          count=0;
      }
      
      public void add(Product p){
          products[count++]=p;
      }
      
      public Product get(int index) {
          return products[index];
      }
       public void bubbleSort(){
           int n = getCount();
           for (int search = 1; search<n;search++){
               for (int i = 0; i<n-search;i++){
                   String nameA= products[i].getItemName();
                   String nameB= products[i+1].getItemName();
                   if(nameA.compareToIgnoreCase(nameB)>0){
                       Product temp = products[i];
                       products[i] = products[i+1];
                       products[i+1]=temp;
                   }
               }
           }
       }
       
       
       public double getInvValue()
       {
           double value = 0.00;
           for(int i = 0;i<count;i++){
               value += products[i].getInvValue();
           }
           return value;
       }
       public int getCount(){
           return count;
       }
  
       class InventoryPart3 extends Product
       {
           // Brings in the variables from Inventory class
           
           private String ManufacturerName;
           
           // define the new constructor with 5 elements
           
           InventoryPart3(String ItemName, int ItemNumber, int Onhand, double ItemCost, String ManufacturerName)
           {
               super(ItemName, ItemNumber, Onhand, ItemCost,ManufacturerName);
            }
           //set ManufacturerName
           public void setManufacturerName(String ManufacturerName)
           {
               this.ManufacturerName=ManufacturerName;
           }//end set manufacturer name
           
           public String getManufacturerName()
           {
               return ManufacturerName;
           }
           // create restocking fee method
           
           @Override
           public double getItemCost()
           {
               return super.getInvValue() * 1.05;
           }
           // calculate restock fee
           public double getRestockFee()
           {
               return super.getInvValue()* 0.05;
           }
           
           @Override
           public String toString()
           {
               String ManufStr="Manufacturer Name" + String.format("%-20s|",ManufacturerName);
               String FeeStr= "Restocking Fee" + String.format("$%.2f|", getRestockFee());
               return(ManufStr + FeeStr + super.toString());
            
               }
           public void showProduct(String showProduct, String ManufacturerName)
           {
               super.showProduct();
               System.out.println( toString());
               System.out.println("\nManufacturer: " + getManufacturerName());
               
               //output restocking fee and inventory value
               
               System.out.printf("\nInventory cost of " + ManufacturerName + "is $%.2f\n",getRestockFee());
               
           }
           
       } 
  }
    



Is This A Good Question/Topic? 0
  • +

Replies To: Inventory 3 program not showing desired output.

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2791
  • View blog
  • Posts: 11,819
  • Joined: 20-September 08

Re: Inventory 3 program not showing desired output.

Posted 29 July 2013 - 08:01 AM

Quote

It is not showing the Manufacturer Name
Because your ctor ignores it:

Quote

   public Product(String ItemName, int ItemNumber, int Onhand,
        double ItemCost, String ManufacturerName) {
        this.ItemName = ItemName;
        this.ItemNumber = ItemNumber;
        this.Onhand = Onhand;
        this.ItemCost = ItemCost;
    }


Was This Post Helpful? 0
  • +
  • -

#3 IceDragon81  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 20-July 13

Re: Inventory 3 program not showing desired output.

Posted 29 July 2013 - 04:56 PM

Ok, I think I am a little confused. This is the first time I have used a subclass/super class format in my codes. I thought that by extending the class, it called with it all the data within such as item name, item number etc. Is this wrong?
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2791
  • View blog
  • Posts: 11,819
  • Joined: 20-September 08

Re: Inventory 3 program not showing desired output.

Posted 29 July 2013 - 05:22 PM

No, constructors are not inherited. The one you have ignores the manufacturer name
Was This Post Helpful? 0
  • +
  • -

#5 IceDragon81  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 20-July 13

Re: Inventory 3 program not showing desired output.

Posted 29 July 2013 - 05:35 PM

Ok, I think I understand. So do I need to rebuild the constructor in the InventoryPart3 class? How else would I recall the data to run in the Inventory3 class?
Was This Post Helpful? 0
  • +
  • -

#6 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 87
  • View blog
  • Posts: 539
  • Joined: 26-June 13

Re: Inventory 3 program not showing desired output.

Posted 29 July 2013 - 09:28 PM

Why do you have a class called InventoryPart3. If it only olds manufacturing information then why not just place that class inside of the product class instead of in the Inventory class?. It makes more sense. That way, you wouldn't need to worry about inheritance because your manufacturer class would have access to your product class.
Was This Post Helpful? 0
  • +
  • -

#7 IceDragon81  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 20-July 13

Re: Inventory 3 program not showing desired output.

Posted 30 July 2013 - 07:26 PM

View Postsalazar, on 29 July 2013 - 09:28 PM, said:

Why do you have a class called InventoryPart3. If it only olds manufacturing information then why not just place that class inside of the product class instead of in the Inventory class?. It makes more sense. That way, you wouldn't need to worry about inheritance because your manufacturer class would have access to your product class.



It is part of the assignment to have the subclass (InventoryPart3) and have it extend the Product Class and add to it which s the manufacturer name and the restocking fee. Below is the code with the change my instructor recommended, however, it is still not producing the restocking fee. Still have no idea what I have wrong.


package inventory3;

import java.text.DecimalFormat;

public class Inventory3 {
    
    
    public static void main(String[] args) {
         //create products
      
    Product one = new Product("Jack", 68048, 46, 89.99);
    Product two = new Product("Compressor", 68501, 73, 119.99);
    Product three = new Product("Flyswatter", 40122, 129, 9.99);
    Product four = new Product("2000lb Winch", 68142, 15, 59.99);
        
    
    // enter products into inventory
    
    Inventory inventory = new Inventory();
    inventory.add(one);
    inventory.add(two);
    inventory.add(three);
    inventory.add(four);
    
    inventory.bubbleSort();
    
    
    
    for(int i=0; i<inventory.getCount();i++)
    {
    Product product = inventory.get(i);
    System.out.println(product);
}
    
    DecimalFormat df= new DecimalFormat("$#,##0.00");
    String value = df.format(inventory.getInvValue());
    System.out.println("\n Value of the entire inventory: " + value);
}
}




class Product{
    
    
    private String ItemName;
    private int ItemNumber;
    private int Onhand;
    private double ItemCost;
    
    
    public Product(String ItemName, int ItemNumber, int Onhand, double ItemCost){
        this.ItemName= ItemName;
        this.ItemNumber= ItemNumber;
        this.Onhand= Onhand;
        this.ItemCost= ItemCost;
        
    }
      
     public String getItemName(){
         return ItemName;
     }
     
     public int getItemNumber(){
         return ItemNumber;
     }
     
     public int getOnhand(){
         return Onhand;
     }
     
     public double getItemCost(){
         return ItemCost;
     }
     public double getInvValue(){
         return Onhand * ItemCost;
     }
     
     
     public void setItemName(String ItemName){
         this.ItemName= ItemName;
     }
     public void setItemNumber(int ItemNumber){
         this.ItemNumber= ItemNumber;
     }
     public void setOnhand(int Onhand){
         this.Onhand= Onhand;
     }
     public void setItemCost(double ItemCost){
         this.ItemCost= ItemCost;
     }
     
     @Override
     public String toString(){
         String NameStr= "Item Name" + String.format("%-20s|",ItemName);
         String NumStr= "Item Number" + String.format("%-4s|",ItemNumber);
         String QuantStr= "On Hand" + String.format("%-4d|",Onhand);
         String CostStr= "Item Cost" + String.format("$%6.2f|", ItemCost);
         String ValueStr= "Inventory Cost" + String.format("$%8.2f",getInvValue());
         return NameStr + NumStr + QuantStr + CostStr + ValueStr;
         
     }

    void showProduct() {
        throw new UnsupportedOperationException("Not supported yet."); 
    }
}

  class Inventory {
      
      private Product products[];
      private int count;
      
      public Inventory(){
          products = new Product[100];
          count=0;
      }
      
      public void add(Product p){
          products[count++]=p;
      }
      
      public Product get(int index) {
          return products[index];
      }
       public void bubbleSort(){
           int n = getCount();
           for (int search = 1; search<n;search++){
               for (int i = 0; i<n-search;i++){
                   String nameA= products[i].getItemName();
                   String nameB= products[i+1].getItemName();
                   if(nameA.compareToIgnoreCase(nameB)>0){
                       Product temp = products[i];
                       products[i] = products[i+1];
                       products[i+1]=temp;
                   }
               }
           }
       }
       
       
       public double getInvValue()
       {
           double value = 0.00;
           for(int i = 0;i<count;i++){
               value += products[i].getInvValue();
           }
           return value;
       }
       public int getCount(){
           return count;
       }
  
       class InventoryPart3 extends Product
       {
           // Brings in the variables from Inventory class
           
           private String ManufacturerName;
           
           // define the new constructor with 5 elements
           
           InventoryPart3(String ItemName, int ItemNumber, int Onhand, double ItemCost, String ManufacturerName)
           {
               super(ItemName, ItemNumber, Onhand, ItemCost);
            }
           //set ManufacturerName
           public void setManufacturerName(String ManufacturerName)
           {
               this.ManufacturerName=ManufacturerName;
           }//end set manufacturer name
           
           public String getManufacturerName()
           {
               return ManufacturerName;
           }
           // create restocking fee method
           
           @Override
           public double getItemCost()
           {
               return super.getInvValue() * 1.05;
           }
           // calculate restock fee
           public double getRestockFee()
           {
               return super.getInvValue()* 0.05;
           }
           
           @Override
           public String toString()
           {
               String ManufStr="Manufacturer Name" + String.format("%-20s|",ManufacturerName);
               String FeeStr= "Restocking Fee" + String.format("$%.2f|", getRestockFee());
               return(ManufStr + FeeStr + super.toString());
            
               }
           public void showProduct(String showProduct, String ManufacturerName)
           {
               super.showProduct();
               System.out.println( toString());
               System.out.println("\nManufacturer: " + getManufacturerName());
               
               //output restocking fee and inventory value
               
               System.out.printf("\nInventory cost of " + ManufacturerName + "is $%.2f\n",getRestockFee());
               
           }
           
       } 
  }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1