473,385 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Finding a Java invetory value

I need help with finding an entire inventory value for a java assignment. This is what I have so far. I am new to Java and am struggling. In need of help, thanks


import java.util.Scanner;//program uses class scanner

public class ProductInventory1

{//main method begins java application

public static void main (String args[])
{


Product invProducts[] = new Product[3];// set new product name

//build array
invProducts[0] = new Product();
invProducts[0].setItemName("Tape");
invProducts[0].setItemQuantity(100);
invProducts[0].setItemPrice(4.99);
invProducts[0].setItemNumber(101);

invProducts[1] = new Product();
invProducts[1].setItemName("knife");
invProducts[1].setItemQuantity(50);
invProducts[1].setItemPrice(2.99);
invProducts[1].setItemNumber(102);

invProducts[2] = new Product();
invProducts[2].setItemName("box");
invProducts[2].setItemQuantity(200);
invProducts[2].setItemPrice(1.99);
invProducts[2].setItemNumber(103);

System.out.print ( "ItemNumber \t productname \t UnitPrice \t quantity\t total value\n");
System.out.print ( "========== \t =========== \t ========= \t ========\t ===========\n");


int counter = 0;//set counter
while (counter <3)//set while loop
{

//output each array elements value
System.out.print ( invProducts[counter].getItemNumber()+ "\t\t"+ invProducts[counter].getItemName()+ "\t\t"+ invProducts[counter].getItemPrice()+ "\t\t"+ invProducts[counter].getItemQuantity()+"\t\t"+ invProducts[counter].itemTotalValue()+"\n" );
counter = counter + 1;




}


}//end main

}//end WorkEquipment
Aug 22 '07 #1
13 1512
dmjpro
2,476 2GB
I need help with finding an entire inventory value for a java assignment. This is what I have so far. I am new to Java and am struggling. In need of help, thanks


import java.util.Scanner;//program uses class scanner

public class ProductInventory1

{//main method begins java application

public static void main (String args[])
{


Product invProducts[] = new Product[3];// set new product name

//build array
invProducts[0] = new Product();
invProducts[0].setItemName("Tape");
invProducts[0].setItemQuantity(100);
invProducts[0].setItemPrice(4.99);
invProducts[0].setItemNumber(101);

invProducts[1] = new Product();
invProducts[1].setItemName("knife");
invProducts[1].setItemQuantity(50);
invProducts[1].setItemPrice(2.99);
invProducts[1].setItemNumber(102);

invProducts[2] = new Product();
invProducts[2].setItemName("box");
invProducts[2].setItemQuantity(200);
invProducts[2].setItemPrice(1.99);
invProducts[2].setItemNumber(103);

System.out.print ( "ItemNumber \t productname \t UnitPrice \t quantity\t total value\n");
System.out.print ( "========== \t =========== \t ========= \t ========\t ===========\n");


int counter = 0;//set counter
while (counter <3)//set while loop
{

//output each array elements value
System.out.print ( invProducts[counter].getItemNumber()+ "\t\t"+ invProducts[counter].getItemName()+ "\t\t"+ invProducts[counter].getItemPrice()+ "\t\t"+ invProducts[counter].getItemQuantity()+"\t\t"+ invProducts[counter].itemTotalValue()+"\n" );
counter = counter + 1;




}


}//end main

}//end WorkEquipment

Welcome to TSDN.
Remember to use code tag only then it would be better to analyze your code.
Read the guidelines.
OK.

Now what's the problem with that.
It seems everything fine.
Any logical problem or syntactical problem.
Be specific.
And a source code is missing, the source code of product class.
Tell us the specific problem you r facing.
Then it would be better to solve your problem.
OK.

