Object Oriented Programming 1 ( Problem 1 )

 NOTE : PLEASE GIVE ME A LIKE ........... I REALLY NEED THOSE LIKES ..... @! HAPPY CHEGGING @!

I HAVE ANSWERED ACCORING TO CHEGG POLICY SO PLEASE DON'T GIVE ANY NEGATIVE RATINGS ............... @ THANK YOU @ ............




 

ANSWER:

CODE:

Account.cs:

namespace OOPs

{

    class Account

    {

        // Private fields and propertied

        private string accName;

        public string AccName

        {

            get { return accName; }

            set { accName = value; }

        }

        private string acid;

        public string AcID

        {

            get { return acid; }

            set { acid = value; }

        }

        private int balance;

        public int Balance

        {

            get { return balance; }

            set { balance = value; }

        }

        /// <summary>

        /// Empty Constructor

        /// </summary>

        public Account()

        {

            accName = "";

            acid = "";

            balance = 0;

        }

        /// <summary>

        /// Valued Constructor

        /// </summary>

        /// <param name="accName"></param>

        /// <param name="acid"></param>

        /// <param name="balance"></param>

        public Account(string accName, string acid, int balance)

        {

           this.accName = accName;

            this.acid = acid;

            this.balance = balance;

        }

        /// <summary>

        /// Deposit amount, if greater than zero

        /// </summary>

        /// <param name="amount"></param>

        public void Deposit(int amount)

        {

            if (amount > 0)

            {

                balance += amount;

            }

        }

        /// <summary>

        /// Withdraw amount, if greater than zero and account has sufficient balance

        /// </summary>

        /// <param name="amount"></param>

        /// <returns>true if withdraw is successful; false, otherwise;</returns>

        public bool Withdraw(int amount)

        {

            if (amount > 0 && balance >= amount)

            {

                balance -= amount;

                return true;

            }

            return false;

        }

        /// <summary>

        /// Transfer amount from this account to receiver

        /// </summary>

        /// <param name="amount"></param>

        /// <param name="receiver"></param>

        /// <returns></returns>

        public bool Transfer(int amount, Account receiver)

        {

            // if this account withdraw is successful, deposit the amount to receiver

            if (this.Withdraw(amount))

            {

                receiver.Deposit(amount);

                return true;

            }

            // else, retrun false

            return false;

        }

    }

}

Program.cs

using System;

namespace OOPs

{

    class Program

    {

        /// <summary>

        /// Display account details

        /// </summary>

        /// <param name="account"></param>

        static void DisplayAccount(Account account)

        {

            Console.WriteLine(account.AcID + ", " + account.AccName + ", Balance: " + account.Balance);

        }

        static void Main(string[] args)

        {

            // Create account1 using empty constructor and assign values using properties

            Account account1 = new Account();

            account1.AccName = "Current";

            account1.AcID = "ACC001";

            account1.Balance = 450;

            Console.WriteLine("Account 1:");

            DisplayAccount(account1);

            // Deposit 150 to account1

            Console.WriteLine();

            Console.WriteLine("Deposit 150 to account1");

            account1.Deposit(150);

            Console.WriteLine("\nUpdated info");

            DisplayAccount(account1);

            //Test Withdraw method

            Console.WriteLine();

            Console.WriteLine("withdraw 700 from account1");

            if (account1.Withdraw(700))

            {

                Console.WriteLine("Withdraw 700 from account1 is successful. Current balance: " + account1.Balance);

            }

            else

            {

                Console.WriteLine("Withdraw 700 from account1 is NOT successful. Current balance: " + account1.Balance);

            }

            Console.WriteLine();

            Console.WriteLine("withdraw 200 from account1");

            if (account1.Withdraw(200))

            {

                Console.WriteLine("Withdraw 200 from account1 is successful. Current balance: " + account1.Balance);

            }

            else

            {

                Console.WriteLine("Withdraw 200 from account1 is NOT successful. Current balance: " + account1.Balance);

            }

            // Create account2 using valued constructor

            Console.WriteLine();

            Account account2 = new Account("Savings", "ACC002", 200);

            Console.WriteLine("Account 2:");

            DisplayAccount(account2);

            // Test Transfer method

            Console.WriteLine();

            if (account1.Transfer(150, account2))

            {

                Console.WriteLine("Transfer 150 from account1 to account2 is successful");

            }

            Console.WriteLine("\nUpdated info: ");

            Console.WriteLine("Account 1:");

            DisplayAccount(account1);

            Console.WriteLine("Account 2:");

            DisplayAccount(account2);

            Console.ReadKey();

        }

    }

}





إرسال تعليق

If you have any doubts, Please let me know.
إرسال تعليق
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.