Thursday, August 2, 2007

code: ATM Machine

This is a very simple code that i wrote. It's just an ATM machine and it's very simple... it uses some "if then" and "switch" statements and it's a good practice for a beginner coder.

If you have any question and/or comments please let me know because I'm fairly new to Java myself so any hints and tricks are welcome :)


/**
* Author: http://javacodee.blogspot.com/
* Date: Aug 1, 2007
* Time: 11:30:18 PM
*/
import java.util.Scanner;

public class AtmMachine
{
private double availableBal =80;
private double totalBal =100;
Scanner input = new Scanner(System.in);

public int userAccount()
{
System.out.print("Enter your account number: ");
int account;
account = input.nextInt();

return account;
}

public int userPin()
{
System.out.print("Enter your pin number: ");
int pin;
pin =input.nextInt();

return pin;
}

public void startAtm()
{
userAccount();
userPin();
drawMainMenu();
}

public void drawMainMenu()
{
int selection;

System.out.println("\nATM main menu:");
System.out.println("1 - View account balance");
System.out.println("2 - Withdraw funds");
System.out.println("3 - Add funds");
System.out.println("4 - Terminate transaction");
System.out.print("Choice: ");
selection =input.nextInt();

switch(selection)
{
case 1:
viewAccountInfo();
break;
case 2:
withdraw();
break;
case 3:
addFunds();
break;
case 4:
System.out.println("Thank you for using this ATM!!! goodbye");
}
}

public void viewAccountInfo()
{
System.out.println("Account Information:");
System.out.println("\t--Total balance: $"+totalBal);
System.out.println("\t--Available balance: $"+availableBal);
drawMainMenu();
}

public void deposit(int depAmount)
{
System.out.println("\n***Please insert your money now...***");
totalBal =totalBal +depAmount;
availableBal =availableBal +depAmount;
}

public void checkNsf(int withdrawAmount)
{
if(totalBal -withdrawAmount < 0)
System.out.println("\n***ERROR!!! Insufficient funds in you accout***");
else
{
totalBal =totalBal -withdrawAmount;
availableBal =availableBal -withdrawAmount;
System.out.println("\n***Please take your money now...***");
}
}

public void addFunds()
{
int addSelection;

System.out.println("Deposit funds:");
System.out.println("1 - $20");
System.out.println("2 - $40");
System.out.println("3 - $60");
System.out.println("4 - $100");
System.out.println("5 - Back to main menu");
System.out.print("Choice: ");
addSelection =input.nextInt();

switch(addSelection)
{
case 1:
deposit(20);
drawMainMenu();
break;
case 2:
deposit(40);
drawMainMenu();
break;
case 3:
deposit(60);
drawMainMenu();
break;
case 4:
deposit(100);
drawMainMenu();
break;
case 5:
drawMainMenu();
break;
}
}

public void withdraw()
{
int withdrawSelection;

System.out.println("Withdraw money:");
System.out.println("1 - $20");
System.out.println("2 - $40");
System.out.println("3 - $60");
System.out.println("4 - $100");
System.out.println("5 - Back to main menu");
System.out.print("Choice: ");
withdrawSelection =input.nextInt();

switch(withdrawSelection)
{
case 1:
checkNsf(20);
drawMainMenu();
break;
case 2:
checkNsf(40);
drawMainMenu();
break;
case 3:
checkNsf(60);
drawMainMenu();
break;
case 4:
checkNsf(100);
drawMainMenu();
break;
case 5:
drawMainMenu();
break;
}
}

public static void main(String args[])
{
AtmMachine myAtm = new AtmMachine();
myAtm.startAtm();
}
}

34 comments:

sidik_l27 said...

how the code, if the number pin has been registered for exp: 12345,

- how if user input the wrong pin, then the atm said : Incorrect Number, please input the correct number or press 0 to exit

- when user input the correct number, all code that you write above will working.

I am sorry, i am very new to this java, i have a bad english

sidik_l27@yahoo.com
msidikp.wordpress.com

