Creating a method to sort objects

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

    #1

    Creating a method to sort objects

    Hi I have created an Inventory program that displays the product name, item number, how many units, and it's price and at the end displays the totals, I have to modify the program so that it will 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.
    In order to do this I have to create a method to calculate the value of the entire inventory and create another method to sort the array items by the name of the product.

    Here is my code:

    import java.text.Numbe rFormat;

    public class Produce {

    //initialize and load array with values

    private static int[] produceNumber = new int[] {2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024};

    private static String[] produceName = new String[] {"carrots","pot atoe", "bananas", "tomatoe", "lettuce", "apples", "oranges", "grapes", "cherrie", "celery"};

    private static int[] produceStock = new int[] {20, 50, 12, 15, 10, 30, 40, 6, 75, 8};

    private static double[] producePrice = new double[] {.50,2.75,.60,1 .00,1.50,2.50,3 .00,1.89,2.50,1 .48};


    public static void DisplayOutput()

    { //Displays inventory and totals

    System.out.prin tln("Item Number\tProduce Name\tStock\tPr ice\tTotal Price\n");

    NumberFormat numForm = NumberFormat.ge tCurrencyInstan ce();//Displays double values as currency

    double totalValue = 0;

    for (int i=0; i<10; i++){

    // String price = numform.format( producePrice[i]);

    System.out.prin tln(produceNumb er[i]+"\t\t"+ produceName[i]+"\t\t"+produce Stock[i]+"\t"+numForm.f ormat(producePr ice[i])+

    "\t"+(numForm.f ormat(produceSt ock[i]*producePrice[i])));

    totalValue=tota lValue+(produce Stock[i]*producePrice[i]);
    }
    System.out.prin tln("\n\nTotal Stock Value: "+numForm.forma t(totalValue));
    }
    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Yep, another person who uses separate arrays where they should've used a
    proper little class to hold the information of an item in the inventory; Fortan
    seems to live on forever ...

    Have a look at this Generic Heap Sort algorithm (there's also a second part)
    that can clear up the array mess.

    Remember: object oriented programming is not about arrays and never rip object
    data apart again just to be able to store them in separate arrays; never,

    kind regards,

    Jos

    Comment

    Working...