
-----------------------------------
Azzy
Tue Sep 21, 2004 3:56 pm

Cashier Program Help
-----------------------------------
I'm making this cashier program in java, my first actual program, and instead of printing the right values, it keeps printing the price as 49.

If anyone can help, I appreciate it.

import java.io.*;
 import java.text.*;
 
 public class cashier
 {
 	public static void main (String[] args)throws Exception
 	{
 		DataInput keyboard = new DataInputStream(System.in);
 		String temp;
 		char response;
 		 			
 		double price = 0.00;
 		double pst = 0.00;
 		double gst = 0.00;
 		double total = 0.00;
 		double pstper = 0.07;
 		double gstper = 0.08;
 		
 		System.out.println ("What is the cost of the item you are buying?");
 		price = System.in.read();
 		
 		pst = price * pstper;
 		gst = price * gstper;
 		total= price + gst+ pst;
 		
 		System.out.print ("Cost    $");
 		System.out.println (price);
 		System.out.print ("PST     $");
 		System.out.println (pst);
 		System.out.print ("GST     $");
 		System.out.println (gst);
 		System.out.print ("Total   $");
 		System.out.println (total);
 	}
 	
 }

-----------------------------------
Andy
Tue Sep 21, 2004 4:26 pm


-----------------------------------
just a thought... u didnt calculate the pst/gst/total price

-----------------------------------
wtd
Tue Sep 21, 2004 8:14 pm


-----------------------------------
import java.lang.*;
import java.io.*;
import java.text.*;

class MoneyReader extends BufferedReader {
	public MoneyReader(Reader in) {
		super(in);
	}

	public double getMonetaryAmount() throws IOException {
		return Double.parseDouble(readLine());
	}
}

public class Cashier
{
	private static MoneyReader keyboard = new MoneyReader(new InputStreamReader(System.in));
	private static DecimalFormat moneyFormat = new DecimalFormat("$#,###,##0.00");

	public static void main (String[] args) throws IOException
	{
		double pstRate = 0.07;
		double gstRate = 0.08;

		System.out.println("What is the cost of the item you are buying?");
		double price = keyboard.getMonetaryAmount();

		double pst = price * pstRate;
		double gst = price * gstRate;
		double total= price + gst + pst;

		System.out.println("Cost    " + moneyFormat.format(price));
		System.out.println("PST     " + moneyFormat.format(pst));
		System.out.println("GST     " + moneyFormat.format(gst));
		System.out.println("Total   " + moneyFormat.format(total));
	}
}

-----------------------------------
Andy
Tue Sep 21, 2004 8:18 pm


-----------------------------------
ha, wtd, i doubt he under stands throws and catches

-----------------------------------
wtd
Tue Sep 21, 2004 8:19 pm


-----------------------------------
I didn't introduce anything in terms of exceptions that wasn't already in his original code.

Though now I have.  :-)

I declared that a NumberFormatException might be thron by the calls to Double.parseDouble.

import java.lang.*;
import java.io.*;
import java.text.DecimalFormat;

class MoneyReader extends BufferedReader {
	public MoneyReader(Reader in) {
		super(in);
	}

	public double getMonetaryAmount() throws IOException, NumberFormatException {
		return Double.parseDouble(readLine());
	}
}

public class Cashier
{
	private static MoneyReader keyboard = new MoneyReader(new InputStreamReader(System.in));
	private static DecimalFormat moneyFormat = new DecimalFormat("$#,###,##0.00");

	public static void main (String[] args) throws IOException
	{
		double pstRate = 0.07;
		double gstRate = 0.08;

		System.out.println ("What is the cost of the item you are buying?");
		double price = keyboard.getMonetaryAmount();

		double pst = price * pstRate;
		double gst = price * gstRate;
		double total= price + gst + pst;

		System.out.println("Cost    " + moneyFormat.format(price));
		System.out.println("PST     " + moneyFormat.format(pst));
		System.out.println("GST     " + moneyFormat.format(gst));
		System.out.println("Total   " + moneyFormat.format(total));
	}
}