sidik_l27 said...

thank you very much if you want to share your knowledge...

i apreciate it..

saif said...

hey,
can u post a java program on the following question:-

WAP to enter a numeric value and print its value for
a. Absolute value of a number
b. Square root of a number
c. Cube root of a number

saif said...

waitin 4 a reply.,...

Eman Hanoun said...

thank u very much for this code ...
i wanna to ask u a bout somthing how can i read data from a file ?
as u know that all the user have their password and account in a file and the ATM will read this and compare !
how i can add it to this code?

regards;
Eman

Rolljin said...

Hi there.
I'm Rolljin Malicdem from Philippines.
I'm a First Year IT student.
I have a problem regarding on how to create an ATM Machine using JAVA.
I wish you could help me on my case study about on how to create a ATM Machine using JAVA.
My e-mail address is malicdemrolljin@yahoo.com.
We need your help as soon as possible.
Thank a lot and I hope that you could give me some tips on how to defense your JAVA code.
Thanks a lot and GODBLESS...

luci-lg said...

Hello..
I'm new to java language and I wanna asking you about this coding, when I was compile file, description cannot resolve symbol class Scanner in 1. import java.util.Scanner; 2. Scanner input = new Scanner(System.in);

What's the problem?? Can you explain to me..??

Waiting your reply, plz..
Luci_lg@yahoo.com

nikita said...

is dis code will work as online ATM system

nikita said...

i need an ATM code that works not only on standalone PC but also as online system. my e-mail id is nikita18katyal@gmail.com

donamie2006 said...

Hi,
My name is Senami, I'm from Nigeria. Please, I would like you to build a biometric ATM with Java program.

My email address is donamie2006@yahoo.com

Thank you.

rasha said...

hi,
did you finish your project . build a biometric ATM with Java program.
thanks
rasha2211@yahoo.com

nickty said...

Thanks for sharing this valuable code.

kabaka said...

men am tyrone 1 year experience programmer thank you for doing this men tell how can i put my atm machine to run on line with in bank pliz my contact is kamyabowl@yahoo.com

ahnnie reine said...

hey,have a nice day....how can this code will be in a windows form of data type...

ahnnie reine said...

hey...have a nice day...can u have a ATM code in java in a windows data type...?

CFBG said...

this blog really help!tqvm

bharath said...

hw to use a bufferedreader claz in dis??,can u frame dis using buffered reader claz??

javed usmani said...

i want pen drive no. as a ATM card no.(when i plug in my pen Drive in to the leaptop then it treat like a ATM Card) in to my ATM Machine java project.................so anybody have a idea about it.
please send me meterial ragarding this (ATM Project)on my mail Id"-javedusmani2@gmail.com.

raghuram said...

really great !
thamk you for your information(program)......

ALEXis01 said...

Hi there.
I'm ALEXIS ESQUEJ from Philippines.
I'm a First Year IT student.
I have a problem regarding on how to create an ATM Machine using JAVA.
I wish you could help me on my case study about on how to create a ATM Machine using JAVA.
My e-mail address is chozen_esquejo@yahoo.com.
We need your help as soon as possible.
Thank a lot and I hope that you could give me some tips on how to defense your JAVA code.
Thanks a lot and GODBLESS...


PLEASE !!! HELP ME :)

ALEXis01 said...
This comment has been removed by the author.
i m said...
This comment has been removed by the author.
i m said...

m jst new in machine programming coding language ... ...

i just wantd 2 knw is der any other easy language 4 machine programming coding is yes den which language is dat .?.!. ... ...


i wil b glad if u reply heyitsm10@gmail.com


thank you ... ...

shanmuka said...

Thank u for your help..i am very glad to you

shanmuka said...

Thank u for your help..i am very glad to you

Janine Villarino said...

i am hoping that you could help me.. i want a code for changing pin.. i dont what to do..

Damon Salvatore said...

nice program for beginners....easy to understand :)

sandi mubha said...

thank so much and helped...
http://sandi-mubha.blogspot.com/

