Skip to content

Commit dd07e30

Browse files
committed
ATM Case Study
1 parent c551733 commit dd07e30

File tree

8 files changed

+387
-0
lines changed

8 files changed

+387
-0
lines changed

13 ATM Case Study/.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>13 ATM Case Study</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>

13 ATM Case Study/src/ATM.java

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
// ATM.java
2+
// Represents an automated teller machine
3+
4+
public class ATM
5+
{
6+
private boolean userAuthenticated; // whether user is authenticated
7+
private int currentAccountNumber; // current user's account number
8+
private Screen screen; // ATM's screen
9+
private Keypad keypad; // ATM's keypad
10+
private CashDispenser cashDispenser; // ATM's cash dispenser
11+
private DepositSlot depositSlot; // ATM's deposit slot
12+
private BankDatabase bankDatabase; // account information database
13+
14+
// constants corresponding to main menu options
15+
private static final int BALANCE_INQUIRY = 1;
16+
private static final int WITHDRAWAL = 2;
17+
private static final int DEPOSIT = 3;
18+
private static final int EXIT = 4;
19+
20+
// no-argument ATM constructor initializes instance variables
21+
public ATM()
22+
{
23+
userAuthenticated = false; // user is not authenticated to start
24+
currentAccountNumber = 0; // no current account number to start
25+
screen = new Screen(); // create screen
26+
keypad = new Keypad(); // create Keypad
27+
cashDispenser = new CashDispenser(); // create cash dispenser
28+
depositSlot = new DepositSlot(); // create deposit slot
29+
bankDatabase = new BankDatabase(); // create acct info database
30+
} // end no-argument ATM constructor
31+
32+
// start ATM
33+
public void run()
34+
{
35+
// welcome and authenticate user; perform transactions
36+
while (true)
37+
{
38+
// loop while user is not yet authenticate
39+
while (!userAuthenticated)
40+
{
41+
screen.displayMessageLine("\nWelcome!");
42+
authenticateUser(); // authenticate user
43+
} // end while
44+
45+
performTransactions(); // user is now authenticated
46+
userAuthenticated = false; // reset before next ATM session
47+
currentAccountNumber = 0; // reset before next ATM session
48+
screen.displayMessageLine("\nThank you! Goodbye!");
49+
50+
} // end while
51+
} // end method run
52+
53+
// attempts to authenticate user against database
54+
private void authenticateUser()
55+
{
56+
screen.diplayMessage("\nPlease enter your account number: ");
57+
int accountNumber = keypad.getInput(); // input account number
58+
screen.diplayMessage("\nEnter your PIN: "); // prompt for PIN
59+
int pin = keypad.getInput(); // input PIN
60+
61+
// set userAuthentigated to boolean value returned by the database
62+
userAuthenticated =
63+
bankDatabase.authenticateUser(accountNumber, pin);
64+
65+
// check whether authentication succeeded
66+
if (userAuthenticated)
67+
{
68+
currentAccountNumber = accountNumber; // save user's account #
69+
} // end if
70+
else
71+
screen.displayMessageLine(
72+
"Invalid account number or PIN. Please try again.");
73+
} // end method authenticateUser
74+
75+
// display the main menu and perform transactions
76+
private void performTransactions()
77+
{
78+
// local variable to store transaction currently being processed
79+
Transaction currentTransaction = null;
80+
boolean userExited = false; // user has not chosen to exit
81+
82+
// loop while user has not chosen option to exit system
83+
while (!userExited)
84+
{
85+
// show main menu and get user selection
86+
int mainMenuSelection = displayMainMenu();
87+
88+
// decide how to proceed based on user's menu selection
89+
switch (mainMenuSelection)
90+
{
91+
// user chose to perform one of three transaction types
92+
case BALANCE_INQUIRY:
93+
case WITHDRAWAL:
94+
case DEPOSIT:
95+
96+
// initialize as new object of chosen type
97+
currentTransaction =
98+
createTransaction(mainMenuSelection);
99+
100+
currentTransaction.execute(); // execute transaction
101+
break;
102+
103+
case EXIT: // user chose to terminate session
104+
screen.displayMessageLine("\nExiting the system...");
105+
userExited = true; // this ATM session should end
106+
break;
107+
default: // user did not enter an integer from 1-4
108+
screen.displayMessageLine(
109+
"\nYou did not enter a valid selection. Try again.");
110+
break;
111+
} // end switch
112+
} // end while
113+
} // end method performTransactions
114+
115+
// display the main menu and return an input selection
116+
private int displayMainMenu()
117+
{
118+
screen.displayMessageLine("\nMain menu:");
119+
screen.displayMessageLine("1 - View my balance");
120+
screen.displayMessageLine("2 - Withdraw cash");
121+
screen.displayMessageLine("3 - View my balance");
122+
screen.displayMessageLine("4 - Withdraw cash");
123+
screen.displayMessageLine("Enter a choice: ");
124+
return keypad.getInput(); // return user's selection
125+
} // end method displayMainMenu
126+
127+
// return object of specified Transaction subclass
128+
private Transaction createTransaction(int type)
129+
{
130+
Transaction temp = null; // temporary Transaction variable
131+
132+
//determine which type of Transaction to create
133+
switch (type)
134+
{
135+
case BALANCE_INQUIRY: // create new BalanceInquiry transaction
136+
temp = new BalanceInquiry(
137+
currentAccountNumber, screen, bankDatabase);
138+
break;
139+
case WITHDRAWAL: // create new Withdrawal transaction
140+
temp = new Withdrawal(currentAccountNumber, screen,
141+
bankDatabase, keypad, cashDispenser);
142+
break;
143+
case DEPOSIT: // create new Deposit transaction
144+
temp = new Deposit(currentAccountNumber, screen,
145+
bankDatabase, keypad, depositSlot);
146+
break;
147+
} // end switch
148+
149+
return temp; // return the newly created object
150+
} // end method createTransaction
151+
} // end class ATM
152+
153+

