Simple Bank Project Part 1 C Sharp
Simple Bank Project Part 1 C Sharp
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.
&our solution explorer should now look something like the example below
Instance variables
+dd in three instance variables as shown below.
namespace People { public class Address { //instance variables string street; string town; string postcode;
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 }
/**************************************** * overridden ToString method **************************************/ public override string ToString( { string strout; strout = Street ! "#n" ! Town ! "#n" ! Postcode ! "#n"; return strout; }
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.
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.
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; } }
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< =< }
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 ; ; ; ;