0% found this document useful (0 votes)
183 views

Simple Bank Project Part 1 C Sharp

This document provides instructions for creating a Person and Address class as part of a banking project using C#. It describes: - Creating a People project with Person and Address classes - Implementing the Address class with properties and a ToString method - Implementing the Person class with name, yearOfBirth and address properties, constructors, and a ToString method. - Example test methods are provided to instantiate Person objects and output their details.

Uploaded by

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

Simple Bank Project Part 1 C Sharp

This document provides instructions for creating a Person and Address class as part of a banking project using C#. It describes: - Creating a People project with Person and Address classes - Implementing the Address class with properties and a ToString method - Implementing the Person class with name, yearOfBirth and address properties, constructors, and a ToString method. - Example test methods are provided to instantiate Person objects and output their details.

Uploaded by

Joel Takoulo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

Object Orientated Programming using Visual C#

Simple Bank Project part 1


Over the next few weeks we are going to build up a Simple Banking system that will eventually have a number of User Interface programs linking into it. The aim is to see how you should implement systems from U ! models. "emember# do not try to add more members into the system $ you need to follow the design as it evolves.

Design
In this part of the exercise you are going to implement a generic %erson class in a namespace called %eople. &ou will later import the classes developed here into a Simple Bank pro'ect.

(otice how strictly you do not need to show the attribute address as the association )has* shows that this is needed. The example at the moment illustrates +n association between %erson and +ddress Implementing public Properties.

&ou will also use the %rogram class to write tester applications for the classes as you develop them.

Step 1: Creating your People Project


,. -reate a new pro'ect in .isual -/ called %eople 0. +dd a new class to your file called %erson 1right click on the pro'ect %eople in the solution explorer and add class2 $ make sure you set the scope operator of the class to public 1the default is private32
namespace People { public class Person { } }

Object Orientated Programming using Visual C#


4. +dd a new class to your file called +ddress 1right click on the pro'ect %eople in the solution explorer and add class2 $ make sure you set the scope operator of the class to public 1the default is private32
namespace People { public class Address { } }

&our solution explorer should now look something like the example below

Remember to save the project to a flash drive!

Step 2: Coding the Address Class


!ooking at the class diagram5 you can see that the %erson class uses the +ddress class5 therefore you need to implement the +ddress class first.

Instance variables
+dd in three instance variables as shown below.

namespace People { public class Address { //instance variables string street; string town; string postcode;

Object Orientated Programming using Visual C#

Public Properties
The instance variables have private scope and hence can only be accessed by members within the class. 6owever in this scenario5 it would make sense to allow public access but this would compromise the principle of encapsulation. &ou can preserve the principle of encapsulation by adding public accessor methods as shown below.

/**************************** * Public Properties * **************************/ public string Street { get { return street; } set { street = value; } } public string Town { //complete this } public string Postcode { //complete this }

Override the ToString method


The ToString method should be overridden as shown below

/**************************************** * overridden ToString method **************************************/ public override string ToString( { string strout; strout = Street ! "#n" ! Town ! "#n" ! Postcode ! "#n"; return strout; }

Object Orientated Programming using Visual C#

onstructors
To help illustrate how public properties can be used5 the +ddress class will 'ust have the default constructor and hence you do not need to add any overloaded constructors.

Test the !ddress

lass

Test the program by adding the following code to the class program 1file program.cs2
static void $ain(string%& args { //'( calling another static method //it is easier to code a series o) test programs TestAddress( ; } static void TestAddress( { Address anaddress = new Address( ; *onsole+,rite-ine(".mpt( anaddress contains " ! anaddress ; //now use the public properties anaddress+Street = "/00 bedroc1 street"; anaddress+Town = "'edroc1"; anaddress+Postcode = "''/ 22/3"; //4se ToString to displa( address *onsole+,rite-ine("using ToString to displa( address" ; *onsole+,rite-ine(anaddress ; //now use properties to displa( constituent parts *onsole+,rite-ine("using properties to displa( address" ; *onsole+,rite-ine("Street5 " ! anaddress+Street ; *onsole+,rite-ine("Town5 " ! anaddress+Town ; *onsole+,rite-ine("postcode5 " ! anaddress+Postcode ; *onsole+,rite-ine("#npress return to continue" ; *onsole+6ead-ine( ; }

(otice how you can use the properties via the period.

Object Orientated Programming using Visual C#

Step 3: Coding the Person Class


6aving coded the +ddress class you can now start coding the %erson class in the file %erson.cs.

Instance variables
&ou should add in the instance variables shown in the class diagram as shown below. public class Person { //instance variables //there should be two more7 8 add them in Address address; }

Public Properties
+dd in t"o public properties called #ame 1for field name2 and $ear 1for field yearOfBirth2. Both properties should have a public getter and a public setter. "emember the data type for the instance variable yearOfBirth is int.

+dd a read only property for the address ield 1instance variable2 as shown below. /******************************************** * getter accessor )or address 8 * note no setter accessor * The propert( is read onl( * *****************************************/ public Address Address { get { return address; } }

Override the ToString method


The ToString method should be overridden so that it displays something like 7red 7linstone ,804 ,99 bedrock street Bedrock BB, 77,0

Object Orientated Programming using Visual C#

Setter %ethod for !ddress


+dd a public set+ddress method as shown below /**************************************** * setter method )or address ***************************************/ public void setAddress(string strst9 string strtwn9 string strpc { address+Street = strst; address+Town = strtwn; address+Postcode = strpc; }

onstructors
-omplete the four overloaded constructors shown below. public Person( { address = new Address( ; setAddress("un1own"9"un1nown"9"un1nown" ; :ear = 0; ;ame = "no name"; } public Person(String strn { //T< =< } public Person(String strn9 int (r { //T< =< } public Person(String strn9 string strst9 string strtwn9 string strpc9 int (r { //T< =< }

Object Orientated Programming using Visual C#

Testing the Person

lass

,. +dd another static method to the class %rogram called test%erson12 0. :dit the ain method to call test%erson

4. +dd the following code to the static method test%erson static void TestPerson( { Person p/ = new Person( ; Person p3 = new Person("Peter" ; Person p> = new Person(",ilma"9/?@A ; Person pB = new Person("2red"9 "/00 bedroc1 st"9 "'edroc1"9 "''/ 322"9 /?/3 ; *onsole+,rite-ine("P/5 *onsole+,rite-ine("p35 *onsole+,rite-ine("P>5 *onsole+,rite-ine("PB5 } *onsole+6ead-ine( ; ;. "un the program and you should get an output something like " " " " ! ! ! ! p/ p3 p> pB ; ; ; ;

You might also like