By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,850 Members | 1,026 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,850 IT Pros & Developers. It's quick & easy.

need help adding to existing code..

P: 17
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.println(dvd);

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

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

dvd = new DVD(4, "The Grinch Who Stole Christmas",3, 10.99);
System.out.println(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(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


any help would be appreciated....
Dec 4 '07 #1
Share this Question
Share on Google+
9 Replies


10K+
P: 13,264
So, have you gone through a tutorial on arrays yet?
Dec 4 '07 #2

P: 17
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?
Dec 4 '07 #3

10K+
P: 13,264
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?
Dec 4 '07 #4

P: 17
How about the horse's mouth?
i saw this site already...but still dont know where to begin.....
Dec 4 '07 #5

P: 17
ok, i'm trying to work on this.....and here is what i got for my code

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. class Inventory2 implements Comparable
  3.  
  4. {
  5.     private String dvdTitle;
  6.     private int dvdItem;
  7.     private int dvdStock;
  8.     private double dvdPrice;
  9.  
  10. }//end main method
  11.  
  12. class Dvd { // Constructor for the Dvd class
  13.    {
  14.       dvdTitle = "";
  15.       dvdItem  = 0.00;
  16.       dvdStock = 0.00;
  17.       dvdPrice = 0.00;
  18.    }
  19.  
  20.  
  21. public Dvd(String title, int item, int stock, double price) // Constructor for the Supplies class
  22.       {
  23.       this.title = title;
  24.       this.item = item;
  25.       this.stock = stock;
  26.       this.price = price;
  27.          }
  28.  
  29.    // set DVD Item
  30.     public void setDvdItem(int item) {
  31.         dvdItem = item;
  32.     } //end method  set Dvd Item
  33.  
  34.     //return DVD Item
  35.     public int getDvdItem() {
  36.         return dvdItem;
  37.     } //end method get Dvd Item
  38.  
  39.     //set DVD Title
  40.     public void setDvdTitle(String title) {
  41.         dvdTitle = title;
  42.     } //end method set Dvd Title
  43.  
  44.     //return Dvd Title
  45.     public String getDvdTitle() {
  46.         return dvdTitle;
  47.     } //end method get Dvd Title
  48.  
  49.     public void setDvdStock(int stock) {
  50.         dvdStock = stock;
  51.     } //end method set Dvd Stock
  52.  
  53.     //return dvd Stock
  54.     public int getDvdStock() {
  55.         return dvdStock;
  56.     } //end method get Dvd Stock
  57.  
  58.     public void setDvdPrice(double price) {
  59.         dvdPrice = price;
  60.     } //end method setdvdPrice
  61.  
  62.     //return DVD Price
  63.     public double getDvdPrice() {
  64.         return dvdPrice;
  65.     } //end  method get Dvd Price
  66.  
  67.     //calculate inventory value
  68.     public double value() {
  69.         return dvdPrice * dvdStock;
  70.     } //end method value
  71.  
  72.     public String toString() {
  73.  
  74.         return String.format("item=%3d   title=%-20s   units=%d   price=%.2f   value=%.2f",
  75.                              dvdItem, dvdTitle, dvdStock, dvdPrice, value());
  76.     }//end class DVD 
  77.  
  78.  
  79. public class Inventory2 {
  80.  
  81.     public static void main(String[] args) {
  82.  
  83.         System.out.println ("Inventory of DVD Movies:\n");
  84.  
  85.    Dvd [] supplies = new Dvds [3];
  86.  
  87.    Dvd d1 = new Dvd (1, "Frosty The Snowman", 5, 14.95);
  88.    Dvd d2 = new Dvd (2, "Charlie Brown Christmas", 7, 12.99);
  89.    Dvd d3 = new Dvd (3, "Rudolph The Red-Nosed Reindeer", 6, 19.99);
  90.    Dvd d4 = new Dvd (4, "The Grinch Who Stole Christmas", 3, 10.99);
  91.  
  92.    supplies[0] = d1;
  93.    supplies[1] = d2;
  94.    supplies[2] = d3;
  95.    supplies[3] = d4;
  96.  
  97.  
  98.    double total = 0.0;
  99.  
  100.    for(int i= 0; i < 3;i++)
  101.    {
  102.    total = total + supplies[i].calculateInvValue();
  103.    }
  104.  
  105.  
  106.  
  107.       for(Dvd d: supplies)
  108.       {
  109.       System.out.println(d);
  110.       System.out.println();
  111.     }
  112.  
  113. for(Dvd d: supplies)
  114.     {
  115.     System.out.println(d);
  116.     System.out.println();
  117.     }
  118.  
  119.  
  120.    System.out.println("Total Value is: $"+total);
  121.  
  122.  
  123. }}//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?
Dec 5 '07 #6

Expert 10K+
P: 11,448
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
Dec 5 '07 #7

P: 17
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:

Expand|Select|Wrap|Line Numbers
  1. }}}//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.Comparable
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.calculateInvValue
total = total + supplies[i].calculateInvValue();

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


do i have things in the wrong place?
Dec 5 '07 #8

10K+
P: 13,264
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.
Dec 5 '07 #9

Expert 10K+
P: 11,448
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
Dec 5 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.