Condensing Code
import cs1.Keyboard;
import java.text.DecimalFormat;
public class BillOfSaleClass
{
static final double PC_PRICE = 999.00;
static final double PRINTER_PRICE = 199.00, TAX_PRICE = 0.075;
static final double MONITOR_PRICE=399.00, RAM_MB_PRICE=000.20, HD_GB_PRICE=001.00;
public static void main(String[] args)
throws java.io.IOException
{
DecimalFormat df = new DecimalFormat("0000.00");
DecimalFormat qtyf = new DecimalFormat("00");
int PC, Printer, Monitor, Ram, HD;
System.out.print("How many PCs do you want to purchase?");
System.out.flush();
PC=cs1.Keyboard.readInt();
System.out.print("How many Printers do you want to purchase?");
System.out.flush();
Printer=cs1.Keyboard.readInt();
System.out.print("How many monitors do you want to purchase?");
System.out.flush();
Monitor=cs1.Keyboard.readInt();
System.out.print("How Mb of Ram do you need?");
System.out.flush();
Ram=cs1.Keyboard.readInt();
System.out.print("How many Gb of hard disk space do you need?");
System.out.flush();
HD=cs1.Keyboard.readInt();
System.out.println("\n\t\t***************\n\t\t*Bill of Sales*"
+ "\n\t\t***************\n");
System.out.println("Qty.\tItem\t\tUnit price\tTotal price\n"
+ "--"
+ "-");
System.out.print(qtyf.format(PC)+"\tPC\t\t$");
System.out.println(df.format(PC_PRICE)+"\t$"+(df.format(PC_PRICE*PC)));
System.out.print(qtyf.format(Printer)+"\tPrintor\t\t$");
System.out.println(df.format(PRINTER_PRICE)+"\t$"+(df.format(PRINTER_PRICE*Printer)));
System.out.print(qtyf.format(Monitor)+"\tMonitor\t\t$");
System.out.println(df.format(MONITOR_PRICE)+"\t$"+(df.format(MONITOR_PRICE*Monitor)));
System.out.print(qtyf.format(Ram)+"\tRam\t\t$");
System.out.println(df.format(RAM_MB_PRICE)+"\t$"+(df.format(RAM_MB_PRICE*Ram)));
System.out.print(qtyf.format(HD)+"\tHd\t\t$");
System.out.println(df.format(HD_GB_PRICE)+"\t$"+(df.format(HD_GB_PRICE*Ram)));
System.out.println("--"
+ "-");
System.out.println("Total without tax\t\t\t$"+(df.format(PC_PRICE*PC+PRINTER_PRICE*Printer+MONITOR_PRICE*Monitor+RAM_MB_PRICE*Ram+HD_GB_PRICE*HD)));
System.out.println("Tax\t\t\t\t\t$"+(df.format((PC_PRICE*PC+PRINTER_PRICE*Printer+MONITOR_PRICE*Monitor+RAM_MB_PRICE*Ram+HD_GB_PRICE*HD)*TAX_PRICE)));
System.out.println("Total with Tax\t\t\t\t$"+(df.format((PC_PRICE*PC+PRINTER_PRICE*Printer+MONITOR_PRICE*Monitor+RAM_MB_PRICE*Ram+HD_GB_PRICE*HD)+(PC_PRICE*PC+PRINTER_PRICE*Printer+MONITOR_PRICE*Monitor+RAM_MB_PRICE*Ram+HD_GB_PRICE*HD)*TAX_PRICE)));
}
}
As you can see, the end of my code is a mess...it works fine but is there an easier way to add the totals up then the way I did it. I tried making new double but it seems that since PC, Printer, Monitor etc are int they can't convert, if you know what I'm trying to say. I sure there is something else I could do...
Kaeloc
[3069 byte] By [
Kaeloca] at [2007-11-26 18:34:58]

