Functional specifications

The account information overview displays savings and credit card information and repayment information (such as subscribing to automatic repayment service and triggering automatic repayment on the repayment day);

The deposit card and credit card can display information, deposit and withdraw money.

The credit card can also set the repayment date, loan, open/cancel the automatic repayment service, manual repayment functions.

The source code to show

The customer class

/ / the customer class
    class Client
    {
        // Fields: name, account
        internal string name;
        internal Accounts accounts;
        // Client class constructor
        public Client(string nam, Accounts acco){ name = nam; accounts = acco; }}Copy the code

Account class

/ / account
    class Accounts
    {
        // Fields: total amount, debit card, credit card
        internal float assets = 0;
        internal DepositCard depo;
        internal CreditCard cred;
        // Account class constructor
        internal Accounts(DepositCard deposit, CreditCard credit)
        {
            depo = deposit;
            cred = credit;
        }
        // Calculate the total amount
        internal void CalAsserts()
        {
            assets = depo.balance + cred.balance;
        }
        // Account information display
        internal void Display(Client client, Delegate repayDelegate)
        {
            Console.WriteLine("It is {0}. ", DateTime.Now);
            RepaymentStatus(client, repayDelegate);
            depo.Display();
            cred.Display();
            Console.WriteLine("\n------SUMMARY------\n");
            CalAsserts();
            Console.WriteLine("Assets: " + assets);
            Console.WriteLine("Debt: " + client.accounts.cred.debt);
        }
        // Payment status
        internal void RepaymentStatus(Client client, Delegate repayDelegate)
        {
            if(client.accounts.cred.debt ! =0)
            {
                if (DateTime.Today.Day == cred.statement)
                {
                    Console.WriteLine("You Need to Pay Your Debt.");
                    // Can trigger the event handling method
                    repayDelegate.Repay(client);
                }
                else
                {
                    int duration;
                    duration = Math.Abs(DateTime.Today.Day - cred.statement);
                    if (DateTime.Today.Day < cred.statement)
                    { 
                        Console.WriteLine("{0} Day(s) Before This Month's Due Day.\n", duration);
                    }
                    else
                    {
                        Console.WriteLine("You Have Over Due for {0} Day(s) ! \n", duration); }}}else
            {
                Console.WriteLine("You Don't Have Debt This Month."); }}// Automatic repayment, credit card is preferred, savings card is second
        internal void AutoRepay(Client client)
        {
            if (cred.balance >= cred.debt)
            {
                cred.AutoRepay();
                Console.WriteLine("-REPAYMENT COMPLETE! -");
            }
            else
            {
                float temp = cred.debt - cred.balance;
                cred.debt = cred.balance;
                cred.AutoRepay();
                cred.debt = temp;
                if (depo.balance >= cred.debt)
                {
                    depo.AutoRepay(client);
                    Console.WriteLine("-REPAYMENT COMPLETE! -");
                }
                else
                {
                    temp = cred.debt - depo.balance;
                    cred.debt = depo.balance;
                    depo.AutoRepay(client);
                    cred.debt = temp;
                    Console.WriteLine("-BOTH DEPOSIT CARD AND CREDIT CARD BALACE INSUFFICIENT! -\n");
                    Console.WriteLine("Credit Card Debt: {0}\nCredit Card Statement: {1}", cred.debt, cred.statement);
                    Console.WriteLine("\nPLEASE TOP-UP!"); }}}}Copy the code

Different classes of

  // Deposit and withdrawal classes
    class TopWithdraw
    {
        / / deposit
        internal void TopUp(ref float balance)
        {
            Console.WriteLine("PLEASE ENTER THE AMOUNT YOU WANT TO TOP-UP: ");
            int deposit = int.Parse(Console.ReadLine());
            balance += deposit;
            Console.WriteLine("Deposit: " + deposit);
        }
        / / withdrawals
        internal void Withdraw(ref float balance)
        {
            Console.WriteLine("PLEASE ENTER THE AMOUNT YOU WANT TO WITHDRAW: ");
            int withdrawal = int.Parse(Console.ReadLine());
            balance -= withdrawal;
            Console.WriteLine("Withdrawal: "+ withdrawal); }}Copy the code

A cash card class

// debit card type
    class DepositCard : TopWithdraw
    {
        // Field: balance
        internal float balance;
        // save card class constructor
        internal DepositCard(float bala)
        {
            balance = bala;
        }
        // Deposit card information display
        internal void Display()
        {
            Console.WriteLine("\n------Deposit Card------\n");
            Console.WriteLine("Deposit Card Balance: " + balance);
        }
        // The debit card will pay automatically
        internal void AutoRepay(Client client)
        {
            Console.WriteLine("\nDEPOSIT CARD REPAYING...");
            balance -= client.accounts.cred.debt;
            Console.WriteLine("Today is Due Day({0}):\n Repayment: {1}\n Deposit Card Balance: {2}", DateTime.Today.Day, client.accounts.cred.debt, balance);
        }
        // Deposit card
        internal void TopUp()
        {
            TopUp(ref balance);
            Display();
        }
        // Debit card for withdrawal
        internal void Withdraw()
        {
            Withdraw(refbalance); Display(); }}Copy the code

