Help :( How do i solve this problem?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Zerofury
    New Member
    • Jun 2007
    • 9

    Help :( How do i solve this problem?

    Okay this is what i'm attempting to do.
    I have to modify this program that i wrote so that it allows the user to list items by alpha as an option on the main menu. Here is my problem. If i sort the array of names by value into alpha, the prices, quantities, ect won't correlate anymore, because the names will have switched positions within the name array. However, the prices in the price array and quantities in the quantity array will not move, and will be mismatched. My teacher has been no help, and i don't have fellow students to work with. Is there any suggestions anyone can make?

    I was considering using a bubble sort, which i can figure out okay, but how do i keep from having my arrays not matching up?!?! By the time this is answered it's going to be too late for me to get any credit on the assignment... I also have to somehow SUBCLASS each of those items. Basically i've come to the conclusion i have to encase each item in an object, but i can't seem to get that to cooperate if they are in the same java file, and i can't figure out how to create an array and fill it with these objects. The teacher said it was the same syntax, but what do i put BEFORE the name of the array? (since it's objects, not integers).
    Simply put i need a lot of help, because i have a teacher who's been little if any help, takes forever to respond, and i have no classmates to work with.

    import java.util.Scann er;

    public class Inventory

    {

    public static void main ( String args[]) //primary user interface

    {

    Scanner input = new Scanner( System.in ); // allow user input

    //declare arrays and variables
    int itemchosen; //item who's information you wish to view
    int choice; //declare variable choice
    String itemname[] = {"Weak Heal Potion", "Regular Heal Potion", "Fire Gem", "Ice Gem", "Thunder Gem", "Water Gem", "Earth Gem", "Roc Feather", "Shield Scroll", "Haste Powder"}; //10 different items
    int itemnumber[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; //item's number
    double price[] = {10.20, 20.40, 50.00, 50.00, 50.00, 50.00, 50.00, 20.70, 30.00, 99.99}; //unit price
    int quantity[] = {5, 4, 3, 7, 8, 9, 10, 3, 44, 5}; //unit quantity
    double stockvalue;//value of quantity of those items
    double inventoryvalue; // overall inventory value

    inventoryvalue = 0; //initialize variable
    do
    {
    System.out.prin tln( "Please select an option and press enter:\n [1] View item information\n [2] View overall stock value\n [3] Full overview of items\n [4] Exit program" ); //main menu

    choice = input.nextInt() ; //input choice

    switch (choice) // check user input to open correct menu

    {

    case 1: // user chooses option 1 to view an item
    System.out.prin tln( "Please input the item who's information you wish to view:");

    itemchosen = input.nextInt() ;

    stockvalue = quantity[itemchosen]*price[itemchosen];

    System.out.prin tf( " Item Number: %d\n Item Name: %s\n Quantity: %d\n Cost:$%.2f\n Value of all in stock: $%.2f\n", itemnumber[itemchosen], itemname[itemchosen], quantity[itemchosen], price[itemchosen], stockvalue);
    choice = 0; // causes loop to repeat
    break;


    case 2: // user chooses option 2 to find out total value of inventory

    for (int counter = 0; counter < itemnumber.leng th; counter++ )
    inventoryvalue += (price[counter]*quantity[counter]);

    System.out.prin tf( " The total value of your inventory is %.2f\n", inventoryvalue) ;
    choice = 0; //causes loop to repeat
    break;

    case 3: //user wants to view a complete store list

    for (int counter = 0; counter < itemnumber.leng th; counter++ )
    System.out.prin tf( " Item Number: %d\n Item Name: %s\n Quantity: %d\n Cost:$%.2f\n\n ", itemnumber[counter], itemname[counter], quantity[counter], price[counter]);
    choice = 0; //causes loop to repeat

    case 4: //user chooses to exit program
    break; //allows program to exit

    default: // user enters anything not accepted
    System.out.prin tf( "That is not a valid option. Please re-enter.");
    choice = 0; // causes loop to repeat
    break;

    } // end switch

    } while (choice == 0);
    } // end main

    } //end file
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    There's an article in the Java Articles Section (see the menu bar above) that does
    exactly what you want. All you have to do is implement one little interface for
    your arrays. See the "generic heap sort" article.

    kind regards,

    Jos

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      And make sure you actually read the whole article and understand it first.

      Comment

      Working...