Storing Data in Objects

Similar documents
Abstract classes and Interfaces

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

Expressions and Casting

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

CS112 Lecture: Defining Instantiable Classes

public class Account { private int id; private static int nextaccountid = 0; private String name; private double balance;

Software Design and Analysis for Engineers

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Chapter 4 Defining Classes I

Lesson 18: There is Only One Line Passing Through a Given Point with a Given

IntForex demonstration bank account list IntForex demonstration rates IntForex demonstration rates... 22

ATM Use Cases. ID: CIS Title: Check Balance Description: Customer aims to know the balance in his/her account

CPS122 Lecture: Defining a Class

Mth 60 Module 2 Section Signed Numbers All numbers,, and

COMP-202 Unit 8: Defining Your Own Classes. CONTENTS: Class Definitions Attributes Methods and Constructors Access Modifiers and Encapsulation

Last lecture. Lecture 9. in a nutshell. in a nutshell 2. Example of encapsulation. Example of encapsulation. Class test. Procedural Programming

Text Alerts. Current accounts

Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007

A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency Lecture 4 Shared-Memory Concurrency & Mutual Exclusion

Chapter 3 - Simple JavaScript - Programming Basics. Lesson 1 - JavaScript: What is it and what does it look like?

Programming Abstractions

HOW CANADIANS BANK. Annual tracking study on Canadian attitudes and behaviours towards banking. Prepared for:

Defining Classes. Chap 1 introduced many concepts informally, in this chapter we will be more formal in defining

CSE332: Data Abstractions Lecture 22: Shared-Memory Concurrency and Mutual Exclusion. Tyler Robison Summer 2010

CSE 332: Data Structures & Parallelism Lecture 17: Shared-Memory Concurrency & Mutual Exclusion. Ruth Anderson Winter 2019

Ackworth Howard Church of England (VC) Junior and Infant School. Child-friendly GDPR privacy notice

CASHNet - Departmental Deposits Instructions for Departments

Defensive Programming

Unit Title: Objects in Visual Basic.NET. Software Development Unit 4. Objects in Visual Basic.NET

C# Programming for Developers Course Labs Contents

Mobile Banking Online Banking Features Dashboard Pending Transactions Account Export Bill Pay Online Bill Pay

VARIABLES AND TYPES CITS1001

Why Use Graphs? Test Grade. Time Sleeping (Hrs) Time Sleeping (Hrs) Test Grade

Software Engineering Testing and Debugging Testing

IT150/IT152 Concepts Summary Sheet

Programmazione di sistemi multicore

Encapsulation. Mason Vail Boise State University Computer Science

Phone Banking BSP. User Guide. Why wait in line? Bank by phone with BSP. Bank South Pacific.

Ticket Machine Project(s)

Lesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014

CSE332: Data Abstractions Lecture 19: Mutual Exclusion and Locking

PROGRAMMING EXAMPLE: Checking Account Balance

Problem 1: Get val soon! (15 marks)

Divisibility Rules and Their Explanations

King Abdulaziz University Faculty of Computing and Information Technology Computer Science Department

Equality for Abstract Data Types

Mutating Object State and Implementing Equality

Introduction to the coursework for CI228

The high-level language has a series of primitive (builtin) types that we use to signify what s in the memory

CS112 Lecture: Working with Numbers

Grade 6 Math Circles November 6 & Relations, Functions, and Morphisms

Tangent line problems

Keep Track of Your Passwords Easily

Linear Programming: A Geometric Approach

CPS122 Lecture: Course Intro; Introduction to Object-Orientation

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Header Description: This use case describes how the ATM user withdraws cash from the ATM.

Lecture 4 Searching Arrays

Lecture Notes on Linear Search

Software Engineering

UNDERSTANDING CLASS DEFINITIONS CITS1001

Classes and Methods: Classes

You can use your PIN to complete your purchases at point-of-sale and for ATM transactions.

7.17: Here is a typical Java solution:

Introduction to User Stories. CSCI 5828: Foundations of Software Engineering Lecture 05 09/09/2014

$ % $ % BankAccount. public class BankAccount {... private??? balance; } "commands, transformers, mutators! "abstract state! "queries, accessors!

Chapter 2.6: Testing and running a solution

PUP (People U Pay) FREQUENTLY ASKED QUESTIONS

Software with a Guarantee

Data types. CISC 1600/1610 Computer Science I. Class dog. Introducing: classes. Class syntax declaration. Class syntax function definitions 12/2/2015

Objectives: 1. Introduce the course. 2. Define programming 3. Introduce fundamental concepts of OO: object, class

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

The Money Is in the Square Cash Lets Users Funds to Friends

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

Handout 9: Imperative Programs and State

Our First Programs. Programs. Hello World 10/7/2013

These are notes for the third lecture; if statements and loops.

Inheritance in Ruby. You are familiar with the idea of inheritance and how to use this in programming.

1.7 Limit of a Function

Central Office Management System

Mr G s Java Jive. #11: Formatting Numbers

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

Object Oriented Methods with UML

How to Create a PayPal Account

EZ Parent Center Directions Parent Sign Up and Meal Preordering

Programs, Data, and Pretty Colors

Understanding class definitions

ATM Audio Users Guide

1. BlueJ bank example with subclasses of BankAccount 2. Transparency of UML diagram for BankAccount class hierarchy

by Pearson Education, Inc. All Rights Reserved. 2

A set, collection, group, or configuration containing members regarded as having certain attributes or traits in common.

FNB ewallet TERMS AND CONDITIONS

EXPRESSIONS AND ASSIGNMENT CITS1001

ECE 122. Engineering Problem Solving with Java

University of Hull Department of Computer Science. Wrestling with Python Week 04 Using Lists

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

Extending the Unit Converter

Data types. CISC 1600/1610 Computer Science I. Class dog. Introducing: classes. Class syntax declaration. Class syntax function definitions 4/19/2015

The Object-Oriented Design Process

Transcription:

Storing Data in Objects Rob Miles Department of Computer Science 28d 08120 Programming 2 Objects and Items I have said for some time that you use objects to represent things in your problem Objects equate to the nouns in a description of a system The Bank will contain a number of different Accounts Each of these will hold a lump of data which is important to your system The job of the programmer is to decide what goes into the objects and the things they need to do 2 Business Objects Objects which represent fundamental entities in the system that you are representing They are sometimes called Domain Objects Four business objects, and one that isn t: Customer Receipt Login Window Tree Photograph 3 1

Designing With Objects A Software Engineer will represent entities in the system with software objects The object holds data and it also does things for us At this level the design of a system is performed by deciding what objects are required, what data they must store and what they need to do for us This is really an extension of the metadata driven approach that we started with 4 Data In Objects Objects can contain data We can protect this by making it private If we make data private we need to provide public methods that allow the data to be used What the data is, and what you can do with it depends on the application you are building 5 Friendly Bank Requirements pay money into the account draw money out of the account find the balance change the name and address of the account holder get the name of an account holder get the address of an account holder change the overdraft limit on an account find the overdraft limit on an account 6 2

Account Class Data The Account class will need to hold data We have to go back to the requirements to decide what data is to be held Also need to determine the type of the data and how we are going to represent it Also need to consider data validation All this means the return of the metadata 7 An Account Class class Account public string AccountName; public string AccountNumber; public decimal AccountBalance; This is our first attempt at an Account business object It only contains part of the system information Other data fields will be required to complete the system We can create an array of these for the bank 8 Protecting Data in Classes Account Rob = new Account (); Rob.AccountBalance = 1000000; When we design our objects we need to consider how the data in them is going to be protected We want to avoid naughty programmers being able to make changes which would upset the state of our objects It is important that we control access to the variable that holds the accountbalance 9 3

An Account Class class Account private string accountname; private string accountnumber; private decimal accountbalance; The data fields in the Account class have now been made private This means that code which is not part of the Account class can t change these values This protection is enforced at compile time 10 Protecting Data in Classes Account Rob = new Account (); Rob.accountBalance = 1000000; When the accountbalance field is made private it is impossible for code outside the Account class to access that field The above code will not compile, unless the statements are part of a method inside the Account object This is how data inside an object is protected 11 Using Private Data Fields If a data field is private this means that only code running inside the object can access it This might make you think that it is impossible for code outside the object to make use of the data in that object However, this is not the case, as the creator of the object can provide methods that will allow external use of these fields There are two kinds of methods that you can create which are called accessors and mutators 12 4

Accessors and Mutators An accessor method provides read access to a data field inside the object (sometimes called a get method) Accessor methods return a value of some kind A mutator method allows you to change (mutate) a data field (sometimes called a set method) The mutator can be given a value that will be used to change the field It should make sure that the change is sensible Mutator methods return a result that indicates whether they worked or not 13 Accessors and Mutators for the Account Balance We can write some of these methods for the balance value of an account There are three things the system will need to do with the balance value Pay in funds Withdraw funds Find out the account balance Which are accessors and mutators? 14 Accessors and Mutators for the Account Balance We can write some of these methods for the balance value of an account There are three things the system will need to do with the balance value Pay in funds Withdraw funds Find out the account balance Which are accessors and mutators? 15 5

A PayInFunds Method class Account private decimal accountbalance; public void PayInFunds (decimal amount) accountbalance = accountbalance + amount; The PayInFunds method is given the amount of money to add to the balance It adds this to the accountbalance value in the account The method is public 16 Using the PayInFunds Method Account Rob = new Account (); Rob.PayInFunds(100); Rob.PayInFunds(50); Users of the Account class can call the PayInFunds method to pay money into the account Each time the method is called the accountbalance value in the account is updated 17 A GetBalance Method class Account private decimal accountbalance; public decimal GetBalance () return accountbalance ; The GetBalance method returns the value of the account balance Note that this does not provide access to the field, instead it provides a copy of the value 18 6

Testing All our behaviours need to be tested Particularly in terms of their error conditions Whenever we create a behaviour we should also create tests for that behaviour These tests should run completely automatically The program should test itself 19 Testing the Account object Account Rob = new Account (); Rob.PayInFunds(100); Rob.PayInFunds(50); if (Rob.GetBalance()!= 150 ) Console.WriteLine ( "Test Failed" ); As soon as we have some behaviours in our object we can write some tests for this object This one tests the PayInFunds and GetBalance methods 20 How Many Tests? The test seems quite sensible, but it is not sufficient to prove that the Account class works correctly If GetBalance always returned 150 this test would pass If every account was created with 150 pounds in it, and PayInFunds did nothing this test would pass The test doesn t test a very good range of input values for PayInFunds - paying in a value of less than 0 is possible Whenever we create a behaviour in a class we should consider how it will be tested In many projects the tests are written first 21 7

Another Example of Test We must provide a method that withdraws funds from the account You tell it how much you want, and it either withdraws the money or tells you it can t The method will be called by other parts of the bank system when the customer uses their account: When they use a cash machine to withdraw money When they withdraw money at a bank branch 22 WithdrawFunds Method The method is called to withdraw money from the account It is given the amount of money to be withdrawn It returns true or false: True means that the withdrawal succeeded and the cash can be released False means there was not enough money in the account 23 A Potential WithdrawFunds method public bool WithdrawFunds ( decimal amount ) if ( accountbalance < amount ) return false ; accountbalance = accountbalance - amount ; return true; A programmer has written this WithDrawFunds It seems sensible, but is it good enough? 24 8

Testing WithdrawFunds Account Rob = new Account (); Rob.PayInFunds(100); if ( Rob.WithdrawFunds(60) == false ) Console.WriteLine ( "Withdraw Test Failed" ); if ( Rob.GetBalance()!= 40 ) Console.WriteLine ( "Balance Test Failed" ); This code creates an Account, pays in some money and then withdraws some If the withdraw fails, or the incorrect amount is left in the account the program prints error messages 25 Other Tests This is not a very good set of tests There are lots of other ones that will be required Withdrawing an amount of 0 Withdrawing a negative amount Withdrawing exactly the amount of money in the account Making sure that the amount in the account only goes down when the withdraw succeeded This means that we will write more code in the tests than we wrote to implement the behaviour This is perfectly normal 26 Preparing for the Worst It should not be possible for anyone (including other programmers) to be able to upset our bank account: Never have a balance lower than the overdraft This is called defensive programming or secure programming Once we have our member information we now need to make sure that we look after it 27 9

Test Driven Development In Test Driven Development the tests are written before the methods themselves The methods are initially empty Then, during the development the methods are filled in so that the tests are passed The tests are run regularly during the development, and particularly after a bug has been fixed (a bug fix usually adds two bugs) You should remember to charge the customer for this work too! 28 Designing with Objects A class can contain data fields which it manages The data fields can be made private to protect them from code outside the class The programmer then creates methods that provide read (access) and write (mutate) behaviours as required Every behaviour must have tests associated with it to prove that the behaviour works correctly 29 10