Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Cashier Program Help
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Azzy




PostPosted: Tue Sep 21, 2004 3:56 pm   Post subject: 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.

code:
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);
        }
        
 }
Sponsor
Sponsor
Sponsor
sponsor
Andy




PostPosted: Tue Sep 21, 2004 4:26 pm   Post subject: (No subject)

just a thought... u didnt calculate the pst/gst/total price
wtd




PostPosted: Tue Sep 21, 2004 8:14 pm   Post subject: (No subject)

code:
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




PostPosted: Tue Sep 21, 2004 8:18 pm   Post subject: (No subject)

ha, wtd, i doubt he under stands throws and catches
wtd




PostPosted: Tue Sep 21, 2004 8:19 pm   Post subject: (No subject)

I didn't introduce anything in terms of exceptions that wasn't already in his original code.

Though now I have. Smile

I declared that a NumberFormatException might be thron by the calls to Double.parseDouble.

code:
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));
        }
}
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: