Object Oriented Design 8

Convert Sequence Diagram into CodeI showed you how to turn your program idea into a Use Case. Then we turned the Use Case into a Domain Model. Then I showed you how to turn the Use Case into a Sequence Diagram. Then we made the Sequence Diagram into a Class Diagram. Now I’m going to show you how to convert the Sequence Diagram into working code.

I make part of the code in this video. The rest will be made in part 9 of my Object Oriented Design Tutorial. All of the code follows the video to help you learn. To get the other diagrams, just click the associated links above.

If you like videos like this, tell Google

Sharing is always welcome

Code From the Video

TestATM.JAVA

public class TestATM {
	
	public static void main(String[] args){
		
		// Create a new account for a customer
		
		Account theAccount = new Account(10, "Derek Banas", "C", 10000.00, 1234);
		
		// Generate an ATM card for the new customer
		
		Card customersATMCard = new Card(theAccount.getPIN(), theAccount.getStripNumber());
		
		// Create the Customer object and give it an ATM card
		
		Customer theCustomer = new Customer(customersATMCard);
		
		// Create the BankComputer that will hold the account
		
		BankComputer xyzBankComputer = new BankComputer("XYZ Bank");
		
		// Add the customer account to the bank computer
		
		xyzBankComputer.addAcctToBank(theAccount);
		
		// Create a network that will hold every bank computer and all
		// the accounts they hold
		
		BankNetwork theBankNetwork = new BankNetwork();
		
		// Add the BankComputer to the BankNetwork
		
		theBankNetwork.addBankToNetwork(xyzBankComputer);
		
		// Create the ATM machine that will allow access to all
		// BankComputers in the BankNetwork
		
		ATM mainStreetATM = new ATM(theBankNetwork);
		
		// Check with the ATM to find out if the card is readable
		
		mainStreetATM.isStripReadable(customersATMCard);
		
		// Check with the BankNetwork to see if the cards member
		// bank is part of the network
		
		mainStreetATM.isATMCardsBankInNetwork(customersATMCard);
		
		// customersATMCard and the PIN are verified by the ATM
		
		mainStreetATM.insertPIN(customersATMCard, 1234);
		
		// Asks the customer whether to withdrawal money from checking or savings
		
		mainStreetATM.pickAcctToAccess();
		
		// Asks the customer how much money they want to withdrawal
		
		mainStreetATM.amountToWithdrawal();
		
		// Provides information on the transaction
		
		mainStreetATM.getTransactionInfo();
		
	}
	


}

Account.java

public class Account {
	
	private int bankID;
	private int acctNumber;
	private String customerName;
	private String acctType;
	private double acctBalance;
	private int pin;
	private int stripNumber;
	
	static int accountNumberIncrementor = 100000;
	
	Account(int bankID, String customerName, String acctType, double acctBalance, int pin){
		
		this.bankID = bankID;
		this.customerName = customerName;
		this.acctType = acctType;
		this.acctBalance = acctBalance;
		this.pin = pin;
		
		stripNumber = generateStripNumber(bankID);
		
		this.acctNumber = generateAccountNumber(stripNumber, acctType );
		
	}
	
	public int getBankID(){ return bankID; }
	
	public int getAcctNumber(){ return acctNumber; }
	
	public String getCustomerName(){ return customerName; }
	
	public String getAcctType(){ return acctType; }
	
	public double getAcctBalance(){ return acctBalance; }
	
	public void setAcctBalance(double newAcctBalance){
		
		this.acctBalance = newAcctBalance;
		
	}
	
	public int getPIN(){ return pin; }
	
	public int getStripNumber(){ return stripNumber; }
	
	// Generates strip numbers by adding the bankID to the front
	// of the automatically generated middle part of each account
	// number taken from accountNumberIncrementor
	
	public int generateStripNumber(int bankID){
		
		accountNumberIncrementor++;
		
		int newStripNumber = (bankID * 1000000) + accountNumberIncrementor;
		
		return newStripNumber;
		
	}
	
	public int generateAccountNumber(int stripNumber, String acctType){
		
		if((acctType.startsWith("s")) || (acctType.startsWith("S"))){
			
			// Savings is stripNumber with a 1 at the end
			
			acctNumber = (stripNumber * 10) + 1;
			
		} else {
			
			// Checking is stripNumber with a 2 at the end
			
			acctNumber = (stripNumber * 10) + 2;
			
		}
		
		return acctNumber;
		
	}

}

