向银行系统添加利率和透支费

Posted

技术标签:

【中文标题】向银行系统添加利率和透支费【英文标题】:Adding Interest Rate and Overdraft Fee to banking system 【发布时间】:2021-09-23 23:42:41 【问题描述】:

这就是我想要实现的目标:

如果请求提款超过允许的透支限额(100 美元),则不会提款,并且会从余额中扣除固定的固定费用(10 美元)。

在下面的代码中,我已尽力做到这一点,但它不起作用,因为当我提取超过余额的金额时,它不会给我带来失败的交易费用,而是给我一个负余额。我也不确定当用户点击他们想要查看的帐户详细信息时,我希望它显示失败的交易费用(如果有的话)如果用户透支了。

任何帮助将不胜感激。就是想不通。

class Program

    class Customer
    
        private string firstName;
        private string lastName;
        private int phoneNumber;
        private string address;

        public Customer()
        
            firstName = "Tayla";
            lastName = "Brown";
            phoneNumber = 027493922;
            address = "55 Grey Rd";
        

        //client info
        public string ClientInfo()
        
            string clientinfo = ("Account Holder: " + firstName + " " + lastName + " " + "Phone Number: " + phoneNumber + " " + "Address: " + address);
            return clientinfo;
        
    

    class Account : Customer
    
        // Fields

        private double accountNumber;
        protected string accountType;

        protected double balance;
        protected double deposit;
        protected double withdrawal;
        protected double interest;
        protected double fee;
        protected double overdraft;

        // Properties

        public string AccountType
        
            get  return this.accountType; 
        

        public double Withdrawal
        
            get  return this.withdrawal; 
            set  this.withdrawal = value; 
        
        public double Deposit
        
            get  return this.deposit; 
            set  this.deposit = value; 
        
        public double AcctNumber
         get  return this.accountNumber;  

        public double Bal
         get  return this.balance;  

        public double f
         get  return this.fee;  

        public double od
         get  return this.overdraft;  

        // Creating Random Account Number

        public virtual double AccountNumb()
        
            Random rand = new Random();
            this.accountNumber = rand.Next(100000000, 1000000000);
            return accountNumber;
        


        public virtual double Fee()                                                                    // Work out Fee
        
            overdraft = 100;
            fee = 10;

            if (balance < withdrawal)
            
                balance -= (overdraft + withdrawal);

            
            else if (balance > withdrawal)
            
                balance -= withdrawal;
                fee -= balance;
            
            return balance;
        

        //Computes General Balance(resets values)
        public virtual double Balance()
        
            balance = balance + deposit - withdrawal;
            deposit = 0;
            withdrawal = 0;
            return balance;
        

        //Computers Balance when withdrawal equals zero
        public virtual double DepositBalance(double input)
        
            deposit = input;
            withdrawal = 0;
            balance = balance + deposit - withdrawal;
            return balance;
        

        //Computers balance when deposit equals zero
        public virtual double WithBalance(double input)
        
            withdrawal = input;
            deposit = 0;
            balance = balance + deposit - withdrawal;
            return balance;
        

        //displays online banking menu
        public virtual void DisplayMenu()
        
            Console.WriteLine("Welcome to your online bank account\nPlease choose from the options below: ");
            Console.WriteLine("");
            Console.WriteLine("1.View Client Info");
            Console.WriteLine("");
            Console.WriteLine("2. View Account Balance:");
            Console.WriteLine("     2A.Everyday\n     2B.Investment\n     2C.Omni");
            Console.WriteLine("");
            Console.WriteLine("3.Deposit Funds:\n     3A.Everyday\n     3B.Investment\n     3C.Omni");
            Console.WriteLine("");
            Console.WriteLine("4.Withdraw Funds:\n     4A.Everyday\n     4B.Investment\n     4C.Omni");
            Console.WriteLine("");
            Console.WriteLine("5.View Everyday Banking Details");
            Console.WriteLine("6.View Investment Banking Details");
            Console.WriteLine("7.View Omni Banking details");
            Console.WriteLine("");
            Console.WriteLine("8.Exit");
        
    

    // Everyday Account

    class Everyday : Account
    
        //field

        private double minBalance;
        private double maxBalance;

        //properties
        public double MinBalance
         get  return this.minBalance;  

        public double MaxBalance
         get  return this.maxBalance;  

        //constructors

        public Everyday(double balance) : base()
        
            this.minBalance = 0;
            this.maxBalance = 1000000000000;
            this.balance = balance;
            accountType = "Everyday Account";
        

        //methods
    

    // Investment Account

    class Investment : Account
    
        //fields
        private int investmentInterest;
        private int feeInvestment;

        //properties
        public int InterestInvestment
        
            get  return this.investmentInterest;                                   // Sets Interest
            set  investmentInterest = 4; 
        

        public int FeeInvestment
        
            get  return this.feeInvestment;                                   // Sets Fee
            set  feeInvestment = 10; 
        

        //constructors
        public Investment(double balance) : base()
        
            this.balance = balance;
            accountType = "Investment Account";

        
        //methods
    

    // Omni Account

    class Omni : Account    // If a request is made to withdraw money over and above the allowed overdraft limit, then no money is withdrawn and 
                            // a fixed flat fee is deducted from the balance
    
        //fields
        private int interestOmni;
        private int overdraft;


        //properties
        public int InterestOmni
        
            get  return this.interestOmni;     // Get interest 
            set  interestOmni = 4; 
        
        public int OverDraftOmni
        
            get  return this.overdraft;      // Get overdraft
        

        //constructors
        public Omni(double balance) : base()
        
            interestOmni = 4;
            overdraft = 1000;                         // Overdraft same as min balance?
            this.balance = balance;
            accountType = "Omni Account";

        
    

    static void Main(string[] args)
    

        // Object Insantiating

        Account client = new Account(); // Our one client

        Everyday everyday = new Everyday(2000);
        everyday.AccountNumb();
        Investment investment = new Investment(500);
        investment.AccountNumb();
        Omni omni = new Omni(1000);
        omni.AccountNumb();


        string streamEverydayAccount = ("Account Number: " + everyday.AcctNumber);
        string streamInvestmentAccount = ("Account Number: " + investment.AcctNumber);
        string streamOmniAccount = ("Account Number: " + omni.AcctNumber);

        string streamClientInfo = (client.ClientInfo());

        string everydayAccountType = (everyday.AccountType);
        string investmentAccountType = (everyday.AccountType);
        string omniAccountType = (everyday.AccountType);

        // StreamWriter Everyday Account

        using (StreamWriter everydaysummary = new StreamWriter(@"EverydaySummary.txt", true))
        
            everydaysummary.WriteLine(everydayAccountType);
            everydaysummary.WriteLine(streamClientInfo);
            everydaysummary.WriteLine(streamEverydayAccount);
        

        // StreamWriter Investment Account

        using (StreamWriter investmentsummary = new StreamWriter(@"InvestmentSummary.txt", true))
        
            investmentsummary.WriteLine(investmentAccountType);
            investmentsummary.WriteLine(streamClientInfo);
            investmentsummary.WriteLine(streamInvestmentAccount);
        

        // StreamWriter Omni Account

        using (StreamWriter omnisummary = new StreamWriter(@"OmniSummary.txt", true))
        
            omnisummary.WriteLine(omniAccountType);
            omnisummary.WriteLine(streamClientInfo);
            omnisummary.WriteLine(streamOmniAccount);
        

        bool test = false;

        do
        
            string everydayDepositPara = ($"Transaction: +$everyday.Deposit at DateTime.Now.ToString() Current Balance: $everyday.Bal");
            string everdayWithdrawalPara = ($"Transaction: -$everyday.Withdrawal at DateTime.Now.ToString() Current Balance: $everyday.Bal");
            string investmentDepositPara = ($"Transaction: +$investment.Deposit at DateTime.Now.ToString() Current Balance: $investment.Bal");
            string investmentWithdrawalPara = ($"Transaction: -$investment.Withdrawal at DateTime.Now.ToString() Current Balance: $investment.Bal");
            string omniDepositPara = ($"Transaction: +$omni.Deposit at DateTime.Now.ToString() Current Balance: $omni.Bal");
            string omniWithdrawalPara = ($"Transaction: -$omni.Withdrawal at DateTime.Now.ToString() Current Balance: $omni.Bal");

            //INSTANTIATING STREAMWRITER FILES

            // Everyday

            using (StreamWriter everdayAccountStreamSummary = new StreamWriter("EverdaySummary.txt", true))
            
                if (everyday.Deposit > 0)
                
                    everdayAccountStreamSummary.WriteLine(everydayDepositPara);
                    everyday.Deposit = 0;
                
                if (everyday.Withdrawal > 0)
                
                    everdayAccountStreamSummary.WriteLine(everdayWithdrawalPara);
                    everyday.Withdrawal = 0;
                
            
            //Investment

            using (StreamWriter investmentAccountStreamSummary = new StreamWriter("InvestmentSummary.txt", true))
            
                if (investment.Deposit > 0)
                
                    investmentAccountStreamSummary.WriteLine(investmentDepositPara);
                    investment.Deposit = 0;
                
                if (investment.Withdrawal > 0)
                
                    investmentAccountStreamSummary.WriteLine(investmentWithdrawalPara);
                    investment.Withdrawal = 0;
                
            
            //Omni

            using (StreamWriter omniAccountStreamSummary = new StreamWriter("OmniSummary.txt", true))
            
                if (omni.Deposit > 0)
                
                    omniAccountStreamSummary.WriteLine(omniDepositPara);
                    omni.Deposit = 0;
                
                if (omni.Withdrawal > 0)
                
                    omniAccountStreamSummary.WriteLine(omniWithdrawalPara);
                    omni.Withdrawal = 0;
                
            

            client.DisplayMenu();//the online banking menu
            string userchoice = Console.ReadLine();//user input from menu options

            switch (userchoice.ToUpper())
            
                case "1": // Display Client Info
                    Console.Clear();
                    Console.WriteLine(client.ClientInfo());
                    break;
                case "2A": // Display Everday Account Balance
                    Console.Clear();
                    everyday.Balance();
                    Console.WriteLine("Everyday Account Balance: $" + everyday.Bal);
                    break;
                case "2B": // Display Investment Account Balance
                    Console.Clear();
                    investment.Balance();
                    Console.WriteLine("Investment Account Balance: $" + investment.Bal);
                    break;
                case "2C": // Display Omni Account Balance
                    Console.Clear();
                    omni.Balance();
                    Console.WriteLine("Omni Account Balance: $" + omni.Bal);
                    break;
                case "3A": // Everyday Account Deposit
                    Console.Clear();
                    Console.WriteLine("How much would you like to deposit?");
                    everyday.Deposit = double.Parse(Console.ReadLine());
                    Console.WriteLine("You deposited: $" + everyday.Deposit);
                    everyday.DepositBalance(everyday.Deposit);
                    break;
                case "3B":
                    Console.Clear(); // Investment Account Deposit
                    Console.WriteLine("How much would you like to deposit?");
                    investment.Deposit = double.Parse(Console.ReadLine());
                    Console.WriteLine("You deposited: $" + investment.Deposit);
                    investment.DepositBalance(investment.Deposit);
                    break;
                case "3C":
                    Console.Clear(); // Omni Account Deposit
                    Console.WriteLine("How much would you like to deposit?");
                    omni.Deposit = double.Parse(Console.ReadLine());
                    Console.WriteLine("You deposited: $" + omni.Deposit);
                    omni.DepositBalance(omni.Deposit);
                    break;
                case "4A": // Everyday account Withdrawal
                    Console.Clear();
                    Console.WriteLine("How much would you like to withdraw?");
                    everyday.Withdrawal = double.Parse(Console.ReadLine());
                    Console.WriteLine("You withdrew: $" + everyday.Withdrawal);
                    everyday.WithBalance(everyday.Withdrawal);
                    break;
                case "4B":
                    Console.Clear(); // Investment Account Withdrawal
                    Console.WriteLine("How much would you like to withdraw?");
                    investment.Withdrawal = double.Parse(Console.ReadLine());
                    Console.WriteLine("You withdrew: $" + investment.Withdrawal);
                    investment.WithBalance(investment.Withdrawal);
                    break;
                case "4C":
                    Console.Clear(); // Omni Account Withdrawal
                    Console.WriteLine("How much would you like to withdraw?");
                    omni.Withdrawal = double.Parse(Console.ReadLine());
                    Console.WriteLine("You withdrew: $" + omni.Withdrawal);
                    omni.WithBalance(omni.Withdrawal);
                    break;
                case "5":
                    Console.Clear(); // Everyday Details
                    Console.WriteLine("Everyday Banking Details");
                    everyday.Balance();
                    Console.WriteLine("Everyday Account Balance: $" + everyday.Bal);
                    break;
                case "6":
                    Console.Clear(); // Investment Details
                    Console.WriteLine("Investment Banking Details");
                    everyday.Balance();
                    Console.WriteLine("Investment Account Balance: $" + investment.Bal);                                                    // Add Interest Rate and Fee
                    Console.WriteLine("Interest Rate: " + investment.InterestInvestment + "%");
                    Console.WriteLine("Fee: "
                        );
                    break;
                case "7":
                    Console.Clear(); // Omni Details
                    Console.WriteLine("Omni Banking Details");
                    everyday.Balance();
                    Console.WriteLine("Omni Account Balance: $" + omni.Bal);
                    Console.WriteLine("Interest Rate: " + omni.InterestOmni + "%");
                    Console.WriteLine("Fee: ");
                    break;
                case "8":
                    Console.Clear(); // Exit Banking
                    Console.WriteLine("You have chosen to exit the online banking. Thanks and come again!");
                    Environment.Exit(0);
                    break;
                default: // catch all, breaks the loop
                    Console.Clear();
                    test = false;
                    break;
            
         while (!test);
    

【问题讨论】:

“在下面的代码中,我已经尽力做到了,但它不起作用” - 请提供您期望发生的情况以及实际发生的情况的详细信息.您可以编辑您的问题来执行此操作。 我已经编辑了我的描述。很抱歉,没有解释发生了什么。 @TaylaB - 你的代码真的坏了。你能发布你的实际代码吗? @Enigmativity 我更新了我的代码并被整个项目插入 Customer 的构造函数中没有默认数据。您已经引入了一个错误,并让Account 继承自Customer。您应该实例化一次Customer 并将数据传递给构造函数,然后让Account 在其构造函数中使用Customer 【参考方案1】:

我已经对您的代码进行了重构 - 真的很难理解您想要做什么 - 所以这是一个相当大的变化。我已经删除了很多重复项,但我无法真正完成所有内容,因为您的代码中也缺少很多内容。

我无法回答如何在您的代码中修复您的一种方法,因为您在帐户类中更改状态的方式真的很难遵循。

在我的重构中,我总是像这样计算Balance

    public decimal Balance => this.Opening + this.Deposits - this.Withdrawals + this.Interest - this.Fees;

所以现在只需正确获取Balance 的各个组件即可。

Withdraw 现在是如何做到这一点的:

    public (decimal Withdrawn, decimal Fee) Withdraw(decimal amount)
    
        if (amount <= 0m)
        
            throw new System.InvalidOperationException("Withdrawal amount must be positive");
        
        decimal fee = 0;
        if (amount > this.Overdraft)
        
            amount = 0m;
            fee = 10m;
        
        else if (this.Balance < amount)
        
            amount = this.Balance;
        
        this.Withdrawals -= amount;
        return (amount, fee);
    

