|
|
#1 | |
|
ಠ_ಠ
Join Date: Dec 2007
Posts: 737
|
If anyone knows java, I need to create a GUI interface ATM with a jtextfield for account etcetera with withdraw and deposit buttons.
It should use an array of accounts (which are in a separate class which has ints for balance, etc). If anyone has skype and thinks he/she can help I would be glad to reimburse you for your time and expertise. I already built the account class to specification, I already have a GUI interface which kind of interacts with a static account but I can't figure out how to get the value inside of a jtextfield and use it as a "current account being used" variable...
__________________
ヽ(`Д´)ノ |
|
|
|
|
|
|
#2 | |
|
Registered User
Join Date: Jan 2006
Location: Silicon Valley
Posts: 4
|
Hello. Is this homework assignment? If so I have been there, but it was many years ago prior to Java. Do not give up. The first few months of programming in college can feel rather hopeless, but it gets easier.
A few of things 1. Using int for the balance is great. In the financial business, you do not want to use float because of the precision (accuracy) of IEEE 754. Use int or long and do your math in pennies or use Java's java.math.BigDecimal. 2. You can get the value of the text in a JTextField with the getText() method. It returns a String. For example to set the text of a JTextField called textFieldBalance with the current balance - the text in the JTextFieldAmount Code:
textFieldBalance.setText( Integer.parseInt( textFieldBalance.getText()) - Integer.parseInt( textFieldAmount.getText()); Thermond |
|
|
|
|
|
|
#3 |
|
Registered User
Join Date: Jan 2006
Location: Silicon Valley
Posts: 4
|
Ok. It is Saturday night, and my date canceled. Plus, I have been lurking at this site for years without doing much for the community.
Here is my attempt to do my yearly one good deed. Take a look at this code I just wrote. Since it appears that you are already off to a good start, this may help when you get stuck. I am unaware of your specification, so this is generally how I would approach the code. My Main class is called ATM. It loads the Gui and tells the Gui of the first account Code:
package atm;
import java.math.BigDecimal;
public class ATM {
/**
* @param args
*/
public static void main(String[] args) {
Gui gui = new Gui();
gui.buildFrame();
gui.pack();
gui.setVisible(true);
Account[] accounts = new Account[1];
accounts[0] = new Account("012345", new BigDecimal("500"));
gui.setAccount(accounts[0]);
}
}
Code:
package atm;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Gui extends JFrame{
/** The textField for the account number */
JTextField textFieldAccount = new JTextField();
/** The textField for the balance */
JTextField textFieldBalance = new JTextField();
/** The textField for the amount to be deposited or withdrawn */
JTextField textFieldAmount = new JTextField();
/** the button for deposit */
JButton buttonDeposit = new JButton("Deposit");
/** the button for withdraw */
JButton buttonWithdraw = new JButton("Withdraw");
/** the account that currently will be displayed */
private Account account = null;
public Gui() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
* Build the JFrame to display by
* 1. Creating a main panel
* 2. Adding the fields to the main panel
* 3. Creating a button panel
* 4. Adding buttons to the button panel
* 5. Adding listers to the buttons
*/
public void buildFrame() {
// get the content pane from the JFrame
Container contentPane = this.getContentPane();
// set the layout manager to a simpleManager
contentPane.setLayout(new BorderLayout());
// this will be the main Panel that displays the account
JPanel panelMain = new JPanel(new GridLayout(3,2));
// add the content to the main panel
panelMain.add(new JLabel("Account"));
panelMain.add(textFieldAccount);
panelMain.add(new JLabel("Balance"));
panelMain.add(textFieldBalance);
panelMain.add(new JLabel("Amount"));
panelMain.add(textFieldAmount);
// now add the main panel to the frame's content pane
contentPane.add(panelMain,BorderLayout.CENTER);
// now lets add some buttons
// create a panel
JPanel panelButton = new JPanel();
// now add an action listener for the deposit button
// which when pressed calls the deposit method
ActionListener buttonDepositAction = new ActionListener() {
public void actionPerformed(ActionEvent e) {
deposit();
}
};
buttonDeposit.addActionListener(buttonDepositAction);
panelButton.add(buttonDeposit);
//ok lets do the same thing for the withdraw button
ActionListener buttonWithdrawAction = new ActionListener() {
public void actionPerformed(ActionEvent e) {
withdraw();
}
};
buttonWithdraw.addActionListener(buttonWithdrawAction);
panelButton.add(buttonWithdraw);
contentPane.add(panelButton,BorderLayout.SOUTH);
}
/**
* Withdraws from the account
*/
void withdraw() {
account.withdraw(new BigDecimal(textFieldAmount.getText()));
textFieldBalance.setText(account.getBalance().toString());
}
/**
* Deposits into the account
*/
void deposit() {
account.deposit(new BigDecimal(textFieldAmount.getText()));
textFieldBalance.setText(account.getBalance().toString());
}
/**
* Sets the account for the Frame (dialog)
* @param account the account that will be shown
*/
public void setAccount(Account account) {
this.account = account;
textFieldAccount.setText(account.getAccountNumber());
textFieldBalance.setText(account.getBalance().toString());
}
}
Code:
package atm;
import java.math.BigDecimal;
public class Account {
private String accountNumber;
private BigDecimal balance;
public Account(String accountNumber, BigDecimal balance) {
this.accountNumber = accountNumber;
this.balance = balance;
}
/**
* Gets the Account number
* @return the text of the account number
*/
public String getAccountNumber() {
return accountNumber;
}
/**
* Get the account balance
* @return the BigDecimal of the account balance
*/
public BigDecimal getBalance(){
return balance;
}
/**
* Withdraw from the account
* @param amount the BigDecimal of the amount to withdraw
*/
public void withdraw(BigDecimal amount){
this.balance = balance.subtract(amount);
}
/**
* Deposits to the account
* @param amount the BigDecimal of the amount to deposit
*/
public void deposit(BigDecimal amount) {
this.balance = balance.add(amount);
}
}
Perhaps the Software development thread would be a better place for conversation. Good Luck Thermond Last edited by fingolfin; 04-25-10 at 02:39 AM. Reason: Forgot to enclose the last code in CODE |
|
|
|
|
|
#4 | |
|
Dethklok Returns!
|
Quote:
![]() |
|
|
|
|
|
|
#5 | |
|
ಠ_ಠ
Join Date: Dec 2007
Posts: 737
|
I worked on your program fingolfin thanks for the starter tips.
The ATM is supposed to work with an array of accounts (10 in size) so i modified your code to work to the assignment's specifications. I added the exception things and stuff. ![]() Quote:
Code:
//package atm;
import java.math.BigDecimal;
public class ATM {
/**
* @param args
*/
public static void main(String[] args) {
Gui gui = new Gui();
gui.buildFrame();
gui.pack();
gui.setVisible(true);
Account[] accounts = new Account[10]; // creates array of 10 accounts
//accounts[0] = new Account("012345", new BigDecimal("500"));
for (int i = 0; i < 10; i++) // creates accounts
{
String i2 = Integer.toString(i);
accounts[i] = new Account(i2, new BigDecimal("500"));
}
//gui.setAccount(accounts[0]);
}
}
Code:
import java.awt.*;
import java.awt.event.*;
import java.math.*;
import javax.swing.*;
public class Gui extends JFrame{
/** The textField for the account number */
JTextField textFieldAccount = new JTextField("1");
/** The textField for the balance */
JTextField textFieldBalance = new JTextField("500");
/** The textField for the amount to be deposited or withdrawn */
JTextField textFieldAmount = new JTextField("0");
/** the button for deposit */
JButton buttonDeposit = new JButton("Deposit");
/** the button for withdraw */
JButton buttonWithdraw = new JButton("Withdraw");
/** the button for check balance */
JButton buttonCheckBalance = new JButton("Check Balance");
/** the account that currently will be displayed */
private Account[] accounts = new Account[10];
private Account account = null;
public Gui() {
for (int i = 0; i < 10; i++)
{
String i2 = Integer.toString(i);
accounts[i] = new Account(i2, new BigDecimal("500"));
}
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
* Build the JFrame to display by
* 1. Creating a main panel
* 2. Adding the fields to the main panel
* 3. Creating a button panel
* 4. Adding buttons to the button panel
* 5. Adding listers to the buttons
*/
public void buildFrame() {
// get the content pane from the JFrame
Container contentPane = this.getContentPane();
// set the layout manager to a simpleManager
contentPane.setLayout(new BorderLayout());
// this will be the main Panel that displays the account
JPanel panelMain = new JPanel(new GridLayout(3,2));
// add the content to the main panel
panelMain.add(new JLabel("Account"));
panelMain.add(textFieldAccount);
panelMain.add(new JLabel("Balance"));
panelMain.add(textFieldBalance);
//panelMain.add(new JLabel("Amount"));
//panelMain.add(textFieldAmount);
// now add the main panel to the frame's content pane
contentPane.add(panelMain,BorderLayout.CENTER);
// now lets add some buttons
// create a panel
JPanel panelButton = new JPanel();
// now add an action listener for the deposit button
// which when pressed calls the deposit method
ActionListener buttonDepositAction = new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
deposit();
} catch (ATMException e1) {
e1.printStackTrace();
}
}
};
buttonDeposit.addActionListener(buttonDepositAction);
panelButton.add(buttonDeposit);
ActionListener buttonCheckBalanceAction = new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
checkBalance();
}
};
buttonCheckBalance.addActionListener(buttonCheckBalanceAction);
panelButton.add(buttonCheckBalance);
// do the same thing for the withdraw button
ActionListener buttonWithdrawAction = new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
withdraw();
} catch (ATMException e1) {
e1.printStackTrace();
}
}
};
buttonWithdraw.addActionListener(buttonWithdrawAction);
panelButton.add(buttonWithdraw);
contentPane.add(panelButton,BorderLayout.SOUTH);
}
void checkBalance(){
int current = Integer.parseInt(textFieldAccount.getText());
JOptionPane.showMessageDialog(buttonCheckBalance, "The balance for account " + current + " is $" + accounts[current].getBalance());
textFieldBalance.setText(accounts[current].getBalance().toString());
}
void withdraw() throws ATMException {
int current = Integer.parseInt(textFieldAccount.getText());
if (current < 1 || current > 10)
{
throw new ATMException("Account number invalid");
}
String s = (String)JOptionPane.showInputDialog(
buttonWithdraw,
"Please enter an amount to withdraw from account " + current + ": \nYour current balance for account " + current + " is $" + accounts[current].getBalance(),
"Withdraw Amount",
JOptionPane.PLAIN_MESSAGE,
null,
null, "0");
int check = Integer.parseInt(s);
if (check <= 0)
{
throw new ATMException("No ammount entered to withdraw.");
}
else
{
//accounts[current].withdraw(new BigDecimal(textFieldAmount.getText()));
accounts[current].withdraw(new BigDecimal(s));
textFieldBalance.setText(accounts[current].getBalance().toString());
}
}
void deposit() throws ATMException {
int current = Integer.parseInt(textFieldAccount.getText());
/** Trying to make an evaluation to say if account field is null, tell user to enter account number
* but doesn't work properly because when it tries to parseInt above with a null value, program screws up. **/
if (current < 1 || current > 10)
{
throw new ATMException("Account number invalid");
}
String s = (String)JOptionPane.showInputDialog(
buttonDeposit,
"Please enter an amount to deposit into account " + current + ": \nYour current balance for account " + current + " is $" + accounts[current].getBalance(),
"Deposit Amount",
JOptionPane.PLAIN_MESSAGE,
null,
null, "0");
int check = Integer.parseInt(s);
if (check <= 0)
{
throw new ATMException("No ammount entered to deposit.");
}
else
{
//accounts[current].deposit(new BigDecimal(textFieldAmount.getText()));
accounts[current].deposit(new BigDecimal(s));
textFieldBalance.setText(accounts[current].getBalance().toString());
}
}
/**
* Sets the account for the Frame (dialog)
* @param account the account that will be shown
*/
public void setAccount(Account account) {
this.account = account;
textFieldAccount.setText(account.getAccountNumber());
textFieldBalance.setText(account.getBalance().toString());
}
}
Code:
//package atm;
import java.math.BigDecimal;
public class Account {
private String accountNumber;
private BigDecimal balance;
public Account(String accountNumber, BigDecimal balance) {
this.accountNumber = accountNumber;
this.balance = balance;
}
/**
* Gets the Account number
* @return the text of the account number
*/
public String getAccountNumber() {
return accountNumber;
}
/**
* Get the account balance
* @return the BigDecimal of the account balance
*/
public BigDecimal getBalance(){
return balance;
}
/**
* Withdraw from the account
* @param amount the BigDecimal of the amount to withdraw
*/
public void withdraw(BigDecimal amount) throws ATMException{
if (this.balance.compareTo(amount) < 0)
{
throw new ATMException("Can't withdraw more than in account.");
}
this.balance = balance.subtract(amount);
}
/**
* Deposits to the account
* @param amount the BigDecimal of the amount to deposit
*/
public void deposit(BigDecimal amount) {
this.balance = balance.add(amount);
}
}
Code:
import javax.swing.JOptionPane;
public class ATMException extends Exception {
public ATMException(String string) {
JOptionPane.showMessageDialog(null, string);
}
}
__________________
ヽ(`Д´)ノ |
|
|
|
|
![]() |
| Thread Tools | |
|
|