Pakcik said...

import java.awt.*;
import java.util.Scanner;
import java.text.DecimalFormat;

/** Account */
class Account {
// declaration
private static int id;
private static double balance;

/** Constructor */
public Account() {
// constructor
this.id = 0;
this.balance = 0.00;
}

/** Withdraw */
public static double withdraw(double balance, double withdraw) {
// withdraw
DecimalFormat decf = new DecimalFormat("0.00");

if (withdraw > balance) {
System.out.println("Insufficient fund");
Toolkit.getDefaultToolkit().beep();
}
else {
balance -= withdraw;
System.out.println("The balance is RM " + decf.format(balance) );
}

return balance;
}

/** Deposit */
public static double deposit(double balance, double deposit) {
// deposit
DecimalFormat decf = new DecimalFormat("0.00");

balance += deposit;
System.out.println("The balance is RM " + decf.format(balance) );

return balance;
}

/** MainMenu */
public static void mainMenu() {
// mainmenu
System.out.println("");
System.out.println("Main menu");
System.out.println("1 : Check balance");
System.out.println("2 : Withdraw");
System.out.println("3 : Deposit");
System.out.println("4 : Exit");
System.out.println("");
}

/** CheckID */
public static boolean checkID(int id, int[] userID) {
// check id
for (int x = 0; x < userID.length; x++)
{
if (id == userID[x]) {
return true;
}
}
return false;
}

/** Main */
public static void main(String[] args) {
// main
Scanner inp = new Scanner(System.in);
DecimalFormat decf = new DecimalFormat("0.00");

int choice;
int[] userID = {0,1,2,3,4,5,6,7,8,9};
double[] iBalance = {100,100,100,100,100,100,100,100,100};

System.out.print("Enter an id: ");
id = inp.nextInt();

boolean boolID = checkID(id,userID);

while (boolID == false)
{
System.out.print("Enter an id: ");
id = inp.nextInt();
boolID = checkID(id,userID);
}

while (1 > 0) { // this statement will always true...

if (boolID == false) {
while (boolID == false)
{
System.out.println("");
System.out.print("Enter an id: ");
id = inp.nextInt();
boolID = checkID(id,userID);
}

}

// before looping again...
balance = iBalance[id];

choice = 0;
while (choice != 4) {

mainMenu();

System.out.print("Enter a choice : ");
choice = inp.nextInt();

switch(choice) {
case 1 :
// check balance
System.out.println("The balance is RM " + decf.format(balance));
balance = iBalance[id];
break;
case 2 :
// withdraw
System.out.print("Enter an amount to withdraw : ");
double amtWithdraw = inp.nextDouble();
iBalance[id] = withdraw(balance, amtWithdraw);
balance = iBalance[id];
break;
case 3 :
// deposit
System.out.print("Enter an amount to deposit : ");
double amtDeposit = inp.nextDouble();
iBalance[id] = deposit(balance, amtDeposit);
balance = iBalance[id];
break;
case 4 :
boolID = false;
break;
} // switch

} // while looping (choice)

} // while looping (infinite loop)

} // main

} // class

Marcin Zimnicki said...

Hi how to create this code in JFrame?
Please email me zimorodek84@gmail.com

raj'scrickinfo said...

hai i'm rajasekhar i'm new to java send me the some of the java blogs which contains real time examples in the blog to: rajsekhar.568@gmail.com

anna marie cauba gentapa said...

Our instructor in our class required us to make a java ATM Program. It is a major problem on me since my knowledge in java is very limited. I've searched in google and VOILA! I found this site. Thank you very much! Muchos Gracias, Amigo! :)

anna marie cauba gentapa said...

This was a great help! Muchos Gracias,amigo! :)

farhannah pumbaya said...

hi.. i really need a help for this ATM machine code.. it's out project in our java class and i'm getting confused on it.. here's my email.. pumbaya12@yahoo.com and i really want to know what's the use of some codes there.. please.. i'm bad on understanding a book...