...

View Full Version : Urgent Help needed with implement a method in a loop



Buffalo_Soldier
01-10-2012, 07:29 AM
Friends i'm in a dire situation only have wednesday to submit ..so heres the problem....i have a course work assigment where i have to hardwire Hardware items and their item codes description and prices and ask the user to input their name address the itemCode and quantity nad then creat an invoice which looks like this


HARDWARE ITEMS
CODE DESCRIPTION UNIT PRICE
K16 Wood screws, brass, 20mm, pack of 50 $ 7.75
D24 Wood glue, clear, 1 litre $ 5.50
M93 Sandpaper, medium grade, 100 sheets $10.25
M94 Sandpaper, fine grade, 100 sheets $14.75

PLACE YOUR ORDER!
NAME: Bill Silic
ADDRESS-1: Apt 23
ADDRESS-2: Building 8
ADDRESS-3: Winchester
POST CODE: 89763
ENTER CODE (XX to Stop):
CODE: D24
QUANTITY: 2
CODE: K16
QUANTITY: 1
CODE:XX


INVOICE FOR ORDER
Bill Silic
Apt 23
Building 8
Winchester POST CODE: 89763
D24 Wood glue, clear, 1 litre 2 @ $ 5.50 $ 11.00
K16 Wood screws, brass, 20mm, pack of 50 1 @ $ 7.75 $ 7.75
TOTAL: $ 18.75
Shipping: 2 items @ 1.00 2.00
TOTAL INCL. SHIPPING: 20.75
VAT at 20% 4.15
TOTAL TO PAY: 24.90

I HAVE ALREADY COMPLETED THIS BY MYSELF AND GOT IT WORKING WITH NO PROBLEM ..But my problem is that the teacher wants us to TEST FOR NULL ENTRY meaning if the user enters the wrong Itemcode or no ItemCode at all and give an sensible output for that case ..... she wants us to put it in a method after main and then call it in main using a do while loop so my method looks something like this


Code:

p
ublic static boolean isSomethingCorrect(String itemCode){
if(itemCode.equals("")||itemCode.equals(null)){
System.out.println("A sensible output");
return true;
}//close if
return false;
}


}

// and i have to implement it where i ask the user for input using a Do while loop in this area


Code:


//a do while loop to test null code entry
do{
System.out.println("Please select a code from the list");
String itemCode= br.readLine();



System.out.println("Please enter quantity of items " +itemCode);
String itemQuantity= br.readLine();
int quantity= Integer.parseInt(itemQuantity);
// I Will be forever grateful for any help on this program because i have tryed for hours upon hours and i just cant get it and i need to submit the day after tomorrow

alykins
01-10-2012, 01:50 PM
keep in mind there are a ton of ways to do this and pending "what" you want to do with it later better/more efficient ways to do so as well...


class Program
{
static void Main(string[] args)
{
Program _demo = new Program();
_demo.demo();
}

private Boolean finished = false;

private void demo()
{
int OrderCode=0;
int OrderQuantity=0;


do
{
System.out.printf("Please select a code from the list.");
string itemCode = Console.ReadLine();
if (valid(itemCode) && itemCode.ToUpper() != "XX")
{
System.out.printf("Please enter the quantity of items");
string quantity = Console.ReadLine();
if (valid(quantity) && quantity.ToUpper() != "XX")
{
OrderCode = int.Parse(itemCode);
OrderQuantity = int.Parse(quantity);
System.out.printf("You ordered %d of item code %2$d!", OrderCode, OrderQuantity);
}
}
} while (!finished);
}

private Boolean valid(string Eval)
{
try
{
int i = int.Parse(Eval);
return true;
}

catch
{
if (Eval.ToUpper() == "XX")
{
finished = true;
return true;
}
else
{
System.out.printf("Invalid Entry.");
return false;
}
}
}
}


please note my native language is C# and not Java so my conversion may be off a little :rolleyes:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum