Go Back   nV News Forums > Software Forums > General Software

Newegg Daily Deals

Reply
 
Thread Tools
Old 04-24-10, 03:50 PM   #1
RaidenWoW
ಠ_ಠ
 
RaidenWoW's Avatar
 
Join Date: Dec 2007
Posts: 737
Default need java programming help (using Eclipse)

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...
__________________
ヽ(`Д)ノ
[/eVGA 8800GTS 640mb OC630mhz eVGA 8800GTS G92 eVGA 9800GTX G92] Gigabyte Radeon 6870 | Q6600 @ 2.92ghz Zalman 9700 45c-55c | Crucial M4 64g | eVga 790i Ultra [/Asus Striker Extreme]
RaidenWoW is offline   Reply With Quote
Old 04-25-10, 01:35 AM   #2
fingolfin
Registered User
 
Join Date: Jan 2006
Location: Silicon Valley
Posts: 4
Default Re: need java programming help (using Eclipse)

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());
Good luck
Thermond
fingolfin is offline   Reply With Quote
Old 04-25-10, 02:11 AM   #3
fingolfin
Registered User
 
Join Date: Jan 2006
Location: Silicon Valley
Posts: 4
Default Re: need java programming help (using Eclipse)

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]);
	}

}
The Gui class is a Jframe which shows the JTextFields for account number, balance, and amount. It provides buttons for withdraw and deposit.
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());
	}
}
The last class is the Account class. It stores the account number and balance. It also provides methods for withdraw and deposit.
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);
	}
	
}
It would be a good idea to add checks to the deposit and withdraw methods so that one could not supply a negative number.

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
fingolfin is offline   Reply With Quote
Old 04-25-10, 02:59 AM   #4
t3hl33td4rg0n
Dethklok Returns!
 
t3hl33td4rg0n's Avatar
 
Join Date: Dec 2004
Location: Kent, OH
Posts: 2,142
Send a message via AIM to t3hl33td4rg0n Send a message via MSN to t3hl33td4rg0n Send a message via Yahoo to t3hl33td4rg0n
Default Re: need java programming help (using Eclipse)

Quote:
Originally Posted by fingolfin View Post
Perhaps the Software development thread would be a better place for conversation.
I was thinking the same thing
__________________


Intel QX9650 @ 3.83GHzXFX 780ieVGA GTX 560Ti8GB GSkill DDR800Creative Sound Blaster X-Fi Titanium PCI-ESeagate 1.5TB . 4x Samsung 2TB

Quote:
Originally Posted by MowTin View Post
....it's so clear how government makes things worse not better...
t3hl33td4rg0n is offline   Reply With Quote
Old 04-25-10, 07:46 PM   #5
RaidenWoW
ಠ_ಠ
 
RaidenWoW's Avatar
 
Join Date: Dec 2007
Posts: 737
Default Re: need java programming help (using Eclipse)

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:
Create a GUI version of your ATM program. The GUI should have a label & textfield for the account id, and buttons for Balance, Withdraw, Deposit. All messages to the user should be in pop-up dialog boxes (System.out.println should not be used anywhere in the program). If the user selects Deposit or Withdraw, you should obtain the amount either from a pop-up dialog (use JOptionPane.showInputDialog). The GUI should display appropriate messages when the Account class throws an Exception. Extra Credit: Have the GUI display a “bank logo”. Any other enhancements you would like to do will also count for extra credit.
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);
	}

}
__________________
ヽ(`Д)ノ
[/eVGA 8800GTS 640mb OC630mhz eVGA 8800GTS G92 eVGA 9800GTX G92] Gigabyte Radeon 6870 | Q6600 @ 2.92ghz Zalman 9700 45c-55c | Crucial M4 64g | eVga 790i Ultra [/Asus Striker Extreme]
RaidenWoW is offline   Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 02:32 AM.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright 1998 - 2014, nV News.