need help adding to existing code..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kiamari
    New Member
    • Nov 2007
    • 17

    #1

    need help adding to existing code..

    here's what i have to do:

    Modify the Inventory Program so the application can handle multiple items. Use an array to store the items. The output should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition, the output should display the value of the entire inventory.
    • Create a method to calculate the value of the entire inventory.
    • Create another method to sort the array items by the name of the product.

    here's my code that i have so far...

    public class Inventory1 {

    public static void main(String[] args) {

    DVD dvd;

    dvd = new DVD(1, "Frosty The Snowman", 5, 14.95);
    System.out.prin tln(dvd);

    dvd = new DVD(2, "Charlie Brown Christmas", 7, 12.99);
    System.out.prin tln(dvd);

    dvd = new DVD(3, "Rudolph The Red-Nosed Reindeer", 6, 19.99);
    System.out.prin tln(dvd);

    dvd = new DVD(4, "The Grinch Who Stole Christmas",3, 10.99);
    System.out.prin tln(dvd);

    } //end main
    } // end class Inventory1

    class DVD {
    private int dvdItem;
    private String dvdTitle;
    private int dvdStock;
    private double dvdPrice;

    public DVD(int item, String title, int stock, double price) {
    dvdItem = item;
    dvdTitle = title;
    dvdStock = stock;
    dvdPrice = price;
    } //end four-argument constructor

    // set DVD Item
    public void setDvdItem(int item) {
    dvdItem = item;
    } //end method set Dvd Item

    //return DVD Item
    public int getDvdItem() {
    return dvdItem;
    } //end method get Dvd Item

    //set DVD Title
    public void setDvdTitle(Str ing title) {
    dvdTitle = title;
    } //end method set Dvd Title

    //return Dvd Title
    public String getDvdTitle() {
    return dvdTitle;
    } //end method get Dvd Title

    public void setDvdStock(int stock) {
    dvdStock = stock;
    } //end method set Dvd Stock

    //return dvd Stock
    public int getDvdStock() {
    return dvdStock;
    } //end method get Dvd Stock

    public void setDvdPrice(dou ble price) {
    dvdPrice = price;
    } //end method setdvdPrice

    //return DVD Price
    public double getDvdPrice() {
    return dvdPrice;
    } //end method get Dvd Price

    //calculate inventory value
    public double value() {
    return dvdPrice * dvdStock;
    } //end method value

    public String toString() {

    return String.format(" item=%3d title=%-20s units=%d price=%.2f value=%.2f",
    dvdItem, dvdTitle, dvdStock, dvdPrice, value());
    }

    } //end class DVD


    any help would be appreciated....
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    So, have you gone through a tutorial on arrays yet?

    Comment

    • Kiamari
      New Member
      • Nov 2007
      • 17

      #3
      Originally posted by r035198x
      So, have you gone through a tutorial on arrays yet?
      i took a look at a few sites...but its still not clear to me.......do you know any sites?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by Kiamari
        i took a look at a few sites...but its still not clear to me.......do you know any sites?
        How about the horse's mouth?

        Comment

        • Kiamari
          New Member
          • Nov 2007
          • 17

          #5
          Originally posted by r035198x
          How about the horse's mouth?
          i saw this site already...but still dont know where to begin.....

          Comment

          • Kiamari
            New Member
            • Nov 2007
            • 17

            #6
            ok, i'm trying to work on this.....and here is what i got for my code

            Code:
            import java.util.*;
            class Inventory2 implements Comparable
             
            {
            	private String dvdTitle;
                private int dvdItem;
                private int dvdStock;
                private double dvdPrice;
                
            }//end main method
            
            class Dvd { // Constructor for the Dvd class
               {
            	  dvdTitle = "";
            	  dvdItem  = 0.00;
            	  dvdStock = 0.00;
            	  dvdPrice = 0.00;
               }
               
                  
            public Dvd(String title, int item, int stock, double price) // Constructor for the Supplies class
            	  {
                  this.title = title;
            	  this.item = item;
            	  this.stock = stock;
            	  this.price = price;
               	  }
             
               // set DVD Item
                public void setDvdItem(int item) {
                    dvdItem = item;
                } //end method  set Dvd Item
            
                //return DVD Item
                public int getDvdItem() {
                    return dvdItem;
                } //end method get Dvd Item
            
                //set DVD Title
                public void setDvdTitle(String title) {
                    dvdTitle = title;
                } //end method set Dvd Title
            
                //return Dvd Title
                public String getDvdTitle() {
                    return dvdTitle;
                } //end method get Dvd Title
            
                public void setDvdStock(int stock) {
                    dvdStock = stock;
                } //end method set Dvd Stock
            
                //return dvd Stock
                public int getDvdStock() {
                    return dvdStock;
                } //end method get Dvd Stock
            
                public void setDvdPrice(double price) {
                    dvdPrice = price;
                } //end method setdvdPrice
            
                //return DVD Price
                public double getDvdPrice() {
                    return dvdPrice;
                } //end  method get Dvd Price
            
                //calculate inventory value
                public double value() {
                    return dvdPrice * dvdStock;
                } //end method value
                
                public String toString() {
                    
                    return String.format("item=%3d   title=%-20s   units=%d   price=%.2f   value=%.2f",
                                         dvdItem, dvdTitle, dvdStock, dvdPrice, value());
                }//end class DVD 
              
             
            public class Inventory2 {
                
                public static void main(String[] args) {
                	
                	System.out.println ("Inventory of DVD Movies:\n");
             
               Dvd [] supplies = new Dvds [3];
             
               Dvd d1 = new Dvd (1, "Frosty The Snowman", 5, 14.95);
               Dvd d2 = new Dvd (2, "Charlie Brown Christmas", 7, 12.99);
               Dvd d3 = new Dvd (3, "Rudolph The Red-Nosed Reindeer", 6, 19.99);
               Dvd d4 = new Dvd (4, "The Grinch Who Stole Christmas", 3, 10.99);
             
               supplies[0] = d1;
               supplies[1] = d2;
               supplies[2] = d3;
               supplies[3] = d4;
             
             
               double total = 0.0;
             
               for(int i= 0; i < 3;i++)
               {
               total = total + supplies[i].calculateInvValue();
               }
             
             
             
                  for(Dvd d: supplies)
                  {
                  System.out.println(d);
                  System.out.println();
                }
             
            for(Dvd d: supplies)
                {
                System.out.println(d);
                System.out.println();
                }
             
             
               System.out.println("Total Value is: $"+total);
             
             
            }}//end class Inventory2
            here is the error i get..

            reached end of file while parsing
            }}//end class Inventory2
            ^
            1 error

            Process completed.



            how do i fix that?

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              The compiler was happily parsing away (no syntax errors yet) and all of a sudden
              there was no more input available. That indicates that you've omitted the last
              right curly brackets and you should fix that. Also read the compiler diagnostic
              message.

              kind regards,

              Jos

              Comment

              • Kiamari
                New Member
                • Nov 2007
                • 17

                #8
                Originally posted by JosAH
                The compiler was happily parsing away (no syntax errors yet) and all of a sudden
                there was no more input available. That indicates that you've omitted the last
                right curly brackets and you should fix that. Also read the compiler diagnostic
                message.

                kind regards,

                Jos
                OMG!!

                so, i added one more curly bracket to the very end of the code like this:

                Code:
                }}}//end class Inventory2
                AND NOW I HAVE 32 ERRORS!!


                Inventory2 is not abstract and does not override abstract method compareTo(java. lang.Object) in java.lang.Compa rable
                class Inventory2 implements Comparable

                lines 23 - 94 have this beginning error... cannot find symbol
                symbol : variable dvdTitle
                location: class Dvd
                dvdTitle = "";

                lines 96 - 99 have this beginning error......operator + cannot be applied to double,Dvd.calc ulateInvValue
                total = total + supplies[i].calculateInvVa lue();

                another error....incompatible types
                found : <nulltype>
                required: double
                total = total + supplies[i].calculateInvVa lue();
                ^
                and another error...inner classes cannot have static declarations
                public static void main(String[] args) {
                ^
                ^


                do i have things in the wrong place?

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Those errors were there all along.
                  You just have to fix them. The compiler didn't report them earilier because it was failing to complete the compilaion process itself.

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by Kiamari
                    OMG!!

                    so, i added one more curly bracket to the very end of the code like this: [ ... ]
                    AND NOW I HAVE 32 ERRORS!! [ ... ]
                    do i have things in the wrong place?
                    Well, I'd say yes you have. A program text is not a linear text as in novels. First
                    the compiler tries to determine if the text by itself makes sense. As in:

                    "fridges chimneys over green fly ferocious".

                    This text most definitely doesn't make sense so the compiler can complain early.
                    If you submit this:

                    "ferocious green fridges fly over chimneys"

                    It makes sense syntactically so the compiler has to check the next 'level' of your
                    text. Can fridges be ferocious? Or green ? Can they fly? This is the 'short term
                    semantic analisys' performed by the compiler, i.e. it checks whether or not
                    methods exist or are reachable etc. etc. Your code violates a lot of constraints.
                    Securely check what the compiler has to say to you and one by one fix your mistakes.

                    kind regards,

                    Jos

                    Comment

                    Working...