Bài tập hiển thị một số giao dịch ATM đơn giản trong C-Sharp.

Đây có thể xem là một bài tập khá thú vụ và rất thức tế khi chúng ta mới bắt đầu học cơ bản về về C-Sharp. Bài tập yêu cầu bạn hiển thị một số qui trình giao dịch thông thình trong cỗ máy ATM, và tất nhiên bài tập chỉ yêu cầu một cách thể hiện các giao dịch này rất cơ bản thôi. Các giao dịch(transaction ) này bao gồm:
  • Balance checking - Kiểm tra tiền dư.
  • Cash withdrawal - Rút tiền mặt.
  • Cash deposition - Gửi tiền.
Và người dùng có thể chọn bất kỳ giao dịch nào họ muốn.



C-Sharp Source Code:
--------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpExample
{
    class ATMTransaction
    {

        static void Main(string[] args)
        {

            int amount = 1000, deposit, withdraw;
            int choice, pin = 0, x = 0;

            Console.WriteLine("Enter Your Pin Number ");
            pin = int.Parse(Console.ReadLine());

            while (true)
            {
                Console.WriteLine("********Welcome to ATM Service**************\n");
                Console.WriteLine("1. Check Balance\n");
                Console.WriteLine("2. Withdraw Cash\n");
                Console.WriteLine("3. Deposit Cash\n");
                Console.WriteLine("4. Quit\n");
                Console.WriteLine("*********************************************\n\n");
                Console.WriteLine("Enter your choice: ");

                choice = int.Parse(Console.ReadLine());
                switch (choice)
                {
                    case 1:
                        Console.WriteLine("\n YOUR BALANCE IN Rs : {0} ", amount);
                        break;
                    case 2:
                        Console.WriteLine("\n ENTER THE AMOUNT TO WITHDRAW: ");
                        withdraw = int.Parse(Console.ReadLine());
                        if (withdraw % 100 != 0)
                        {
                            Console.WriteLine("\n PLEASE ENTER THE AMOUNT IN MULTIPLES OF 100");
                        }
                        else if (withdraw > (amount - 500))
                        {
                            Console.WriteLine("\n INSUFFICENT BALANCE");
                        }

                        else
                        {
                            amount = amount - withdraw;
                            Console.WriteLine("\n\n PLEASE COLLECT CASH");
                            Console.WriteLine("\n YOUR CURRENT BALANCE IS {0}", amount);
                        }
                        break;
                    case 3:
                        Console.WriteLine("\n ENTER THE AMOUNT TO DEPOSIT");
                        deposit = int.Parse(Console.ReadLine());
                        amount = amount + deposit;
                        Console.WriteLine("YOUR BALANCE IS {0}", amount);
                        break;
                    case 4:
                        Console.WriteLine("\n THANK U USING ATM");
                        Environment.Exit(0);
                        break;
                    default:
                        Console.WriteLine("Default case");
                        break;
                }

            }


        }


    }
}

-------------------------












No comments:

Post a Comment