如果我理解正确,当您尝试提取的金额超过Overdraft 金额时,您将获得$10 费用,并且没有任何内容被提取。我添加了一张支票,以确保您提取的金额不能超过余额。

这是我重构的其余部分。让我知道你需要讨论什么:

class Program

    public class Client
    
        private string _firstName;
        private string _lastName;
        private string _phoneNumber;
        private string _address;

        public Client(string firstName, string lastName, string phoneNumber, string address)
        
            _firstName = firstName;
            _lastName = lastName;
            _phoneNumber = phoneNumber;
            _address = address;
        

        public string ClientInfo =>
            $"Account Holder: _firstName _lastName Phone Number: _phoneNumber Address: _address";
    

    public abstract class Account
    
        private Client _client;

        public Account(Client client, int accountNumber, decimal opening)
        
            _client = client;
            this.AccountNumber = accountNumber;
            this.Opening = opening;
        

        public Account(Client client, decimal opening) : this(client, Account.GenerateAccountNumber(), opening)
         

        public int AccountNumber  get; private set; 

        public string AccountType => $"this.GetType().Name Account";

        public abstract decimal Overdraft  get; protected set; 

        public decimal Opening  get; protected set; 
        public decimal Deposits  get; protected set; 
        public decimal Withdrawals  get; protected set; 
        public decimal Interest  get; protected set; 
        public decimal Fees  get; protected set; 

        public decimal InterestRate  get; protected set;  = 4m;

        public decimal Balance => this.Opening + this.Deposits - this.Withdrawals + this.Interest - this.Fees;

        private static Random _random = new Random();
        public static int GenerateAccountNumber() => _random.Next(100000000, 1000000000);

        public (decimal Withdrawn, decimal Fee) Withdraw(decimal amount)
        
            if (amount <= 0m)
            
                throw new System.InvalidOperationException("Withdrawal amount must be positive");
            
            decimal fee = 0;
            if (amount > this.Overdraft)
            
                amount = 0m;
                fee = 10m;
            
            else if (this.Balance < amount)
            
                amount = this.Balance;
            
            this.Withdrawals += amount;
            this.Fees += fee;
            return (amount, fee);
        

        public decimal Deposit(decimal amount)
        
            if (amount <= 0m)
            
                throw new System.InvalidOperationException("Deposit amount must be positive");
            
            this.Deposits += amount;
            return amount;
        

        public IEnumerable<string> GetTransactions()
        
            var now = DateTime.Now;
            if (this.Deposits > 0m)
            
                yield return $"Deposits: +$this.Deposits at now.ToString() Current Balance: $this.Balance";
            
            if (this.Withdrawals > 0m)
            
                yield return $"Withdrawals: +$this.Withdrawals at now.ToString() Current Balance: $this.Balance";
            
        

        public string Summary => String.Join(Environment.NewLine, this.AccountType, _client.ClientInfo, "Account Number: " + this.AccountNumber);
    

    public class Everyday : Account
    
        public decimal MinBalance  get; private set;  = 0m;
        public decimal MaxBalance  get; private set;  = 1000000000000m;

        public override decimal Overdraft  get; protected set;  = 100m;

        public Everyday(Client client, decimal opening) : base(client, opening)
         

        public Everyday(Client client, int accountNumber, decimal opening) : base(client, accountNumber, opening)
         
    

    public class Investment : Account
    
        public decimal InvestmentFee  get; private set;  = 10m;

        public override decimal Overdraft  get; protected set;  = 100m;

        public Investment(Client client, decimal opening) : base(client, opening)
         

        public Investment(Client client, int accountNumber, decimal opening) : base(client, accountNumber, opening)
         
    

    class Omni : Account
    
        public override decimal Overdraft  get; protected set;  = 1000m;

        public Omni(Client client, decimal opening) : base(client, opening)
         

        public Omni(Client client, int accountNumber, decimal opening) : base(client, accountNumber, opening)
         
    

    private static void DisplayBalance(Account account)
    
        Console.WriteLine($"account.AccountType Balance: account.Balance:$#,##0.00");
    

    private static void DepositAmount(Account account)
    
        Console.WriteLine("How much would you like to deposit?");
        decimal deposited = account.Deposit(decimal.Parse(Console.ReadLine()));
        Console.WriteLine($"You deposited: deposited:$#,##0.00 into your account.AccountType");
    

    private static void WithdrawAmount(Account account)
    
        Console.WriteLine("How much would you like to withdraw?");
        var result = account.Withdraw(decimal.Parse(Console.ReadLine()));
        Console.WriteLine($"You withdrew: result.Withdrawn:$#,##0.00");
        if (result.Fee != 0m)
        
            Console.WriteLine($"With fee: result.Fee:$#,##0.00");
        
    

    private static void DisplayDetails(Everyday account)
    
        Console.WriteLine("Everyday Banking Details");
        Console.WriteLine($"account.AccountType Balance: account.Balance:$#,##0.00");
    

    private static void DisplayDetails(Investment account)
    
        Console.WriteLine("Investment Banking Details");
        Console.WriteLine($"account.AccountType Balance: account.Balance:$#,##0.00");
        Console.WriteLine("Interest Rate: " + account.InterestRate + "%");
        Console.WriteLine("Fee: ");
    

    private static void DisplayDetails(Omni account)
    
        Console.WriteLine("Omni Banking Details");
        Console.WriteLine($"account.AccountType Balance: account.Balance:$#,##0.00");
        Console.WriteLine("Interest Rate: " + account.InterestRate + "%");
        Console.WriteLine("Fee: ");
    

    private static void Main(string[] args)
    
        bool test = false;

        Client client = new Client("Tayla", "Brown", "027493922", "55 Grey Rd"); // Our one client

        Everyday everyday = new Everyday(client, Account.GenerateAccountNumber(), 2000m);
        Investment investment = new Investment(client, 500m);
        Omni omni = new Omni(client, 1000m);

        File.WriteAllText(@"EverydaySummary.txt", everyday.Summary);
        File.WriteAllText(@"InvestmentSummary.txt", investment.Summary);
        File.WriteAllText(@"OmniSummary.txt", omni.Summary);

        do
        
            File.WriteAllLines("EverdaySummary.txt", everyday.GetTransactions());
            File.WriteAllLines("InvestmentSummary.txt", investment.GetTransactions());
            File.WriteAllLines("OmniSummary.txt", omni.GetTransactions());

            DisplayMenu();
            string userchoice = Console.ReadLine();//user input from menu options

            switch (userchoice.ToUpper())
            
                case "1": // Display Client Info
                          //Console.Clear();
                    Console.WriteLine(client.ClientInfo);
                    break;
                case "2A": // Display Everday Account Balance
                           //Console.Clear();
                    DisplayBalance(everyday);
                    break;
                case "2B": // Display Investment Account Balance
                           //Console.Clear();
                    DisplayBalance(investment);
                    break;
                case "2C": // Display Omni Account Balance
                           //Console.Clear();
                    DisplayBalance(omni);
                    break;
                case "3A": // Everyday Account Deposit
                           //Console.Clear();
                    DepositAmount(everyday);
                    break;
                case "3B":
                    //Console.Clear(); // Investment Account Deposit
                    DepositAmount(investment);
                    break;
                case "3C":
                    //Console.Clear(); // Omni Account Deposit
                    DepositAmount(omni);
                    break;
                case "4A": // Everyday account Withdrawal
                           //Console.Clear();
                    WithdrawAmount(everyday);
                    break;
                case "4B":
                    //Console.Clear(); // Investment Account Withdrawal
                    WithdrawAmount(investment);
                    break;
                case "4C":
                    //Console.Clear(); // Omni Account Withdrawal
                    WithdrawAmount(omni);
                    break;
                case "5":
                    //Console.Clear(); // Everyday Details
                    DisplayDetails(everyday);
                    break;
                case "6":
                    //Console.Clear(); // Investment Details
                    DisplayDetails(investment);
                    break;
                case "7":
                    //Console.Clear(); // Omni Details
                    DisplayDetails(omni);
                    break;
                case "8":
                    //Console.Clear(); // Exit Banking
                    Console.WriteLine("You have chosen to exit the online banking. Thanks and come again!");
                    test = true;
                    break;
                default: // catch all, breaks the loop
                         //Console.Clear();
                    test = false;
                    break;
            
         while (!test);
    

    //displays online banking menu
    private static void DisplayMenu()
    
        Console.WriteLine("Welcome to your online bank account\nPlease choose from the options below: ");
        Console.WriteLine("");
        Console.WriteLine("1.View Client Info");
        Console.WriteLine("");
        Console.WriteLine("2. View Account Balance:");
        Console.WriteLine("     2A.Everyday\n     2B.Investment\n     2C.Omni");
        Console.WriteLine("");
        Console.WriteLine("3.Deposit Funds:\n     3A.Everyday\n     3B.Investment\n     3C.Omni");
        Console.WriteLine("");
        Console.WriteLine("4.Withdraw Funds:\n     4A.Everyday\n     4B.Investment\n     4C.Omni");
        Console.WriteLine("");
        Console.WriteLine("5.View Everyday Banking Details");
        Console.WriteLine("6.View Investment Banking Details");
        Console.WriteLine("7.View Omni Banking details");
        Console.WriteLine("");
        Console.WriteLine("8.Exit");
    


