• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

I cannot fix this program! if anyone can help!

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class App {
public static void main (String[] args) {
//Author: Brandon Astley
//Date last modified: 05-25-04
// This array holds the names of the parts.
String[] names = {"Framistan", "widget", "Gadget"};
// This array will hold the price of each part.
double[] prices = new double[3];
// This array will hold the quantity of the part.
int[] quantity = new int[3];

// Variable for holding the user's menu selection.
byte choice = 0;
// Constants for testing the user's menu selection.
final byte CHANGE_PART_PRICE = 1;
final byte CHANGE_PART_QUANTITY = 2;
final byte SHOW_INVENTORY_INFORMATION = 3;
final byte EXIT = 4;
// This is the main loop. It displays the menu and processes the user's
// menu choice until they choose to EXIT.

while (choice != EXIT) {
// Draw a separator and display the menu.
Utility.separator(80, '=');
System.out.println("\tSALES PROCESSING");
Utility.skip();
System.out.println("1 - Change Part Price");
System.out.println("2 - Change Part Quantity");
System.out.println("3 - Show Inventory Information");
System.out.println("4 - Exit");
Utility.skip();
System.out.print("Enter your selection: ");
// Read the user's menu selection.
choice = Keyboard.readByte();
// Process the user's selection.
switch (choice) {
// This case changes the part price
//*********************************************************************************************************************
case CHANGE_PART_PRICE: {
// Local variables for storing the part price
int Nindex;
double Nprice;
boolean storeBoolean;
// Draw a separator on the screen.
Utility.separator(80, '=');
Nindex = askPart(names);
if (Nindex == -1)
{
System.out.print("Invalid Selection");
Utility.pressEnterKey();

do {
System.out.print("\tEnter Price: ");
Nprice = Keyboard.readDouble();
if (Nprice <= 0) {
Utility.skip();
System.out.println("\t\tERROR - Invalid price");
Utility.separator(80, '=');
}
} while (Nprice <= 0);
storeBoolean = store(prices, Nprice, Nindex);
if (storeBoolean = true)
{
System.out.println("\t" + "Price Stored");
Utility.pressEnterKey();
}
else
{
System.out.println("Invalid Price");
Utility.pressEnterKey();
}
break;
}
// This case changes the part quantity.
//**********************************************************************************************************************
case CHANGE_PART_QUANTITY:{
// Local variables for storing the part price
int Nindex;
int Nquan;
boolean storeBoolean;
// Draw a separator on the screen.
Utility.separator(80, '=');
Nindex = askPart(names);
if (Nindex == -1)
{
System.out.print("Invalid Selection");
Utility.pressEnterKey();
break;
}
else
{

do {
System.out.print("\tEnter Quantity: ");
Nquan = Keyboard.readInt();
if (Nquan <= 0) {
Utility.skip();
System.out.println("\t\tERROR - Invalid Quantity");
Utility.separator(80, '=');
}
} while (Nquan <= 0);
storeBoolean = store(quantity, Nquan, Nindex);
if (storeBoolean = true)
{
System.out.println("\t" + "Quantity Stored");
Utility.pressEnterKey();
}
else
{
System.out.println("Invalid Quantity");
Utility.pressEnterKey();
}
break;
}}
}
// This case displays the top-performing sales rep.
//***********************************************************************************************************
case SHOW_INVENTORY_INFORMATION:{
// Draw a separator on the screen.
Utility.separator(80, '=');
// Display column headings.
Utility.printField(10, "Part Name",Utility.ALIGN_LEFT);
Utility.printField(16, "Price");
Utility.printField(25, "Quantity");// );
Utility.printField(28, "Inventory Value");
Utility.skip();
// Display column headings UNDERLINE.
Utility.printField(10, "----------",Utility.ALIGN_LEFT);
Utility.printField(17, "-------");
Utility.printField(25, "----------");
Utility.printField(27, "---------------");
Utility.skip();
for (int i = 0; i < names.length; i++)
{
Utility.printField(13, "" + names[i],Utility.ALIGN_LEFT);
Utility.printField(13, Utility.moneyFormat(prices[i]));
Utility.printField(22, "" + quantity[i]);
Utility.printField(22, "" + Utility.moneyFormat(prices[i] * quantity[i]));
Utility.skip();
}
// Skip a line, and ask the user to press the ENTER key. When they
// do, break out of the switch.
Utility.skip();
Utility.pressEnterKey();
break;
}
// This case will exit.
//*******************************************************************************************************************
case EXIT: {
Utility.separator(80, '=');
System.out.println("\tPROCESSING COMPLETE");
break; }
// This case handles an invalid menu selection. It draws a separator
// line, an error message, and breaks out of the switch.
default: {
Utility.separator(80, '=');
System.out.println("\tERROR - Invalid menu choice");
break; }
}
}
}

//********************************************************************************
// This method is passed a String Array & a String.
// It compares the User Choice to Items stored.
// If/when it finds a match, records index position saved and returned.
public static int askPart(String[] nm)
{
int nameIndex;
int Index;
for (int i = 0; i < nm.length; i++)
{
Utility.printField(1, "\t" + (i+1));
Utility.printField(2, " - " + nm[i]);
Utility.skip();
}
Utility.skip();
System.out.print("\t" + "Enter your selection: ");

nameIndex = Keyboard.readInt();
//itemChoice(Keyboard.readInt());
if (nameIndex== 1)
{
return Index = 0;
}
if (nameIndex == 2)
{
return Index = 1;
}
if (nameIndex == 3)
{
return Index = 2;
}
return Index = -1;
}
//************************************************************************************
// STORE overloaded method;
public static boolean store(double[] da, double price, int i)
{
boolean storeBoolean = false;
da [i] = price;
if (da[i] == price)
{
return storeBoolean = true;
}
else
{
return storeBoolean = false;
}
}
// STORE
public static boolean store(int[] ia, int value, int i)
{
boolean storeBoolean = false;
ia [i] = value;
if (ia[i] == value)
{
return storeBoolean = true;
}
else
{
return storeBoolean = false;
}
}

}
 
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Brandon,
That's rather a lot of code - what problem are you having?
After a quick look, one thing to note - System.in.read() returns a byte representing a character's ASCII code (Umm, maybe not ASCII, but some Unicode subset...internationalisation aside). The ASCII code for character '1' is 49.
So when you try to compare to, for example, CHANGE_PART_PRICE (ie, integer 1) the result is False. You'll need to convert from Byte '1' == int 49 to Int 1, etc.
Also, it looks like your code would be a lot easier to read if you broke it up a bit -

Finally, the code in your posts will be easier to read if you use the CODE UBB tag.
Cheers,

--Tim
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic