Initial Commit

This commit is contained in:
2012-01-22 16:14:32 -05:00
commit f5e306f67f
155 changed files with 5097 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="output" path="bin"/>
</classpath>

17
Java/BankAccount/.project Normal file
View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>BankAccount</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@@ -0,0 +1,12 @@
#Sun Nov 21 20:19:35 EST 2010
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.6

View File

@@ -0,0 +1,123 @@
/**
* @author Twenty Codes, LLC
* @author ricky barrette
* @date Nov 21, 2010
*/
package com.TwentyCodes.java.BankAccount;
/**
* This is an account class
* @author ricky barrette
*/
public class Account implements Comparable<Account>{
private double mBalance;
private String mOwner;
private long mAccountNumber;
/**
* Creates a new Account
* @param balance starting balance
* @param owner
* @param accountNumber
* @author ricky barrette
*/
public Account(double balance, String owner, long accountNumber) {
mBalance = balance;
mOwner = owner;
mAccountNumber = accountNumber;
}
/**
* withdraws money from the account
* @param amount to withdraw
* @return new balance
* @author ricky barrette
*/
public double withdraw(double amount){
mBalance = mBalance - amount;
return mBalance;
}
/**
* deposits money into the account
* @param amount to deposit
* @return new balance
* @author ricky barrette
*/
public double deposit(double amount){
mBalance = mBalance + amount;
return amount;
}
/**
* getter method for the account balance
* @return current balance
* @author ricky barrette
*/
public double getBalance(){
return mBalance;
}
/**
* getter method for the owner name
* @return owners name
* @author ricky barrette
*/
public String getOwner(){
return mOwner;
}
/**
* returns a human readable string
* (non-Javadoc)
* @see java.lang.Object#toString()
* @return human readable string
* @author ricky barrette
*/
@Override
public String toString(){
return "Owner: "+ mOwner +"; Acount: "+ mAccountNumber +"; Banace: "+ mBalance;
}
/**
* changes the owners name
* @param name
* @author ricky barrette
*/
public void changeOwnerName(String name){
mOwner = name;
}
/**
* charges a $10 to the account
* @return new balance
* @author ricky barrette
*/
public double chargeFee(){
mBalance = mBalance - 10;
return mBalance;
}
/**
* compares accounts by owners names
* (non-Javadoc)
* @see java.lang.Comparable#compareTo(java.lang.Object)
* @author ricky barrette
*/
@Override
public int compareTo(Account account) {
return this.mOwner.compareTo(account.getOwner());
}
/**
* gets the account number
* @return account number
* @author ricky barrette
*/
public long getAccount() {
return mAccountNumber;
}
}

View File

