help with an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • porky008
    New Member
    • Oct 2006
    • 20

    #1

    help with an array

    I can not figure out how to use an array to store the different things and the total is only showing 0.0 instead of the total of the inventory. Can I please get some help on this?
    Code:
    // This calls the external class scanner 
    import java.util.Scanner; 
    
     class inventory
    { // start of class
    
           // Variable Declaration 
         private String productname;       // name of movie
         private double productnumber;     // product number
         private double numberinstock;     // number of movie in stock
         private double priceofitem;       // the price of the movie
     	
     	      // gets the product name                                                         
         	public void getproductname(String productname)
        {
    		productname = productname;
     	}
            // gets the product number	
     	public void setproductnumber(double productnumber)
        {
    		productnumber = productnumber;
     	}
     	public double getproductnumber()
        {
    		return productnumber;
       	}
             // gets the number in stock
        public void setnumberinstock(double numberinstock)
       {
          numberinstock = numberinstock;
       }
         public double getnumberinstock()
       {
          return numberinstock;
       }
              // gets the price of the item
        public void setpriceofitem(double priceofitem)
       {
          priceofitem = priceofitem;
       }
         public double getpriceofitem()
       {
          return priceofitem;
       } 
        
           // This calculates the value of inventory, but i can not get it to work
           public double value()
          {
              return numberinstock * priceofitem;
          }
          
      } // end of class
    // This calls the external class scanner 
     import java.util.Scanner;
    
     
     public class whatever 
     
      { // starts the class
    
      public static void main( String args[])
      { // starts the program
        Scanner input = new Scanner( System.in ); 
        
        inventory myinventory = new inventory();
        
        // get the name of the movie
        System.out.printf( "\n\nEnter the name of the movie, enter stop when done):");
        String productname = (input.next());
        while(!productname.equals("stop"))
        { // should start the loop
        
          // gets the product number for the movie
          System.out.printf( "\nEnter the product number: " ); 
          myinventory.setproductnumber(input.nextDouble());
        
          // gets the number you are puting into stock
          System.out.printf( "\nEnter the number in stock: "); 
          myinventory.setnumberinstock(input.nextDouble());
        
          // gets the price of the movie
          System.out.printf( "\nEnter the items price: "); 
          myinventory.setpriceofitem(input.nextDouble());
        
          // this should show the value of the inventory
          System.out.printf( "\n\nThis is what the value of the inventory should be: $ "+ myinventory.value());
        
          // get the name of the movie
          System.out.printf( "\n\nEnter the name of the movie, enter stop when done):");
          myinventory.getproductname(input.next());
        
        } // should end the loop
        
      } // end of program
       
      } // end of class
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Originally posted by porky008
    I can not figure out how to use an array to store the different things and the total is only showing 0.0 instead of the total of the inventory. Can I please get some help on this?
    Code:
    // This calls the external class scanner 
    import java.util.Scanner; 
    
     class inventory
    { // start of class
    
           // Variable Declaration 
         private String productname;       // name of movie
         private double productnumber;     // product number
         private double numberinstock;     // number of movie in stock
         private double priceofitem;       // the price of the movie
     	
     	      // gets the product name                                                         
         	public void getproductname(String productname)
        {
    		productname = productname;
     	}
            // gets the product number	
     	public void setproductnumber(double productnumber)
        {
    		productnumber = productnumber;
     	}
     	public double getproductnumber()
        {
    		return productnumber;
       	}
             // gets the number in stock
        public void setnumberinstock(double numberinstock)
       {
          numberinstock = numberinstock;
       }
         public double getnumberinstock()
       {
          return numberinstock;
       }
              // gets the price of the item
        public void setpriceofitem(double priceofitem)
       {
          priceofitem = priceofitem;
       }
         public double getpriceofitem()
       {
          return priceofitem;
       } 
        
           // This calculates the value of inventory, but i can not get it to work
           public double value()
          {
              return numberinstock * priceofitem;
          }
          
      } // end of class
    // This calls the external class scanner 
     import java.util.Scanner;
    
     
     public class whatever 
     
      { // starts the class
    
      public static void main( String args[])
      { // starts the program
        Scanner input = new Scanner( System.in ); 
        
        inventory myinventory = new inventory();
        
        // get the name of the movie
        System.out.printf( "\n\nEnter the name of the movie, enter stop when done):");
        String productname = (input.next());
        while(!productname.equals("stop"))
        { // should start the loop
        
          // gets the product number for the movie
          System.out.printf( "\nEnter the product number: " ); 
          myinventory.setproductnumber(input.nextDouble());
        
          // gets the number you are puting into stock
          System.out.printf( "\nEnter the number in stock: "); 
          myinventory.setnumberinstock(input.nextDouble());
        
          // gets the price of the movie
          System.out.printf( "\nEnter the items price: "); 
          myinventory.setpriceofitem(input.nextDouble());
        
          // this should show the value of the inventory
          System.out.printf( "\n\nThis is what the value of the inventory should be: $ "+ myinventory.value());
        
          // get the name of the movie
          System.out.printf( "\n\nEnter the name of the movie, enter stop when done):");
          myinventory.getproductname(input.next());
        
        } // should end the loop
        
      } // end of program
       
      } // end of class

    Each time you have a set<whatever> function, you are setting the same variable equal to itself. Either define the parameter to each function by a name different than your instance variables, or use the this. operator before the left side of each assignment.

    Try this, and come back with other questions.

    Comment

    • porky008
      New Member
      • Oct 2006
      • 20

      #3
      Originally posted by Ganon11
      Each time you have a set<whatever> function, you are setting the same variable equal to itself. Either define the parameter to each function by a name different than your instance variables, or use the this. operator before the left side of each assignment.

      Try this, and come back with other questions.
      Cool thank you for the help on getting the total to show up.

      Comment

      Working...