Inventory Program Part2

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lilsugaman
    New Member
    • Jul 2008
    • 12

    Inventory Program Part2

    I have to assignment which includes the following: 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.

    I have tried so far this is what I have;


    Code:
    1. import java.text.NumberFormat;
    2. 
    3. public class Produce {
    4.
    5.    //initialize and load array with values  
    6.  
    7.  private static int[] produceNumber = new int[] {2015, 2016, 2017, 2018,   2019, 2020, 2021, 2022, 2023, 2024};
    8. 
    9. 
    10.  private static String[] produceName = new String[] {"carrots","potatoe", "bananas", "tomatoe", "lettuce", "apples", "oranges", "grapes", "cherrie", "celery"};
    11. 
    12.
    13. private static int[] produceStock = new int[] {20, 50, 12, 15, 10, 30, 40, 6, 75, 8};
    14.  
    15. 
    16.
    17. private static double[] producePrice = new double[] {.50,2.75,.60,1.00,1.50,2.50,3.00,1.89,2.50,1.48};
    18.
    19.
    20. public static void DisplayOutput()
    21.
    22. {   //Displays inventory and totals
    23.
    24.   System.out.println("Item Number\tProduce Name\tStock\tPrice\tTotal Price\n");
    25.
    26.   NumberFormat numForm = NumberFormat.getCurrencyInstance();//Displays double values as currency
    27.
    28.        double totalValue = 0;
    29.
    30.   for (int i=0; i<10; i++){
    31.
    32.        //   String price = numform.format(producePrice[i]);
    33.
    34.           System.out.println(produceNumber[i]+"\t\t"+ produceName[i]+"\t\t"+produceStock[i]+"\t"+numForm.format(producePrice[i])+
    35.
    36.                   "\t"+(numForm.format(produceStock[i]*producePrice[i])));
    37.
    38.           totalValue=totalValue+(produceStock[i]*producePrice[i]);
    }
    39.        System.out.println("\n\nTotal Stock Value: "+numForm.format(totalValue));
    40.}
    41.}
    42.
    43.// Inventory Part 2 July 17, 2008
    44.
    45.   function setObject (produceName, produceNumber, produceStock, producePrice){
    46.      myObjectArray[objectArrayIndex++] = new ownObject (produceName, produceNumber, produceStock, producePrice);
    47.
    48.   }
    49.
    50.
    51.
    52.   var objectArrayIndex =0;
    53.   var myObjectArray = new Array();
    54.
    55.   SetObject ("carrots","potatoe", "bananas", "tomatoe", "lettuce", "apples", "oranges", "grapes", "cherrie", "celery");
    56.   SetObject (2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024);
    57.   SetObject (20, 50, 12, 15, 10, 30, 40, 6, 75, 8);
    58.   SetObject (.50,2.75,.60,1.00,1.50,2.50,3.00,1.89,2.50,1.48);
    59.
    60.   function showArray (produceName, produceNumber, produceStock, producePrice) {
    61.
    62.     document.write (produceName + ':');
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    What is your actual question? Are you not getting the desired output, are there compilation/runtime errors?

    Comment

    • lilsugaman
      New Member
      • Jul 2008
      • 12

      #3
      I received 24 compile errors

      Comment

      • lilsugaman
        New Member
        • Jul 2008
        • 12

        #4
        Produce.java:53 : class, interface, or enum expected
        }
        ^

        Produce.java:58 : class, interface, or enum expected
        var myObjectArray = new Array();
        ^

        Produce.java:60 : class, interface, or enum expected
        SetObject ( "carrots", "potatoe",. ..)


        I have 24 errors consisting of above

        Comment

        • BigDaddyLH
          Recognized Expert Top Contributor
          • Dec 2007
          • 1216

          #5
          var?! Is this Java or JavaScript? Seems an ungodly mix of the two.

          Comment

          • chaarmann
            Recognized Expert Contributor
            • Nov 2007
            • 785

            #6
            Originally posted by BigDaddyLH
            var?! Is this Java or JavaScript? Seems an ungodly mix of the two.
            The beginning is surely java, but at line 41, javascript starts
            ("document.writ e", "function", "new Array()" etc.)
            This forum entry should be splitted and the bottom should be moved to the javascript forum.

            Comment

            • lilsugaman
              New Member
              • Jul 2008
              • 12

              #7
              my codes are wrong it was java script and it should be java, the main thing is I need help sorting arrays that hold multiple data, displaying the data one at a time like orange, 00215, 24, $5.00. Can someone please help me do this I have found another sort code and changed it, but now I'm getting the error duplicate class.

              Code:
              1. import java.util.Arrays;
              2. import java.text.NumberFormat;
              3.
              4.  public class Produce {
              5.
              6.  //initialize and load array with values  
              7. 
              8.  private static int[] produceNumber = new int[] {2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024};
              9.  
              10. private static String[] produceName = new String[] {"carrots","potatoe", "bananas", "tomatoe", "lettuce", "apples", "oranges", "grapes", "cherrie", "celery"};
              11.
              12. private static int[] produceStock = new int[] {20, 50, 12, 15, 10, 30, 40, 6, 75, 8};
              13.
              14. private static double[] producePrice = new double[] {.50,2.75,.60,1.00,1.50,2.50,3.00,1.89,2.50,1.48};
              15. 
              16.
              17. public static void DisplayOutput()
              18.
              19. {   //Displays inventory and totals
              20.
              21.   System.out.println("Item Number\tProduce Name\tStock\tPrice\tTotal Price\n");
              22.
              23.   NumberFormat numForm = NumberFormat.getCurrencyInstance();//Displays double values as currency
              24.
              25.        double totalValue = 0;
              26.
              27.   for (int i=0; i<10; i++){
              28.
              29.        //   String price = numform.format(producePrice[i]);
              30.
              31.           System.out.println(produceNumber[i]+"\t\t"+ produceName[i]+"\t\t"+produceStock[i]+"\t"+numForm.format(producePrice[i])+                   "\t"+(numForm.format(produceStock[i]*producePrice[i])));
              32.
              33.           totalValue=totalValue+(produceStock[i]*producePrice[i]);
              34.  }
              35.        System.out.println("\n\nTotal Stock Value: "+numForm.format(totalValue));
              36.  }
              37.     }
              38.
              39.  // Inventory Part 2 July 17, 2008
              40. 
              41.  public class Produce {
              42.
              43.  public static void main(String[] args) {
              44.
              45.
              46.   double[] lengths = {.50,2.75,.60,1.00,1.50,2.50,3.00,1.89,2.50,1.48};
              47.    Arrays.sort(lengths);
              48.   
              49.    }
              50.  }

              Comment

              • Laharl
                Recognized Expert Contributor
                • Sep 2007
                • 849

                #8
                Right. You declare 'public class Produce' twice within the file.

                Comment

                • lilsugaman
                  New Member
                  • Jul 2008
                  • 12

                  #9
                  how would I start the sort at first I had public class Sort and I was getting the error "Class Sort is public, should be in a file called Sort.java

                  Comment

                  • Laharl
                    Recognized Expert Contributor
                    • Sep 2007
                    • 849

                    #10
                    You can only have one class declared public per file. Just declare the other as 'class Sort', rather than 'public class Sort' and the compiler will shut up.

                    Comment

                    • lilsugaman
                      New Member
                      • Jul 2008
                      • 12

                      #11
                      it did shut up but it didn't sort the data.

                      Comment

                      • BigDaddyLH
                        Recognized Expert Top Contributor
                        • Dec 2007
                        • 1216

                        #12
                        Originally posted by lilsugaman
                        it did shut up but it didn't sort the data.
                        This collections Java tutorial describes interfaces, implementations, and algorithms in the Java Collections framework

                        Comment

                        • lilsugaman
                          New Member
                          • Jul 2008
                          • 12

                          #13
                          Thanks for the site but can you give me an example of how you would start the sort because the site you gave me basically has the same thing I have to sort but it's not sorting one product at a time with the name, number, units, and price.

                          Comment

                          • JosAH
                            Recognized Expert MVP
                            • Mar 2007
                            • 11453

                            #14
                            Originally posted by lilsugaman
                            Thanks for the site but can you give me an example of how you would start the sort because the site you gave me basically has the same thing I have to sort but it's not sorting one product at a time with the name, number, units, and price.
                            That's because you've split different attributes that belong in one single class,
                            not spread out over separate arrays; the is so Fortranesque and is cursing at
                            proper OO programming. The following little article can help you out but you do
                            have to read and understand it first; good luck with it.

                            kind regards,

                            Jos

                            Comment

                            • lilsugaman
                              New Member
                              • Jul 2008
                              • 12

                              #15
                              ok, I have changed my code about 15 million times, I am a beginner at this and when I say beginner like this is the first time I laid eyes on programming. No I don't understand everything but the more I work at it and perform practices programs I will conquer it. So here's my new code.

                              [CODE]

                              1. import java.util.*;
                              2. import java.text.Numbe rFormat;
                              3.
                              4. public class Produce {
                              5.
                              6. //initialize and load array with values
                              7.
                              8. private static int[] produceNumber = new int[] {2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024};
                              9.
                              10. private static String[] produceName = new String[] {"carrots","pot atoe", "bananas", "tomatoe", "lettuce", "apples", "oranges", "grapes", "cherrie", "celery"};
                              11.
                              12. private static int[] produceStock = new int[] {20, 50, 12, 15, 10, 30, 40, 6, 75, 8};
                              13.
                              14. private static double[] producePrice = new double[] {.50,2.75,.60,1 .00,1.50,2.50,3 .00,1.89,2.50,1 .48};
                              15.
                              16.
                              17. public static void DisplayOutput()
                              18.
                              19. { //Displays inventory and totals
                              20.
                              21. System.out.prin tln("Item Number\tProduce Name\tStock\tPr ice\tTotal Price\n");
                              22.
                              23. NumberFormat numForm = NumberFormat.ge tCurrencyInstan ce();//Displays double values as currency
                              24.
                              25. double totalValue = 0;
                              26.
                              27. for (int i=0; i<10; i++){
                              28.
                              29. // String price = numform.format( producePrice[i]);
                              30.
                              31. System.out.prin tln(produceNumb er[i]+"\t\t"+ produceName[i]+"\t\t"+produce Stock[i]+"\t"+numForm.f ormat(producePr ice[i])+
                              32.
                              33. "\t"+(numForm.f ormat(produceSt ock[i]*producePrice[i])));
                              34.
                              35. totalValue=tota lValue+(produce Stock[i]*producePrice[i]);
                              36. }
                              37. System.out.prin tln("\n\nTotal Stock Value: "+numForm.forma t(totalValue));
                              38. }
                              39. }
                              40.
                              41. // Inventory Part 2 July 17, 2008
                              42.
                              43.
                              44.
                              45. class Sort {
                              46.
                              47. public static void main(String[] args) {
                              48.
                              49.
                              50. List<String> list = Arrays.asList (args);
                              51. Collections.sor t (list);
                              52. System.out.prin tln (list);
                              53.
                              54.
                              55. }
                              56. }

                              Comment

                              Working...