这里有一些测试代码表明现在应该可以工作了。

private static void Main(string[] args)

    Client client = new Client("Tayla", "Brown", "027493922", "55 Grey Rd");
    Everyday everyday = new Everyday(client, Account.GenerateAccountNumber(), 30m);

    Console.WriteLine($"Balance: everyday.Balance");
    var r1 = everyday.Withdraw(15m);
    Console.WriteLine($"Withdrawn: r1.Withdrawn, Fee: r1.Fee, Balance: everyday.Balance");
    var r2 = everyday.Withdraw(1000m);
    Console.WriteLine($"Withdrawn: r2.Withdrawn, Fee: r2.Fee, Balance: everyday.Balance");
    var r3 = everyday.Withdraw(10m);
    Console.WriteLine($"Withdrawn: r3.Withdrawn, Fee: r3.Fee, Balance: everyday.Balance");

这给了:

Balance: 30
Withdrawn: 15, Fee: 0, Balance: 15
Withdrawn: 0, Fee: 10, Balance: 5
Withdrawn: 5, Fee: 0, Balance: 0

这是一个带有交易的版本。

class Program

    public class Client
    
        private string _firstName;
        private string _lastName;
        private string _phoneNumber;
        private string _address;

        public Client(string firstName, string lastName, string phoneNumber, string address)
        
            _firstName = firstName;
            _lastName = lastName;
            _phoneNumber = phoneNumber;
            _address = address;
        

        public string ClientInfo =>
            $"Account Holder: _firstName _lastName Phone Number: _phoneNumber Address: _address";
    

    public abstract class Account
    
        private Client _client;

        public Account(Client client, int accountNumber, decimal opening)
        
            _client = client;
            this.AccountNumber = accountNumber;
            this.Transactions.Add(Transaction.Create(DateTime.Now, opening, TransactionType.Opening));
        

        public Account(Client client, decimal opening) : this(client, Account.GenerateAccountNumber(), opening)
         

        public int AccountNumber  get; private set; 

        public string AccountType => $"this.GetType().Name Account";

        public abstract decimal Overdraft  get; protected set; 

        public List<Transaction> Transactions  get; private set;  = new List<Transaction>();

        public decimal InterestRate  get; protected set;  = 4m;

        public decimal Balance => this.Transactions.Sum(t => t.Amount);

        private static Random _random = new Random();
        public static int GenerateAccountNumber() => _random.Next(100000000, 1000000000);

        public (decimal Withdrawn, decimal Fee) Withdraw(decimal amount)
        
            if (amount <= 0m)
            
                throw new System.InvalidOperationException("Withdrawal amount must be positive");
            
            decimal fee = 0;
            if (amount > this.Overdraft)
            
                amount = 0m;
                fee = 10m;
            
            else if (this.Balance < amount)
            
                amount = this.Balance;
            
            if (amount > 0m)
            
                this.Transactions.Add(Transaction.Create(DateTime.Now, amount, TransactionType.Withdrawal));
            
            if (fee > 0m)
            
                this.Transactions.Add(Transaction.Create(DateTime.Now, fee, TransactionType.Fee));
            
            return (amount, fee);
        

        public decimal Deposit(decimal amount)
        
            if (amount <= 0m)
            
                throw new System.InvalidOperationException("Deposit amount must be positive");
            
            this.Transactions.Add(Transaction.Create(DateTime.Now, amount, TransactionType.Deposit));
            return amount;
        

        public string Summary => String.Join(Environment.NewLine, this.AccountType, _client.ClientInfo, "Account Number: " + this.AccountNumber);
    

    public class Everyday : Account
    
        public decimal MinBalance  get; private set;  = 0m;
        public decimal MaxBalance  get; private set;  = 1000000000000m;

        public override decimal Overdraft  get; protected set;  = 100m;

        public Everyday(Client client, decimal opening) : base(client, opening)
         

        public Everyday(Client client, int accountNumber, decimal opening) : base(client, accountNumber, opening)
         
    

    public class Investment : Account
    
        public decimal InvestmentFee  get; private set;  = 10m;

        public override decimal Overdraft  get; protected set;  = 100m;

        public Investment(Client client, decimal opening) : base(client, opening)
         

        public Investment(Client client, int accountNumber, decimal opening) : base(client, accountNumber, opening)
         
    

    class Omni : Account
    
        public override decimal Overdraft  get; protected set;  = 1000m;

        public Omni(Client client, decimal opening) : base(client, opening)
         

        public Omni(Client client, int accountNumber, decimal opening) : base(client, accountNumber, opening)
         
    
    
    public class Transaction
    
        public DateTime When  get; private set; 
        public decimal Amount  get; private set; 
        public TransactionType Type  get; private set; 
        
        public static Transaction Create(DateTime when, decimal amount, TransactionType type)
        
            if (amount <= 0m)
            
                throw new System.InvalidOperationException("Transaction amount must be positive");
            
            return new Transaction(when, (type == TransactionType.Withdrawal || type == TransactionType.Fee) ? -amount : amount, type);
        
        
        private Transaction(DateTime when, decimal amount, TransactionType type)
        
            this.When = when;
            this.Amount = amount;
            this.Type = type;
        
    

    public enum TransactionType  Opening, Deposit, Withdrawal, Fee, Interest 
    
    private static void DisplayBalance(Account account)
    
        Console.WriteLine($"account.AccountType Balance: account.Balance:$#,##0.00");
    

    private static void DepositAmount(Account account)
    
        Console.WriteLine("How much would you like to deposit?");
        decimal deposited = account.Deposit(decimal.Parse(Console.ReadLine()));
        Console.WriteLine($"You deposited: deposited:$#,##0.00 into your account.AccountType");
    

    private static void WithdrawAmount(Account account)
    
        Console.WriteLine("How much would you like to withdraw?");
        var result = account.Withdraw(decimal.Parse(Console.ReadLine()));
        Console.WriteLine($"You withdrew: result.Withdrawn:$#,##0.00");
        if (result.Fee != 0m)
        
            Console.WriteLine($"With fee: result.Fee:$#,##0.00");
        
    

    private static void DisplayDetails(Everyday account)
    
        Console.WriteLine("Everyday Banking Details");
        Console.WriteLine($"account.AccountType Balance: account.Balance:$#,##0.00");
    

    private static void DisplayDetails(Investment account)
    
        Console.WriteLine("Investment Banking Details");
        Console.WriteLine($"account.AccountType Balance: account.Balance:$#,##0.00");
        Console.WriteLine("Interest Rate: " + account.InterestRate + "%");
        Console.WriteLine("Fee: ");
    

    private static void DisplayDetails(Omni account)
    
        Console.WriteLine("Omni Banking Details");
        Console.WriteLine($"account.AccountType Balance: account.Balance:$#,##0.00");
        Console.WriteLine("Interest Rate: " + account.InterestRate + "%");
        Console.WriteLine("Fee: ");
    

    private static void Main(string[] args)
    
        bool test = false;

        Client client = new Client("Tayla", "Brown", "027493922", "55 Grey Rd"); // Our one client

        Everyday everyday = new Everyday(client, Account.GenerateAccountNumber(), 2000m);
        Investment investment = new Investment(client, 500m);
        Omni omni = new Omni(client, 1000m);

        File.WriteAllText(@"EverydaySummary.txt", everyday.Summary);
        File.WriteAllText(@"InvestmentSummary.txt", investment.Summary);
        File.WriteAllText(@"OmniSummary.txt", omni.Summary);

        do
        
            File.WriteAllLines("EverdaySummary.txt", everyday.Transactions.Select(t => $"t.When t.Type t.Amount"));
            File.WriteAllLines("InvestmentSummary.txt", investment.Transactions.Select(t => $"t.When t.Type t.Amount"));
            File.WriteAllLines("OmniSummary.txt", omni.Transactions.Select(t => $"t.When t.Type t.Amount"));

            DisplayMenu();
            string userchoice = Console.ReadLine();//user input from menu options

            switch (userchoice.ToUpper())
            
                case "1": // Display Client Info
                          //Console.Clear();
                    Console.WriteLine(client.ClientInfo);
                    break;
                case "2A": // Display Everday Account Balance
                           //Console.Clear();
                    DisplayBalance(everyday);
                    break;
                case "2B": // Display Investment Account Balance
                           //Console.Clear();
                    DisplayBalance(investment);
                    break;
                case "2C": // Display Omni Account Balance
                           //Console.Clear();
                    DisplayBalance(omni);
                    break;
                case "3A": // Everyday Account Deposit
                           //Console.Clear();
                    DepositAmount(everyday);
                    break;
                case "3B":
                    //Console.Clear(); // Investment Account Deposit
                    DepositAmount(investment);
                    break;
                case "3C":
                    //Console.Clear(); // Omni Account Deposit
                    DepositAmount(omni);
                    break;
                case "4A": // Everyday account Withdrawal
                           //Console.Clear();
                    WithdrawAmount(everyday);
                    break;
                case "4B":
                    //Console.Clear(); // Investment Account Withdrawal
                    WithdrawAmount(investment);
                    break;
                case "4C":
                    //Console.Clear(); // Omni Account Withdrawal
                    WithdrawAmount(omni);
                    break;
                case "5":
                    //Console.Clear(); // Everyday Details
                    DisplayDetails(everyday);
                    break;
                case "6":
                    //Console.Clear(); // Investment Details
                    DisplayDetails(investment);
                    break;
                case "7":
                    //Console.Clear(); // Omni Details
                    DisplayDetails(omni);
                    break;
                case "8":
                    //Console.Clear(); // Exit Banking
                    Console.WriteLine("You have chosen to exit the online banking. Thanks and come again!");
                    test = true;
                    break;
                default: // catch all, breaks the loop
                         //Console.Clear();
                    test = false;
                    break;
            
         while (!test);
    

    //displays online banking menu
    private static void DisplayMenu()
    
        Console.WriteLine("Welcome to your online bank account\nPlease choose from the options below: ");
        Console.WriteLine("");
        Console.WriteLine("1.View Client Info");
        Console.WriteLine("");
        Console.WriteLine("2. View Account Balance:");
        Console.WriteLine("     2A.Everyday\n     2B.Investment\n     2C.Omni");
        Console.WriteLine("");
        Console.WriteLine("3.Deposit Funds:\n     3A.Everyday\n     3B.Investment\n     3C.Omni");
        Console.WriteLine("");
        Console.WriteLine("4.Withdraw Funds:\n     4A.Everyday\n     4B.Investment\n     4C.Omni");
        Console.WriteLine("");
        Console.WriteLine("5.View Everyday Banking Details");
        Console.WriteLine("6.View Investment Banking Details");
        Console.WriteLine("7.View Omni Banking details");
        Console.WriteLine("");
        Console.WriteLine("8.Exit");
    

【讨论】:

嘿,非常感谢。但是当我运行这段代码时,我仍然可以提取可用余额。是的,你是正确的,我希望它不让用户超过 100 次透支限制,如果他们这样做,那么给他们 20 的费用。 我在Everyday everyday = new Everyday(client, 2000, Account.GenerateAccountNumber()); 上写错了——应该是Everyday everyday = new Everyday(client, Account.GenerateAccountNumber(), 2000m);。 C# 做了一些隐式转换,允许代码编译它不应该编译。 @TaylaB - 我正在努力确保代码适用于提款。 我犯的另一个错误是在提款代码中。应该是this.Withdrawals += amount;,而不是this.Withdrawals -= amount; @TaylaB - 我忘记了 this.Fees += fee; 方法中的 Withdraw 行。

以上是关于向银行系统添加利率和透支费的主要内容,如果未能解决你的问题,请参考以下文章

Java基础-实验7Banking_7 -添加银行透支扣款系统的 thorw异常机制

银行透支罚款是不是与技术限制有关? [关闭]

银行账户透支方式

邮储银行手机银行finish校验失败怎么回事

如何在 C# 中获取利率和银行同业拆借利率?

12月主要银行最新定期存款利率表一览