@@ -0,0 +1,215 @@
/**
* @author Twenty Codess, LLC
* @author ricky barrette
* @data Dec 18, 2010
*/
package com.TwentyCodes.java.BankAccount;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
/**
* This class will be a account database
*/
public class AccountDB {
private ArrayList<Account> array;
/**
* Creates a new AccountDB
*/
public AccountDB() {
array = new ArrayList<Account>(0);
}
/**
* returns a human readable string (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
for (Account item : array)
sb.append(item.toString() + "\n");
return sb.toString();
}
/**
* saves the database to a text file
* @param file to be saved to
* @author ricky barrette
* @throws IOException
*/
public void save(File file) throws IOException{
StringBuffer sb = new StringBuffer();
for(Account item : array)
sb.append(item.getBalance()+","+item.getOwner()+","+item.getAccount()+"\n");
FileOutputStream theFile = new FileOutputStream(file);
theFile.write(sb.toString().getBytes());
theFile.flush();
theFile.close();
}
public void load(File file) throws FileNotFoundException{
Scanner scan = new Scanner(file);
String line = null;
String [] lineParts;
do{
line = scan.nextLine();
lineParts = line.split(",");
try {
addAccount(new Account(Double.parseDouble(lineParts[0]), lineParts[1], Long.parseLong(lineParts[2])));
} catch (AccountExistsException e) {
e.printStackTrace();
}
}while(scan.hasNextLine());
}
/**
* added the account to the database
*
* @param account
* @throws AccountExistsException
*/
public void addAccount(Account account) throws AccountExistsException {
boolean isExisting = getAccount(account.getAccount()) != null ? true : false;
if(! isExisting){
array.add(account);
Collections.sort(array);
} else
throw new AccountExistsException("Account "+ account.getAccount()+" Already Exists");
}
/**
* finds the all occurrences of an account with the owner
*
* @param owner
* @return a list containing all the accounts with the provided owner
*/
public ArrayList<Account> findAccount(String owner) {
ArrayList<Account> temp = new ArrayList<Account>(0);
for (Account item : array)
if (item.getOwner().equals(owner))
temp.add(item);
return temp;
}
/**
*
* @param search
* @return arraylist containing all the
* @author ricky barrette
*/
public ArrayList<Account> searchForAccount(String search) {
long account = -1;
try {
account = Long.parseLong(search);
} catch (Exception e) {
// TO NOTHING
}
ArrayList<Account> temp = new ArrayList<Account>(0);
for (Account item : array) {
if (item.getOwner().contains(search))
temp.add(item);
if (account > 0 && item.getAccount() == account)
temp.add(item);
}
return temp;
}
/**
* removes the all occurrences of an account with the owner
*
* @param owner
*/
public void deleteAccounts(String owner) {
long account = -1;
try {
account = Long.parseLong(owner);
} catch (Exception e) {
// TO NOTHING
}
Object[] temp = array.toArray();
for (int index = 0; index < temp.length; index++) {
if (((Account) temp[index]).getOwner().equals(owner))
temp[index] = null;
if (account > 0 && ((Account) temp[index]).getAccount() == account)
temp[index] = null;
}
array.removeAll(array);
for (Object item : temp)
if (item != null)
array.add((Account) item);
}
/**
* returns the specified account
*
* @param accountNumber
* @return account or null if account doesnt exist
* @author ricky barrette
*/
public Account getAccount(long accountNumber) {
for (Account item : array)
if (item.getAccount() == accountNumber)
return item;
return null;
}
/**
* deposits money into the specified account
* @param accountNumber
* @param amount
* @return true if successful
* @author ricky barrette
*/
public boolean deposit(long accountNumber, double amount) {
for (Account item : array)
if (item.getAccount() == accountNumber) {
item.deposit(amount);
return true;
}
return false;
}
/**
* charges a fee to the specified account
* @param accountNumber
* @return true if successful
* @author ricky barrette
*/
public boolean chargeFee(long accountNumber) {
for (Account item : array)
if (item.getAccount() == accountNumber) {
item.chargeFee();
return true;
}
return false;
}
/**
* withdraws money from the specified account
* @param accountNumber
* @param amount
* @return true if successful
* @author ricky barrette
*/
public boolean withdraw(long accountNumber, double amount) {
for (Account item : array)
if (item.getAccount() == accountNumber) {
item.withdraw(amount);
return true;
}
return false;
}
}

View File

@@ -0,0 +1,56 @@
/**
* @author Twenty Codes, LLC
* @author ricky barrette
* @date Dec 24, 2010
*/
package com.TwentyCodes.java.BankAccount;
/**
*
* @author ricky barrette
*/
public class AccountExistsException extends Exception {
/**
* @author ricky barrette
*/
private static final long serialVersionUID = 4605163237489852355L;
/**
* Creates a new AccountExistsException
* @author ricky barrette
*/
public AccountExistsException() {
super();
}
/**
* Creates a new AccountExistsException
* @param arg0
* @author ricky barrette
*/
public AccountExistsException(String arg0) {
super(arg0);
}
/**
* Creates a new AccountExistsException
* @param arg0
* @author ricky barrette
*/
public AccountExistsException(Throwable arg0) {
super(arg0);
}
/**
* Creates a new AccountExistsException
* @param arg0
* @param arg1
* @author ricky barrette
*/
public AccountExistsException(String arg0, Throwable arg1) {
super(arg0, arg1);
}
}

View File

@@ -0,0 +1,17 @@
/**
* @author Twenty Codes, LLC
* @author ricky barrette
* @date Dec 22, 2010
*/
package com.TwentyCodes.java.BankAccount;
/**
* this enum will represent trantions types
* @author ricky barrette
*/
public enum TransactionType {
DEPOSIT, WITHDRAW, CHARGE_FEE
}