Kind regards,
Dmjpro.
Aug 22 '07 #2
r035198x
13,262 8TB
1.) Please use code tags when posting code.
2.) To get the value of the inventory, you need to sum the values of quantity * price for each product in your inventory. You do that in that while loop you have there.
3.) When looping through arrays in Java, it is more common to use a for loop instead of a while loop because you (usually) know the number of times you're going to loop before hand (i.e the size of the array).
Aug 22 '07 #3
Nepomuk
3,112 Expert 2GB
Hi spicster!
Do you mean, you want to do something like invProducts.get("Tape")? If so, how about using a HashMap instead of an Array?
Here's how to do so:
Expand|Select|Wrap|Line Numbers
  1. import java.util.HashMap;
  2. ...
  3. HashMap invProducts = new HashMap();
  4. invProducts.put("Tape",new Product());
  5. ((Product) invProducts.get("Tape")).setItemName("Tape");
  6. ((Product) invProducts.get("Tape")).setItemQuantity(100);
  7. ((Product) invProducts.get("Tape")).setItemPrice(4.99);
  8. ((Product) invProducts.get("Tape")).setItemNumber(101);
  9.  
and then you can access it by using:
Expand|Select|Wrap|Line Numbers
  1. ((Product) invProducts.get("Tape")).getItemName()
  2.  
for example:
Expand|Select|Wrap|Line Numbers
  1. System.out.println(((Product) invProducts.get("Tape")).getItemName());
  2. System.out.println(((Product) invProducts.get("Tape")).getItemQuantity());
  3. System.out.println(((Product) invProducts.get("Tape")).getItemPrice());
  4. System.out.println(((Product) invProducts.get("Tape")).getItemNumber());
  5.  
Probably, it would be a good idea, to have a second constructor in the class "Product", which takes the Name, Quantity, Price and Number - in that case you could save using all the set functions manually.
Aug 22 '07 #4
1.) Please use code tags when posting code.
2.) To get the value of the inventory, you need to sum the values of quantity * price for each product in your inventory. You do that in that while loop you have there.
3.) When looping through arrays in Java, it is more common to use a for loop instead of a while loop because you (usually) know the number of times you're going to loop before hand (i.e the size of the array).
Thanks for the help and my instructor told me this also (using the while loop). Is is possible to show me an example on how to do this. The assignment also asks to find the entire inventory value. I am still a rookie to java script. thanks
Aug 22 '07 #5
Nepomuk
3,112 Expert 2GB
Thanks for the help and my instructor told me this also (using the while loop). Is is possible to show me an example on how to do this. The assignment also asks to find the entire inventory value. I am still a rookie to java script. thanks
Sure, here's how to use a FOR-Loop:

Instead of
Expand|Select|Wrap|Line Numbers
  1. int counter = 0; //set counter
  2. while (counter <3) //set while loop
  3. {
  4. // do whatever you want to do
  5. ...
  6. counter = counter + 1;
  7. }
  8.  
use
Expand|Select|Wrap|Line Numbers
  1. for(int counter = 0; counter < 3; counter++) // Create an int called counter, set it to 0, as long as it is smaller than 3, do what ever is in the loop, after everything is done, increment (= add 1) to the counter
  2. {
  3. // do whatever you want to do
  4. ...
  5. }
  6.  
By the way, you could use
Expand|Select|Wrap|Line Numbers
  1. for(int counter = 0; counter < 3; counter = counter + 1)
however the command
Expand|Select|Wrap|Line Numbers
  1. counter++
instead of
Expand|Select|Wrap|Line Numbers
  1. counter = counter + 1
does the same in this case and is much shorter and more commonly used.

PS.: java script is different to java - check Wikipedia about Java and Java Script if you like.
Aug 22 '07 #6
r035198x
13,262 8TB
Thanks for the help and my instructor told me this also (using the while loop). Is is possible to show me an example on how to do this. The assignment also asks to find the entire inventory value. I am still a rookie to java script. thanks
Just go through Sun's tutorial on arrays and loops and try it again.
You can find a link to Sun's tutorials here.

P.S Java != Javascript
Aug 22 '07 #7
r035198x
13,262 8TB
Sure, here's how to use a FOR-Loop:

Instead of
Expand|Select|Wrap|Line Numbers
  1. int counter = 0; //set counter
  2. while (counter <3) //set while loop
  3. {
  4. // do whatever you want to do
  5. ...
  6. counter = counter + 1;
  7. }
  8.  
