# Need help for a Java homework mimic ATM Machine .

• 03-29-2012, 07:34 AM
ihmflu
Need help for a Java homework mimic ATM Machine .
write a java program using classes to mimic an ATM of a bank.

The interactions are done with a menu.
Save every accounts info in a text file using FileWriter and PrintWriter. Every withdrawal costs 20 cents.
WELCOME
what do you want to do today?
1.create a new account
2.deposit into an existing account
3.withdraw from an existing account
4.check balance of an existing account
5.print Summary of transactions
6.exit

Sample text file:
Name: JOE , Balance = 120.80 \$
You have deposited 2 times with the amount of 10\$
You have withdraw 1 times with the amount of 5\$
You were charged 0.20\$

Name: Bob , Balance = 80.60 \$
You have deposited 2 times with the amount of 20\$
You have withdraw 2 times with the amount of 7\$
You were charged 0.40\$

----------------------------------------------------------------------------
So here is what I got for my main and class:

// class
import java.util.*;
import java.text.*;
import java.io.*;

public class ATM
{
double input;
double balance, withdraw, deposit;
int number;
double Deposit;
double totalDeposit;
double totalWithdraw;
double totalCharge;
int countDeposit;
int countWithdraw;
double charge;

public String createAccount() throws IOException
{
String account;
Scanner kb = new Scanner(System.in);
System.out.print("Enter an account name you want to create.");
account = kb.nextLine();
System.out.println("Your account has been created. ");

return account;
}

public void deposit(double dep)
{

balance += dep;
countDeposit +=1;
totalDeposit += dep;
}

public void withdraw(double w)
{

countWithdraw++;
totalWithdraw +=w;
balance -=0.20;
balance -=w;
charge += 0.20;
}

public double getBalance()
{
return balance;

}
public void setBalance(double sb)
{
balance = sb;
}
public double getCountDeposit()
{
return countDeposit;
}
public double getCountWithdraw()
{
return countWithdraw;
}
public double gettotalDeposit()
{
}
public double getCharge()
{ return charge;
}

public double gettotalWithdraw()
{
}

}

This is my main :

import java.util.*;
import java.io.*;
import java.text.*;
public class ATMTest
{
public static void main(String[] args) throws IOException
{
int number;

DecimalFormat formatter = new DecimalFormat("#,###.00");
ATM account = new ATM();

Scanner kb = new Scanner(System.in);

System.out.println("WELCOME! WHAT DO YOU WANT TO DO TODAY");

while(true)
{
System.out.println("1.create a new account");
System.out.println("2.deposit into an existing account");
System.out.println("3.withdraw from an existing account");
System.out.println("4.check balance of an existing account");
System.out.println("5.print Summary of transactions");
System.out.println("6.exit ");
number= kb.nextInt();

if(number ==1)
{
String newAccount = account.createAccount();
}
else if (number == 2)

{ System.out.println("How much do you want to deposit?");
double deposit = kb.nextDouble();
account.deposit(deposit);
}

else if (number == 3)
{
System.out.println("How much do you want to withdraw?");
double withdraw = kb.nextDouble();

if (withdraw > account.getBalance()){
System.out.println("You don't have enough money to withdraw");
}

else
{
account.withdraw(withdraw);
}

}
else if (number == 4)
{

}

else if (number == 5)
{
System.out.println("\$" +account.getBalance());
System.out.println("you have deposit: ");
System.out.print(account.getCountDeposit() + "times");
System.out.println("you have withdraw: ");
System.out.println(account.getCountWithdraw()+ " times");
System.out.println("you have deposit: ");
System.out.println("\$" + account.gettotalDeposit() );
System.out.println("You have withdraw: ");
System.out.println("\$" +account.gettotalWithdraw() );
System.out.println(" You were charged: ");
System.out.println("\$" + account.getCharge() );
}
else if (number == 6)
System.exit(0);

} //while loop

}//main
} //class

It worked well but the thing is I don't know how to save the account to a text file and how to access to an existing account from the file. Can anyone please help?
• 03-29-2012, 10:36 AM
ojn
Re: Need help for a Java homework mimic ATM Machine .