Credit card type

 // Credit card class
    class CreditCard : TopWithdraw
    {
        // Fields: repayment date, Debt, balance
        internal int statement;
        internal float debt;
        internal float balance;
        // Credit card class constructor
        internal CreditCard(int stat, float dbt, float bala)
        {
            statement = stat;
            debt = dbt;
            balance = bala;
        }
        // Set a repayment date
        internal void SetStatement()
        {
            Console.WriteLine("PLEASE SET STATEMENT OF THE MONTH: ");
            do
            {
                statement = int.Parse(Console.ReadLine());
            } while (statement < 1 || statement > 31);
        }
        / / lending
        internal void Loan()
        {
            Console.WriteLine("PLEASE ENTER THE AMOUNT YOU WANT TO LOAN: ");
            int loan = int.Parse(Console.ReadLine());
            debt += loan;
            Console.WriteLine("Loan: " + loan);
            Display();
        }
        // Credit card information display
        internal void Display()
        {
            Console.WriteLine("\n------Credit Card------\n");
            Console.WriteLine("Credit Card Balance: {0}\nCredit Card Debt: {1}\nCredit Card Statement: {2}", balance, debt, statement);
        }
        // Automatic credit card payment
        internal void AutoRepay()
        {
            Console.WriteLine("\nCREDIT CARD REPAYING...");
            balance -= debt;
            debt = 0;
            Console.WriteLine("Today is Due Day({0}):\n Repayment: {1}\n Credit Card Balance: {2}", DateTime.Today.Day, debt, balance);
        }
        // Credit card deposit
        internal void TopUp()
        {
            TopUp(ref balance);
            Display();
        }
        // Credit card withdrawal
        internal void Withdraw()
        {
            Withdraw(ref balance);
            Display();
        }
        // Enable/cancel the automatic repayment service
        internal void AutoRepayService(Client client, Delegate repayDelegate)
        {
            Console.WriteLine("Do You Want to Subscribe Auto Repay Service? \nY for Yes, N for No.\nPLEASE ENTER: ");
            string choice = Console.ReadLine();
            if (choice == "Y")
            {
                // Subscribe event AutoRepay, register method accounts for delegate methods.AutoRepay, whose signature needs to be the same as RepaymentDelegate
                repayDelegate.AutoRepay += new Delegate.RepaymentDelegate(client.accounts.AutoRepay);
                Console.WriteLine("-SERVICE SUBSCRIBED! -");
            }
            else if(choice == "N")
            {
                // Unsubscribe
                repayDelegate.AutoRepay -= new Delegate.RepaymentDelegate(client.accounts.AutoRepay);
                Console.WriteLine("-SERVICE UNSUBSCRIBED! -"); }}}Copy the code

Menu class