Card.java

// Used as the fictional card passed in to check 
// if it is valid

public class Card {
	
	private int pin;
	private int stripNumber;
	
	// Used to create a temp card to check for
	// a valid card strip number in a bank database
		
	Card(int stripNumber){
			
		this.stripNumber = stripNumber;
			
	}
	
	// Used to create a temp card to check for
	// a valid card strip number and PIN in a 
	// bank database
	
	Card(int pin, int stripNumber){
		
		this.pin = pin;
		this.stripNumber = stripNumber;
		
	}
	
	public int getPIN(){ return pin; }
	public int getStripNumber(){ return stripNumber; }
	
	public void setPIN(int pin){ 
		
		this.pin = pin; 
		
	}
	
	public void setStripNumber(int stripNumber){ 
		
		this.stripNumber = stripNumber; 
		
	}

}

Customer.java

public class Customer {
	
	private Card customersATMCard;

	// Replaces public Card insertATMCard()

	Customer(Card newATMCard){
		
		customersATMCard = newATMCard; 
		
	}
	
	public Card getATMCard() { return customersATMCard; }

}

Transaction.java

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Transaction {
	
	// Formats the date of the transaction
	
	DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

	private int bankID;
	private Date currentDateTime;
	private String customerName;
	private double acctBalance;
	private int withdrawalAmt;
	private int accountNumberUsed;
	private int pin;
	private int stripNumber;
	private int acctToWithdrawalFrom;
	
	// I decided to add verification that the card is allowed
	// to access the bank accounts
	private boolean didCardVerify = false;
	
	public int getBankID() { return bankID; }
	public String getCustomerName() { return customerName; }
	
	public void setCustomerName(String customerName){
		
		this.customerName = customerName;
		
	}
	
	// returns the current date and time as a String
	public String getCurrentDateTime() { return dateFormat.format(currentDateTime); }
	
	public int getPIN(){ return pin; }
	public void setPIN(int pin){
		
		this.pin = pin;
		
	}
	
	public void setStripNumber(int stripNumber){
		
		this.stripNumber = stripNumber;
		
	}
	
	public int getStripNumber(){ return stripNumber; }
	
	// Sets that the card has a valid stripNumber
	
	public void setDidTheCardVerify(boolean cardVerified){
		
		didCardVerify = (cardVerified)?true:false;
		
	}
	
	public boolean getDidCardVerify(){ return didCardVerify; }
	
	// Gets sent a 1 for savings or a 2 for checking
	// That number is added to the end of the accountNumberUsed
	
	public void setAcctToWithdrawalFrom(int acctToWithdrawalFrom){
		
		this.acctToWithdrawalFrom = acctToWithdrawalFrom;
		
		this.accountNumberUsed = (stripNumber * 10) + acctToWithdrawalFrom;
		
	}
	
	public int getAcctToWithdrawalFrom(){ return acctToWithdrawalFrom; }
	
	public void setWithdrawalAmt(int withdrawalAmt){
		
		this.withdrawalAmt = withdrawalAmt;
		
	}
	
	public int getWithdrawalAmt(){ return withdrawalAmt; }

	public int getAccountNumberUsed() { return accountNumberUsed; }
	
	public void setAcctBalance(double newAcctBalance){
		
		this.acctBalance = newAcctBalance;
		
	}
	
	public double getAcctBalance(){ return acctBalance; }

	
	Transaction(int stripNumber){
		
		bankID = BankNetwork.getFirstTwoDigits(stripNumber);
		
		currentDateTime = new Date();
		
		// Holds the account number minus either 1 for savings, or
		// 2 for checking at the end until that is added by 
		// setAccountNumberUsed() below
		
		accountNumberUsed = stripNumber;
		
	}
	
}

7 Responses to “Object Oriented Design 8”

  1. Chris says:

    Great tutorials! I’m porting this to C# for practice and noticed you don’t have BankComputer, BankNetwork, or ATM classes defined in the example code here.

  2. mare says:

    Hi Derek,

    what if we have case where two or more classes have reference point to same class? For example, BankComputer and Customer might have reference to Account class, or any other class. Do we need to avoid this, beacuse from BankComputer or Customer we can access to Account?

  3. mare says:

    Sorry Derek but I don’t understand.

Leave a Reply

Your email address will not be published.

Google+