bankcustomer.cs

来自「c#标准教程适合与处于不同学习阶段的人」· CS 代码 · 共 158 行

CS
158
字号
using System;

namespace BankCustomerApp
{

	/// <summary>
	/// This class represents a single bank customer.
	/// </summary>
	public class BankCustomer : ICloneable
	{
		private string   m_FirstName; 
		private string   m_LastName;
		private decimal  m_Balance;

		/// <summary>
		/// Constructs a new BankCustomer from given names and balance.
		/// </summary>
		/// <param name="fn"></param>
		/// <param name="ln"></param>
		/// <param name="balance"></param>
		public BankCustomer(string fn, string ln, decimal balance)
		{
			this.m_FirstName = fn;
			this.m_LastName  = ln;
			this.m_Balance = balance;
		}

		/// <summary>
		/// Constructs a new BankCustomer from given name; balance is set to 0.00.
		/// </summary>
		/// <param name="fn"></param>
		/// <param name="ln"></param>
		public BankCustomer(string fn, string ln)  // initial balance = 0.00
		: this(fn, ln, 0.0M)
		{ }

		/// <summary>
		/// Allows access to customer's first name.
		/// </summary>
		public string FirstName
		{
			get { return this.m_FirstName; }
		}

		/// <summary>
		/// Allows access to customer's last name.
		/// </summary>
		public string LastName
		{
			get { return this.m_LastName; }
		}

		/// <summary>
		/// Allows access to customer's raw balance.
		/// </summary>
		public decimal Balance
		{
			get { return this.m_Balance; }
		}

		/// <summary>
		/// Provides balance as a nicely-formatted string.
		/// </summary>
		public string FormattedBalance
		{
			get { return this.GetFormattedBalance(); }
		}

		/// <summary>
		/// Returns balance formatted according to control panel's locality setting.
		/// </summary>
		/// <returns>currency-formatted string (e.g. "$1,000.99")</returns>
		public string GetFormattedBalance()
		{
			return this.Balance.ToString("C", System.Globalization.NumberFormatInfo.CurrentInfo);
		}

		//
		// methods
		//

		/// <summary>
		/// Deposits the given amount into the customer's account.
		/// </summary>
		/// <param name="amt">amount to deposit (must be >= 0)</param>
		public void Deposit(decimal amt)
		{
			if (amt < 0)
				throw new System.ArgumentException("BankCustomer.Deposit(amt): negative amount.");
			else
				this.m_Balance += amt;
		}

		/// <summary>
		/// Withdraws the given amount from the customer抯 account.
		/// </summary>
		/// <param name="amt">amount to withdraw (must be >= 0)</param>
		/// <exception cref="Exception">Throw if sufficient funds are not available</exception>
		public void Withdraw(decimal amt)
		{
			if (amt < 0)  // negative!
				throw new System.ArgumentException("BankCustomer.Withdraw(amt): negative amount.");
			else if (amt > this.Balance)  // insufficient funds!
				throw new System.ApplicationException("BankCustomer.Withdraw(amt): insufficient funds.");
			else  // go ahead, withdraw...
				this.m_Balance -= amt;
		}

		/// <summary>
		/// Returns string-based representation of object.
		/// </summary>
		/// <returns>string in format "lastname, firstname"</returns>
		public override string ToString()
		{
			return this.LastName + ", " + this.FirstName;
		}

		/// <summary>
		/// Returns true if THIS object has the same last and first name as the given object obj; comparison is
		/// case-sensitive.
		/// </summary>
		/// <param name="obj">object to be compared to (must be an instance of BankCustomer)</param>
		/// <returns>true if names are equal, false if not (case-sensitive compare)</returns>
		public override bool Equals(object obj)
		{
			if (obj == null) 
				return false;
			if ((obj.GetType().Equals(this.GetType())) == false)
				return false;

			BankCustomer  other;
			other = (BankCustomer) obj;

			return this.FirstName.Equals(other.FirstName) && 
				   this.LastName.Equals(other.LastName);
		}

		/// <summary>
		/// Returns an integer hash code for this object.
		/// </summary>
		/// <returns>integer code appropraite for hashing</returns>
		public override int GetHashCode()
		{
			return this.FirstName.GetHashCode() + this.LastName.GetHashCode();
		}

		/// <summary>
		/// Returns a shallow copy of this object.
		/// </summary>
		/// <returns>shallow copy</returns>
		public object Clone()
		{
			return this.MemberwiseClone();  // shallow copy
		}

	}//class
}//namespace

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?