C#银行ATM程序--练习

个人更喜欢后面的 WinForm程序 (第二个),功能更多也更美观,使用起来也比控制台直观。这个WinForm程序写的很有成就感哈哈^~^(来自一个菜鸟的内心独白,大佬多多指点)—— WinForm的程序下载(解决方案)见文末

目录

控制台程序

WinForm应用程序(控制台的改进)—— 新添加 事件、委托、自定义异常等

功能描述

具体代码实现


控制台程序

具体代码:(可能有错,请帮忙指出^~^  谢谢~~)

using System;

namespace Bank_ATM
{
    class Account
    {
        //Constructor
        Account() { }
        Account(long bank_cardID, string password)
        {
            this.bank_cardID = bank_cardID;
            this.account_password = password;
        }

        //fields
        private static long initial_ID = 1000000001;       //the 1st one to create an account get this ID number
        private static string bank_name = "ICBC";
        private long bank_cardID;
        public string account_password;
        private long total_amount = 100000;       //initial account
        private string[] data = new string[5];
        private string[] keys =
        {
            "card ID","holder's name", "total sum", "latest withdraw","latest deposit"
        };

        //property
        public long latest_withdraw { set; get; }
        public long latest_deposit { set; get; }
        public string date_withdraw { set; get; }
        public string date_deposit { set; get; }
        public string date_create { set; get; }

        //indexer
        public string this[int i]
        {
            set
            {
                data[i] = value;
            }
            get
            {
                if (i >= 0 && i < data.Length)
                    return data[i];
                return null;
            }
        }
        public string this[string key]
        {
            get
            {
                return this[FindIndex(key)];
            }
        }
        private int FindIndex(string key)
        {
            for (int i = 0; i < keys.Length; i++)
                if (keys[i] == key)
                    return i;
            return -1;
        }

        //methods
        //withdraw from the account, record the current time
        public void withdrawMoney()
        {
            Console.Write("amount(withdraw): ");
            latest_withdraw = Convert.ToInt32(Console.ReadLine());
            if (latest_withdraw <= total_amount)
            {
                total_amount -= latest_withdraw;
                this[2] = Convert.ToString(total_amount); 
                date_withdraw = DateTime.Now.ToString();
                this[3] = Convert.ToString(latest_withdraw);
            }
            else
                Console.WriteLine("Lack of balance. Operation is refused\n");
        }

        //deposit from the account, record the current time
        public void depositMoney()
        {
            Console.Write("amount(deposit): ");
            latest_deposit = Convert.ToInt32(Console.ReadLine());
            if (latest_deposit > 0)
            {
                total_amount += latest_deposit;
                this[2] = Convert.ToString(total_amount);
                date_deposit = DateTime.Now.ToString();
                this[4] = Convert.ToString(latest_deposit);
            }
            else
                Console.WriteLine("Invalid operation\n");
        }

        //get information about the account 
        void get_card_info()              //try 4 choices below
        {
            Console.WriteLine("( card ID / holder's name / total sum / latest withdraw / latest deposit )?");
            string instr = Console.ReadLine();
            if (instr == "card ID" || instr == "holder's name" || instr == "total sum" || instr == "latest withdraw"
                || instr == "latest deposit")
            {
                this[3] = Convert.TOString(latest_withdraw);
                this[2] = Convert.ToString(total_amount);
                Console.Write(instr + " is " + this[instr]);
                if (instr == "latest withdraw")
                    Console.WriteLine("         " + date_withdraw);
                else if (instr == "latest deposit")
                    Console.WriteLine("         " + date_deposit);
                else if (instr == "card ID")
                    Console.WriteLine("         " + date_create);
                else if (instr == "card ID" || instr == "total sum")
                    Console.WriteLine("\n");
            }
            else
                Console.WriteLine("Invalid input!!");
        }

        //Inheritance, subclass CreditAccount
        protected class CreditAccount : Account
        {
            //Constructor
            CreditAccount(long bank_cardID, string password)
            {
                this.bank_cardID = bank_cardID;
                this.account_password = password;
            }
            //new field
            private long line_of_credit;        //line of credit 

            //new property
            public string credit_rating { set; get; }

            //new method
            public long get_line_of_credit()        //line of credit according to the credit rating
            {
                if (credit_rating == "3" || credit_rating == "2")
                    line_of_credit = 50000;
                else if (credit_rating == "1" || credit_rating == "0")
                    line_of_credit = 10000;
                else
                    line_of_credit = 0;
                return line_of_credit;
            }

            //override method withdrawMoney()
            new public void withdrawMoney()
            {
                Console.Write("amount(withdraw): ");
                latest_withdraw = Convert.ToInt32(Console.ReadLine());
                if (latest_withdraw <= total_amount + line_of_credit)
                {
                    total_amount -= latest_withdraw;
                    this[2] = Convert.ToString(total_amount);
                    date_withdraw = DateTime.Now.ToString();
                    this[3] = Convert.ToString(latest_withdraw);
                    if (latest_withdraw >= total_amount)
                    {
                        Console.WriteLine("warning: you're using your credit!!   Withdraw successfully");
                        int temp = Convert.ToInt32(credit_rating);
                        credit_rating = Convert.ToString(--temp);
                        get_line_of_credit();
                    }
                }
                else
                {
                    Console.WriteLine("Lack of balance. Operation is refused\n");
                }
       
  • 15
    点赞
  • 102
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值