use
Expand|Select|Wrap|Line Numbers
  1. for(int counter = 0; counter < 3; counter++) // Create an int called counter, set it to 0, as long as it is smaller than 3, do what ever is in the loop, after everything is done, increment (= add 1) to the counter
  2. {
  3. // do whatever you want to do
  4. ...
  5. }
  6.  
By the way, you could use
Expand|Select|Wrap|Line Numbers
  1. for(int counter = 0; counter < 3; counter = counter + 1)
however the command
Expand|Select|Wrap|Line Numbers
  1. counter++
instead of
Expand|Select|Wrap|Line Numbers
  1. counter = counter + 1
does the same in this case and is much shorter and more commonly used.

PS.: java script is different to java - check Wikipedia about Java and Java Script if you like.
Hey, who's this new guy with a faster keyboard than mine? and copying my PSs too.


P.S I see you're enjoying yourself here already.
Aug 22 '07 #8
Nepomuk
3,112 Expert 2GB
Hey, who's this new guy with a faster keyboard than mine? and copying my PSs too.


P.S I see you're enjoying yourself here already.
Hehe, I do my best! ^^
Aug 22 '07 #9
Hey, who's this new guy with a faster keyboard than mine? and copying my PSs too.


P.S I see you're enjoying yourself here already.
Thanks nepomuk, I plugged in the for loop and worked (compiled) just fine. One other thing the instructor advised me to use the %s,%d, for uniformity. How do these work and are they easier to work with?

spicster
Aug 22 '07 #10
Nepomuk
3,112 Expert 2GB
Hi spicster!
You're welcome for the help!

About the %s and %d - they are new to me, but if I understood it correctly, they are quite new (since Java 5.0) and are used for formating Strings. I found the following information:
Expand|Select|Wrap|Line Numbers
  1. String[] arg = { "Today", "Tomorrow" }; 
  2. String s = String.format( "Right now it is %s, but soon it will be %s.", arg ); 
  3. System.out.println( s );
  4.  
Will produce the line
Right now it is Today, but soon it will be Tomorrow.
You can get the same effect with
Expand|Select|Wrap|Line Numbers
  1. String s = String.format( "Right now it is %s, but soon it will be %s.", "Today", "Tomorrow" );
  2. System.out.println( s );
  3.  
or even
Expand|Select|Wrap|Line Numbers
  1. System.out.printf( "Right now it is %s, but soon it will be %s.", "Today", "Tomorrow" );
  2.  
There are a few different so called "Format specifiers" - and here's a list of them:
  • %n = new line
  • %% = percent symbol
  • %c = unicode character
  • %C = unicode character (in capitals)
  • %x = hexadecimal number
  • %X = hexadecimal number (all letters as capitals)
  • %f = floating point number
  • %g = floating point number
  • %G = floating point number
  • %a = floating point number
  • %A = floating point number
  • %b = boolean
  • %B = boolean (in capitals)
  • %s = String
  • %S = String (in capitals)
  • %d = decimal number
  • %t = date and time
  • %T = date and time
  • %e = scientific notation
  • %E = scientific notation
  • %h = hash code
  • %H = hash code
