練習題:試使用C#編程實現銀行、ATM等功能


 

練習題:試使用編程實現銀行、ATM等功能

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            //創建賬戶wangcehnjun
            Account wangchenjun = new Account("wangchenjun", "123456");
            //創建賬戶niejiong
            Account niejiong = new Account("niejiong", "789456");

            //創建ATM機Transfer1
            ATM Transfer1 =new ATM();
            //用戶wangchenjun存入余額100萬元
            wangchenjun.toDeposit(1000000);

            Console.WriteLine("wangchenjun用戶的余額為: {0}元",wangchenjun.getBalance());
            
            //使用ATM機Transfer1,從用戶wangchenjun向用戶niejiong轉賬500元
            if (Transfer1.toTransfer(wangchenjun, niejiong, 500))
                Console.WriteLine("轉賬已完成!");
            else Console.WriteLine("轉賬未完成!");

            //用戶wangchenjun取出50元現金
            wangchenjun.toTakeOut(50);

            Console.WriteLine("wangchenjun用戶的余額為:{0}元",wangchenjun.getBalance());
            Console.WriteLine("niejiong用戶的余額為:{0}元", niejiong.getBalance());
          
            Console.ReadKey();

        }
    }
    class Account
    {
        //用戶名
        private string userName = "";

        //用戶密碼
        private string userPassword = "";

        //用戶余額(balance)
        private int userBalance = 0;

        //創建賬號,構造函數,輸入姓名和密碼
        public Account(string name, string password)
        {
            userName = name;
            userPassword = password;
        }

        //獲取該賬戶的用戶名
        public string getUserName()
        {
            return userName;
        }

        //獲取賬戶余額
        public int getBalance()
        { return userBalance; }

        //存錢
        public bool toDeposit(int numOfDeposit)
        {
            if (numOfDeposit >= 0)          //存儲金額為正數
            {
                userBalance = userBalance + numOfDeposit;
                return true;
            }
            else
            {
                return false;
            }        
        }

        //取錢
        public bool toTakeOut(int numOfTakeOut)
        {
            if ((numOfTakeOut >= 0) && (numOfTakeOut <=userBalance)) //要保證取錢數為正數,且要小於余額
            {
                userBalance = userBalance - numOfTakeOut;
                return true;
            }
            else return false;
        }

    }

    class ATM
    {
        //ATM主要用於轉賬,該類只包含轉賬這一種方法
        public bool toTransfer(Account account1,Account  account2,int amount)
        {
            bool isTakeOutOK = account1.toTakeOut(amount);
            if (isTakeOutOK)
            {
                Console.Write("賬戶1({0})轉出{1}元成功!",account1.getUserName(), amount);
                bool isDepositOK = account2.toDeposit(amount);
                if (isDepositOK)
                {
                    Console.Write("賬戶2({0})轉入{1}元成功!", account2.getUserName(),amount);
                    return true;                   
                }
                else
                {
                    account1.toDeposit(amount);  //將扣的錢退回到account1
                    Console.Write("轉賬失敗!");
                    return false;
                }
            }
            else
            {
                Console.Write("轉賬失敗!");
                return false;
            }
        }
    }
}

運行結果:

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM