کانال بله, جهت پشتیبانی و اطلاع رسانی کانال بله, جهت پشتیبانی و اطلاع رسانی
عضویت

بازگرداندن متد در شرایط گوناگون (C# method return types)


لیست دانلود فیلم های آموزشی مربوط به C#
  1. فایل Management.cs را بدین ترتیب تغییر دهید.
                
using System;
namespace Tahlildadeh.com_CSharpTutorials
{
    public class Management
    {
        private AccountType SpecifyAccountType()
        {
            byte type = 0;
            Console.WriteLine("What type of account the customer wants to open");
            Console.WriteLine("1 - Checking Account");
            Console.WriteLine("2 - Savings Account");
            Console.Write("Enter account type: ");
            type = byte.Parse(Console.ReadLine());
            if (type == 1)
                return AccountType.Checking;
            else if (type == 2)
                return AccountType.Saving;
            else
                return AccountType.Other;
        }
        private Customer CreateNewAccount()
        {
            Customer client = new Customer();
            byte typeOfAccount = 0;
            Console.WriteLine("===========================================");
            Console.WriteLine("==-= National Bank =-======================");
            Console.WriteLine("-------------------------------------------");
            Console.Write("Enter a number for the new account(000-000000-000): ");
            client.AccountNumber = Console.ReadLine();
            Console.WriteLine("What type of account the customer wants to open");
            Console.WriteLine("1 - Checking Account");
            Console.WriteLine("2 - Savings Account");
            Console.Write("Enter account type: ");
            typeOfAccount = byte.Parse(Console.ReadLine());
            if (typeOfAccount == 1)
                client.Type = AccountType.Checking;
            else if (typeOfAccount == 2)
                client.Type = AccountType.Saving;
            else
                client.Type = AccountType.Other;
            Console.Write("Enter customer name: ");
            client.FullName = Console.ReadLine();
            Console.Write("Ask the customer to enter a PIN: ");
            client.PIN = short.Parse(Console.ReadLine());
            return client;
        }
        public double Deposit()
        {
            double amount = 0;
            Console.Write("Amount to deposit: ");
            amount = double.Parse(Console.ReadLine());
            return amount;
        }
        public double Withdraw(Customer cust)
        {
            double amount = 0;
            Console.Write("Amount to withdraw: ");
            amount = double.Parse(Console.ReadLine());
            if (amount > cust.Balance)
            {
                Console.WriteLine(
                "You are not allowed to withdraw more money than your account has.");
                Console.ReadKey();
                return 0.00D;
            }
            else
                return amount;
        }
        private void ShowAccountInformation(Customer cust)
        {
            Console.WriteLine("===========================================");
            Console.WriteLine("==-= National Bank =-======================");
            Console.WriteLine("Customer Account Information");
            Console.WriteLine("-------------------------------------------");
            Console.WriteLine("Account #:    {0}", cust.AccountNumber);
            Console.WriteLine("Account Type: {0}", cust.Type);
            Console.WriteLine("Full Name:    {0}", cust.FullName);
            Console.WriteLine("PIN #:        {0}", cust.PIN);
            Console.WriteLine("Balance:      {0:F}", cust.Balance);
            Console.WriteLine("===========================================");
            return;
        }
        public static int Main()
        {
            double amount = 0;
            byte nextAction = 0;
            Customer accountHolder = null;
            Management registration = new Management();
            Console.Title = "National Bank";
            accountHolder = registration.CreateNewAccount();
            Console.WriteLine("Enter the customer's initial deposit");
            accountHolder.Balance = registration.Deposit();
            Console.Clear();
            registration.ShowAccountInformation(accountHolder);
            do
            {
                Console.WriteLine("What do you want to do now?");
                Console.WriteLine("0 - Close the application");
                Console.WriteLine("1 - Check account balance");
                Console.WriteLine("2 - Make a deposit");
                Console.WriteLine("3 - Withdraw money");
                Console.WriteLine("4 - Transfer money from one account to another");
                Console.Write("Enter your choice: ");
                nextAction = byte.Parse(Console.ReadLine());
                Console.Clear();
                switch (nextAction)
                {
                    case 1:
                        Console.Clear();
                        registration.ShowAccountInformation(accountHolder);
                        Console.Write("Press Enter for next operation");
                        Console.ReadKey();
                        break;
                    case 2:
                        amount = registration.Deposit();
                        accountHolder.Balance += amount;
                        Console.Clear();
                        registration.ShowAccountInformation(accountHolder);
                        break;
                    case 3:
                        amount = registration.Withdraw(accountHolder);
                        accountHolder.Balance -= amount;
                        Console.Clear();
                        registration.ShowAccountInformation(accountHolder);
                        break;
                    case 4:
                        Console.WriteLine(
                       "Operation not available: You have only one account with us");
                        break;
                    default:
                        return 0;
                }
                if ((nextAction < 1) || (nextAction > 4))
                    Console.WriteLine("Invalid Action: Please enter a value between 1 and 4");
            } while ((nextAction >= 1) && (nextAction <= 4));
            Console.ReadKey();
            return 0;
        }
    }
}
  1. کلید F5 را زده تا برنامه اجرا شود.
  2. اطلاعات لازم را به این ترتیب وارد کنید.
Account #:
257-484902-444
Account Type:
1
Customer Name:
Josh Nay
PIN:
5008
Initial Deposit:
250
===========================================‎
‎==-= National Bank =-======================‎
‎-------------------------------------------‎
New Account Number (000-000000-000): 257-484902-444‎
What type of account the customer wants to open
‎1 - Checking Account
‎2 - Savings Account
Enter account type: 1‎
Enter customer name: Josh Nay
Ask the customer to enter a PIN: 5008‎
Enter the customer's initial deposit
Amount to deposit: 250
  1. کلید Enter را فشار دهید.
===========================================‎
‎==-= National Bank =-======================‎
Customer Account Information
‎-------------------------------------------‎
Account #:    257-484902-444‎
Account Type: Checking
Full Name:    Josh Nay
PIN #:        5008‎
Balance:      250.00‎
‎===========================================‎
What do you want to do now?‎
‎0 - Close the application
‎1 - Check account balance
‎2 - Make a deposit
‎3 - Withdraw money
‎4 - Transfer money from one account to another
Enter your choice:
  1. حال به منظور برداشت مبلغ مورد نظر از حساب 3 را وارد کرده، سپس Enter را بزنید.
  2. 164.37 را به عنوان مبلغ واریزی تایپ کنید و کلید Enter را بزنید.
===========================================‎
‎==-= National Bank =-======================‎
Customer Account Information
‎-------------------------------------------‎
Account #:    257-484902-444‎
Account Type: Checking
Full Name:    Josh Nay
PIN #:        5008‎
Balance:      85.63‎
‎===========================================‎
What do you want to do now?‎
‎0 - Close the application
‎1 - Check account balance
‎2 - Make a deposit
‎3 - Withdraw money
‎4 - Transfer money from one account to another
Enter your choice:
  1. اکنون عدد 2 را وارد کنید و Enter را بزنید.
  2. مبلغ سپرده را 116.18 وارد کنید، سپس Enter را بزنید.
===========================================‎
‎==-= National Bank =-======================‎
Customer Account Information
‎-------------------------------------------‎
Account #:    257-484902-444‎
Account Type: Checking
Full Name:    Josh Nay
PIN #:        5008‎
Balance:      201.81‎
‎===========================================‎
What do you want to do now?‎
‎0 - Close the application
‎1 - Check account balance
‎2 - Make a deposit
‎3 - Withdraw money
‎4 - Transfer money from one account to another
Enter your choice:
  1. در این مرحله، عدد 3 را وارد کنید و کلید Enter را بزنید.
  2. رقم 300 را به عنوان مبلغ برداشتی وارد کنید، سپس Enter را بزنید.
Amount to withdraw: 300‎
You are not allowed to withdraw more money than your account has.
  1. کلید Enter را بزنید.
  2. عدد 0 را وارد کرده و دوباره Enter را بزنید.

While (true)

تاکنون در موقعیت های مختلفی که شرط while را بکار بردیم، وسیله ای برای بررسی شرط مورد نظر نیز گنجاندیم. به جای این کار همچنین می توان ثابت صحیح بولی را داخل پرانتزهای مقدار true قرار داد.

مثال:

                
using System;
public class Exercise
{
    public static int Main()
    {
        while (true)
            Console.WriteLine("C# Programming is fun!!!");
        return 0;
    }
}

این نوع دستور معتبر بوده و کار می کند ولی هیچ راهی برای متوقف کردن آن وجود ندارد، زیرا دستور نام برده به کامپایلر می گوید " تا زمانی که این درست است ("as long as this is true،…") ". سوال مطرح این است که منظور از "this" چیست ؟ در نتیجه، برنامه بدون هیچ گونه توقف و برای همیشه ادامه پیدا می کند. بنابراین، در صورت ایجاد شرط while(true)، در بدنه ی دستور مزبور، لازم است وسیله ای برای متوقف کردن کامپایلر فراهم کرد (به عبارت دیگر، راهی که از آن طریق شرط به false ارزیابی شود). برای نیل به این هدف، باید شرط if را به کار برد.

مثال:

                
using System;
public class Exercise
{
    public static int Main()
    {
        int i = 0;
        while (true)
        {
            if (i > 8)
                break;
            Console.WriteLine("C# Programming is fun!!!");
            i++;
        }
        return 0;
    }
}

این بار برنامه از compiler درخواست می کند جمله ای را در صفحه ی کنسول نمایش دهد که از 0 تا 8 می شمارد ولی بمحض رسیدن به 8 متوقف شود. برنامه ی بالا این نتیجه را به دست می دهد.

C# Programming is fun!!!‎
C# Programming is fun!!!‎
C# Programming is fun!!!‎
C# Programming is fun!!!‎
C# Programming is fun!!!‎
C# Programming is fun!!!‎
C# Programming is fun!!!‎
C# Programming is fun!!!‎
C# Programming is fun!!!‎
Press any key to continue...

در برنامه ی بالا، از عملگر break استفاده کردیم. البته می توان از هر مکانیزم دیگری برای متوقف کردن حلقه کمک گرفت مادام اینکه شرط مورد نظر false شود. مثال زیر شیوه ی دیگری از متوقف کردن حلقه را به نمایش می گذارد.

                
using System;‎
public class Exercise
‎{‎
‎    public static int Main()‎
‎    {‎
‎        int i = 0;‎
        
        while (true)
        {
            if (i < 8)
                Console.WriteLine("C# Programming is fun!!!");
            else
                return 0;
            i++;
        }
‎    }‎
‎}

نتیجه ی آن با مثال پیشین یکسان می باشد.

به جای استفاده از while (true)، می توان ابتدا متغیر بولی را تعریف و مقداردهی اولیه کرد، یا می توان متغیر بولی به کاربرد که مقدار آن از پیش شناس و مشخص است. مقدار مربوط می تواند از یک متد بیاید یا از هر وسیله ی دیگری.

1394/07/27 4704 2790
رمز عبور : tahlildadeh.com یا www.tahlildadeh.com
نظرات شما

نظرات خود را ثبت کنید...