View File

@@ -0,0 +1,128 @@
/**
* @author Twenty Codes, LLC
* @author ricky barrette
* @date Dec 18, 2010
*/
package com.TwentyCodes.java.BankAccount.UI;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import com.TwentyCodes.java.BankAccount.Account;
/**
* this class will allow the user to enter a new vehicle into the database
*/
public class AddAccountDialog extends JFrame implements ActionListener{
private static final long serialVersionUID = 3165335189780349167L;
private JButton ok;
private JLabel errorMsg;
private JTextField owner;
private JTextField account;
private JTextField balance;
/**
* Creates a new AddAccountDialog
*/
public AddAccountDialog() {
super();
setTitle("Add Account");
this.setSize(744, 118);
//create a JPanel to hold the text area and button
JPanel panel = new JPanel();
JLabel label_1 = new JLabel("Owner: ");
owner = new JTextField();
owner.setColumns(10);
JLabel label_2 = new JLabel("Account Number: ");
account = new JTextField();
account.setColumns(10);
JLabel label = new JLabel("Initial Banlance: ");
balance = new JTextField();
balance.setColumns(10);
ok = new JButton("Ok");
ok.addActionListener(this);
//add the JPanel to the frame, and display
getContentPane().add(panel, BorderLayout.SOUTH);
errorMsg = new JLabel("Please verify the account information, and try again");
errorMsg.setVisible(false);
GroupLayout gl_panel = new GroupLayout(panel);
gl_panel.setHorizontalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addGap(21)
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
.addGroup(gl_panel.createSequentialGroup()
.addComponent(errorMsg)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(ok, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(gl_panel.createSequentialGroup()
.addComponent(label_1)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(owner, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(label_2)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(account, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(label)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(balance, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
.addContainerGap(877, Short.MAX_VALUE))
);
gl_panel.setVerticalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
.addComponent(owner, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
.addComponent(label_2, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
.addComponent(account, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
.addComponent(label, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
.addComponent(balance, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
.addComponent(errorMsg, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
.addComponent(ok))
.addContainerGap())
);
panel.setLayout(gl_panel);
// pack();
setVisible(true);
}
/**
* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == ok) {
try{
long accountNumber = Long.parseLong(account.getText());
MainWindow.db.addAccount(new Account(Double.parseDouble(balance.getText()), owner.getText(), accountNumber));
setVisible(false);
} catch(Exception ex){
errorMsg.setVisible(true);
}
}
}
}

View File

@@ -0,0 +1,131 @@
/**
* @author Twenty Codes, LLC
* @author ricky barrette
* @date Dec 18, 2010
*/
package com.TwentyCodes.java.BankAccount.UI;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.TwentyCodes.java.BankAccount.AccountDB;
/**
* this is the main window and class of this application
*/
public class MainWindow extends JFrame implements ActionListener{
private JButton search;
private JButton addAccount;
private JButton addTransaction;
private JButton removeAccount;
private JButton showAllAccount;
private JButton saveToFile;
private JButton loadFile;
private JFileChooser fc;
public static AccountDB db;
/**
* Generated Serial Version ID
*/
private static final long serialVersionUID = 1841715561053331517L;
public MainWindow() {
setTitle("Account Database");
JPanel panel = new JPanel();
search = new JButton("Search");
addAccount = new JButton("Add Account");
addTransaction = new JButton("Add Transaction");
removeAccount = new JButton("Remove Account");
showAllAccount = new JButton("Show All Accounts");
loadFile = new JButton("Load File");
saveToFile = new JButton("Save");
search.addActionListener(this);
addAccount.addActionListener(this);
addTransaction.addActionListener(this);
removeAccount.addActionListener(this);
showAllAccount.addActionListener(this);
saveToFile.addActionListener(this);
loadFile.addActionListener(this);
panel.add(search);
panel.add(addAccount);
panel.add(addTransaction);
panel.add(removeAccount);
panel.add(showAllAccount);
panel.add(loadFile);
panel.add(saveToFile);
add(panel);
setVisible(true);
pack();
db = new AccountDB();
fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
}
/**
* main method, called when the application starts
* @param args
*/
public static void main(String[] args){
new MainWindow();
}
/**
* called when a button is clicked
* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == search) {
new SearchDialog();
}
if (e.getSource() == addAccount) {
new AddAccountDialog();
}
if (e.getSource() == removeAccount) {
new RemoveAccounteDialog();
}
if (e.getSource() == showAllAccount) {
new ShowAllDialog(db.toString());
}
if(e.getSource() == addTransaction){
new TransactionDialog();
}
if(e.getSource() == saveToFile){
try {
if(fc.showSaveDialog(MainWindow.this) == JFileChooser.APPROVE_OPTION)
db.save(fc.getSelectedFile());
} catch (HeadlessException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if(e.getSource() == loadFile){
try {
if(fc.showOpenDialog(MainWindow.this) == JFileChooser.APPROVE_OPTION)
db.load(fc.getSelectedFile());
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}

View File

@@ -0,0 +1,66 @@
/**
* @author Twenty Codes, LLC
* @author ricky barrette
* @date Dec 18, 2010
*/
package com.TwentyCodes.java.BankAccount.UI;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* This class will be the delete vehicle dialog
*/
public class RemoveAccounteDialog extends JFrame implements ActionListener {
private static final long serialVersionUID = -36245710370532319L;
private JButton ok;
private JTextField textField;
/**
* Creates a new RemoveVehicleDialog
*/
public RemoveAccounteDialog(){
super();
setTitle("Delete Accounts");
//create a JPanel to hold the text area and button
JPanel panel = new JPanel();
ok = new JButton("Ok");
JLabel label = new JLabel("Enter Account Owner or Number");
ok.addActionListener(this);
panel.add(label);
textField = new JTextField();
panel.add(textField);
textField.setColumns(10);
panel.add(ok);
//add the JPanel to the frame, and display
getContentPane().add(panel);
pack();
setVisible(true);
}
/**
* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == ok) {
MainWindow.db.deleteAccounts(textField.getText());
setVisible(false);
}
}
}

View File

@@ -0,0 +1,71 @@
/**
* @author Twenty Codes, LLC
* @author ricky barrette
* @data Dec 18, 2010
*/
package com.TwentyCodes.java.BankAccount.UI;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import com.TwentyCodes.java.BankAccount.Account;
/**
* This class will be the dialog that will ask the user for a specific make to show
*/
public class SearchDialog extends JFrame implements ActionListener {
private static final long serialVersionUID = 1750326106927701404L;
private JButton ok;
private JTextField editText;
/**
* Creates a new ShowAllMakeDialog
*/
public SearchDialog() {
super();
setTitle("Search");
//create a JPanel to hold the text area and button
JPanel panel = new JPanel();
ok = new JButton("Ok");
JLabel label = new JLabel("Enter Acount Owner or Number:");
ok.addActionListener(this);
panel.add(label);
editText = new JTextField();
panel.add(editText);
editText.setColumns(10);
panel.add(ok);
//add the JPanel to the frame, and display
getContentPane().add(panel);
pack();
setVisible(true);
}
/**
* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == ok) {
StringBuilder sb = new StringBuilder();
for(Account item : MainWindow.db.searchForAccount(editText.getText()))
sb.append(item.toString()+"\n");
new ShowAllDialog(sb.toString());
setVisible(false);
}
}
}

View File

@@ -0,0 +1,75 @@
/**
* @author Twenty Codes, LLC
* @author ricky barrette
* @date Dec 18, 2010
*/
package com.TwentyCodes.java.BankAccount.UI;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.LayoutStyle.ComponentPlacement;
/**
* This panel will be used to display all Vehicles in the VechicleDB
*/
public class ShowAllDialog extends JFrame implements ActionListener {
private static final long serialVersionUID = -8416144493079733535L;
/**
* Creates a new ShowAllDialog
*/
public ShowAllDialog(String list){
super();
setTitle("Show All Accounts");
//create a JPanel to hold the text area and button
JPanel panel = new JPanel();
this.setSize(400, 540);
//add the JPanel to the frame, and display
getContentPane().add(panel);
JButton ok = new JButton("Ok");
ok.addActionListener(this);
JTextArea results = new JTextArea();
results.setEditable(false);
GroupLayout gl_panel = new GroupLayout(panel);
gl_panel.setHorizontalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
.addContainerGap()
.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
.addComponent(results, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 517, Short.MAX_VALUE)
.addComponent(ok, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 517, Short.MAX_VALUE))
.addContainerGap())
);
gl_panel.setVerticalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
.addComponent(results, GroupLayout.DEFAULT_SIZE, 319, Short.MAX_VALUE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(ok)
.addContainerGap())
);
panel.setLayout(gl_panel);
results.setText(list);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent arg0) {
setVisible(false);
}
}

View File

@@ -0,0 +1,296 @@
/**
* @author Twenty Codes, LLC
* @author ricky barrette
* @date Dec 22, 2010
*/
package com.TwentyCodes.java.BankAccount.UI;
import java.awt.BorderLayout;
import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import com.TwentyCodes.java.BankAccount.Account;
import com.TwentyCodes.java.BankAccount.TransactionType;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class TransactionDialog extends JFrame implements ActionListener {
private static final long serialVersionUID = -6130586697078071325L;
private JPanel mContentPane;
private JTextField mAmount;
private JTextField mAccountNumber;
private JRadioButton mDeposit;
private JRadioButton mWithdraw;
private JRadioButton mChargeFee;
private JButton mBtnFind;
private JButton mBtnOk;
private TransactionType mType = TransactionType.DEPOSIT;
private JLabel mErrorMsg;
private JLabel mAccountInfoLabel;
/**
* Create the frame.
*/
public TransactionDialog() {
setTitle("Add Transaction");
setBounds(100, 100, 495, 181);
mContentPane = new JPanel();
mContentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
mContentPane.setLayout(new BorderLayout(0, 0));
setContentPane(mContentPane);
JPanel panel = new JPanel();
mContentPane.add(panel, BorderLayout.WEST);
// create radio buttons and group them
ButtonGroup group = new ButtonGroup();
mDeposit = new JRadioButton("Deposit");
mDeposit.setSelected(true);
mWithdraw = new JRadioButton("Withdraw");
mChargeFee = new JRadioButton("Charge Fee");
group.add(mDeposit);
group.add(mWithdraw);
group.add(mChargeFee);
// radio button action listeners
mDeposit.addActionListener(this);
mWithdraw.addActionListener(this);
mChargeFee.addActionListener(this);
JLabel lblTransationType = new JLabel("Transation Type");
GroupLayout gl_panel = new GroupLayout(panel);
gl_panel.setHorizontalGroup(gl_panel
.createParallelGroup(Alignment.LEADING)
.addGroup(
gl_panel.createSequentialGroup()
.addGroup(
gl_panel.createParallelGroup(
Alignment.LEADING)
.addGroup(
gl_panel.createSequentialGroup()
.addContainerGap()
.addComponent(
mDeposit))
.addGroup(
gl_panel.createSequentialGroup()
.addContainerGap()
.addComponent(
lblTransationType))
.addGroup(
gl_panel.createSequentialGroup()
.addContainerGap()
.addComponent(
mWithdraw))
.addGroup(
gl_panel.createSequentialGroup()
.addContainerGap()
.addComponent(
mChargeFee)))
.addContainerGap(GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)));
gl_panel.setVerticalGroup(gl_panel.createParallelGroup(
Alignment.LEADING).addGroup(
gl_panel.createSequentialGroup().addContainerGap()
.addComponent(lblTransationType)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(mDeposit)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(mWithdraw)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(mChargeFee)
.addContainerGap(156, Short.MAX_VALUE)));
panel.setLayout(gl_panel);
JPanel panel_1 = new JPanel();
mContentPane.add(panel_1, BorderLayout.CENTER);
JLabel lblTransationAmount = new JLabel("Transation Amount");
mAmount = new JTextField();
mAmount.setColumns(10);
JLabel lblAccountNumber = new JLabel("Account Number");
mAccountNumber = new JTextField();
mAccountNumber.setColumns(10);
mBtnFind = new JButton("Find");
mBtnFind.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
mBtnOk = new JButton("Ok");
mBtnOk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
GroupLayout gl_panel_1 = new GroupLayout(panel_1);
gl_panel_1
.setHorizontalGroup(gl_panel_1
.createParallelGroup(Alignment.LEADING)
.addGroup(
gl_panel_1
.createSequentialGroup()
.addContainerGap()
.addGroup(
gl_panel_1
.createParallelGroup(
Alignment.TRAILING)
.addGroup(
Alignment.LEADING,
gl_panel_1
.createSequentialGroup()
.addComponent(
lblTransationAmount)
.addPreferredGap(
ComponentPlacement.RELATED)
.addComponent(
mAmount,
GroupLayout.DEFAULT_SIZE,
164,
Short.MAX_VALUE))
.addComponent(
mBtnOk,
GroupLayout.DEFAULT_SIZE,
GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addGroup(
Alignment.LEADING,
gl_panel_1
.createSequentialGroup()
.addComponent(
lblAccountNumber)
.addPreferredGap(
ComponentPlacement.RELATED)
.addComponent(
mAccountNumber,
GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE)
.addPreferredGap(
ComponentPlacement.RELATED)
.addComponent(
mBtnFind)))
.addGap(94)));
gl_panel_1
.setVerticalGroup(gl_panel_1
.createParallelGroup(Alignment.LEADING)
.addGroup(
gl_panel_1
.createSequentialGroup()
.addContainerGap()
.addGroup(
gl_panel_1
.createParallelGroup(
Alignment.BASELINE)
.addComponent(
lblAccountNumber)
.addComponent(
mAccountNumber,
GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(mBtnFind))
.addPreferredGap(
ComponentPlacement.RELATED)
.addGroup(
gl_panel_1
.createParallelGroup(
Alignment.BASELINE)
.addComponent(
lblTransationAmount)
.addComponent(
mAmount,
GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE))
.addPreferredGap(
ComponentPlacement.RELATED)
.addComponent(mBtnOk).addGap(41)));
panel_1.setLayout(gl_panel_1);
mErrorMsg = new JLabel("");
mContentPane.add(mErrorMsg, BorderLayout.SOUTH);
mAccountInfoLabel = new JLabel("");
mContentPane.add(mAccountInfoLabel, BorderLayout.NORTH);
// Action listners for ok and find buttons
mBtnOk.addActionListener(this);
mBtnFind.addActionListener(this);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == mDeposit) {
mType = TransactionType.DEPOSIT;
mAmount.setEnabled(true);
}
if (e.getSource() == mWithdraw) {
mType = TransactionType.WITHDRAW;
mAmount.setEnabled(true);
}
if (e.getSource() == mChargeFee) {
mType = TransactionType.CHARGE_FEE;
mAmount.setEnabled(false);
mAmount.setText("10.00");
}
if (e.getSource() == mBtnFind) {
try {
Account account = MainWindow.db.getAccount(Long.parseLong(mAccountNumber.getText()));
if(account != null){
mAccountInfoLabel.setText(account.toString());
mErrorMsg.setText("");
} else {
mAccountInfoLabel.setText("");
mErrorMsg.setText("No Such Account");
}
} catch (NumberFormatException e1) {
mAccountInfoLabel.setText("");
mErrorMsg.setText("Invalid Account Entry");
}
}
if (e.getSource() == mBtnOk) {
try{
switch(mType){
case DEPOSIT:
if(MainWindow.db.deposit(Long.parseLong(mAccountNumber.getText()), Double.parseDouble(mAmount.getText())))
setVisible(false);
else
mErrorMsg.setText("No Such Account");
break;
case WITHDRAW:
if(MainWindow.db.withdraw(Long.parseLong(mAccountNumber.getText()), Double.parseDouble(mAmount.getText())))
setVisible(false);
else
mErrorMsg.setText("No Such Account");
break;
case CHARGE_FEE:
if(MainWindow.db.chargeFee(Long.parseLong(mAccountNumber.getText())))
setVisible(false);
else
mErrorMsg.setText("No Such Account");
break;
}
} catch(Exception ex){
mErrorMsg.setText("Invalid Entry, Check Input");
}
}
}
}