private static void BabyAccountExample() { IAccount b = new BabyAccount(); b.PayInFunds(50); Console.WriteLine("Balance: {0}", b.GetBalance()); Console.WriteLine("Trying to withdraw 20"); if (b.WithdrawFunds(20)) { Console.WriteLine("Balance: {0}", b.GetBalance()); } else { Console.WriteLine("Not allowed"); } /* You can write the code below: */ IAccount b2 = new BabyAccount2(); b2.PayInFunds(50); Console.WriteLine("Balance: {0}", b.GetBalance()); /* This works because, although BabyAccount2 does not have a PayInFunds method, the base class does. * This means that the PayInFunds method from the BankAccount class is used at this point. * Instances of the BabyAccount2 class have abilities which they pick up from their base class. In * fact, at the moment, the BabyAccount2 class has no behaviors of its own; it gets everything from * its base class. */ }
public static void Main(string[] args) { // Creating an array of Inerface IAccount to hold the max customers IAccount[] accounts = new IAccount[Max_Cust]; // Creating a CustomerAccount object and assigning it to index 0 of the array accounts[0] = new CustomerAccount(); accounts[0].PayInFunds(50); Console.WriteLine($"Balance: { accounts[0].GetBalance() }"); // Creating a BabyAccount object and assigning it to index 1 of the array accounts[1] = new BabyAccount(); accounts[1].PayInFunds(20); Console.WriteLine($"Balance: { accounts[1].GetBalance() }"); // Since both objects implement the same interface they can both be stored // in the same IAccount array if (accounts[0].WithdrawlFunds(20)) { Console.WriteLine("Withdrawl OK"); } if (accounts[1].WithdrawlFunds(20)) { Console.WriteLine("Withdrawl OK"); } Console.ReadLine(); }
public static void Main() { DictionaryBank ourBank = new DictionaryBank(); CustomerAccount newAccount = new CustomerAccount("Rob", 1000000); if (ourBank.StoreAccount(newAccount)) { Console.WriteLine("CustomerAccount added to bank"); } BabyAccount newBabyAccount = new BabyAccount("David", 100, "Rob"); if (ourBank.StoreAccount(newBabyAccount)) { Console.WriteLine("BabyAccount added to bank"); } ourBank.Save("Test.txt"); DictionaryBank loadBank = DictionaryBank.Load("Test.txt"); IAccount storedAccount = loadBank.FindAccount("Rob"); if (storedAccount != null) { Console.WriteLine("CustomerAccount found in bank"); } storedAccount = loadBank.FindAccount("David"); if (storedAccount != null) { Console.WriteLine("BabyAccount found in bank"); } }
public static void Main() { IAccount[] accounts = new IAccount[MAX_CUST]; accounts[0] = new CustomerAccount(); Console.WriteLine("Account: " + accounts[0].RudeLetterString()); accounts[1] = new BabyAccount(); Console.WriteLine("Baby Account: " + accounts[1].RudeLetterString()); }
private void CreateBabyAccount() { string name = Validation.ValidateName("\nWhat would you like the name on the account to be?"); decimal balance = Validation.ValidateDecimal("How much money would you like initially deposited into the account?", 0, 10000); string parentName = Validation.ValidateName("What would you like the parent name on the account to be?"); BabyAccount newAccount = new BabyAccount(name, balance, parentName); Console.WriteLine("\nAccount summary: \n" + newAccount.ToString()); ourBank.StoreAccount(newAccount); }
public void InheritanceFromBaseClass() { IAccount account = new BabyAccount(); account.PayInFunds(50); // This works because, although BabyAccount does not have a PayInFunds method, the base class does. // This means that the PayInFunds method from the BankAccount class is used at this point. // Instances of the BabyAccount class have abilities which they pick up from their base class. // In fact, at the moment, the BabyAccount class has no behaviors of its own; it gets everything from its base class. // A program can use the is and as operators when working with class hierarchies and interfaces. // The is operator determines if the type of a given object is in a particular class hierarchy or implements // a specified interface. You apply the is operator between a reference variable and a type or // interface and the operator returns true if the reference can be made to refer to objects of that type. if (account is IAccount) { Console.WriteLine("acc is IAccount"); } }
public static void Main() { IAccount[] accounts = new IAccount[MAX_CUST]; accounts[0] = new CustomerAccount(); accounts[0].PayInFunds(50); Console.WriteLine("Balance: " + accounts[0].GetBalance()); accounts[1] = new BabyAccount(); accounts[1].PayInFunds(20); Console.WriteLine("Balance: " + accounts[1].GetBalance()); if (accounts[0].WithdrawFunds(20)) { Console.WriteLine("Withdraw OK"); } if (accounts[1].WithdrawFunds(20)) { Console.WriteLine("Withdraw OK"); } }
private void btnSubmit_Click(object sender, EventArgs e) { if (radNorm.Checked == true) { Account check = new Account(); check.obalance = Convert.ToDecimal(txtBalance.Text); check.amount = Convert.ToDecimal(txtDeWith.Text); check.Calc(check.dewith, check.amount); if (radDeposit.Checked == true) { check.dewith = true; } else { check.dewith = false; } check.baby = false; lblOut.Text = check.ToString(); } else { BabyAccount baby = new BabyAccount(); baby.obalance = Convert.ToDecimal(txtBalance.Text); baby.amount = Convert.ToDecimal(txtDeWith.Text); baby.Calc(baby.dewith, baby.amount); if (radDeposit.Checked == true) { baby.dewith = true; } else { baby.dewith = false; } baby.baby = true; lblOut.Text = baby.ToString(); } }