Finding a Java invetory value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • spicster
    New Member
    • Aug 2007
    • 4

    #1

    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.Scann er;//program uses class scanner

    public class ProductInventor y1

    {//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("T ape");
    invProducts[0].setItemQuantit y(100);
    invProducts[0].setItemPrice(4 .99);
    invProducts[0].setItemNumber( 101);

    invProducts[1] = new Product();
    invProducts[1].setItemName("k nife");
    invProducts[1].setItemQuantit y(50);
    invProducts[1].setItemPrice(2 .99);
    invProducts[1].setItemNumber( 102);

    invProducts[2] = new Product();
    invProducts[2].setItemName("b ox");
    invProducts[2].setItemQuantit y(200);
    invProducts[2].setItemPrice(1 .99);
    invProducts[2].setItemNumber( 103);

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


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

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




    }


    }//end main

    }//end WorkEquipment
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    Originally posted by spicster
    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.Scann er;//program uses class scanner

    public class ProductInventor y1

    {//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("T ape");
    invProducts[0].setItemQuantit y(100);
    invProducts[0].setItemPrice(4 .99);
    invProducts[0].setItemNumber( 101);

    invProducts[1] = new Product();
    invProducts[1].setItemName("k nife");
    invProducts[1].setItemQuantit y(50);
    invProducts[1].setItemPrice(2 .99);
    invProducts[1].setItemNumber( 102);

    invProducts[2] = new Product();
    invProducts[2].setItemName("b ox");
    invProducts[2].setItemQuantit y(200);
    invProducts[2].setItemPrice(1 .99);
    invProducts[2].setItemNumber( 103);

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


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

    //output each array elements value
    System.out.prin t ( invProducts[counter].getItemNumber( )+ "\t\t"+ invProducts[counter].getItemName()+ "\t\t"+ invProducts[counter].getItemPrice() + "\t\t"+ invProducts[counter].getItemQuantit y()+"\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.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      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).

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        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:
        [CODE=java]
        import java.util.HashM ap;
        ...
        HashMap invProducts = new HashMap();
        invProducts.put ("Tape",new Product());
        ((Product) invProducts.get ("Tape")).setIt emName("Tape");
        ((Product) invProducts.get ("Tape")).setIt emQuantity(100) ;
        ((Product) invProducts.get ("Tape")).setIt emPrice(4.99);
        ((Product) invProducts.get ("Tape")).setIt emNumber(101);
        [/CODE]
        and then you can access it by using:
        [CODE=java]
        ((Product) invProducts.get ("Tape")).getIt emName()
        [/CODE]
        for example:
        [CODE=java]
        System.out.prin tln(((Product) invProducts.get ("Tape")).getIt emName());
        System.out.prin tln(((Product) invProducts.get ("Tape")).getIt emQuantity());
        System.out.prin tln(((Product) invProducts.get ("Tape")).getIt emPrice());
        System.out.prin tln(((Product) invProducts.get ("Tape")).getIt emNumber());
        [/CODE]

        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.

        Comment

        • spicster
          New Member
          • Aug 2007
          • 4

          #5
          Originally posted by r035198x
          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

          Comment

          • Nepomuk
            Recognized Expert Specialist
            • Aug 2007
            • 3111

            #6
            Originally posted by spicster
            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
            [CODE=java]
            int counter = 0; //set counter
            while (counter <3) //set while loop
            {
            // do whatever you want to do
            ...
            counter = counter + 1;
            }
            [/CODE]

            use
            [CODE=java]
            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
            {
            // do whatever you want to do
            ...
            }
            [/CODE]

            By the way, you could use
            [CODE=java]for(int counter = 0; counter < 3; counter = counter + 1)[/CODE]
            however the command
            [CODE=java]counter++[/CODE]
            instead of
            [CODE=java]counter = counter + 1[/CODE]
            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.

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by spicster
              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

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by nepomuk
                Sure, here's how to use a FOR-Loop:

                Instead of
                [CODE=java]
                int counter = 0; //set counter
                while (counter <3) //set while loop
                {
                // do whatever you want to do
                ...
                counter = counter + 1;
                }
                [/CODE]

                use
                [CODE=java]
                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
                {
                // do whatever you want to do
                ...
                }
                [/CODE]

                By the way, you could use
                [CODE=java]for(int counter = 0; counter < 3; counter = counter + 1)[/CODE]
                however the command
                [CODE=java]counter++[/CODE]
                instead of
                [CODE=java]counter = counter + 1[/CODE]
                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.

                Comment

                • Nepomuk
                  Recognized Expert Specialist
                  • Aug 2007
                  • 3111

                  #9
                  Originally posted by r035198x
                  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! ^^

                  Comment

                  • spicster
                    New Member
                    • Aug 2007
                    • 4

                    #10
                    Originally posted by r035198x
                    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

                    Comment

                    • Nepomuk
                      Recognized Expert Specialist
                      • Aug 2007
                      • 3111

                      #11
                      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:
                      [CODE=java]
                      String[] arg = { "Today", "Tomorrow" };
                      String s = String.format( "Right now it is %s, but soon it will be %s.", arg );
                      System.out.prin tln( s );
                      [/CODE]Will produce the line
                      Right now it is Today, but soon it will be Tomorrow.
                      You can get the same effect with
                      [CODE=java]
                      String s = String.format( "Right now it is %s, but soon it will be %s.", "Today", "Tomorrow" );
                      System.out.prin tln( s );
                      [/CODE]or even
                      [CODE=java]
                      System.out.prin tf( "Right now it is %s, but soon it will be %s.", "Today", "Tomorrow" );
                      [/CODE]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):
                      [CODE=java]
                      import java.util.Date;

                      ...

                      int i = 123;

                      System.out.prin tf( "|%d| |%d|%n" , i, -i ); // |123| |-123|
                      System.out.prin tf( "|%5d| |%5d|%n" , i, -i ); // | 123| | -123|
                      System.out.prin tf( "|%-5d| |%-5d|%n" , i, -i ); // |123 | |-123 |
                      System.out.prin tf( "|%+-5d| |%+-5d|%n" , i, -i ); // |+123 | |-123 |
                      System.out.prin tf( "|%05d| |%05d|%n%n", i, -i ); // |00123| |-0123|

                      System.out.prin tf( "|%X| |%x|%n", 0xabc, 0xabc ); // |ABC| |abc|
                      System.out.prin tf( "|%08x| |%#x|%n%n", 0xabc, 0xabc ); // |00000abc| |0xabc|

                      double d = 12345.678;

                      System.out.prin tf( "|%f| |%f|%n" , d, -d ); // |12345,678000| |-12345,678000|
                      System.out.prin tf( "|%.2f| |%.2f|%n" , d, -d ); // |12345,68| |-12345,68|
                      System.out.prin tf( "|%,10f| |%,10f|%n" , d, -d ); // |1234,567800| |-1234,567800|
                      System.out.prin tf( "|%10.2f| |%10.2f|%n" , d, -d ); // | 12345,68| | -12345,68|
                      System.out.prin tf( "|%010.2f| |%010.2f|%n",d, -d ); // |0012345,68| |-012345,68|

                      String s = " Interesting";

                      System.out.prin tf( "%n|%s|%n", s ); // |Interesting|
                      System.out.prin tf( "|%20s|%n", s ); // | Interesting|
                      System.out.prin tf( "|%-20s|%n", s ); // |Interesting |
                      System.out.prin tf( "|%7s|%n", s ); // |Interesting|
                      System.out.prin tf( "|%.7s|%n", s ); // |Interesting|
                      System.out.prin tf( "|%20.7s|%n ", s ); // | Interesting|

                      Date t = new Date();

                      System.out.prin tf( "%tT%n", t ); // 11:01:39
                      System.out.prin tf( "%tD%n", t ); // 05/23/06
                      System.out.prin tf( "%1$te. %1$tb%n", t ); // 23. Mai
                      [/CODE](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.

                      Comment

                      • spicster
                        New Member
                        • Aug 2007
                        • 4

                        #12
                        o.k. you lost me. I am leaving that for another time. How do I calculate the entire value of my inventory?

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          Originally posted by spicster
                          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

                          Comment

                          • Nepomuk
                            Recognized Expert Specialist
                            • Aug 2007
                            • 3111

                            #14
                            Originally posted by spicster
                            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.

                            Comment

                            Working...