Object Oriented Design 9

Object Oriented DesignWelcome to part 9 of my Object Oriented Design tutorial! This all started back in part 3 in my UML Use Case Tutorial.

Here I’ll finish turning sequence diagrams into code. If you get good at this process you’ll be able to make any program no matter how complicated it may be.

All of the code from this and the last tutorial can be found below to help you learn. In the tutorials that follow I’ll answer all of the design pattern tutorials I’ve been receiving.

If you like tutorials like this, it helps to tell Google so more people find it

Share with anyone you know that likes Object Oriented Design

Turning Sequence Diagrams into Code

The additional code used can be found in Object Oriented Design 8

 ATM.java

import java.util.Scanner;

public class ATM {
	
	private Transaction theTransaction = null;
	private BankNetwork networkOfBanksOnATM = null;
	
	// Used to get input from the customer
	
	static Scanner userInput = new Scanner(System.in);
	
	ATM(BankNetwork networkOfBanksOnATM){
		
		this.networkOfBanksOnATM = networkOfBanksOnATM;
		
	}
	
	public boolean isStripReadable(Card theCard){
		
		// Returns the number of digits in stripNumber
		
		int numberOfDigitsInStrip = (int) (Math.log10(theCard.getStripNumber())+1);
		
		theTransaction = new Transaction(theCard.getStripNumber());
		
		if(numberOfDigitsInStrip == 10){ 
			
			return true; 
			
		} else {
			
			return false;	
		}
		
	}
	
	// Sends the card inserted to BankNetwork to see if there
	// are any matching bankIDs
	
	public boolean isATMCardsBankInNetwork(Card theCard){
		
		if(networkOfBanksOnATM.isATMCardsBankInNetwork(theCard)){
			
			theTransaction.setStripNumber(theCard.getStripNumber());
			
			// Sets that the stripNumber has been verified so it is ok 
			// to access account info to verify pin later
			
			theTransaction.setDidTheCardVerify(true);
			
			return true;
			
		} else {
		
		return false;
		
		}
		
	}
	
	public boolean insertPIN(Card theCard, int pin){
		
		// Finds the number of digits in the PIN
		
		int numberOfDigitsInPIN = (int) (Math.log10(pin)+1);
		
		if(numberOfDigitsInPIN != 4){
			
			System.out.println("You have to enter 4 digits for a PIN");
			
			return false;
			
		} else {
			
			theTransaction.setPIN(pin);
			
			
			// I decided to pass the transaction instead of the card
			// like I did in the sequence diagram
			
			networkOfBanksOnATM.verifyThePIN(theTransaction);
			
		}
		
		return false;
		
	}
	
	public void pickAcctToAccess(){
		
		System.out.println("What Account do you Want to Withdrawal From ");
		System.out.println("( 1 - Savings, 2 - Checking ) ");
		
		if (userInput.hasNextInt()){
			
			int numberEntered = userInput.nextInt();
			
			theTransaction.setAcctToWithdrawalFrom(numberEntered);
			
		} else { 
			
			System.out.println("You Entered an Invalid Option");
			
		}

	}
	
	public void amountToWithdrawal(){
		
		System.out.println("How much do you want to withdrawal ");
		System.out.println("( Increments of 10 ) ");
		
		if (userInput.hasNextInt()){
			
			int numberEntered = userInput.nextInt();
			
			theTransaction.setWithdrawalAmt(numberEntered);
			
			networkOfBanksOnATM.requestWithdrawalAmt(theTransaction);
			
		} else { 
			
			System.out.println("You Entered an Invalid Amount");
			
		}
		
	}
	
	public void getTransactionInfo(){
		
		System.out.println("Thank you " + theTransaction.getCustomerName() + " for using the XYZ Bank ATM\n");
		
		System.out.println("Date / Time of Transaction: " + theTransaction.getCurrentDateTime());
		System.out.println("\nTransaction");
		
		System.out.print("Removed $" + theTransaction.getWithdrawalAmt() + " from your ");
		
		if(theTransaction.getAcctToWithdrawalFrom() == 1){
			
			System.out.println("Savings Account.\nYour current balance is " + theTransaction.getAcctBalance());
			
		} else {
			
			System.out.println("Checking Account.\nYour current balance is " + theTransaction.getAcctBalance());
			
		}
		
	}

}

BankNetwork.java

import java.util.ArrayList;

public class BankNetwork {
	
	private ArrayList<BankComputer> banksInDatabase = new ArrayList<BankComputer>();
	
	public void addBankToNetwork(BankComputer newBank){
		
		this.banksInDatabase.add(newBank);
		
	}
	