13 ATM Case Study/src/Account.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Account.java
2+
// Represents a bank account
3+
4+
public class Account
5+
{
6+
private int accountNumber; //
7+
private int pin; // PIN for authentication
8+
private double availableBalance; // funds available for withdrawal
9+
private double totalBalance; // funds available + pending deposits
10+
11+
// Account constructor initializes attributes
12+
public Account(int theAccountNumber, int thePIN,
13+
double theAvailableBalance, double theTotalBalance)
14+
{
15+
accountNumber = theAccountNumber;
16+
pin = thePIN;
17+
availableBalance = theAvailableBalance;
18+
totalBalance = theTotalBalance;
19+
} // end Account constructor
20+
21+
// determines whether a user-specific PIN matches PIN in Account
22+
public boolean validatePIN(int userPIN)
23+
{
24+
if(userPIN == pin)
25+
return true;
26+
else
27+
return false;
28+
} // end method validatePIN
29+
30+
// return available balance
31+
public double getAvailableBalance()
32+
{
33+
return availableBalance;
34+
} // end getAvailableBalance
35+
36+
// returns the total balance
37+
public double getTotalBalance()
38+
{
39+
return totalBalance;
40+
} // end method getTotalBalance
41+
42+
// credits an amount to the account
43+
public void credit(double amount)
44+
{
45+
totalBalance += amount; // add to total balance
46+
} // end method credit
47+
48+
// debits an amount from the account
49+
public void debit(double amount)
50+
{
51+
availableBalance -= amount; // subtract from available balance
52+
totalBalance -= amount; // subtract from total balance
53+
} // end method debit
54+
55+
// returns account number
56+
public int getAccountNumber()
57+
{
58+
return accountNumber;
59+
} // end method getAccountNumber
60+
} // end class Account
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// BankDatabase.java
2+
// Represents the bank account information database
3+
4+
public class BankDatabase
5+
{
6+
private Account[] accounts; // array of Accounts
7+
8+
// no-argument BankDatabase constructor initializes accounts
9+
public BankDatabase()
10+
{
11+
accounts = new Account[2]; // just 2 accounts for testing
12+
accounts[0] = new Account(12345, 54321, 1000.0, 1200.0);
13+
accounts[1] = new Account(98765, 56789, 200.0, 200.0);
14+
} // end no-argumnent BankDatabase constructor
15+
16+
// retrieve Account object containing specified account number
17+
private Account getAccount(int accountNumber)
18+
{
19+
// loop through accounts searching for matching account number
20+
for (Account currentAccount : accounts)
21+
{
22+
// return current account if match found
23+
if (currentAccount.getAccountNumber() == accountNumber)
24+
return currentAccount;
25+
// end for
26+
}
27+
28+
return null; // if no matching account was found, return null
29+
} // end method getAccount
30+
31+
// determine whether user-specified account number and PIN match
32+
// those of an account in the database
33+
public boolean authenticateUser(int userAccountNumber, int userPIN)
34+
{
35+
// attempt to retrieve the account with the account number
36+
Account userAccount = getAccount(userAccountNumber);
37+
38+
// if account exists, return result of Account method validatePIN
39+
if(userAccount != null)
40+
return userAccount.validatePIN(userPIN);
41+
else
42+
return false; // account number not found, so return false
43+
} // end method authenticateUser
44+
45+
// return available balance of Account with specified account number
46+
public double getAvailableBalance(int userAccountNumber)
47+
{
48+
return getAccount(userAccountNumber).getAvailableBalance();
49+
} // end method getAvailableBalance
50+
51+
// return total balance of Account with specified account number
52+
public double getTotalBalance(int userAccountNumber)
53+
{
54+
return getAccount(userAccountNumber).getTotalBalance();
55+
} // end method getTotalBalance
56+
57+
// credit an amount to Account with specified account number
58+
public void credit(int userAccountNumber, double amount)
59+
{
60+
getAccount(userAccountNumber).credit(amount);
61+
} // end method credit
62+
63+
// debit an amount from Account with specified account number
64+
public void debit(int userAccountNumber, double amount)
65+
{
66+
getAccount(userAccountNumber).debit(amount);
67+
}
68+
69+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// CashDispenser.java
2+
// Represents the cash dispenser of the ATM
3+
4+
public class CashDispenser
5+
{
6+
// the default initial number of bills in the cash dispenser
7+
private final static int INITIAL_COUNT = 500;
8+
private int count; // number of $20 bills remaining.
9+
10+
// no-argument CashDispenser
11+
public CashDispenser()
12+
{
13+
count = INITIAL_COUNT; // set count attribute to default
14+
} // end CashDispenser constructor
15+
16+
// simulates dispensing of specified amount of cash
17+
public void dispenseCash(int amount)
18+
{
19+
int billsRequired = amount / 20; // number of $20 bills required
20+
count -= billsRequired; // update the count of bills
21+
} // end method dispenseCash
22+
23+
// indicates whether cash dispenser can dispense desired amount
24+
public boolean isSufficientCashAvailable(int amount)
25+
{
26+
int billsRequired = amount / 20; // number of $20 bills required
27+
28+
if (count >= billsRequired)
29+
return true; // enough bills available
30+
else
31+
return false; // not enough bills available
32+
} // end method isSufficientCashAvailable
33+
} // end class CashDispenser
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// DepositSlot.java
2+
// Represents the deposit slot of the ATM
3+
4+
public class DepositSlot
5+
{
6+
// indicates whether envelope was recieved (always returns true,
7+
// because this is only a software simulation of a real deposit slot)
8+
public boolean isEnvelopeRecieved()
9+
{
10+
return true; // deposit envelope was received
11+
} // end method isEvelopeRecieved
12+
} // end class DepositSlot

13 ATM Case Study/src/Keypad.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Keypad.java
2+
// Represents the keypad of the ATM
3+
import java.util.Scanner; // program uses Scanner to obtain user input
4+
5+
public class Keypad
6+
{
7+
private Scanner input; // reads data from the command line
8+
9+
// no-argument constructor initializes the Scanner
10+
public Keypad()
11+
{
12+
input = new Scanner(System.in);
13+
} // end no-argument Keypad constructor
14+
15+
// return an integer value entered by user
16+
public int getInput()
17+
{
18+
return input.nextInt();
19+
} // end method getInput
20+
} // end class Keypad

13 ATM Case Study/src/Screen.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Screen.java
2+
// Represents the screen of the ATM
3+
4+
public class Screen
5+
{
6+
// display a message without a carriage return
7+
public void diplayMessage(String message)
8+
{
9+
System.out.print(message);
10+
} // end method displayMessage
11+
12+
// display a message with a carriage return
13+
public void displayMessageLine(String message)
14+
{
15+
System.out.println(message);
16+
} // end method displayMessageLine
17+
18+
// display a dollar amount
19+
public void displayDollarAmount(double amount)
20+
{
21+
System.out.printf("$%,.2f", amount);
22+
} // end method displayDollarAmount
23+
} // end class Screen

0 commit comments

Comments
 (0)