Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
One of my earlier programming assignments for AP Computer Science circa Spring 2005
//********************************************************************
// Account.java Author: Lewis/Loftus/Cocking
//
// Represents a bank account with basic services such as deposit
// and withdraw.
//********************************************************************
import java.text.NumberFormat;
public class Account
{
private NumberFormat fmt = NumberFormat.getCurrencyInstance();
private final double RATE = 0.035;
private int acctNumber;
private double balance;
private String name;
public Account (String owner, int account, double initial)
{
name = owner;
acctNumber = account;
balance = initial;
}
public Account (String owner, int account)
{
name = owner;
acctNumber = account;
balance = 0;
}
public static void transfer (Account from, Account to, double transAmt, double fee)
{
if (transAmt+fee > from.getBalance())
{
System.out.println("Insufficient funds!");
return;
}
from.withdraw(transAmt, fee);
to.deposit(transAmt);
System.out.println("Transfer successful!");
return;
}
public double deposit (double amount)
{
if (amount < 0)
{
System.out.println ();
System.out.println ("Error: Deposit amount is invalid.");
System.out.println (acctNumber + " " + fmt.format(amount));
}
else
balance = balance + amount;
System.out.println("Deposit successful!");
return balance;
}
public double withdraw (double amount, double fee)
{
amount += fee;
if (amount < 0)
{
System.out.println ();
System.out.println ("Error: Withdraw amount is invalid.");
System.out.println ("Account: " + acctNumber);
System.out.println ("Requested: " + fmt.format(amount));
}
else
if (amount > balance)
{
System.out.println ();
System.out.println ("Error: Insufficient funds.");
System.out.println ("Account: " + acctNumber);
System.out.println ("Requested: " + fmt.format(amount));
System.out.println ("Available: " + fmt.format(balance));
}
else
balance = balance - amount;
System.out.println("Withdrawal successful!");
return balance;
}
public double withdraw (double amount)
{
this.withdraw(amount, 0);
return balance;
}
public double addInterest ()
{
balance += (balance * RATE);
System.out.println("Interest added");
return balance;
}
public double getBalance ()
{
return balance;
}
public int getAccountNumber ()
{
return acctNumber;
}
public String toString ()
{
return (acctNumber + "\t" + name + "\t" + fmt.format(balance));
}
}
/* DumbATM.java Author: Mark Cerqueira
* Acts as a Dumb ATM; user inputs how much money they have
* to begin with, which is why the ATM is so dumb. Other than
* that I would consider this ATM a superior form of intelligence.
*/
import cs1.Keyboard;
import java.util.Random;
import java.text.NumberFormat;
public class DumbATM
{
public static void main(String[] args)
{
String owner;
int ChkAccountNum, SavAccountNum, option = -1, acctOption, bankClient;
double fee, ChkInitial, SavInitial, depositAmt, totalWithdrawals = 0;
final double MAX_WITHDRAWAL = 400;
NumberFormat money = NumberFormat.getCurrencyInstance();
System.out.println("-----------------------------------");
System.out.println("Welcome to a Java National ATM");
System.out.println("-----------------------------------");
// Determines fee for using ATM
System.out.println("If you are a Java National Client press 1");
System.out.println("If you are with any other bank press 2");
System.out.print("\nI am with (insert number): ");
bankClient = Keyboard.readInt();
if (bankClient != 1)
fee = 1.5;
else
fee = 0;
System.out.print("\nEnter your name: ");
owner = Keyboard.readString();
// Sets up savings account
System.out.print("\nSavings Account Set-Up:\nEnter account number: ");
SavAccountNum = Keyboard.readInt();
System.out.print("Amount in this account: $");
SavInitial = Keyboard.readDouble();
Account savings = new Account (owner, SavAccountNum, SavInitial);
// Sets up checking account
System.out.print("\nChecking Account Set-Up:\nEnter account number: ");
ChkAccountNum = Keyboard.readInt();
System.out.print("Amount in this account: $");
ChkInitial = Keyboard.readDouble();
Account checking = new Account (owner, ChkAccountNum, ChkInitial);
//Displays the two newly created account objects
System.out.println("\n" + "Savings account: " + savings);
System.out.println("Checking account: " + checking);
while (option != 5)
{
System.out.println("\n-----------------------------");
System.out.println("O P T I O N S");
System.out.println("-----------------------------");
// Allows user to do various methods on their new accounts
System.out.println("\nPress 1 to Deposit");
System.out.println("Press 2 to Withdraw");
System.out.println("Press 3 to View Balance");
System.out.println("Press 4 to Transfer");
System.out.println("Press 5 to Quit"); //sentinel value
System.out.print("\nI want to (insert number): ");
option = Keyboard.readInt();
if (option == 1) //deposit
{
System.out.println("\nPress 1 to Deposit into Savings Account");
System.out.println("Press 2 to Deposit into Checking Account");
System.out.print("\nI want to Deposit to(insert number): ");
acctOption = Keyboard.readInt();
System.out.print("I want to deposit: $");
depositAmt = Keyboard.readDouble();
if (acctOption == 1)
{
savings.deposit(depositAmt);
}
if (acctOption == 2)
{
checking.deposit(depositAmt);
}
}
if (option == 2) //withdraw
{
double withdrawal20, withdrawalCasted, withdrawalReal, withdrawal;
System.out.println("\nPress 1 to Withdraw from my Savings Account");
System.out.println("Press 2 to Withdraw from my Checking Account");
System.out.print("\nI want to Withdraw from(insert number): ");
acctOption = Keyboard.readInt();
System.out.println("\nWithdrawing is allowed in increments of $20.00");
System.out.print("\nYou wish to withdraw: $");
withdrawal = Keyboard.readDouble();
withdrawal20 = (withdrawal)/20;
withdrawalCasted = (int)withdrawal20;
withdrawalReal = withdrawalCasted * 20;
totalWithdrawals += withdrawalReal;
if (totalWithdrawals > 400)
{
System.out.println("You have exceeded the limit for withdrawing.");
System.out.println("Come back in 24 hours!");
}
else
{
System.out.print("You want to withdraw " + money.format(withdrawal) + " and ");
System.out.println(money.format(withdrawalReal) + " will be withdrawn.");
if (acctOption == 1)
{
savings.withdraw(withdrawalReal, fee);
}
if (acctOption == 2)
{
checking.withdraw(withdrawalReal, fee);
}
}
}
if (option == 3) //get balance
{
System.out.print("Balance for your savings account: ");
System.out.println(money.format(savings.getBalance()));
System.out.print("Balance for your checking account: ");
System.out.println(money.format(checking.getBalance()));
}
if (option == 4) //transfer
{
System.out.println("\nPress 1 to Transfer from Savings to Checking");
System.out.println("Press 2 to Transfer from Checking to Savings");
System.out.print("I want to transfer (number): ");
acctOption = Keyboard.readInt();
System.out.print("\nI want to transfer this amount: $");
double transAmt = Keyboard.readInt();
if (acctOption == 1)
savings.transfer(savings, checking, transAmt,fee);
if (acctOption == 2)
checking.transfer(checking, savings, transAmt, fee);
}
if (option == 2288) //this is a secret add interest option that cool people know about...
{
savings.addInterest();
checking.addInterest();
}
}
System.out.println("\nDone! Here is your receipt. Come to this Dumb ATM again.");
System.out.println(savings);
System.out.println(checking);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.