天天看点

Nearth===019/c#类和对象, 继承(练习题2)

2、个人银行业务主要由存款、取款、查询余额、转帐组成,一般银行为我们办理个人业务时,需要我们实现存入一部分钱方可为我们开户,创建一个PersonalAccount类,实现以上功能。
1)创建一个名为Bank的控制台应用程序。
2)实例化个人帐号,测试功能。
//https://wenku.baidu.com/view/a2a09dae250c844769eae009581b6bd97e19bc12.html
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//2、个人银行业务主要由存款、取款、查询余额、转帐组成,
//一般银行为我们办理个人业务时,
//需要我们实现存入一部分钱方可为我们开户,
//创建一个PersonalAccount类,实现以上功能。
//1)创建一个名为Bank的控制台应用程序。
//2)实例化个人帐号,测试功能。
namespace Bank
{
    class PersonalAccount{
        //定义所需变量
        private string accountName;
        private double accountReamainValue;
        public string AccountName{
            set{
                accountName = value;
            }
            get{
                return accountName;
            }
        }
        public double AccountReamainValue
        {
            set
            {
                accountReamainValue = value;
            }
            get
            {
                return accountReamainValue;
            }
        }
    //实现开户
        public PersonalAccount(string accountName, double accountReamainValue)
        {
            this.accountName = accountName;
            this.accountReamainValue = accountReamainValue;
        }
      //实现存,取,转,显示余额功能
        public void deposit(double money){//存钱
            this.accountReamainValue += money;
        }
        public void withdraw(double money)//取钱
        {
            this.accountReamainValue -= money;
        }
        public void transfer(PersonalAccount toAccount,double money){//转账
            this.accountReamainValue -= money;
            toAccount.accountReamainValue += money;
        }
        public void showReamainValue(){
            Console.WriteLine("尊敬的客户{0},你的账号余额为:{1:0.00}元·····",accountName,accountReamainValue);
        }
    }
    
    class Program
    {  
        static void Main(string[] args)
        {
            Console.WriteLine("======================================");
            Console.WriteLine("欢迎来到个人银行管理系统");
            Console.WriteLine("======================================");
            PersonalAccount personOne = new PersonalAccount("周瑜",1200);
            PersonalAccount personTwo = new PersonalAccount("小乔",500);
            bool b = true;
            while(b){
                Console.Write("请输入你要进行的交易{1-存款,2-提款,3-转账,4-显示余额,5-退出交易}==>");
                switch (Console.ReadLine())
                {
                    case "1":
                        handleDeposit(personOne);
                        break;
                    case "2":
                        handleWithdraw(personOne);
                        break;
                    case "3":
                        handleTransfer(personOne,personTwo);
                        break;
                    case "4":
                        handleShow(personOne);
                        break;
                    default:
                        b = false;
                        Console.WriteLine("交易结束!欢迎下次再来····");
                        Console.ReadLine();
                        break;

                }
            
            }
        }
        static void handleDeposit(PersonalAccount account){
            Console.Write("请输入你要存入的金额:");
            double money = double.Parse(Console.ReadLine());
            account.AccountReamainValue += money;
            Console.WriteLine("存钱完成!");
            account.showReamainValue();
        }
        static void handleWithdraw(PersonalAccount account){
            Console.Write("请输入你要取款的金额:");
            double money = double.Parse(Console.ReadLine());
            if (money > account.AccountReamainValue)
            {
                Console.WriteLine("尊敬的用户,你的账户余额不足!");
                return;
            }
                account.AccountReamainValue -= money;
                Console.WriteLine("取款完成!");
                account.showReamainValue();
        }
        static void handleTransfer(PersonalAccount fromAccount,PersonalAccount toAccount){
            Console.Write("请输入你要转账的金额:");
            double money = double.Parse(Console.ReadLine());
            if(fromAccount.AccountReamainValue<money){
                Console.WriteLine("余额不足,不能转账!抱歉·····");
                return;
            }
            fromAccount.AccountReamainValue -= money;
            toAccount.AccountReamainValue += money;
            fromAccount.showReamainValue();
            toAccount.showReamainValue();
        }
        static void handleShow(PersonalAccount account){
            Console.WriteLine("目前你的账户余额为:{0}元····",account.AccountReamainValue);
        }
    }
}      

我学习,我很开心··············································

继续阅读