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
{
int PC=2, Printer=3, Monitor=2, Ram=64, HD=4;
System.out.print("How many PCs do you want to purchase?");
System.out.flush();
System.out.print("How many Printers do you want to purchase?");
System.out.flush();
System.out.print("How many monitors do you want to purchase?");
System.out.flush();
System.out.print("How Mb of Ram do you need?");
System.out.flush();
System.out.print("How many Gb of hard disk space do you need?");
System.out.flush();
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.println(PC+"\tPC\t\t$"+PC_PRICE+"\t\t$"+PC_PRICE*PC);
System.out.println(Printer+"\tPrinter\t\t$"+PRINTER_PRICE+"\t\t$"+PRINTER_PRICE*Printer);
System.out.println(Monitor+"\tMonitor\t\t$"+MONITOR_PRICE+"\t\t$"+MONITOR_PRICE*Monitor);
System.out.println(Ram+"\tRam\t\t$"+RAM_MB_PRICE+"\t\t$"+RAM_MB_PRICE*Ram);
System.out.println(HD+"\tHd\t\t$"+HD_GB_PRICE+"\t\t$"+HD_GB_PRICE*HD);
System.out.println("--"
+ "-");
System.out.println("Total without tax\t\t\t$"+(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$"+(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");
}
}
Here is my code...
I now that it could be simplified a lot, but this is my weak attempt at my first programs.
So I want to have keyboard input but I don't have a keyboard class yet. Thats why I don't import the keyboard here. Also under each system.out.flush() I want to read from the keyboard and use that to assign values to int PC, Printer , Monitor, etc. Since I don't have the keyboard class yet I just took the keyboard out for now so that I could test the program and the formatting. As you can see I assign values to the int fields so that I could test the multiplication later on in the code.
There has to be a easier way to format the output but this is the best I could do with what I know right now.
At static final double at the top you can see where I was trying to get the decimals formatting to work...it didn't.
Also when at the end of the program I am adding up the totals, is there a better way to add the purchase totals then what I did?
Any help appreciated...
You need some brackets in there otherwise your calcualations will be out.
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)
{
double totalWithoutTax = (PC_PRICE*1)+(PRINTER_PRICE*1)+(MONITOR_PRICE*1)+(RAM_MB_PRICE*1)+(HD_GB_PRICE*1);
System.out.println(String.format("Total without tax\t\t\t$%.2f", totalWithoutTax));
}
Ted.
> You need some brackets in there otherwise your
> calcualations will be out.
> > 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)
> {
> double totalWithoutTax =
> =
> (PC_PRICE*1)+(PRINTER_PRICE*1)+(MONITOR_PRICE*1)+(RAM_
> MB_PRICE*1)+(HD_GB_PRICE*1);
> System.out.println(String.format("Total without
> ut tax\t\t\t$%.2f", totalWithoutTax));
> }
> Ted.
I don't understand the reasoning behind (PC_PRICE*1)...why multiply it times 1?
import cs1.Keyboard;
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
{
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.println(PC+"\tPC\t\t$"+PC_PRICE+"\t\t$"+PC_PRICE*PC);
System.out.println(Printer+"\tPrinter\t\t$"+PRINTER_PRICE+"\t\t$"+PRINTER_PRICE*Printer);
System.out.println(Monitor+"\tMonitor\t\t$"+MONITOR_PRICE+"\t\t$"+MONITOR_PRICE*Monitor);
System.out.println(Ram+"\tRam\t\t$"+RAM_MB_PRICE+"\t\t$"+RAM_MB_PRICE*Ram);
System.out.println(HD+"\tHd\t\t$"+HD_GB_PRICE+"\t\t$"+HD_GB_PRICE*HD);
System.out.println("--"
+ "-");
System.out.println("Total without tax\t\t\t$"+(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$"+(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");
}
}
Ok here is my code...but I need to know how to get the decimal points to line up at the end.
Any help would be greatly appreciated...as in Duke points.
Try this (you need to have a constant width font)
package testing;
import java.text.DecimalFormat;
public class Test
{
public static void main(String[] args)
{
DecimalFormat df = new DecimalFormat("00000.00");
System.out.println(df.format(127.3));
System.out.println(df.format(12732));
System.out.println(df.format(12));
System.out.println(df.format(0.01));
}
}
#
Message was edited by:
duckbill
Kaeloc,
I didn't do it all for you, I showed you how it should be done. It appears you are far too lazy to apply the idea yourself. The reason for the 1 was because I didn't have an variable called PC like you do. I was hoping you had the intelligence to understand this. My mistake in making that assumption.
Ted.
> Kaeloc,
>
> I didn't do it all for you, I showed you how it
> should be done. It appears you are far too lazy to
> apply the idea yourself. The reason for the 1 was
> because I didn't have an variable called PC like you
> do. I was hoping you had the intelligence to
> understand this. My mistake in making that
> assumption.
>
> Ted.
Ted...
Being lazy and not understanding a new concept are two completely different things. You said so yourself, "I was hoping you had the intelligence to understand this. My mistake in making that assumption." You're right, you did make a mistake, both in assuming and then in your judgment with the above post. When I post I do not want anyone to do the work for me. I was just asking for clarification. Trust me, I spent the entire yesterday working on the problem. And as anyone knows, talking to real people is usually a lot more helpful then reading multiple tutorials where most of the stuff is over your head. In this case, I guess I was wrong.
Ted, in no way do I hold any animosity against you. In fact, I am grateful for the time you spent trying to help me with the problem. What you need to remember is that new programmers are exactly that...new and that means that they don't always understand what may be simple to you. Remember that and you will not get so frustrated when some people need a little more explanation.
Kaeloc
Message was edited by: Kaeloc
P.S.
I did solve my problem thanks to duckbill. I understood what his code meant. He was rewarded for his time. Sorry I couldn't say the same for you.
Kaeloc
> Thanks Ted...
>
> Here I am a brand-new highschool programmer and a
> real professional programmer shows me how to
> act maturely. Great example Ted. And who said
> programmers weren't supportive of each other? I wish
> you the best Ted, in everything you attempt to do.
>
> Kaeloc
Awww thanks. I must admit, i'm not that mature. Nice of you to notice my professionalism!
What makes you think im not a high school goer to?
Ted.