Dear all,
This example is compleet ,but wont load the file. Can anyone help me with puzzle?hier is the code:
Class ATMWindow
import java.io.IOException;
import javax.swing.JFrame;
public class ATMWindow {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
if (args.length == 0)
{
System.out.println("Usage: ATMSimulation propertiesFile");
System.exit(0);
}
else
{
try
{
SimpleDataSource.init(args[0]);
}
catch ( IOException | ClassNotFoundException ex)
{
System.exit(0);
}
}
JFrame frame = new ATM();
frame.setTitle("HOOGESCHOOL ROTTERDAM BANK");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(550, 700);
frame.pack();
frame.show();
}
}
Class ATM
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
final class ATM extends JFrame {
public ATM() {
theBank = new Bank();
pad = new Keypad();
display = new JTextArea(4, 20);
aButton = new JButton(" A ");
aButton.addActionListener(new AButtonListener());
bButton = new JButton(" B ");
bButton.addActionListener(new BButtonListener());
cButton = new JButton(" C ");
cButton.addActionListener(new CButtonListener());
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(3, 1));
buttonPanel.add(aButton);
buttonPanel.add(bButton);
buttonPanel.add(cButton);
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(pad);
contentPane.add(display);
contentPane.add(buttonPanel);
try
{
setNextState(START_STATE);
}
catch (SQLException exception)
{
JOptionPane.showMessageDialog(null,
exception.getMessage());
}
}
public void setCustomerNumber() throws SQLException
{
customerNumber = (int)pad.getValue();
setNextState(PIN_STATE);
}
public void selectCustomer() throws SQLException
{
int pin = (int)pad.getValue();
currentCustomer = theBank.findCustomer(customerNumber, pin);
if (currentCustomer == null)
setNextState(START_STATE);
else
setNextState(ACCOUNT_STATE);
}
public void selectAccount(int account) throws SQLException
{
if (account == CHECKING_ACCOUNT)
currentAccount
= currentCustomer.getCheckingAccount();
else
currentAccount
= currentCustomer.getSavingsAccount();
setNextState(TRANSACT_STATE);
}
public void withdraw() throws SQLException
{
currentAccount.withdraw(pad.getValue());
setNextState(ACCOUNT_STATE);
}
public void deposit() throws SQLException
{
currentAccount.deposit(pad.getValue());
setNextState(ACCOUNT_STATE);
}
public void setNextState(int newState)
throws SQLException
{
state = newState;
pad.clear();
if (state == START_STATE)
display.setText("Enter customer number\nA = OK");
else if (state == PIN_STATE)
display.setText("Enter PIN\nA = OK");
else if (state == ACCOUNT_STATE)
display.setText("Select Account\n"
+ "A = Checking\nB = Savings\nC = Exit");
else if (state == TRANSACT_STATE)
display.setText("Balance = "
+ currentAccount.getBalance()
+ "\nEnter amount and select transaction\n"
+ "A = Withdraw\nB = Deposit\nC = Cancel");
}
private class AButtonListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent event)
{
try
{
if (state == START_STATE)
setCustomerNumber();
else if (state == PIN_STATE)
selectCustomer();
else if (state == ACCOUNT_STATE)
selectAccount(CHECKING_ACCOUNT);
else if (state == TRANSACT_STATE)
withdraw();
}
catch (SQLException exception)
{
JOptionPane.showMessageDialog(null,
exception.getMessage());
}
}
}
private class BButtonListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent event)
{
try
{
if (state == ACCOUNT_STATE)
selectAccount(SAVINGS_ACCOUNT);
else if (state == TRANSACT_STATE)
deposit();
}
catch (SQLException exception)
{
JOptionPane.showMessageDialog(null,
exception.getMessage());
}
}
}
private class CButtonListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent event)
{
try
{
if (state == ACCOUNT_STATE)
setNextState(START_STATE);
else if (state == TRANSACT_STATE)
setNextState(ACCOUNT_STATE);
}
catch (SQLException exception)
{
JOptionPane.showMessageDialog(null,
exception.getMessage());
}
}
}
private int state;
private int customerNumber;
private Bank theBank;
private Customer currentCustomer;
private BankAccount currentAccount;
private JButton aButton;
private JButton bButton;
private JButton cButton;
private Keypad pad;
private JTextArea display;
private static final int START_STATE = 1;
private static final int PIN_STATE = 2;
private static final int ACCOUNT_STATE = 3;
private static final int TRANSACT_STATE = 4;
private static final int CHECKING_ACCOUNT = 1;
private static final int SAVINGS_ACCOUNT = 2;
}
Class Bank
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Bank
{
public Customer findCustomer(int customerNumber, int pin)
throws SQLException
{
Customer c = null;
try (Connection conn = SimpleDataSource.getConnection();
Statement stat = conn.createStatement();
ResultSet result = stat.executeQuery("SELECT *"
+ " FROM Customer WHERE Customer_Number = "
+ customerNumber)) {
if (result.next() && pin == result.getInt("PIN"))
c = new Customer(customerNumber,
result.getInt("Checking_Account_Number"),
result.getInt("Savings_Account_Number"));
}
return c;
}
}
Class BankAccount
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class BankAccount
{
private int accountNumber;
/**
Constructs a bank account with a given balance.
@param anAccountNumber the account number
*/
public BankAccount(int anAccountNumber)
{
accountNumber = anAccountNumber;
}
/**
Deposits money into a bank account.
@param amount the amount to deposit
*/
public void deposit(double amount)
throws SQLException
{
try (Connection conn = SimpleDataSource.getConnection();
Statement stat = conn.createStatement()) {
stat.execute("UPDATE Account"
+ " SET Balance = Balance + " + amount
+ " WHERE Account_Number = " + accountNumber);
}
}
/**
Withdraws money from a bank account.
@param amount the amount to withdraw
*/
public void withdraw(double amount)
throws SQLException
{
try (Connection conn = SimpleDataSource.getConnection();
Statement stat = conn.createStatement()) {
stat.execute("UPDATE Account"
+ " SET Balance = Balance + " + amount
+ " WHERE Account_Number = " + accountNumber);
}
}
/**
Gets the balance of a bank account.
@return the account balance
*/
public double getBalance()
throws SQLException
{
double balance = 0;
try (Connection conn = SimpleDataSource.getConnection();
Statement stat = conn.createStatement();
ResultSet result = stat.executeQuery("SELECT Balance"
+ " FROM Account WHERE Account_Number = "
+ accountNumber)) {
if (result.next())
balance = result.getDouble(1);
}
return balance;
}
}
Class Customer
public class Customer
{
public Customer(int aCustomerNumber,
int checkingAccountNumber, int savingsAccountNumber)
{
customerNumber = aCustomerNumber;
checkingAccount = new BankAccount(checkingAccountNumber);
savingsAccount = new BankAccount(savingsAccountNumber);
}
/**
Gets the checking account of this customer.
@return the checking account
*/
public BankAccount getCheckingAccount()
{
return checkingAccount;
}
/**
Gets the savings account of this customer.
@return the savings account
*/
public BankAccount getSavingsAccount()
{
return savingsAccount;
}
private int customerNumber;
private BankAccount checkingAccount;
private BankAccount savingsAccount;
}
Class Keypad
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Keypad extends JPanel
{
private JPanel buttonPanel;
private JButton clearButton;
private JTextField display;
/**
Constructs the keypad panel.
*/
public Keypad()
{
setLayout(new BorderLayout());
// Add display field
display = new JTextField();
add(display, "North");
// Make button panel
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 3));
// Add digit buttons
addButton("7");
addButton("8");
addButton("9");
addButton("4");
addButton("5");
addButton("6");
addButton("1");
addButton("2");
addButton("3");
addButton("0");
addButton(".");
// Add clear entry button
clearButton = new JButton("CE");
buttonPanel.add(clearButton);
class ClearButtonListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent event)
{
display.setText("");
}
}
ActionListener listener = new ClearButtonListener();
clearButton.addActionListener(
new ClearButtonListener());
add(buttonPanel, "Center");
}
/**
Adds a button to the button panel.
@param label the button label
*/
private void addButton(final String label)
{
class DigitButtonListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent event)
{
// Don't add two decimal points
if (label.equals(".")
&& display.getText().indexOf(".") != -1)
{
return;
}
// Append label text to button
display.setText(display.getText() + label);
}
}
JButton button = new JButton(label);
buttonPanel.add(button);
ActionListener listener = new DigitButtonListener();
button.addActionListener(listener);
}
/**
Gets the value that the user entered.
@return the value in the text field of the keypad
*/
public double getValue()
{
return Double.parseDouble(display.getText());
}
/**
Clears the dislay.
*/
public void clear()
{
display.setText("");
}
}
Class SimpleDataSource
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class SimpleDataSource
{
private static String url;
private static String username;
private static String password;
/**
Initializes the data source.
@param fileName the name of the property file that
contains the database driver, URL, username, and password
*/
public static void init(String fileName)
throws IOException, ClassNotFoundException
{
Properties props = new Properties();
FileInputStream in = new FileInputStream(fileName);
props.load(in);
String driver = props.getProperty("jdbc.driver");
url = props.getProperty("jdbc.url");
username = props.getProperty("jdbc.username");
if (username == null) { username = ""; }
password = props.getProperty("jdbc.password");
if (password == null) { password = ""; }
if (driver != null) { Class.forName(driver); }
}
/**
Gets a connection to the database.
@return the database connection
*/
public static Connection getConnection() throws SQLException
{
return DriverManager.getConnection(url, username, password);
}
}