Page 1 of 2 12 LastLast
Results 1 to 20 of 34
  1. #1
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Not getting correct output.

    Why is my program not calculating the total and average of the price of the items and not printing it out?

    Java Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package assignment4;
    
    /**
     *
     * @author sithr_000
     */
    import javax.swing.JOptionPane;
    
    class GetData {
        
    
        static String str;
    
        static double getDouble(String s) {
            return Double.parseDouble(getWord(s));
        }
        static char getChar(String s)
        {
            String ste = getWord(s);
            return str.charAt(0);
        }
    
        static int getInt(String s) {
            return Integer.parseInt(str);
        }
    
        static String getWord(String s) {
            return JOptionPane.showInputDialog(s);
        }
    }
    MainClass
    Java Code:
    package assignment4;
    
    /**
     *
     * @author sprat009
     */
    import javax.swing.JOptionPane;
    
    public class Cashier {
    
        String name, s;
        double price;
        double totalPrice;
        int count;
        String receipt;
        double tendered;
        double average;
        double makeChange;
    
        Cashier() {
            name = " ";
            s = " ";
            totalPrice = 0;
            count = 0;
    
        }
    
        public void add(String name, double price) {
            receipt += name + ".............\t" + price + "\r\n";
            totalPrice += price;
            count++;
            JOptionPane.showMessageDialog(null, "Product is " + name + "\tPrice $" + price,
                    "My Super Store", JOptionPane.INFORMATION_MESSAGE);
    
            System.out.println("Product is " + name + "\tPrice is: $" + price);
        }
    
        public void makeChange() {
            makeChange = totalPrice - tendered;
        }
    
        public void tendered(double amount) {
            tendered += amount;
        }
    
        public void average() {
            average = totalPrice / count;
        }
        
    }
    TestClass
    Java Code:
    package assignment4;
    
    /**
     *
     * @author sithr_000
     */
    import javax.swing.JOptionPane;
    import javax.swing.JTextArea;
    import javax.swing.JScrollPane;
    import java.util.Date;
    import java.text.DateFormat;
    import java.text.NumberFormat; 
    
    class TestCashier {
    
        public static void main(String[] arg) {
            Date d = new Date(); 
            DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
            System.out.println("Today is: " + df.format(d));
            NumberFormat nf = NumberFormat.getCurrencyInstance();
    
            Cashier c = new Cashier();
    
            String name = GetData.getWord("Enter name of item");
            double price = GetData.getDouble("Enter price of item");
            c.add(name, price);
    
            name = GetData.getWord("Enter name of item");
            price = GetData.getDouble("Enter price of item");
            c.add(name, price);
    
            name = GetData.getWord("Enter name of item");
            price = GetData.getDouble("Enter price of item");
            c.add(name, price);
    
            name = GetData.getWord("Enter name of item");
            price = GetData.getDouble("Enter price of item");
            c.add(name, price);
    
            // Now average the price of the items
            c.average();
    
    // Make payment
            double amount = GetData.getDouble("Enter amount of money for payment");
    
            c.tendered(amount); // For example twenty dollars were tendered
            c.makeChange();
    
            generateReceipt(c);
        }
    
        static void generateReceipt(Cashier c) {
            
            JTextArea text = new JTextArea(c.receipt, 6, 20);
            JScrollPane pane = new JScrollPane(text);
    
            JOptionPane.showMessageDialog(null, pane,
                    "Welcome – thanks for stopping by!!", JOptionPane.INFORMATION_MESSAGE);
            // Write the necessary code that will generate a customer’s receipt.
            // The output must be displayed in a scrollable pane
        }
    
    }
    It prints out everything but the total of the items and I dont know why. :(

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Not getting correct output.

    So where exactly do you print out or display the total price and the average?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Re: Not getting correct output.

    In the main class I have the average = totalPrice/count. in the the testclass I have c.average. <----shouldn't this print out the total? but my output is just this:
    Java Code:
    Today is: Jun 27, 2014
    Product is Bread	Price is: $2.99
    Product is Penut Butter	Price is: $3.5
    Product is Jam	Price is: $4.78
    Product is Cheese	Price is: $3.6
    BUILD SUCCESSFUL (total time: 48 seconds)
    When I do System.out.println(c.average); I just get 0.0
    Last edited by sithclone3; 06-28-2014 at 12:41 AM.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Not getting correct output.

    Where exactly are you putting that print statement and where are you printing out the totalPrice?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Re: Not getting correct output.

    I put the that print statement in the testclass right under; // Now average the price of the items. I dont have totalPrice anywhere expect in the mainclass. I dont know how to write it in the test class? since it is already defined and implemented in c.average?

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Not getting correct output.

    Quote Originally Posted by sithclone3 View Post
    I put the that print statement in the testclass right under; // Now average the price of the items. I dont have totalPrice anywhere expect in the mainclass. I dont know how to write it in the test class? since it is already defined and implemented in c.average?
    Are you placing it after you calculate the average via c.average() or before? It must be after.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Re: Not getting correct output.

    I appreciate that, I now get this, which is the average:
    Java Code:
    Today is: Jun 27, 2014
    Product is bread	Price is: $2.87
    Product is jam	Price is: $2.56
    Product is cheese	Price is: $3.46
    Product is meat	Price is: $4.89
    3.4450000000000003 <----average
    BUILD SUCCESSFUL (total time: 31 seconds)
    But I dont get a total still, should I put something like; System.out.println(get.totalPrice()); before c.average?

    UPDATE: Aight, so I added System.out.println(c.totalPrice); before I calculate the average, and i get the total.
    PART OF UPDATE:
    Java Code:
    Today is: Jun 27, 2014
    Product is ham	Price is: $3.02
    Product is cheese	Price is: $3.05
    Product is jam	Price is: $5.6
    Product is penuts	Price is: $3.98
    15.65 <----TOTAL
    3.9125<-----average
    BUILD SUCCESSFUL (total time: 35 seconds)
    I mean, the total and average do not appear on the scrollpane. Any ideas?
    Last edited by sithclone3; 06-28-2014 at 01:19 AM.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Not getting correct output.

    That won't work because 'get' doesn't exist as a class instance anywhere. The only way to access totalPrice the way you have written it is to print it out
    using c.totalPrice. That's because your totalPrice only exists in your Cashier class. You could put a method in your Cashier class to retrieve the total price (which would be a better way to do it). It is usually better to access instance fields with getters and setters than to access them directly.

    Similarly, you should probably change your average to something like this:

    Java Code:
    public double getAverage() {
           return totalPrice / count;
    }
    
    // Then later you can get the average by invoking the method...
    
    c.getAverage();
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Re: Not getting correct output.

    Quote Originally Posted by jim829 View Post
    That won't work because 'get' doesn't exist as a class instance anywhere. The only way to access totalPrice the way you have written it is to print it out
    using c.totalPrice. That's because your totalPrice only exists in your Cashier class. You could put a method in your Cashier class to retrieve the total price (which would be a better way to do it). It is usually better to access instance fields with getters and setters than to access them directly.

    Similarly, you should probably change your average to something like this:

    Java Code:
    public double getAverage() {
           return totalPrice / count;
    }
    
    // Then later you can get the average by invoking the method...
    
    c.getAverage();
    Regards,
    Jim
    Thank you, I have the average and total amount shown now, but its not being shown the scrollpane with the items. Average and Total are shown in the systemprint area. Can you help me with this?

    UPDATE: i HAVE BEEN ABLE TO GET EVERYTHING INTO A SCROLLPANE.
    Last edited by sithclone3; 06-28-2014 at 02:59 AM.

  10. #10
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Re: Not getting correct output.

    But now my issue is the fact that I get a 0.0 after every items price. Im not sure why or how to get rid of it. This only appears in the scrollpane.

    output:
    Java Code:
    nullBread.............  2.99
    0.0
    -   0.0Cheese.............  2.67
    2.99
    -   0.0Ham............. 4.68
    5.66
    -   0.0Jam............. 5.89
    10.34
    -   0.0
    P.S. I updated my MainClass to this:

    Java Code:
    Java Code:
    package assignment4;
     
    /**
     *
     * @author sprat009
     */
    import javax.swing.JOptionPane;
     
    public class Cashier {
     
        String name, s;
        double price;
        double totalPrice;
        int count;
        String receipt;
        double tendered;
        double average;
        double makeChange;
     
        Cashier() {
            name = " ";
            s = " ";
            totalPrice = 0;
            count = 0;
     
        }
     
        public void add(String name, double price) {
            receipt += name + ".............\t" + price + "\r\n" + totalPrice + "\n" + "-\t" + tendered;
            totalPrice += price;
            count++;
            JOptionPane.showMessageDialog(null, "Product is " + name + "\tPrice $" + price,
                    "My Super Store", JOptionPane.INFORMATION_MESSAGE);
     
            System.out.println("Product is " + name + "\tPrice is: $" + price);
        }
     
        public void makeChange() {
            makeChange = tendered - totalPrice ;
        }
     
        public void tendered(double amount) {
            tendered += amount;
        }
     
        public double getAverage() {
            return totalPrice / count;
        }
     
    }

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Not getting correct output.

    The problems you are having with extra output are from the following statement:

    Java Code:
    receipt += name + ".............\t" + price + "\r\n" + totalPrice + "\n" + "-\t" + tendered;
    You print a carriage return and newline followed by totalPrice followed by another line, a "-" and then tendered.

    But tendered will always be zero until the end because that is when you collect the money from the customer.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Re: Not getting correct output.

    How would I write it so I can get tendered to be what I input?

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Not getting correct output.

    Quote Originally Posted by sithclone3 View Post
    How would I write it so I can get tendered to be what I input?
    Don't concatenate those values to the receipt string until you call their respective methods.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  14. #14
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Re: Not getting correct output.

    Dont I call them though?

    Java Code:
    c.makeChange();
            System.out.print(c.makeChange);
    Java Code:
    c.getAverage();
            System.out.println(c.average);
    Java Code:
     System.out.println(c.totalPrice);
    these are found in the TestClass

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Not getting correct output.

    If that's how you want to print them, fine. Then remove the extraneous stuff from the end of the receipt string.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  16. #16
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Re: Not getting correct output.

    But if they're not in the reciept string, they dont appear in the OptionPane....Im so confused

  17. #17
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Not getting correct output.

    You need to add the values onto the receipt string in the appropriate method. For add, you are doing it correctly except that you also add totalPrice and tendered. Remove the last two.

    Now in the average() method, add the average to the receipt string. You can add the totalPrice and tendered in some other method that you call at the very end. You need to think about this. Think about the logical flow of what you are doing.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  18. #18
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Re: Not getting correct output.

    Ok Jim, I did what you said. I now get this in the JOptionPane:

    Java Code:
    nullBread.............	2.99
    Chicken.............	3.79
    Milk.............	4.99
    Jam.............	5.67
    
    __________________________
    Total Price is.....17.439999999999998
    
    The number of items being purchased........4
    The average price per item is........0.0
    
    Amount tendered is......20.0
    Your change is.....2.5600000000000023
    I am curious as to why it says null before bread, but not any other item and what would I do to get rid of that? Also, it doesnt give me the average anymore, it just says 0.0.



    This is my new TestClass:

    Java Code:
    package assignment4;
    
    /**
     *
     * @author sithr_000
     */
    import javax.swing.JOptionPane;
    import javax.swing.JTextArea;
    import javax.swing.JScrollPane;
    import java.util.Date;
    import java.text.DateFormat;
     
    
    class TestCashier {
    
        public static void main(String[] arg) {
            Date d = new Date(); 
            DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
            System.out.println("Today is: " + df.format(d));
            
    
            Cashier c = new Cashier();
    
            String name = GetData.getWord("Enter name of item");
            double price = GetData.getDouble("Enter price of item");
            c.add(name, price);
    
            name = GetData.getWord("Enter name of item");
            price = GetData.getDouble("Enter price of item");
            c.add(name, price);
    
            name = GetData.getWord("Enter name of item");
            price = GetData.getDouble("Enter price of item");
            c.add(name, price);
    
            name = GetData.getWord("Enter name of item");
            price = GetData.getDouble("Enter price of item");
            c.add(name, price);
            
            System.out.println("______________________");
            
            System.out.println(c.totalPrice);
    
            // Now average the price of the items
            c.average();
            System.out.println(c.average);
    
    // Make payment
            double amount = GetData.getDouble("Enter amount of money for payment");
    
            c.tendered(amount);
            System.out.println(c.tendered);// For example twenty dollars were tendered
            
            c.makeChange();
            System.out.print(c.makeChange);
    
            generateReceipt(c);
        }
    
        static void generateReceipt(Cashier c) {
            
            JTextArea text = new JTextArea(c.receipt + "\n" + "__________________________" + "\n" + "Total Price is....." + c.totalPrice + 
                    "\n\n" + "The number of items being purchased........" + c.count + 
                    "\n" + "The average price per item is........" + c.average +  "\n\n" + 
                    "Amount tendered is......" + c.tendered + "\n" + "Your change is....." + c.makeChange, 30, 40);
            
            JScrollPane pane = new JScrollPane(text);
    
            JOptionPane.showMessageDialog(null, pane,
                    "Welcome – thanks for stopping by!!", JOptionPane.INFORMATION_MESSAGE);
            // Write the necessary code that will generate a customer’s receipt.
            // The output must be displayed in a scrollable pane
        }
    
    }
    I didnt change my MainClass at all. I only added on to my TestClass.
    Last edited by sithclone3; 06-28-2014 at 09:56 PM.

  19. #19
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Not getting correct output.

    The null is probably because receipt was not intialized so it defaults to null. I should be initialized to "". The reason average is zero is because the field average is not used anymore. It should be c.getAverage(). You may also want to investigate String.format() to format your numbers to look more reasonable.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  20. #20
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Re: Not getting correct output.

    Java Code:
    JTextArea text = new JTextArea(c.receipt + "\n" + "__________________________" + "\n" + "Total Price is....." + c.totalPrice + 
                    "\n\n" + "The number of items being purchased........" + c.count + 
                    "\n" + "The average price per item is........" + c.getAverage +  "\n\n" + 
                    "Amount tendered is......" + c.tendered + "\n" + "Your change is....." + c.makeChange, 15, 20);
    c.getAverage is invalid. and same here
    Java Code:
    c.getAverage();
            System.out.println(c.getAverage);
    I have it defined:
    Java Code:
     public double getAverage() {
            return totalPrice / count;
        }
    
    }
    it only recognizes it when i use c.average.
    Last edited by sithclone3; 06-28-2014 at 10:48 PM.

Page 1 of 2 12 LastLast

Similar Threads

  1. How to set layout to have correct output
    By Prajin in forum AWT / Swing
    Replies: 6
    Last Post: 07-07-2010, 04:40 PM
  2. Getting correct output
    By WarmRegards in forum New To Java
    Replies: 9
    Last Post: 11-01-2009, 05:41 PM
  3. Is this the correct Output?
    By Teny in forum New To Java
    Replies: 17
    Last Post: 04-13-2009, 01:52 PM
  4. Output correct grammar
    By JordashTalon in forum New To Java
    Replies: 2
    Last Post: 03-06-2009, 01:22 AM
  5. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-26-2009, 12:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •