0% found this document useful (0 votes)
4 views29 pages

Class Design Example (Account)

The document outlines the process of designing and implementing an object-oriented program for managing bank accounts. It covers requirement analysis, class design, and method implementation, including constructors, instance variables, and behaviors such as deposits and withdrawals. The document provides code examples for creating and testing an Account class in Java.

Uploaded by

nooripad95
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views29 pages

Class Design Example (Account)

The document outlines the process of designing and implementing an object-oriented program for managing bank accounts. It covers requirement analysis, class design, and method implementation, including constructors, instance variables, and behaviors such as deposits and withdrawals. The document provides code examples for creating and testing an Account class in Java.

Uploaded by

nooripad95
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

instructor : Sumaya Alkhatib

OOP A0312201
 Requirement Analysis.
 Design.
 Implementation .
 Testing.

OOP A0312201
 Think about the classes you need before
you start writing.
 This makes programming easier and your
programs will have fewer bugs.
 Object oriented design means deciding
what classes you need, what data the
objects hold, and how the objects will
behave.

OOP A0312201
To start, decide what account objects do
and what data they hold.
This process is called requirements
analysis and is the most important
phase of writing big programs. This
makes programming easier and your
programs will have fewer bugs.

OOP A0312201
 Think of three data items that will be
part of account.
 Answer:
 The following seem reasonable to me.
You might have thought of others.
 Account number
 Name of account holder
 Current balance

OOP A0312201
 A real account would contain a great deal more

information, but for this example, the previous is

enough.

 Next, think about what methods are needed.

 That is, what behavior does account have?

OOP A0312201
 Think of three behaviors account
should have.
 Answer:
 The following seem reasonable to me:
 Get the current balance
 Accept a deposit
 Process Withdrawal
 You might have thought of others, but
the above will be enough for now.
OOP A0312201
 The constructor creates a new account and initializes it
with its account number, the account holder's name, and
the starting balance.
 Now we have a fair idea about what the class looks like:
 Data
 Account number
 Name of account holder
 Current balance
 Constructor
 Create the object; initialize the three data items
 Methods
 Get the current balance
 Accept a deposit
 Process Withdrawal
OOP A0312201
 Is there enough information to start
writing the program?
 Answer:
 Not quite. We need to know more
about how deposits and Withdrawal
are processed.

OOP A0312201
 The requirements describe what each method does.
 The method to accept a deposit adds an amount to the
current balance.
 It is time to think about design. The first thing to do is
sketch out the class definition:
class Account
{
instance variables declarations
constructor
methods
}
 Although Java syntax does not require it, it is helpful to
put different parts of a class definition in different
sections, as above.
OOP A0312201
 Think of a reasonable variable name (identifier) and data
type for each variable:

OOP A0312201
 The account number should be a String because it is not
expected to take part in arithmetic operations .
Sometimes account numbers contain dashes or other
non-digit characters.
 So far, the Account class looks like this:
class Account
{ // instance variables
String accountNumber;
String accountHolder;
double balance;
constructor
methods
}

OOP A0312201
 What must the constructor of
Account be named?
 Answer:
The constructor must be named
Account, the same as the name of
the class.

OOP A0312201
 The constructor has the same name as the class, and looks like
this:
Account( parameter-list )
{ initialize the data }
 The constructor is used with the new operator to create a new
account object.
 It then initializes the account number, the account holder's
name, and starting balance. Actual parameters are supplied to
the constructor when it is used, such as in the following:
Account account1= new Account( "123", "Bob", 100 ) ;
 This statement creates a new Account object by calling the
constructor.
 The constructor will initialize the object's accountNumber to
"123", its accountHolder to "Bob", and its balance to 100.

OOP A0312201
class Account
{ // instance variables
String accountNumber;
String accountHolder;
double balance;
//constructor
Account( String accNumber, String holder, double start )
{
accountNumber = accNumber ;
accountHolder = holder;
balance = start ;
}
// methods
}

OOP A0312201
Class AccountTester
{
public static void main( String[] args )
{
Account account1 = new Account( "123", "Bob", 100 );
[Link]( [Link] + " " +
[Link] + " " + [Link] );
}
}
The output will be:
123 Bob 100
The expression [Link] means use the variable
accountNumber that is part of the object referred to by
account1.

OOP A0312201
1. Get the current balance
2. Accept a deposit
3. Process Withdrawal
 Remember the syntax for method
definition:
returnType methodName ( parameterList )
{ statementList }

OOP A0312201
 Implementing the Method
class Account
{
// instance variables
String accountNumber;
String accountHolder;
double balance;
//constructor . . . .
// methods
double getBalance()
{
return balance ;
}
}
OOP A0312201
Class AccountTester {
public static void main( String[] args ) {
Account account1 = new Account( "123", "Bob", 100 );
Account account2 = new Account( "92a-44-33", "Kathy
Emerson", 0 );
[Link]( [Link] + " " +
[Link] + " " + [Link]() );
[Link]( [Link] + " " +
[Link] + " " + [Link]() );
}
}
Output
123 Bob 100
92a-44-33 Kathy Emerson 0

OOP A0312201
 The method will have one parameter, the number be
added to the balance
 The method will not return a value, so its return type will
be void.
class Account
{
// instance variables
String accountNumber;
String accountHolder;
double balance;
//constructor . . . .
// methods . . . .
void acceptDeposit ( double amount )
{ balance = balance + amount ; }
} OOP A0312201
 The method will have one parameter,
the number be subtracted from the
balance
 If the amount > current balance the
error message will print.
 Otherwise The amount subtracted
from the balance.
 The return type is void.
OOP A0312201
void withdrawal ( double amount )
{
if ( amount > balance )
[Link] (“error the
amount is greater than balance”);
else
balance = balance - amount;
}
OOP A0312201
Class Account {
// instance variables
String accountNumber;
String accountHolder;
double balance;
//constructor
Account( String accNumber, String holder, double start ){
accountNumber = accNumber ;
accountHolder = holder ;
balance = start ;
}
// methods
double getBalance() {
return balance ;
}
void acceptDeposit( double amount ){
balance = balance + amount ;
}
void withdrawal ( double amount ) {
if ( amount>balance )
[Link] (“error the amount is greater than balance”);
else
balance = balance - amount;
}
}

OOP A0312201
class AccountTester {
public static void main( String[] args ) {
Account account1 = new Account( "123", "Bob", 100 );
[Link]( [Link]() );
account1. acceptDeposit( 2000 );
account1. withdrawal( 1500 );
[Link]( [Link]() );

}
}
OOP A0312201
void display() {
[Link]( accountNumber +
"\t" + accountHolder + "\t" + balance );
}
(The string "\t" tells the compiler that
you want the tab character.)

OOP A0312201
 In fact, the display() method can not even
see the parameters of the constructor.
 Another way of saying this is that the scope
of the parameters is limited to the body of the
method.
 The compiler will complain that accNumber,
holder, and start are "undefined variables"
because they are used outside of their
scope.
OOP A0312201
 When the display() has been defined, the testing
program can be more easily written:
Class AccountTester {
public static void main( String[] args ) {
Account account1 = new Account( "123", "Bob", 100 );
[Link]() ;
account1. acceptDeposit( 2000 );
[Link]() ;
account1. withdrawal( 1500 );
[Link]() ;
}
}
OOP A0312201
Create a new object, account2
The account number is "007"
The account holder is Bond. James Bond.
The account amount is $500
Display account2.
Deposit $700 in account2.
Process a $100 check.
Display account2.
OOP A0312201
Class AccountTester{
public static void main(String[] args){
Account account1 = new Account("123", "Bob", 100);
[Link]() ;
account1. acceptDeposit(2000);
[Link]() ;
account1. withdrawal(1500);
[Link]() ;
Account account2 = new Account("007", "James Bond", 50000);
[Link]() ;
account2. acceptDeposit(70000);
[Link]() ;
account2. withdrawal(10000);
[Link]() ;
}
}

OOP A0312201

You might also like