473,378 Members | 1,354 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,378 software developers and data experts.

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.Scanner;

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.println( "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.println( "Please input the item who's information you wish to view:");

itemchosen = input.nextInt();

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

System.out.printf( " 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.length; counter++ )
inventoryvalue += (price[counter]*quantity[counter]);

System.out.printf( " 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.length; counter++ )
System.out.printf( " 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.printf( "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
Jun 29 '07 #1
2 1339
JosAH
11,448 Expert 8TB
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
Jun 29 '07 #2
r035198x
13,262 8TB
And make sure you actually read the whole article and understand it first.
Jun 29 '07 #3

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

Similar topics

1
by: the_proud_family | last post by:
HELP ME PLEASE!! my email is the_proud_family@yahoo.com I can't get the ball to go up right side and then I need it to turn around and keep turning until velocity=0 I have been at it for the ...
28
by: stu_gots | last post by:
I have been losing sleep over this puzzle, and I'm convinced my train of thought is heading in the wrong direction. It is difficult to explain my circumstances, so I will present an identical...
15
by: Alfonso Morra | last post by:
Hi, I have some code from an example, that I want to retrofit into my project. The code from the example has the following line: SharedAppenderPtr myAppender( new...
1
by: Tito Brezovacki via .NET 247 | last post by:
Hi. The problem is very simple to describe, but tough to solve :( I have a printer that doesn't print on win98 using the usualprocedures (print() in C#) . I could barely get it to work on xpby...
4
by: Adrian | last post by:
This is off topic - however, I badly need to solve this problem. A program has been installed in Program Files of an W98 machine. Subsequently the program was removed from Program Files, not in...
16
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and...
21
by: c | last post by:
Hi everybody. I'm working on converting a program wriiten on perl to C, and facing a problem with concatenate strings. Now here is a small program that descripe the problem, if you help me to...
8
by: Dhananjay | last post by:
hello everyone Do you have any information how to generate a tool using .net which is used to translate the web page contents to html format. Plz reply me asap Thanks in advance Dhananjay
8
by: SanjaiGandhi | last post by:
Hi ...i am new to programing....pls help to overcome this program.. The Program is..: if a = 557..using for loop or while or dowhile ..we have to get the answer for 5+5+7..that is what ever...
6
by: Kyote | last post by:
I'm trying to make, what I thought, would be a simple application. This application will help me organize files I have a lot of, on my computer(ebooks, pictures, etc..). I'm currently dealing with...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.