Question: Using C#, we need to implement IComparable interface and the CompareTo method that will sort accounts for clients based on their type_of_account (chequing, saving, other)
Using C#, we need to implement "IComparable" interface and the "CompareTo" method that will sort accounts for clients based on their type_of_account (chequing, saving, other) and their balance (any numbers).
We first need to check and sort based on type_of_account and if the types are the same we sort based on balance. Overloading the + operator that will take 2 accounts as parameters and the return type should be a new account that merges both accounts and its type_of_account comes from the first parameter and the balance will be the sum of the 2 accounts' balance.
We need to test the "Compare T o" method by having an array of 2 Account objects, initialize each of the objects to different values and display the array before calling Array.Sort and then display the newly sorted array.
Started like this but didn't figure out how to continue or if I am on the right track!:
for the class:
namespace Accounts { public enum type_of_account { Chequing, Savings, Other }
class Account {
public double the_balance { get; set; } public type_of_account Type_Of_Account { get; set; }
public Account(int accID, string Name, double The_Balance, type_of_account AcctType) { Type_Of_Account = AcctType; the_balance = The_Balance; return; }
public void display() { Console.WriteLine(""); Console.WriteLine(" Account Type: " + Type_Of_Account.ToString()); Console.WriteLine(" Balance $" + Math.Round(the_balance, 2)); Console.WriteLine(""); return; } } }
for the main program:
namespace Accounts { class Program { static void Main(string[] args) {
Account account1 = new Account(Chequing, 5000); Account account2 = new Account(Savings, 2500); Console.ReadLine(); return; } }
interface IComparable { int CompareTo(Object obj); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
