JOptionPane, JTextArea and JScrollPane displaying output

This is the HW assignment given to the class and I believe everything is working correctly for me, but I am having trouble displaying all the information I need in the output. If you look TestCashier.java

, you can see my intended output. I need to know how to make a display generateReceipt()

, average()

, makeChange()

and tendered()

on the panel. Any help whatsoever would be appreciated. I've been stuck on this part for quite some time.

If anyone wants to take the extra step of helping me out on mine add()

and figuring out why I can't get it to work when I ask the user for each item's price. I need the total to work with help add()

, but I can only get my program to work correctly when I manually add them together and assign that value totalSum

. Thank you for taking the time to read this and all feedback / feedback is appreciated. (Even criticism ... I'm here to learn)

Cashier.java

package cashier;
import java.text.NumberFormat;

public class Cashier {
    static int numItems;
    static double totalSum,averagePrice,price;
    private int   pennies,nickles, dimes, quarters, dollars;
    private double tendered,change;

    public void Cashier(){
        this.numItems = 0;
        this.totalSum = 0;
    }

    public void average(){
       averagePrice = totalSum/numItems; 
       NumberFormat nf1 = NumberFormat.getCurrencyInstance();
       System.out.println("The average price per item is "+ nf1.format(averagePrice));//Must format

    }
      public void add(String name, Double price) {
        numItems++;
        //totalSum =+price;
    }


    public void tendered(double t) {
       this.tendered = t;
       this.change = tendered - totalSum;
       NumberFormat df1 = NumberFormat.getCurrencyInstance();
       System.out.println("Amount tendered is " + df1.format(tendered));
   }

    void makeChange(Cashier c){

        change = (tendered-totalSum);
        change =change*100;
        NumberFormat df = NumberFormat.getCurrencyInstance();
        System.out.println("The change is: " + df.format(change/100)+"\n");//Must format


        dollars = (int)(change/100);
        change %= 100;


        quarters = (int) (change/25);
        change %=25;


        dimes = (int) (change/10);
        change = change%10;


        nickles = (int) (change/5);
        change = change%5;

        pennies = (int)change;

        System.out.println("The change includes...");
        System.out.println(dollars+" dollars");
        System.out.println(quarters+" quarters");
        System.out.println(dimes+" dimes");
        System.out.println(nickles+" nickles");
        System.out.println(pennies+" pennies");

    }






}

      

GetData.java

package cashier;


import javax.swing.JOptionPane;
import java.text.NumberFormat;


public class GetData {

    static double getDouble(String c){
        return Double.parseDouble(getWord(c));
    }

    static String getWord(String c){
        return JOptionPane.showInputDialog(c);
    }


}

      

TestCashier.java

package cashier;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;



import static cashier.Cashier.totalSum;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.Date;

public class TestCashier{

    public static void main(String[]arg){
        NumberFormat nf = NumberFormat.getCurrencyInstance();
        Cashier c = new Cashier();

        String name = GetData.getWord(" Enter name of first item");
        double price = GetData.getDouble("Enter price of item");
        c.add(name,price);


        String name2 = GetData.getWord(" Enter name of second item");
        double price2 = GetData.getDouble("Enter price of item");
        c.add(name2,price2);

        String name3 = GetData.getWord(" Enter name of third item");
        Double price3 = GetData.getDouble("Enter price of item");
        c.add(name3,price3);

        String name4 = GetData.getWord(" Enter name of fourth item");
        Double price4 = GetData.getDouble("Enter price of item");
        c.add(name4,price4);

        totalSum = price+price2+price3+price4;



        //make payment 
        double tendered = GetData.getDouble("Enter amount of money for payment");



        generateReceipt(c);
        NumberFormat nf1 = NumberFormat.getCurrencyInstance();
        String s = (name+"\t\t"+nf1.format(price));
        s = s +("\n"+name2+"\t\t"+nf1.format(price2));
        s = s +("\n"+name3+"\t\t"+nf1.format(price3));
        s = s +("\n"+name4+"\t\t"+nf1.format(price4));
        s = s +("\n"+"_______________________________");
        s = s +("\n"+"Total:\t\t"+nf1.format(totalSum));


        s = s +("\n\n"+"The number of items purchased is "+Cashier.numItems+" item(s)");


        c.tendered(tendered);
        c.makeChange(c);

        JTextArea text = new JTextArea(s,30,30);
        JScrollPane pane = new JScrollPane(text);
        JOptionPane.showMessageDialog(null, pane,"THE RIP-OFF STORE",JOptionPane.PLAIN_MESSAGE);



    }
    static void generateReceipt(Cashier c){

       Date current = new Date();
       DateFormat df = DateFormat.getDateInstance(DateFormat.FULL);
       System.out.println("WELCOME TO THE RIP-OFF STORE!");
       System.out.println("Home of the 'not so good' deals...");
       System.out.println("Thank you for stopping by on "+ df.format(current));
       System.out.println("");


    }
}

      

+3


source to share


1 answer


You need to change your functions to concatenate the string and return the string. In your function generateReceipt()

edit like so:

static String generateReceipt(Cashier c){
    String s = "";
    Date current = new Date();
    DateFormat df = DateFormat.getDateInstance(DateFormat.FULL);
    s += ("WELCOME TO THE RIP-OFF STORE!\n");
    s += ("Home of the 'not so good' deals...\n");
    s += ("Thank you for stopping by on "+ df.format(current)) + "\n\n";
    return s;
}

      

and get information from this function, just call it like this:

String s = generateReceipt(c);

The same goes for all the functions you need to output from a class Cashier.java

. I will not show you every function, as they all share the same idea. Remember that it will be named like this:

s += c.average();

Here are all your string concatenations from TestCashier and how I have it:



    String s = generateReceipt(c);
    NumberFormat nf1 = NumberFormat.getCurrencyInstance();
    s += (name+"\t\t"+nf1.format(price));
    s = s +("\n"+name2+"\t\t"+nf1.format(price2));
    s = s +("\n"+name3+"\t\t"+nf1.format(price3));
    s = s +("\n"+name4+"\t\t"+nf1.format(price4));
    s = s +("\n"+"_______________________________");
    s = s +("\n"+"Total:\t\t"+nf1.format(totalSum));


    s = s +("\n\n"+"The number of items purchased is "+Cashier.numItems+" item(s)");

    s += c.average();
    s += c.tendered(tendered);
    s += c.makeChange(c);

    JTextArea text = new JTextArea(s,30,30);
    JScrollPane pane = new JScrollPane(text);
    JOptionPane.showMessageDialog(null, pane,"THE RIP-OFF STORE",JOptionPane.PLAIN_MESSAGE);

      

I haven't looked at your add () function, but I would be happy. Talk to me if you need help.

** Here is an imgur link of what the output looks like on my IDE (IntelliJ Idea): http://imgur.com/gkG01Oc

EDIT:

You almost had the right feature added. In Cashier.java

your add()

it has been the expression:

totalSum =+ price;

just switch operators totalSum += price;

Create a getter like getTotalSum()

or something along those lines because you always want to use getters and setters instead of directly accessing class variables (at least in most cases). Name it TestCashier.java

as totalSum = c.getTotalSum();

and it should work correctly. Notice how you have it name

as a parameter to your function add()

. Why is this? If you don't need it or use it, drop it.

0


source







All Articles