1. Use code tags2. I would suggest create a method that would prompt and output messages. Rather than repeating so much code.
I not going to pretend I know what your talking about...but thanks. I go research that now.Kaeloc
Precalculate total, tax and total+tax in double variables. Then print them. You can safely sayint x = 4;double d = 9.90;double d2 = x*d; #
> I not going to pretend I know what your talking> about...but thanks. I go research that now.> > KaelocBy using code tags I mean: http://forum.java.sun.com/help.jspa?sec=formattingDo you know how to create new methods yet?
Do mean something like this?...Instead of this...System.out.print("How many PCs do you want to purchase?");System.out.flush();PC=cs1.Keyboard.readInt();Do this?PC=cs1.Keyboard.readInt("How many PCS do you want to purchase?");Kaeloc
> Do mean something like this?...
Sure. And the same for the output like:
System.out.print(qtyf.format(Monitor)+"\tMonitor\t\t$");
System.out.println(df.format(MONITOR_PRICE)+"\t$"+(df.format(MONITOR_PRICE*Monitor)));
After you do that you might still find other ways to improve the code.
O ok! Code tags right!For a second I thought that code tags were something in java that I hadn't learned yet. lolKaeloc
> Precalculate total, tax and total+tax in double
> variables. Then print them. You can safely say
> > int x = 4;
> double d = 9.90;
> double d2 = x*d;
>
> #
I had already tried that...but I always get the error message that it can't find the symbol for PC. Should I put it before or after the main?
Kaeloc
Try something like this:
double pcTotal = (PC_PRICE*PC),
printerTotal = (PRINTER_PRICE*Printer),
monitorTotal = (MONITOR_PRICE*Monitor),
ramTotal = (RAM_MB_PRICE*Ram),
hdTotal = (HD_GB_PRICE*HD),
hardwareTotal= (pcTotal+printerTotal+monitorTotal+ramTotal+hdTotal);
taxTotal = (hardwareTotal*TAX_PRICE),
hardwareAndTaxAmount = (hardwareTotal+taxTotal);
System.out.print(qtyf.format(PC)+"\tPC\t\t$");
System.out.println(df.format(PC_PRICE)+"\t$"+(df.format(pcTotal)));
System.out.print(qtyf.format(Printer)+"\tPrintor\t\t$");
System.out.println(df.format(PRINTER_PRICE)+"\t$"+(df.format(printerTotal)));
System.out.print(qtyf.format(Monitor)+"\tMonitor\t\t$");
System.out.println(df.format(MONITOR_PRICE)+"\t$"+(df.format(monitorTotal)));
System.out.print(qtyf.format(Ram)+"\tRam\t\t$");
System.out.println(df.format(RAM_MB_PRICE)+"\t$"+(df.format(ramTotal)));
System.out.print(qtyf.format(HD)+"\tHd\t\t$");
System.out.println(df.format(HD_GB_PRICE)+"\t$"+(df.format(hdTotal)));
System.out.println("--"
+ "-");
System.out.println("Total without tax\t\t\t$"+(df.format(hardwareTotal)));
System.out.println("Tax\t\t\t\t\t$"+(df.format((taxTotal)));
System.out.println("Total with Tax\t\t\t\t$"+(df.format((hardwareAndTaxAmount)));
Then consider creating a method to do the calc and another to print.
import cs1.Keyboard;
import java.text.DecimalFormat;
public class BillOfSaleClass
{
static final double PC_PRICE = 999.00;
static final double PRINTER_PRICE = 199.00, TAX_PRICE = 0.075;
static final double MONITOR_PRICE=399.00, RAM_MB_PRICE=000.20, HD_GB_PRICE=001.00;
static int PC, Printer, Monitor, Ram, HD;
static final double pcTotal = (PC_PRICE*PC),
printerTotal = (PRINTER_PRICE*Printer),
monitorTotal = (MONITOR_PRICE*Monitor),
ramTotal = (RAM_MB_PRICE*Ram),
hdTotal = (HD_GB_PRICE*HD),
hardwareTotal= (pcTotal+printerTotal+monitorTotal+ramTotal+hdTotal);
static final double taxTotal= (hardwareTotal*TAX_PRICE),
hardwareAndTaxAmount = (hardwareTotal+taxTotal);
public static void main(String[] args)
throws java.io.IOException
{
DecimalFormat df = new DecimalFormat("0000.00");
DecimalFormat qtyf = new DecimalFormat("00");
System.out.print("How many PCs do you want to purchase?");
System.out.flush();
PC=cs1.Keyboard.readInt();
System.out.print("How many Printers do you want to purchase?");
System.out.flush();
Printer=cs1.Keyboard.readInt();
System.out.print("How many monitors do you want to purchase?");
System.out.flush();
Monitor=cs1.Keyboard.readInt();
System.out.print("How Mb of Ram do you need?");
System.out.flush();
Ram=cs1.Keyboard.readInt();
System.out.print("How many Gb of hard disk space do you need?");
System.out.flush();
HD=cs1.Keyboard.readInt();
System.out.println("\n\t\t***************\n\t\t*Bill of Sales*"
+ "\n\t\t***************\n");
System.out.println("Qty.\tItem\t\tUnit price\tTotal price\n"
+ "--"
+ "-");
System.out.print(qtyf.format(PC)+"\tPC\t\t$");
System.out.println(df.format(PC_PRICE)+"\t$"+(df.format(PC_PRICE*PC)));
System.out.print(qtyf.format(Printer)+"\tPrintor\t\t$");
System.out.println(df.format(PRINTER_PRICE)+"\t$"+(df.format(PRINTER_PRICE*Printer)));
System.out.print(qtyf.format(Monitor)+"\tMonitor\t\t$");
System.out.println(df.format(MONITOR_PRICE)+"\t$"+(df.format(MONITOR_PRICE*Monitor)));
System.out.print(qtyf.format(Ram)+"\tRam\t\t$");
System.out.println(df.format(RAM_MB_PRICE)+"\t$"+(df.format(RAM_MB_PRICE*Ram)));
System.out.print(qtyf.format(HD)+"\tHd\t\t$");
System.out.println(df.format(HD_GB_PRICE)+"\t$"+(df.format(HD_GB_PRICE*Ram)));
System.out.println("--"
+ "-");
System.out.println("Total without tax\t\t\t$"+(df.format(PC_PRICE*PC+PRINTER_PRICE*Printer+MONITOR_PRICE*Monitor+RAM_MB_PRICE*Ram+HD_GB_PRICE*HD)));
System.out.println("Tax\t\t\t\t\t$"+(df.format((PC_PRICE*PC+PRINTER_PRICE*Printer+MONITOR_PRICE*Monitor+RAM_MB_PRICE*Ram+HD_GB_PRICE*HD)*TAX_PRICE)));
System.out.println("Total with Tax\t\t\t\t$"+(df.format(hardwareAndTaxAmount)));
}
}
When I do it like this the hardwareAndTaxAmount comes out to zero.
What did I do wrong?
Kaeloc
> When I do it like this the hardwareAndTaxAmount comes> out to zero.> What did I do wrong?It appears that you are calculating the totals before you have assigned the values to pc, printer, etc.So total them after you have assigned values to them.
Did you even take a look at what I posted?Mod: Yes I can see that you did - but as was pointed out, you placed the calc B4 the concole inquiries ... notice that I had placed them immediately before the prints.~Bill
I see...the light is beginning to dawn on me...I shall try again.Thanks,Kaeloc
import cs1.Keyboard;
import java.text.DecimalFormat;
public class BillOfSaleClass
{
static final double PC_PRICE = 999;
static final double PRINTER_PRICE = 199, TAX_PRICE = .075;
static final double MONITOR_PRICE=399, RAM_MB_PRICE=.20, HD_GB_PRICE=1;
public static void main(String[] args)
throws java.io.IOException
{
DecimalFormat df = new DecimalFormat("0000.00");
DecimalFormat qtyf = new DecimalFormat("00");
int PC, Printer, Monitor, Ram, HD;
System.out.print("How many PCs do you want to purchase?");
System.out.flush();
PC=cs1.Keyboard.readInt();
System.out.print("How many Printers do you want to purchase?");
System.out.flush();
Printer=cs1.Keyboard.readInt();
System.out.print("How many monitors do you want to purchase?");
System.out.flush();
Monitor=cs1.Keyboard.readInt();
System.out.print("How Mb of Ram do you need?");
System.out.flush();
Ram=cs1.Keyboard.readInt();
System.out.print("How many Gb of hard disk space do you need?");
System.out.flush();
HD=cs1.Keyboard.readInt();
double pcTotal= (PC_PRICE*PC),
printerTotal = (PRINTER_PRICE*Printer),
monitorTotal = (MONITOR_PRICE*Monitor),
ramTotal = (RAM_MB_PRICE*Ram),
hdTotal = (HD_GB_PRICE*HD),
totalWithoutTax= (pcTotal+printerTotal+monitorTotal+ramTotal+hdTotal);
double taxTotal= (totalWithoutTax*TAX_PRICE),
total = (totalWithoutTax+taxTotal);
System.out.println("\n\t\t***************\n\t\t*Bill of Sales*"
+ "\n\t\t***************\n");
System.out.println("Qty.\tItem\t\tUnit price\tTotal price\n"
+ "--"
+ "-");
System.out.print(qtyf.format(PC)+"\tPC\t\t$");
System.out.println(df.format(PC_PRICE)+"\t$"+(df.format(PC_PRICE*PC)));
System.out.print(qtyf.format(Printer)+"\tPrintor\t\t$");
System.out.println(df.format(PRINTER_PRICE)+"\t$"+(df.format(PRINTER_PRICE*Printer)));
System.out.print(qtyf.format(Monitor)+"\tMonitor\t\t$");
System.out.println(df.format(MONITOR_PRICE)+"\t$"+(df.format(MONITOR_PRICE*Monitor)));
System.out.print(qtyf.format(Ram)+"\tRam\t\t$");
System.out.println(df.format(RAM_MB_PRICE)+"\t$"+(df.format(RAM_MB_PRICE*Ram)));
System.out.print(qtyf.format(HD)+"\tHd\t\t$");
System.out.println(df.format(HD_GB_PRICE)+"\t$"+(df.format(HD_GB_PRICE*HD)));
System.out.println("--"
+ "-");
System.out.println("Total without tax\t\t\t$"+(df.format(totalWithoutTax)));
System.out.println("Tax\t\t\t\t\t$"+(df.format(taxTotal)));
System.out.println("Total with Tax\t\t\t\t$"+(df.format(total)));
}
}
When I try to..
PC=cs1.Keyboard.readInt("How many PCs do you want to purchase?");
Instead of this...
System.out.print("How many PCs do you want to purchase?");
System.out.flush();
PC=cs1.Keyboard.readInt();
I get an error message saying that "readInt() in cs1.Keyboard cannot be applied to (java.lang.String)"
What I am doing wrong?
Kaeloc
Do my Keyboard class methods not support those kinds of prompting messages?Kaeloc
You had said in your orig post that the pgm worked. I strongly suggest then that you do not change the part that is working regarding the obtaining of I/P values, and only change the parts regarding the streamlining of the O/P printing.
I don't know how I could change anything with the output (that's a little outside my realm of knowledge).Thanks Zadok and Bill for all the help. I can truthfully say that I learned more today about Java then I ever have before.Duke Points have been awarded...Kaeloc
Was I off base then when I responded to your original post statement/question:
"As you can see, the end of my code is a mess...it works fine but is there an easier way to add the totals up then the way I did it. I tried making new double but it seems that since PC, Printer, Monitor etc are int they can't convert, if you know what I'm trying to say. I sure there is something else I could do..."
Next, I don't understand what you mean by: " I don't know how I could change anything with the output ... "
I provided the code - you just need to replace what you have with the new code. I did not compile it, but it propable is not far from complete.
At any rate, you are welcome.
~Bill