	public boolean isATMCardsBankInNetwork(Card theCard){
		
		Boolean cardVerification = false;
		
		int cardBankID = getFirstTwoDigits(theCard.getStripNumber());
		
		for(BankComputer bank : banksInDatabase){
			
			if(bank.getBankID() == cardBankID){
				
				cardVerification = true;
				
			}
			
		}
		
		return cardVerification;
		
	}
	
	// Returns the first 2 digits from the number passed to it
	
	static int getFirstTwoDigits(int stripNumber){
		
		String stringOfStripNumber = Integer.toString(stripNumber);
		
		// Get the first 2 numbers from the stripNumber and save them as an int
		
		int bankIDFromStrip = Integer.parseInt(stringOfStripNumber.substring(0, 2));
		
		return bankIDFromStrip;
		
	}
	
	public boolean verifyThePIN(Transaction theTransaction){
		
		boolean cardVerification = false;
		
		if(theTransaction.getDidCardVerify()){
			
			for(BankComputer bank : banksInDatabase){
				
				if(bank.getBankID() == theTransaction.getBankID()){
					
					bank.verifyThePIN(theTransaction);
					
					cardVerification = true;
					
				} else {
					
					System.out.println("The card Bank ID doesn't match any in the system");
					
				}
				
			}
		}
		
		return cardVerification;
		
	}
	
	public void requestWithdrawalAmt(Transaction theTransaction){
		
		if(theTransaction.getDidCardVerify()){
			
			for(BankComputer bank : banksInDatabase){
				
				if(bank.getBankID() == theTransaction.getBankID()){
					
					bank.requestWithdrawalAmt(theTransaction);
					
				}
				
			}
			
		} else {
		
			System.out.println("An Error Occurred During the Withdrawal");
			
		}
			
		
	}

}

BankComputer.java

import java.util.ArrayList;

public class BankComputer {
	
	private int bankID = 9;
	private String bankName ="";
	
	private ArrayList<Account> accounts = new ArrayList<Account>();
	
	public static int numberOfBanks = 0;
	
	BankComputer(String newBankName){
		
		bankName = newBankName;
		
		numberOfBanks++;
		
		this.bankID+= numberOfBanks;
		
	}
	
	public void addAcctToBank(Account newAccount){
		
		this.accounts.add(newAccount);
		
	}
	
	public int getBankID(){ return bankID; }
	
	// Verifies that a card with the strip number is in a bank
	
	public Boolean verifyTheStripNumber(Card theCard){
		
		Boolean cardVerification = false;
		
		for(Account account : accounts){
			
			if(account.getStripNumber() == theCard.getStripNumber()){
				
				cardVerification = true;
				
			}
			
		}
		
		return cardVerification;
		
	}
	
	// Verifies that a card with the strip number and PIN is in a bank
	
	public Boolean verifyThePIN(Transaction theTransaction){
		
		Boolean cardVerification = false;
		
		if(theTransaction.getDidCardVerify()){
		
			for(Account account : accounts){
			
				if((account.getPIN() == theTransaction.getPIN()) && (account.getStripNumber() == theTransaction.getStripNumber())){
				
				cardVerification = true;
				
				theTransaction.setCustomerName(account.getCustomerName());
				
				}
			
			}
			
		}
		
		return cardVerification;
		
	}
	
	public void requestWithdrawalAmt(Transaction theTransaction){
		
		for(Account account : accounts){
			
			if(account.getAcctNumber() == theTransaction.getAccountNumberUsed()){
				
				if(account.getAcctBalance() >= theTransaction.getWithdrawalAmt()){
					
					double newAcctBalance = account.getAcctBalance() - theTransaction.getWithdrawalAmt();
					
					account.setAcctBalance(newAcctBalance);
					
					theTransaction.setAcctBalance(newAcctBalance);
					
				} else {
					
					System.out.println("You can't withdrawal that much money");
					
				}
				
			}
			
		}
		
	}
	
	

}

4 Responses to “Object Oriented Design 9”

  1. salvadore says:

    Hi Derek, trying to watch part 9 of this tutorial, and the video seems to be broken somehow, on youtube it stops after only a minute, then on your site it stops after about 2 minutes.

    Just a heads up, and I would love to continue watching this tutorial.

    Thanks

  2. marko says:

    Hi Derek. As you said, this is important topic. I dont want to move on Android tutorials before watching OOD tuts. I can understand how this ATM works, but how did you decide when and where to make Transaction object? You made it when isStripReadable is called. Thank you Derek, you are great.

    • Derek Banas says:

      Hi Marko, It is hard to really say how I decided to set up the system. I tried to simulate the process of creating a design over the course of this entire tutorial series. I optimized and made design decisions along the way. I also did most of this largely out of my head while recording to simulate the decision making process. The final results are based on that process. I hope that makes sense :)

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Google+