/ / menu
    class Menu
    {
        // Accept and determine whether the options entered by the user are valid
        internal int GetChoice(int lower, int upper)
        {
            int choice;
            do
            {
                Console.WriteLine("PLEASE ENTER YOUR CHOICE: ");
                choice = int.Parse(Console.ReadLine());
            } while (choice < lower || choice > upper);
            Console.WriteLine("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -");
            return choice;
        }
        / / the main menu
        internal void HomePage(Client client, Delegate repayDelegate)
        {
            int choice;
            Console.WriteLine("Hello {0}, what can we help you with? \n", client.name);
            Console.WriteLine(" (1) Accounts Status\n");// Account information and repayment status overview, such as subscribing to automatic repayment service, automatic repayment will be triggered on the repayment date
            Console.WriteLine(" (2) Deposit Card\n");/ / cash card
            Console.WriteLine(" (3) Credit Card\n");/ / credit card
            Console.WriteLine(" (0) EXIT\n");// Exit the program
            choice = GetChoice(0.3);
            switch (choice)
            {
                case 1:
                    client.accounts.Display(client, repayDelegate);
                    break;
                case 2:
                    DepoMenu(client, repayDelegate);
                    break;
                case 3:
                    CretMenu(client, repayDelegate);
                    break;
                case 0:
                    Application.Exit();
                    break;
            }
            Console.WriteLine("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -");
            Console.WriteLine("PRESS ANY KEY TO CONTINUE... ");
            Console.ReadKey(true);
            HomePage(client, repayDelegate);
        }
        // Debit card menu
        internal void DepoMenu(Client client, Delegate repayDelegate)
        {
            int choice;
            Console.WriteLine("\n------Deposit Card------\n");
            Console.WriteLine(" (1) Status\n");// Debit card information
            Console.WriteLine(" (2) Top-up\n");// Deposit card
            Console.WriteLine(" (3) Withdraw\n");// Debit card for withdrawal
            Console.WriteLine(" (0) BACK\n");// Return to the previous page
            choice = GetChoice(0.3);
            switch (choice)
            {
                case 1:
                    client.accounts.depo.Display();
                    break;
                case 2:
                    client.accounts.depo.TopUp();
                    break;
                case 3:
                    client.accounts.depo.Withdraw();
                    break;
                case 0:
                    HomePage(client, repayDelegate);
                    break;
            }
            Console.WriteLine("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -");
            Console.WriteLine("PRESS ANY KEY TO CONTINUE... ");
            Console.ReadKey(true);
            Console.WriteLine("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -");
            DepoMenu(client, repayDelegate);
        }
        // Credit card menu
        internal void CretMenu(Client client, Delegate repayDelegate)
        {
            int choice;
            Console.WriteLine("\n------Credit Card------\n");
            Console.WriteLine(" (1) Status\n");// Credit card information
            Console.WriteLine(" (2) Top-up\n");// Credit card deposit
            Console.WriteLine(" (3) Withdraw\n");// Credit card withdrawal
            Console.WriteLine(" (4) Set Statement\n");// Set credit card payment date
            Console.WriteLine(" (5) Loan\n");// Credit card loan
            Console.WriteLine(" (6) Auto Repay Service\n");// Enable/cancel the automatic repayment service
            Console.WriteLine(" (7) Manual Repay\n");// Pay by hand
            Console.WriteLine(" (0) BACK\n");// Return to the previous page
            choice = GetChoice(0.7);
            switch (choice)
            {
                case 1:
                    client.accounts.cred.Display();
                    break;
                case 2:
                    client.accounts.cred.TopUp();
                    break;
                case 3:
                    client.accounts.cred.Withdraw();
                    break;
                case 4:
                    client.accounts.cred.SetStatement();
                    break;
                case 5:
                    client.accounts.cred.Loan();
                    break;
                case 6:
                    client.accounts.cred.AutoRepayService(client,repayDelegate);
                    break;
                case 7:
                    client.accounts.AutoRepay(client);
                    break;
                case 0:
                    HomePage(client, repayDelegate);
                    break;
            }
            Console.WriteLine("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -");
            Console.WriteLine("PRESS ANY KEY TO CONTINUE... ");
            Console.ReadKey(true);
            Console.WriteLine("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -"); CretMenu(client, repayDelegate); }}Copy the code

Delegate class

/ / the delegate class
    class Delegate
    {
        // Define the delegate object RepaymentDelegate method, return type void, and take Client
        internal delegate void RepaymentDelegate(Client client);
        // Define events AutoRepay under the RepaymentDelegate delegate method
        internal event RepaymentDelegate AutoRepay;
        // Define the event handling method. The signature does not need to be the same as the RepaymentDelegate method
        internal bool Repay(Client client)
        {
            // Trigger conditions, events AutoRepay is not empty, and that is the repay day
            if(AutoRepay ! =null && DateTime.Today.Day == client.accounts.cred.statement)
            {
                // Trigger content
                Console.WriteLine("AUTO REPAYMENT INITIATING ...");
                // With the same name as the event and the same signature as the RepaymentDelegate delegate method
                AutoRepay(client);
                return true;
            }
            return false; }}Copy the code

The main program

 class Program
    {
        static void Main()
        {
            // Initialize user information
            DepositCard depo = new DepositCard(10000);
            CreditCard cred = new CreditCard(8.5000.100);
            Accounts accounts = new Accounts(depo, cred);
            Client client = new Client("Hazel", accounts);
            // Delegate class instantiation
            Delegate repayDelegate = new Delegate();
            // Enter the menu
            Menu menu = newMenu(); menu.HomePage(client, repayDelegate); }}Copy the code

A functional test

The test case

User name: Hazel Deposit card Initial amount: 10000 Credit card initial amount: 100 Credit card initial debt: 5000 Credit card initial repayment date: 8Copy the code

1 Account Information overview

1.1 The repayment date is not reached

1.2.1 Repayment Date – Not subscribed to automatic repayment service

1.2.2 Repayment Date – Subscribe to automatic repayment service

1.3 late

1.4 no arrears

2 bank card functions – onlyThe credit cardAs an example

2.1 Display of bank card information

2.2 Bank Card Deposit

2.3 Bank Card Withdrawal

2.4 Set credit card repayment date

2.5 Credit Card Loans

2.6 Credit Card automatic repayment service is enabled/cancelled

See 1.2.2 for results after triggering

2.7 Manual credit card repayment

tips

Through this assignment, the differences and connections between Event and Dalegate are clarified: Event and Dalegate must be declared as public; Dalegate is a type that corresponds to a pointer to a function. The signature (return value and parameter) of the registered/unregistered function must match the defined delegate method. An Event is an object that is generated based on a Dalegate and cannot be assigned with “=” or called directly from outside the class.

As a banking system, there is a lack of security.

Repository

Gitee