1

I've just finished reading 'Java for Dummies' and have begun to create a simple POS program. I have been having trouble getting the program to do what I want it to do! I have two actionListeners linked to each of the following buttons, 'Amigos' and 'Fosters'. I also have two text fields, one showing the price of the individual drink and then other used for a sub-total. I did have the sub-total working to add up multiples of the same drink, but not an 'Amigos' with a 'Fosters'. This was programmed by trying to share a sub-total variable. I tried to work around my amateur java programming by reading and writing to a single text file but that is proving difficult for me as well! Below is my code trying to implement my read and write work around.

This is my first ever Java program so please forgive the format, punctuation and Java conventions I have got wrong. Also forgive my lack of comments. Any advice is greatly appreciated!

Regards

Louis

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.NumberFormat;
import java.io.FileNotFoundException;

class till_v2 {

    public static void main(String args[]) {

        JFrame frame;
        Container content_pane;
        JTextField textField, subTotal;
        JButton b1Amigos, b2Fosters;
        FlowLayout layout;

        frame = new JFrame();
        frame.setTitle("Louis' Till");

        content_pane = frame.getContentPane();

        textField = new JTextField("Price displayed here.",15);
        subTotal = new JTextField("Sub-Total.", 5);

        b1Amigos = new JButton("Amigos");
        b1Amigos.addActionListener(new AmigosAL(textField));
        b1Amigos.addActionListener(new subTotalAmigosUD(subTotal));

        b2Fosters = new JButton("Fosters");
        b2Fosters.addActionListener(new FostersAL(textField));
        b2Fosters.addActionListener(new subTotalFostersUD(subTotal));

        content_pane.add(textField);
        content_pane.add(subTotal);
        content_pane.add(b1Amigos);
        content_pane.add(b2Fosters);
        layout = new FlowLayout();
        content_pane.setLayout(layout);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);

    }
}

class subTotalFostersUD implements ActionListener {

    JTextField subTotal;
    int itemPrice;
    double sub_total;
    SUBTOTAL SUBTOTALobject = new SUBTOTAL();

    subTotalFostersUD(JTextField subTotal) {
        this.subTotal = subTotal;
    }
    //The problem could be here!
    public void actionPerformed(ActionEvent e) {
        try {
            itemPrice = 320;
            sub_total = SUBTOTALobject.SUBTOTAL(itemPrice);
            subTotal.setText("£"+sub_total);
        }

        catch (FileNotFoundException err) {
            System.out.println("1!");
        }
    }
}

class subTotalAmigosUD implements ActionListener {

    JTextField subTotal;
    int itemPrice;
    double sub_total;
    SUBTOTAL SUBTOTALobject = new SUBTOTAL();

    subTotalAmigosUD(JTextField subTotal) {
        this.subTotal = subTotal;
    }
    //Same problem as above!
    public void actionPerformed(ActionEvent e) {
        try {
            itemPrice = 330;
            sub_total = SUBTOTALobject.SUBTOTAL(itemPrice);
            subTotal.setText("£"+sub_total);
        }

        catch (FileNotFoundException err) {
            System.out.println("2!");
        }
    }
}

class AmigosAL implements ActionListener {

    JTextField textField;

    AmigosAL(JTextField textField) {
        this.textField = textField;
    }

    public void actionPerformed(ActionEvent e) {
        textField.setText("£3.30");
    }
}


class FostersAL implements ActionListener {

    JTextField textField;

    FostersAL(JTextField textField) {
        this.textField = textField;
    }

    public void actionPerformed(ActionEvent e) {
        textField.setText("£3.20");
    }
}

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.io.*;

//The problem could be here!
public class SUBTOTAL {
    BufferedReader in;
    BufferedWriter out;
    int pennies;
    int itemPrice;
    public double sub_total;        

    public double SUBTOTAL(int itemPrice) throws FileNotFoundException {
        try {
            in = new BufferedReader(new FileReader("sub_total.txt"));
            pennies = Integer.parseInt(in.readLine());
            pennies = pennies + itemPrice;
            in.close();
        }

        catch(IOException e) {
            System.out.println("3!");
        }

        try {
            out = new BufferedWriter(new FileWriter("sub_total.txt"));
            out.write(pennies);
            out.close();
        }

        catch(IOException e){
            System.out.println("4!");
        }

        sub_total = pennies;
        sub_total = sub_total / 100;
        return sub_total;   
    }
}
Nikita Rybak
  • 67,365
  • 22
  • 157
  • 181
LtBL
  • 13
  • 1
  • 2
  • For naming Conventions refer http://geosoft.no/development/javastyle.html#Naming%20Conventions – Alpine Feb 20 '11 at 18:25
  • I'd personally recommend the extremely well written Sun (umn.. Oracle?) coding conventions for java: http://www.oracle.com/technetwork/java/codeconvtoc-136057.html. Those cover the important topics quite well and understandably and are pretty much accepted by the community in my opinion (Google also uses it as far as I'm aware instead of creating their own which speaks for itself) – Voo Feb 20 '11 at 18:42

2 Answers2

3

you are getting this error because you are missing file sub_total.txt

create this file with desired content. In the same folder where you are having .class file.

Vivek Goel
  • 22,942
  • 29
  • 114
  • 186
  • I would also add that OP must write 0 in that file when he creates, otherwise in.readLine() will return null which will be fed into parseInt resulting in an exception. – Anas Elghafari Feb 20 '11 at 17:41
  • I do have the sub_total.txt created in the right place with the content I want in it. Could there possibly be another reason why the program is not working? I really appreciate the quick reply! – LtBL Feb 20 '11 at 18:10
  • You're reading the file from a relative path, so it depends on how you execute your program. If you're using Eclipse it should be in the projects base path. I.e. in the same folder where your src and bin folders are. – Voo Feb 20 '11 at 18:39
  • I'm compiling and executing the program from JCreator. I'll have a look at how that finds files, do you know how JCreator treats this situation off the top of your head at all? – LtBL Feb 21 '11 at 12:58
1

As Vivek said it should solve your problem.

But then you will get NumberFormatException

out.write(pennies); writes to file in int data type specifying a character to be written.

But subtotal can be more than one character,(subtotal increases as you increase the no of items).

When the subtotal is more than a character it writes junk values in the sub_total.txt textfile

Integer.parseInt(in.readLine()); tries to read the data as String and parse it into a int which

results in NumberFormatException


The Solution:

Write the data to file as String

PrintWriter txt = new PrintWriter(out);
txt.print(pennies);
txt.close();

instead of out.write(pennies); and read the data as

pennies = Integer.parseInt(in.readLine());


And remember before you run your program, don't forget to store an integer value in sub_total.txt

Alpine
  • 3,838
  • 1
  • 25
  • 18
  • Thanks, I will modify my code with what you suggested. I will also make sure that 0 is written in the sub_total.txt file. May I ask how my OOP understanding is? Are you able to get an idea from that program alone? – LtBL Feb 20 '11 at 18:36
  • I am able to get an idea from that program. But good naming conventions will improve readability and thereby the understanding and the maintainability and general quality of the code – Alpine Feb 20 '11 at 18:52
  • Is there a good tutorial for such things? No worries found you earlier post :) – LtBL Feb 20 '11 at 20:34
  • You can refer the comments below the question pointing to some guides on naming conventions – Alpine Feb 20 '11 at 20:40