RunMyAtm
package ATM;
import java.util.*;
import java.io.*;
public class RunMyAtm
{
int input;
static Scanner sc = new Scanner(System.in);
Account[] myAccounts = new Account[3];
public static void main(String[] args)
{
RunMyAtm rma = new RunMyAtm();
rma.preAtmMenu();
}
public void preAtmMenu()
{
while (input != 5)
{
System.out.println("1.) Populate Accounts");
System.out.println("2.) Pick Account");
System.out.println("3.) Load Accounts");
System.out.println("4.) Save Account");
System.out.println("5.) Exit");
System.out.print("Please select one of the options: ");
input = sc.nextInt();
System.out.println();
if (input == 1)
{
populateAccts();
System.out.println();
}
else if (input == 2)
{
pickAccts();
System.out.println();
}
else if (input == 3)
{
loadAccount();
}
else if (input == 4)
{
saveAccount();
}
else if (input <=0 || input >=6)
{
System.out.println("Please enter a nubmer from the Menu");
}
}
}
public void populateAccts()
{
for(int i = 0; i < myAccounts.length; i++)
{
myAccounts[i]= new Account ((i+1), 100);
System.out.println(myAccounts[i].getAcctNum());
}
}
public void pickAccts()
{
while (input != 4)
{
System.out.println("Press 1 for account 1");
System.out.println("Press 2 for account 2");
System.out.println("Press 3 for account 3");
System.out.println("Press 4 to exit");
System.out.print("Select an account: ");
input = sc.nextInt();
System.out.println();
if (input <1 || input >4)
{
System.out.println("Please enter another number");
}
else if(input == 1 || input == 2 || input ==3)
{
myAccounts[input - 1].AtmMenu();
saveAccount();
}
}
}
public void saveAccount()
{
try
{
FileOutputStream outStream = new FileOutputStream("E:/03INFSYS
3806001 - Mngrl Appl Obj-Orntd Prg
/tempfile1/BankAccounts.txt");
ObjectOutputStream os = new ObjectOutputStream(outStream);
os.writeObject(myAccounts);
os.flush();
os.close();
}
catch (IOException ioe)
{
System.err.println(ioe);
}
}
void loadAccount()
{
try
{
FileInputStream inStream = new FileInputStream("E:/03INFSYS
3806 001-Mngrl Appl Obj-Orntd Prg
/tempfile1/BankAccounts.txt");
ObjectInputStream is = new ObjectInputStream(inStream);
myAccounts = (Account[])is.readObject();
is.close();
}
catch (Exception ioe)
{
System.out.println(ioe.getMessage());
}
}
Account
package ATM;
import java.io.Serializable;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;
public class Account implements Serializable
{
int acctnum;
double newBalance;
double withdraw;
double deposit;
double amount;
int firstdate;
int seconddate;
double rate;
Date date = new Date();
boolean dateflag = false;
static Scanner sc = new Scanner(System.in);
Calendar cal1 = new GregorianCalendar();
Calendar cal2 = new GregorianCalendar();
DecimalFormat df = new DecimalFormat("#.##");
static NumberFormat fmt = NumberFormat.getCurrencyInstance(Locale.US);
Account()
{
}
Account(int acctnum, double newBalance)
{
this.newBalance = newBalance;
this.acctnum = acctnum;
}
public void setAcctNum(int newId)
{
acctnum = newId;
}
public int getAcctNum()
{
return this.acctnum;
}
public void withdraw(int amount)
{
System.out.println("Your current balance is :" +
fmt.format(this.getNewBalance()) + "\n");
System.out.print("Enter withdraw amount: ");
amount = sc.nextInt();
System.out.println();
if (this.getNewBalance() >= amount)
{
newBalance = this.getNewBalance() - amount;
System.out.println("Your current balance is: "
+ fmt.format(newBalance));
}
else
{
System.out.println("Insufficient Funds Availiable" + "\n");
}
}
public void deposit(double amount)
{
System.out.println("Your current balance is :"
+ fmt.format(this.getNewBalance()) + "\n");
System.out.print("Enter deposit amount: ");
amount = sc.nextDouble();
newBalance = amount + this.getNewBalance();
System.out.println("Your new balance is: " + fmt.format(newBalance));
System.out.println();
}
public void newBalance()
{
System.out.println("Your balance is: " + fmt.format(newBalance) +"\n");
}
public double getNewBalance()
{
return this.newBalance;
}
public void calcInterest()
{
getDate1();
getDate2();
if (firstdate > seconddate)
{
System.out.println("You must enter a future date:");
getDate2();
}
else
{
System.out.println(" Thank you:");
}
int datediff = seconddate - firstdate;
rate = .05/365;
double ratetime = Math.pow(1+rate,datediff);
newBalance = getNewBalance() * ratetime;
System.out.println("Your Balance with interest is: "
+ df.format(newBalance));
}
public void getDate1()
{
System.out.print("Enter first date(mm/dd/yyyy): ");
String input = sc.next();
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
ParsePosition pos = new ParsePosition(0);
Date date = formatter.parse(input, pos);
cal1.setTime(date);
firstdate = cal1.get(Calendar.DAY_OF_YEAR);
dateflag = true;
}
public void getDate2()
{
System.out.print("Enter second date(mm/dd/yyyy): ");
String input = sc.next();
System.out.println();
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
ParsePosition pos = new ParsePosition(0);
Date date = formatter.parse(input, pos);
cal2.setTime(date);
seconddate = cal2.get(Calendar.DAY_OF_YEAR);
dateflag = true;
}
public void AtmMenu()
{
int input = 0;
while (input !=5)
{
System.out.println("1.) Withdraw");
System.out.println("2.) Deposit");
System.out.println("3.) Check Balance");
System.out.println("4.) Calculate Interst");
System.out.println("5.) Exit");
System.out.print("Please enter a nubmer from the menu above"
+ " and press enter:" );
input = sc.nextInt();
System.out.println();
if (input == 1)
{
withdraw((int) input);
}
else if (input == 2)
{
deposit(input);
}
else if (input == 3)
{
newBalance();
}
else if (input == 4)
{
calcInterest();
}
else if (input <=0 || input >=6)
{
System.out.println("Please enter a nubmer from the Menu");
}
}
}
}