|
Go4Expert Member
|
|
| 26Nov2009,18:24 | #11 |
|
could explain to me a little more thanks
|
|
Ambitious contributor
|
![]() |
| 26Nov2009,19:26 | #12 |
|
Ok. Here is the way you can append/concatenate two variables values:
Code:
str="";
str1="";
x="value of x";
y="value of y";
str = str + x + y;
str1 = str1 + x + "\n" + y;
System.out.println("str value: " + str);
System.out.println("str1 value: " + str1);
Code:
str value: value of xvalue of y str1 value: value of x value of y
Stormstreet
like this
|
|
Go4Expert Member
|
|
| 27Nov2009,05:39 | #13 |
|
str = str+"\n"+r+" "+ de +" "+ ic+" "+"@"+" "+c+" "+ " TOTAL $" + r*c +"\n"+;
I USED THIS AND IT WORKED LIKE A CHARM THANKS ALOT. ONE MORE THING THOUGH LETS SAY I WANNA ADD UP THE GRAND TOTAL OF WEIGHT OR TOTAL COST OF ALL ITEMS CAN I DO THAT???? |
|
Go4Expert Member
|
|
| 27Nov2009,06:40 | #14 |
|
i actually am geeting through
will post any other problems thanks u really r gr8!!!!!!!!!!!!!!!!!!!!1 |
|
Ambitious contributor
|
![]() |
| 27Nov2009,07:05 | #15 |
|
Quote:
Originally Posted by Stormstreet
Stormstreet
like this
|
|
Go4Expert Member
|
|
| 27Nov2009,07:11 | #16 |
|
U rock u should become a teacher lol
|
|
Go4Expert Member
|
|
| 28Nov2009,00:36 | #17 |
|
can i nest these instead of having all these do while loops or should i leave them??
//code do{ System.out.println("ENTER Weight OF ITEM IN GRAMS:"); s = br.readLine(); w = Integer.parseInt(s); }while( !isValidIw(w)); do{ System.out.println("ENTER COST OF ITEM ($):"); s = br.readLine(); c = Float.parseFloat(s); }while( !isValidIc(c)); do{ System.out.println("ENTER NUMBER OF ITEMS REQUIRED:"); s = br.readLine(); r = Integer.parseInt(s); }while( !isValidIr(r)); //code |
|
Ambitious contributor
|
![]() |
| 28Nov2009,05:16 | #18 |
|
You should not nest the WHILE loops, instead you can combine the three loops to have a single loop alone.
|
|
Go4Expert Member
|
|
| 29Nov2009,00:30 | #19 |
|
think i'll leave it as that thanks though
|
|
Go4Expert Member
|
|
| 9Jan2010,22:39 | #20 |
|
i decided to use vectors here it the code
code : Code:
import java.io.*;
import java.util.*;
public class Cw1{
//Section 1: global variables
public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static String input,code,desc, weight,cost,amount;
static float TotalWeight = 0.0f,TotalCost = 0.0f,grandTotalWeight=0.0f,grandTotalCost=0.0f;
static boolean isItemCode = false,isItemDesc = false,isItemWeight = false,isItemCost = false,isItemAmount = false;
public static void main(String[ ] args) throws Exception{
// Section 2 - Welcome message
System.out.println("*****************************");
System.out.println("** Welcome to Trekker **");
System.out.println("*****************************");
// Section 3 - Loop that prompts for order information,
// tests if it is valid, prints it in a summary
// and stores it in a vector until user is done
Vector v = new Vector();
do{
System.out.println("Enter Item code");
input = br.readLine();
code = input;
System.out.println("Enter Item Description");
do{
input = br.readLine();
desc = input;
isItemDesc = isDescCorrect(input);
}
while(!isItemDesc);
System.out.println("Enter Item weight");
do{
input = br.readLine();
weight = input;
isItemWeight = isItemWeightCorrect(input);
}
while(!isItemWeight);
System.out.println("Enter Item cost");
do{
input = br.readLine();
cost = input;
isItemCost = isItemCostCorrect(input);
}
while(!isItemCost);
System.out.println("Enter Item amount");
do{
input = br.readLine();
amount = input;
isItemAmount = isItemAmountCorrect(input);
}
while(!isItemAmount);
Items myItems = new Items(code, desc, Float.parseFloat(weight), Float.parseFloat(cost), Integer.parseInt(amount),TotalCost);
// print summary for item
System.out.println("SUMMARY DATA FOR ITEM "+myItems.getItemCode()+", "+desc);
TotalWeight = calculateTotalWeight(Float.parseFloat(weight),Integer.parseInt(amount));
System.out.println("TOTAL WEIGHT: "+TotalWeight+" grams");
TotalCost = calculateTotalCost(Float.parseFloat(cost),Integer.parseInt(amount));
System.out.println("TOTAL COST: $"+TotalCost);
// Section 5 - store user input in vector
v.add(myItems);
grandTotalWeight=grandTotalWeight+TotalWeight;
grandTotalCost=grandTotalCost+TotalCost;
System.out.println("");
System.out.println("Would you like to add another item?");
System.out.println("Please type Y or y for yes");
input = br.readLine();
if(input.equals("Y")||input.equals("y")){
System.out.println("");
}else{
System.out.println("You have ordered");
for(int i =0; i<v.size(); i++){
System.out.print(((Items)v.elementAt(i)).getItemAmount()+" ");
System.out.print(((Items)v.elementAt(i)).getItemDesc()+" ");
System.out.print("("+((Items)v.elementAt(i)).getItemCode()+") ");
System.out.print("@"+" ");
System.out.print("$"+((Items)v.elementAt(i)).getItemCost()+" ");
System.out.println("TOTAL COST $"+((Items)v.elementAt(i)).getItemTotalCost());
}//close for loop
System.out.println("GRAND TOTAL WEIGHT:"+grandTotalWeight+" grams");
System.out.println("GRAND TOTAL COST: $"+grandTotalCost);
System.out.println("Good bye");
break;
}//close else
}//close outer do loop
while(true);
System.exit(0);
}//close main
// SECTION 4 METHODS
public static boolean isDescCorrect(String itemDescription){
if(itemDescription.equals("")||itemDescription.equals(" ")){
System.out.println("Invalid Item,please re-enter Item.");
return false;
}else{
return true;
}
}
public static float calculateTotalWeight(float itemWeight, int itemAmount){
float totalWeight = itemWeight * itemAmount;
return totalWeight;
}
public static float calculateTotalCost(float itemCost, int itemAmount){
float totalCost = itemCost * itemAmount;
return totalCost;
}
public static boolean isItemWeightCorrect(String ItemWeight){
float ItemWt=Float.parseFloat(ItemWeight);
if(ItemWt <= 0){
System.out.println("Please re-enter weight,cannot be less than or equal to 0");
return false;
}else{
return true;
}
}
public static boolean isItemCostCorrect(String ItemCost){
float ItemCst=Float.parseFloat(ItemCost);
if(ItemCst <= 0){
System.out.println("Please re-enter cost,cannot be less than or equal to 0");
return false;
}else{
return true;
}
}
public static boolean isItemAmountCorrect(String ItemAmount){
int ItemAmt=Integer.parseInt(ItemAmount);
if(ItemAmt <= 0){
System.out.println("Please re-enter items required,cannot be less than or equal to zero");
return false;
}else{
return true;
}
}
}
Last edited by shabbir; 11Jan2010 at 10:38.. Reason: Code Block |


