# Notes
# Use Case Scenario (UML)
# Class Diagram (UML)
# Consider an Application sample run of actions
Automated Teller Machine
Choose 1 for Withdraw
Choose 2 for Deposit
Choose 3 for Check Balance
Choose 4 for EXIT
Choose the operation you want to perform:1
Enter money to be withdrawn:2000
Please collect your money
Automated Teller Machine
Choose 1 for Withdraw
Choose 2 for Deposit
Choose 3 for Check Balance
Choose 4 for EXIT
Choose the operation you want to perform:3
Balance : 3000
Automated Teller Machine
Choose 1 for Withdraw
Choose 2 for Deposit
Choose 3 for Check Balance
Choose 4 for EXIT
Choose the operation you want to perform:4
import java.util.Scanner; | |
public class BANKATM { | |
public static void main(String args[]) { | |
double balance = 5000, withdraw, deposit; | |
Scanner s = new Scanner(System.in); | |
while(true) { | |
System.out.println("Automated Teller Machine"); | |
System.out.println("Choose 1 for Withdraw"); | |
System.out.println("Choose 2 for Deposit"); | |
System.out.println("Choose 3 for Check Balance"); | |
System.out.println("Choose 4 for EXIT"); | |
System.out.print("Choose the operation you want to perform: "); | |
int n = s.nextInt(); | |
switch(n) { | |
case 1: | |
System.out.print("Enter money to be withdrawn:"); | |
withdraw = s.nextInt(); | |
if(balance >= withdraw) { | |
balance = balance - withdraw; | |
System.out.println("Please collect your money"); | |
} | |
else | |
System.out.println("Insufficient Balance"); | |
System.out.println(""); | |
break; | |
case 2: | |
System.out.print("Enter money to be deposited:"); | |
deposit = s.nextInt(); | |
balance = balance + deposit; | |
System.out.println("Your Money has been successfully depsited"); | |
System.out.println(""); | |
break; | |
case 3: | |
System.out.println("Balance : "+balance); | |
System.out.println(""); | |
break; | |
case 4: | |
System.exit(0); | |
s.close(); | |
} | |
} | |
} | |
} |
# Learning about Classes and Objects
A class is a blueprint consisting of an idea. Think of it like a data structure.
EX. Creating a building, making a cake, registering students, gradebook, simpleInterest…..
A class also is an Abstract Data Type.
EX.
public Class Building ….
where Building becomes the datatype!
Create a Building object (variable)
Building stuartObj = new Building(); // where stuartObj is of type Building | |
Building lsObj = new Building(); // where lsObj is of type Building |
Objects can be REPRESENTATIVES of the class.
What kind of offerings does the class have for its objects??
ENTER METHODS!! (METHODS ARE BEHAVIORS OF THE CLASS)
ENTER ATTRIBUTES!! (DATA VALUES THAT AN INDIVIDUAL OBJECT HAVE OF THE CLASS)
# Lab
- PROJECT
Bank account simulator program - Objective
To write a program that performs various bank transactions.
# PROJECT DESCRIPTION
Bank of IIT has contacted you to write, compile and execute a complete program that creates bank account information and executes various transaction details for their clients.
Your program will prompt users for options such as creating an initial balance, entering deposits or withdrawals. Also, your program will allow for the printing of account information including interest at various interest rates.
Use any loops, user defined methods, conditional and relational logic and the basics of OOP to accomplish the objectives of this program.
Error trapping will be part of your grade so don’t forget to include some basic error trapping logic! Always comment your code thoroughly and include brief program descriptions as well where applicable for your source files for maximum points.
# Project Details
For this program you will create two separate Java files within your package, namely AccountHolder
and AccountHolderTest
.
The AccountHolder
file must include the following class field members and data methods to allow for transaction processing.
Field Name | Field Type |
---|---|
annualInterestRate | static / double |
balance | double |
*Method Name | Method (Instance) | Argument | Return Type |
---|---|---|---|
AccountHolder | Constructor | double | none |
deposit | Instance | double | void |
withdrawal | Instance | double | void |
monthlyInterest | Instance | void | void |
getBalance | Instance | void | double |
*assume all methods are declared as public
Of course if you would like to add any extra fields or methods in your class(es) feel free
to do so.
Coding detail for your methods must include the following:
Allow the Constructor to accept an argument representing an initial balance for the Account holder. Set your balance member equal to the value passed in via the class constructor.
Balances cannot start off negative! Include an error message if this is the situation.Define in your monthlyInterest method body, an assignment statement to update the account holders’ balance to be affected as follows:
balance += balance * (annualInterestRate / 12.0);
For your deposit & withdrawal methods, either have your method body either increase or decrease the holder’s current balance accordingly.
An added rule to follow here:
Disallow the withdrawal account balance to drop below $50.
Include comments on what your method is to perform (ex. any data to be received and what to return back to the calling environment).
For your AccountHolderTest file, include any local variables in your main method to work the application. Include the following transactional detail from your main method, to be executed in the following order for runtime needs. Run your program twice and follow details below and snapshot your results for each run. Label snapshots accordingly in your documentation.
# Snapshot 1 – run #1
Create an AccountHolder object and prompt the user for an initial account balance and have the balance passed into the AccountHolder constructor.
Allow the interest for the bank to be initially set at 4%. This can be hard coded in.
Prompt the user to enter in a deposit amount.
Prompt the user for a withdrawal amount. Show an example when a user enters a withdrawal that will drop their balance below $50. Display a message stating that a balance cannot go below $50.
Display the ending or updated balance with included interest amount applied.
# Snapshot 2 – run #2
Prompt the user for an initial account balance. Include a negative starting balance
entry and thus allow for a positive value only to be accepted.Allow the interest for the bank to be initially set at 4%. This can be hard coded in.
Prompt the user to enter in a deposit amount.
Prompt the user for a withdrawal amount.
Display the ending or updated balance with included interest amount applied.
# Objectives / General Tips
# UML of classes
Error: Failed to launch the browser process! spawn /Applications/Google Chrome.app/Contents/MacOS/Google Chrome ENOENT TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md
# Accessing data
2 ways to access data
For static members (fields)
ClassName.staticmember
Ex.
AccountHolder.annualInterestRate = .04;
For instance members (fields)
objectName.method()
# The Constructor
When object is created the Constructor is called automatically.
Ex.
AccountHolder accObj1 = new AccountHolder(balance); |
# Error trappings!
if (balance < 0.0) | |
throw new IllegalArgumentException("balance must be non-negative"); |
other:
while (true) { | |
balance = sc.nextDouble(); | |
if (balance < 0) | |
System.out.println("Pls. reenter a positive beginning balance"); | |
else | |
break; | |
} |
# Avoiding Self-Referential assigning!
*Use this (which refers to a current object’s instance) keyword in a method to avoid self referential integrity!this.balance = balance;
ex.
// constructor, creates a new account with the specified balance | |
public AccountHolder(double balance) { | |
this.balance = balance; //assign local variable to class member | |
} |
# Print formatting
Use various print formatting with the format specifier %
symbol followed by a converter.
Popular converters to use include:
%f | float |
%d | int |
%s | string |
%n | newline |
Ex.
System.out.printf("$%.2f", balance); //print currency style | |
System.out.format ("%-10s%n", "Total balance"); |
# code
public class AccountHolder { | |
// class level data members | |
private double balance; | |
static public double annualInterestRate; | |
/* class methods */ | |
/** | |
* constructor method | |
* Create a new account with the specified balance | |
* | |
* @param balance | |
*/ | |
public AccountHolder(double balance) { | |
this.balance = balance; // assign local variable to class member | |
} | |
/** | |
* Set a deposit | |
* | |
* @param balance | |
*/ | |
public void deposit(double balance) { | |
this.balance += balance; //ADD to existing BALANCE local variable | |
} | |
/** | |
* Set a withdrawal | |
* | |
* @param balance | |
*/ | |
public void withdrawal(double balance) { | |
/* Check the existing balance!!! | |
* DO NOT allow the balance to go below $50 | |
*/ | |
if (this.balance - balance < 50.0) | |
{ | |
throw new IllegalArgumentException("Disallow the withdrawal account balance to drop below $50."); | |
} | |
this.balance -= balance; //SUBTRACT from existing BALANCE local variable | |
} | |
/** | |
* Monthly Interest | |
* | |
* update the account holders’ balance based on annualInterestRate / 12.0 | |
*/ | |
public void monthlyInterest() { | |
balance += balance * (annualInterestRate / 12.0); | |
} | |
// getters | |
public double getBalance() { | |
return balance; | |
} | |
} |
import java.util.InputMismatchException; | |
import java.util.Scanner; | |
public class AccountHolderTest { | |
public static void main(String[] args) { | |
//set up program local variables | |
Scanner in = new Scanner(System.in); | |
double balance = 0; | |
System.out.println("Automated Teller Machine"); | |
// prompt the user for an initial account balance | |
while (true) { | |
try { | |
System.out.print("Please enter your initial balance: "); | |
balance = in.nextDouble(); | |
if (balance < 0.0) // Balances cannot start off negative | |
{ | |
throw new IllegalArgumentException("Balance must be non-negative. Please re-enter a positive beginning balance."); | |
} | |
else | |
{ | |
break; | |
} | |
} catch (InputMismatchException e) { | |
System.err.println("Illegal value. Please re-enter a double."); | |
in.nextLine(); | |
} catch(Exception e) { | |
System.err.println(e.getMessage()); | |
} | |
} | |
//set AccountHolder object | |
AccountHolder accObj = new AccountHolder(balance); | |
//set interest rate at 4% | |
AccountHolder.annualInterestRate = .04; | |
System.out.printf("\nA new account created successfully. Initial balance " | |
+ "is $%.2f.\n" , balance ); | |
System.out.println(""); //new line | |
// Operation selector | |
while(true) { | |
try { | |
System.out.println("Choose 1 for Deposit"); | |
System.out.println("Choose 2 for Withdraw"); | |
System.out.println("Choose 3 for Check Balance"); | |
System.out.println("Choose 4 for EXIT"); | |
System.out.print("Choose the operation you want to perform: "); | |
int n = in.nextInt(); | |
switch(n) { | |
// set a deposit | |
case 1: | |
while (true) { | |
try { | |
System.out.println(""); //new line | |
System.out.print("Please enter a deposit: "); | |
balance = in.nextDouble(); | |
accObj.deposit(balance); | |
System.out.println("Your Money has been successfully depsited."); | |
break; | |
} catch(InputMismatchException e) { | |
System.err.println("Illegal value. Please re-enter a correct value."); | |
in.nextLine(); | |
} | |
} | |
break; | |
// set a withdrawal | |
case 2: | |
while (true) { | |
try { | |
System.out.println(""); //new line | |
System.out.print("Please enter a withdrawal: "); | |
balance = in.nextDouble(); | |
accObj.withdrawal(balance); | |
System.out.println("Please collect your money."); | |
break; | |
} catch(InputMismatchException e) { | |
System.err.println("Illegal value. Please re-enter a correct value."); | |
in.nextLine(); | |
} catch(Exception e) { | |
System.err.print(e.getMessage()); | |
} | |
} | |
break; | |
// check balance | |
case 3: | |
// update the account holders’ balance with included interest amount applied | |
accObj.monthlyInterest(); | |
System.out.printf("\nBalance with interest applied " | |
+ "= $%.2f \n" , accObj.getBalance() ); | |
break; | |
// exit the system | |
case 4: | |
System.out.println("\nThanks for using this system. See you next time."); | |
System.exit(0); | |
in.close(); | |
break; | |
default: | |
System.err.println("\nChoosing the wrong option, please reselect."); | |
break; | |
} | |
System.out.println(""); //new line | |
} catch (InputMismatchException e) { | |
System.err.println("Illegal value. Please re-enter a correct value."); | |
in.nextLine(); | |
} | |
} | |
} | |
} |