Hi guys, my problem is that I want to add a way for whatever information which is displayed on the GUI to be saved onto a file.txt somewhere. For example, I want the contents for the number of "tries" a person has undertaken, the number of winnings, the number of deposits into their account, the number of withdrawals, and the amount they withdraw/ deposit, their net balance, etc. Here is my code:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class RaffleGUI extends JFrame implements ActionListener {
private JButton generate , dep, with;
private JTextField n1, n2, n3, a1, a2, a3, cash, depo, withd, dispBalance;
private static JTextField triesDisplay;
private JPanel keypad, display;
private JLabel empty, cashDisplay, nu1, nu2, nu3, au1, au2, au3, balan, triesLabel;
private GridLayout a, acc;
int num1, num2, num3, rand1, rand2, rand3, balance = 10, withdraw, deposit, tries = 0;
public void updateBalance() {
balance = balance - 5;
}
public void deposit() {
deposit = Integer.parseInt(depo.getText());
balance = balance + deposit;
String strBal;
strBal = Integer.toString(balance);
dispBalance.setText(strBal);
}
public void withdraw() {
deposit = Integer.parseInt(withd.getText());
if((balance > 0) && (balance >= deposit)) {
balance = balance - deposit;
String strBal;
strBal = Integer.toString(balance);
dispBalance.setText(strBal);
}
else
JOptionPane.showMessageDialog(null, "You have insufficient funds to withdraw that amount.", "Error!", JOptionPane.ERROR_MESSAGE);
}
public void getBalance() {
int winnings = 0;
winnings = winnings + Integer.parseInt(cash.getText());
balance = balance + winnings;
}
public void generateNumbers() {
String strBal;
int bal = balance;
strBal = Integer.toString(bal);
dispBalance.setText(strBal);
if (balance > 0) {
String ans1, ans2, ans3;
num1 = Integer.parseInt(n1.getText());
num2 = Integer.parseInt(n2.getText());
num3 = Integer.parseInt(n3.getText());
if (num1 < 0 || num1 > 40)
JOptionPane.showMessageDialog(null, "Enter a number between 0 & 40.", "Error!", JOptionPane.ERROR_MESSAGE);
if (num2 < 0 || num2 > 40)
JOptionPane.showMessageDialog(null, "Enter a number between 0 & 40.", "Error!", JOptionPane.ERROR_MESSAGE);
if (num3 < 0 || num3 > 10)
JOptionPane.showMessageDialog(null, "Enter a number between 0 & 10.", "Error!", JOptionPane.ERROR_MESSAGE);
rand1 = (int) (Math.random() * 40);
rand2 = (int) (Math.random() * 40);
rand3 = (int) (Math.random() * 10);
int randx = rand1;
int randy = rand2;
int randz = rand3;
ans1 = Integer.toString(rand1);
a1.setText(ans1);
ans2 = Integer.toString(rand2);
a2.setText(ans2);
ans3 = Integer.toString(rand3);
a3.setText(ans3);
if ((num1 != randx) && (num2 != randy) && (num3 != randz)) // 0 0 0
cash.setText("0");
if ((num1 != randx) && (num2 != randy) && (num3 == randz)) // 0 0 1
cash.setText("50");
if ((num1 != randx) && (num2 == randy) && (num3 == randz)) // 0 1 1
cash.setText("500");
if ((num1 == randx) && (num2 == randy) && (num3 == randz)) // 1 1 1
cash.setText("50000");
if ((num1 != randx) && (num2 == randy) && (num3 != randz)) // 0 1 0
cash.setText("50");;
if ((num1 == randx) && (num2 != randy) && (num3 != randz)) // 1 0 0
cash.setText("50");
if ((num1 == randx) && (num2 != randy) && (num3 == randz)) // 1 0 1
cash.setText("500");
if ((num1 == randx) && (num2 == randy) && (num3 != randz)) // 1 1 0
cash.setText("500");
// updates balance
updateBalance();
getBalance();
}
else{
JOptionPane.showMessageDialog(null, "Insufficent funds!", "Error!", JOptionPane.ERROR_MESSAGE);
}
}
// event handler
public void actionPerformed(ActionEvent e){
if (e.getSource().equals(generate)) {
generateNumbers();
tries++;
String strTries;
strTries = Integer.toString(tries);
triesDisplay.setText(strTries);
}
else if(e.getSource().equals(dep)) {
deposit();
}
else if(e.getSource().equals(with)) {
withdraw();
}
}
public RaffleGUI() {
// sets title
super("Raffle 1.0");
// pressing "x" will close the gui
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// sets the layout of the gui
a = new GridLayout(15,5);
acc = new GridLayout(5, 3);
keypad = new JPanel(a);
display = new JPanel(acc);
empty = new JLabel("");
nu1 = new JLabel("1:");
nu2 = new JLabel("2:");
nu3 = new JLabel("3:");
au1 = new JLabel("1:");
au2 = new JLabel("2:");
au3 = new JLabel("3:");
triesLabel = new JLabel("Tries:");
balan = new JLabel("Balance:");
cashDisplay = new JLabel("Won:");
depo = new JTextField(10);
withd = new JTextField(10);
dispBalance = new JTextField(10);
n1 = new JTextField(10);
n2 = new JTextField(10);
n3 = new JTextField(10);
a1 = new JTextField(10);
a2 = new JTextField(10);
a3 = new JTextField(10);
cash = new JTextField(10);
triesDisplay = new JTextField(10);
generate = new JButton("Generate");
dep = new JButton("Deposit");
with = new JButton("Withdraw");
keypad.add(nu1);
keypad.add(n1);
keypad.add(nu2);
keypad.add(n2);
keypad.add(nu3);
keypad.add(n3);
keypad.add(generate);
keypad.add(au1);
keypad.add(a1);
keypad.add(au2);
keypad.add(a2);
keypad.add(au3);
keypad.add(a3);
keypad.add(cashDisplay);
keypad.add(cash);
display.add(balan);
display.add(dispBalance);
display.add(depo);
display.add(dep);
display.add(withd);
display.add(with);
display.add(triesLabel);
display.add(triesDisplay);
this.add(display, BorderLayout.NORTH);
this.add(keypad, BorderLayout.SOUTH);
this.pack();
generate.addActionListener(this);
dep.addActionListener(this);
with.addActionListener(this);
}
public static void main(String[] args) {
new RaffleGUI().setVisible(true);
try {
FileWriter outFile = new FileWriter("MyText.txt");
PrintWriter out = new PrintWriter(outFile);
out.println("Number of tries: " +triesDisplay.getText());
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}