And, just for the fun of it, let's try all of them (the comments behind the lines are the output of that line):
Expand|Select|Wrap|Line Numbers
  1. import java.util.Date;
  2.  
  3. ...
  4.  
  5. int i = 123; 
  6.  
  7. System.out.printf( "|%d|   |%d|%n" ,     i, -i );   // |123|   |-123| 
  8. System.out.printf( "|%5d| |%5d|%n" ,     i, -i );   // |  123| | -123| 
  9. System.out.printf( "|%-5d| |%-5d|%n" ,   i, -i );   // |123  | |-123 | 
  10. System.out.printf( "|%+-5d| |%+-5d|%n" , i, -i );   // |+123 | |-123 | 
  11. System.out.printf( "|%05d| |%05d|%n%n",  i, -i );   // |00123| |-0123| 
  12.  
  13. System.out.printf( "|%X| |%x|%n", 0xabc, 0xabc );   // |ABC| |abc| 
  14. System.out.printf( "|%08x| |%#x|%n%n", 0xabc, 0xabc ); // |00000abc| |0xabc| 
  15.  
  16. double d = 12345.678; 
  17.  
  18. System.out.printf( "|%f| |%f|%n" ,         d, -d ); // |12345,678000| |-12345,678000| 
  19. System.out.printf( "|%.2f| |%.2f|%n" ,     d, -d ); // |12345,68| |-12345,68| 
  20. System.out.printf( "|%,10f| |%,10f|%n" ,   d, -d ); // |1234,567800| |-1234,567800| 
  21. System.out.printf( "|%10.2f| |%10.2f|%n" , d, -d ); // |  12345,68| | -12345,68| 
  22. System.out.printf( "|%010.2f| |%010.2f|%n",d, -d ); // |0012345,68| |-012345,68| 
  23.  
  24. String s = " Interesting"; 
  25.  
  26. System.out.printf( "%n|%s|%n", s );                 // |Interesting| 
  27. System.out.printf( "|%20s|%n", s );                 // |        Interesting| 
  28. System.out.printf( "|%-20s|%n", s );                // |Interesting        | 
  29. System.out.printf( "|%7s|%n", s );                  // |Interesting| 
  30. System.out.printf( "|%.7s|%n", s );                 // |Interesting| 
  31. System.out.printf( "|%20.7s|%n", s );               // |             Interesting| 
  32.  
  33. Date t = new Date(); 
  34.  
  35. System.out.printf( "%tT%n", t );                    // 11:01:39 
  36. System.out.printf( "%tD%n", t );                    // 05/23/06 
  37. System.out.printf( "%1$te. %1$tb%n", t );           // 23. Mai
  38.  
(Source: Java ist auch eine Insel (german))

Now, THAT is a lot to learn... ;-) But your instructor only said something about %s (include a String) and %d (include a decimal), it shouldn't be to difficult.
Aug 22 '07 #11
o.k. you lost me. I am leaving that for another time. How do I calculate the entire value of my inventory?
Aug 23 '07 #12
r035198x
13,262 8TB
o.k. you lost me. I am leaving that for another time. How do I calculate the entire value of my inventory?
Write down the steps that you would take to do it manually on a piece of paper then change those steps to a Java program
Aug 23 '07 #13
Nepomuk
3,112 Expert 2GB
o.k. you lost me. I am leaving that for another time. How do I calculate the entire value of my inventory?
Doesn't surprise me at all - it's a lot of information. However, probably the first 3 examples should do the job.
Aug 23 '07 #14

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

Similar topics

4
by: Hal Vaughan | last post by:
I want to have a config file for my program, which means I need to know where the config file is. If I type: java myclass and it runs myclass.class, is there any way to obtain the location of...
3
by: John J. Walton | last post by:
Hi ! I wonder if anyone can give me some advice. I have been in IT for 30 yrs. Most of the time I have coded in Cobol on medium size systems (Data General, now a dinosaur). For the past 2 years...
4
by: Kevin Dean | last post by:
I'm trying to create an XSL transformation that will strip out development-specific attributes from deployment descriptors and other XML files. I have already successfully done so with web.xml but...
7
by: ralphNOSPAM | last post by:
Is there a PHP script that can find unused variables? I'd like to 'clean up' my scripts. Thanks...
2
by: ElkGroveR | last post by:
Hi there! I'm using PHP to create a simple, dynamic MySQL SELECT query. The user chooses a selection from a HTML Form SELECT element's many options and submits the form via a POST action. ...
21
by: Tom Gur | last post by:
Hi, It's seems that csh and tcsh acts a bit different when handling special characters in quotes. i.e: if i'll supply my program with the following arguments: -winpath "c:\\temp\\" tcsh will...
2
by: blitz1989 | last post by:
Hello all, I'm new to this forum and Java and having a alot of problems understanding this language. I am working on an invetory program part 4. The assignment requirements are listed but the...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.