C#面向对象
2009-11-05 16:51:48 来源:WEB开发网核心提示:1、写一个账户类(Account),属性:Id:账户号码PassWord:账户密码Name:真实姓名PersonId:身份证号码 Email:客户的电子邮箱Balance:账户余额 方法:Deposit:存款方法,参数是double型的金额Withdraw:取款方法,参数是double型的金额 构造方法:有参和无参,有
1、写一个账户类(Account),属性:
Id:账户号码
PassWord:账户密码
Name:真实姓名
PersonId:身份证号码
Email:客户的电子邮箱
Balance:账户余额
方法:
Deposit:存款方法,参数是double型的金额
Withdraw:取款方法,参数是double型的金额
构造方法:
有参和无参,有参构造方法用于设置必要的属性
2、银行的客户分为两类,储蓄账户(SavingAccount)和信用账户(CreditAccount),区别在于储蓄账户不允许透支,而信用账户可以透支,并允许用户设置自己的透支额度(使用ceiling表示).
为这两种用户编写相关的类
3、编写Bank类,属性:
(1)当前所有的账户对象的集合
(2)当前账户数量
方法:
(1)用户开户:
需要的参数:id、密码、姓名、身份证号码、邮箱、账户类型(储蓄账户还是信用账户);
(2)用户登录:
参数:id、密码;返回Account对象
(3)用户存款:
参数:id、存款数额
(4)用户取款:参数:id、取款数额
(5)设置透支额度:参数:id、新的额度;这个方法需要验证账户是否是信用账户
用户会通过调用Bank对象以上的方法来操作自己的账户,请分析各个方法需要的参数
另外,请为Bank类添加几个统计方法
(6)统计银行所有账户余额总数
(7)统计所有信用账户透支额度总数
4、写个主方法测试你写的类
Account.cs
view plaincopy to clipboardPRint?
using System;
using System.Collections.Generic;
using System.Text;
namespace EXE1
{
abstract class Account
{
//账户号码
protected long id;
public long ID
{
get { return id; }
set { id = value; }
}
//账户密码
protected string password;
public string Password
{
get { return password; }
set { password = value; }
}
//户主姓名
protected string name;
public string Name
{
get { return name; }
set { name = value; }
}
//身份证号码
protected string personId;
public string PersonId
{
get { return personId; }
set { personId = value; }
}
//email
protected string email;
public string Email
{
get { return email; }
set { email = value; }
}
protected double balance;
public double Balance
{
get { return balance; }
set { balance = value; }
}
//静态的id号码生成器
private static long idBuilder = 100000;
public static long IdBuilder
{
get { return idBuilder; }
set { idBuilder = value; }
}
/// <summary>
/// 存款
/// </summary>
/// <param name="sum"></param>
public void Deposit(double sum)
{
if(sum<0)
{
throw new InvalidOperationException("输入的金额为负数!");
}
balance += sum;
}
/// <summary>
/// 抽象函数:取款,需要在子类中重写此方法
/// </summary>
/// <param name="sum"></param>
public abstract void Withdraw(double sum);
public Account()
{
}
/// <summary>
/// 有参数构造函数
/// </summary>
public Account(string password,string name,string personId,string email)
{
this.id = ++idBuilder;
this.password = password;
this.name = name;
this.personId = personId;
this.email = email;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace EXE1
{
abstract class Account
{
//账户号码
protected long id;
public long ID
{
get { return id; }
set { id = value; }
}
//账户密码
protected string password;
public string Password
{
get { return password; }
set { password = value; }
}
//户主姓名
protected string name;
public string Name
{
get { return name; }
set { name = value; }
}
//身份证号码
protected string personId;
public string PersonId
{
get { return personId; }
set { personId = value; }
}
//email
protected string email;
public string Email
{
get { return email; }
set { email = value; }
}
protected double balance;
public double Balance
{
get { return balance; }
set { balance = value; }
}
//静态的id号码生成器
private static long idBuilder = 100000;
public static long IdBuilder
{
get { return idBuilder; }
set { idBuilder = value; }
}
/// <summary>
/// 存款
/// </summary>
/// <param name="sum"></param>
public void Deposit(double sum)
{
if(sum<0)
{
throw new InvalidOperationException("输入的金额为负数!");
}
balance += sum;
}
/// <summary>
/// 抽象函数:取款,需要在子类中重写此方法
/// </summary>
/// <param name="sum"></param>
public abstract void Withdraw(double sum);
public Account()
{
}
/// <summary>
/// 有参数构造函数
/// </summary>
public Account(string password,string name,string personId,string email)
{
this.id = ++idBuilder;
this.password = password;
this.name = name;
this.personId = personId;
this.email = email;
}
}
}
CreditAccount.cs
view plaincopy to clipboardprint?
using System;
using System.Collections.Generic;
using System.Text;
namespace EXE1
{
class CreditAccount : Account
{
protected double ceiling;
public double Ceiling
{
get { return ceiling; }
set { ceiling = value; }
}
public CreditAccount(string password, string name, string personId, string email)
: base(password, name, personId, email)
{
}
/// <summary>
/// 信用账户的取款操作
/// </summary>
public override void Withdraw(double sum)
{
if (sum < 0)
{
throw new InvalidOperationException("输入的金额为负数!");
}
if (sum > balance+ceiling)
{
throw new InvalidOperationException("金额已经超出余额和透支度的总数!");
}
balance -= sum;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace EXE1
{
class CreditAccount : Account
{
protected double ceiling;
public double Ceiling
{
get { return ceiling; }
set { ceiling = value; }
}
public CreditAccount(string password, string name, string personId, string email)
: base(password, name, personId, email)
{
}
/// <summary>
/// 信用账户的取款操作
/// </summary>
public override void Withdraw(double sum)
{
if (sum < 0)
{
throw new InvalidOperationException("输入的金额为负数!");
}
if (sum > balance+ceiling)
{
throw new InvalidOperationException("金额已经超出余额和透支度的总数!");
}
balance -= sum;
}
}
}
SavingAccount.cs
view plaincopy to clipboardprint?
using System;
using System.Collections.Generic;
using System.Text;
namespace EXE1
{
class SavingAccount:Account
{
public SavingAccount(string password, string name, string personId, string email)
:base(password,name,personId,email)
{
}
/// <summary>
/// Withdraw from saving account.
/// </summary>
/// <param name="sum"></param>
public override void Withdraw(double sum)
{
if (sum < 0)
{
throw new InvalidOperationException("输入的金额为负数!");
}
if (sum > balance)
{
throw new InvalidOperationException("金额已经超出余额!");
}
balance -= sum;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace EXE1
{
class SavingAccount:Account
{
public SavingAccount(string password, string name, string personId, string email)
:base(password,name,personId,email)
{
}
/// <summary>
/// Withdraw from saving account.
/// </summary>
/// <param name="sum"></param>
public override void Withdraw(double sum)
{
if (sum < 0)
{
throw new InvalidOperationException("输入的金额为负数!");
}
if (sum > balance)
{
throw new InvalidOperationException("金额已经超出余额!");
}
balance -= sum;
}
}
}
Bank.cs
view plaincopy to clipboardprint?
using System;
using System.Collections.Generic;
using System.Text;
namespace EXE1
{
/// <summary>
/// Bank类,对当前银行中的所有账户进行管理
/// </summary>
class Bank
{
//存放账户的集合
private List<Account> accounts;
public List<Account> Accounts
{
get { return accounts; }
set { accounts = value; }
}
//当前银行的账户数量
private uint currentAccountNumber;
public uint CurrentAccountNumber
{
get { return currentAccountNumber; }
set { currentAccountNumber = value; }
}
/// <summary>
/// 构造函数.
/// </summary>
public Bank()
{
accounts=new List<Account>();
}
/// <summary>
/// 开户;
/// typeOfAccount:1 for saving account, 2 for credit account, 3 for loan saving account, 4 for loan credit account..
/// </summary>
public Account OpenAccount(string password, string confirmationPassword, string name, string personId, string email, int typeOfAccount)
{
Account newAccount;
if (!password.Equals(confirmationPassword))
{
throw new InvalidOperationException("两次密码输入的不一致!");
}
switch (typeOfAccount)
{
case 1: newAccount=new SavingAccount(password, name, personId, email); break;
case 2: newAccount=new CreditAccount(password, name, personId, email); break;
default: throw new ArgumentOutOfRangeException("账户类型是1到2之间的整数");
}
//把新开的账户加到集合中
accounts.Add(newAccount);
return newAccount;
}
/// <summary>
/// 根据账户id得到账户对象
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
private Account GetAccountByID(long id)
{
foreach (Account account in accounts)
{
if (account.ID == id)
{
return account;
}
}
return null;
}
/// <summary>
/// 根据账号和密码登陆账户
/// </summary>
public Account SignIn(long id, string password)
{
foreach (Account account in accounts)
{
if (account.ID == id && account.Password.Equals(password))
{
return account;
}
}
throw new InvalidOperationException("用户名或者密码不正确,请重试!");
return null;
}
/// <summary>
/// 存款
/// </summary>
public Account Deposit(long id, double sum)
{
Account account = GetAccountByID(id);
if (account != null)
{
account.Deposit(sum);
return account;
}
throw new InvalidOperationException("非法账号!");
return null;
}
/// <summary>
/// 取款
/// </summary>
public Account Withdraw(long id, double sum)
{
Account account = GetAccountByID(id);
if (account != null)
{
account.Withdraw(sum);
return account;
}
throw new InvalidOperationException("非法账号!");
return null;
}
/// <summary>
/// 设置透支额度
/// </summary>
public Account SetCeiling(long id, double newCeiling)
{
Account account = GetAccountByID(id);
try
{
(account as CreditAccount).Ceiling = newCeiling;
return account;
}
catch (Exception ex)
{
throw new InvalidOperationException("此账户不是信用账户!");
return account;
}
throw new InvalidOperationException("非法账号!");
return null;
}
/// <summary>
/// 统计银行所有账户余额总数
/// </summary>
/// <returns></returns>
public double GetTotalBalance()
{
double totalBalance = 0;
foreach (Account account in accounts)
{
totalBalance += account.Balance;
}
return totalBalance;
}
/// <summary>
/// 统计所有信用账户透支额度总数
/// </summary>
public double GetTotalCeiling()
{
double totalCeiling = 0;
foreach (Account account in accounts)
{
if (account is CreditAccount)
{
totalCeiling += (account as CreditAccount).Ceiling;
}
}
return totalCeiling;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace EXE1
{
/// <summary>
/// Bank类,对当前银行中的所有账户进行管理
/// </summary>
class Bank
{
//存放账户的集合
private List<Account> accounts;
public List<Account> Accounts
{
get { return accounts; }
set { accounts = value; }
}
//当前银行的账户数量
private uint currentAccountNumber;
public uint CurrentAccountNumber
{
get { return currentAccountNumber; }
set { currentAccountNumber = value; }
}
/// <summary>
/// 构造函数.
/// </summary>
public Bank()
{
accounts=new List<Account>();
}
/// <summary>
/// 开户;
/// typeOfAccount:1 for saving account, 2 for credit account, 3 for loan saving account, 4 for loan credit account..
/// </summary>
public Account OpenAccount(string password, string confirmationPassword, string name, string personId, string email, int typeOfAccount)
{
Account newAccount;
if (!password.Equals(confirmationPassword))
{
throw new InvalidOperationException("两次密码输入的不一致!");
}
switch (typeOfAccount)
{
case 1: newAccount=new SavingAccount(password, name, personId, email); break;
case 2: newAccount=new CreditAccount(password, name, personId, email); break;
default: throw new ArgumentOutOfRangeException("账户类型是1到2之间的整数");
}
//把新开的账户加到集合中
accounts.Add(newAccount);
return newAccount;
}
/// <summary>
/// 根据账户id得到账户对象
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
private Account GetAccountByID(long id)
{
foreach (Account account in accounts)
{
if (account.ID == id)
{
return account;
}
}
return null;
}
/// <summary>
/// 根据账号和密码登陆账户
/// </summary>
public Account SignIn(long id, string password)
{
foreach (Account account in accounts)
{
if (account.ID == id && account.Password.Equals(password))
{
return account;
}
}
throw new InvalidOperationException("用户名或者密码不正确,请重试!");
return null;
}
/// <summary>
/// 存款
/// </summary>
public Account Deposit(long id, double sum)
{
Account account = GetAccountByID(id);
if (account != null)
{
account.Deposit(sum);
return account;
}
throw new InvalidOperationException("非法账号!");
return null;
}
/// <summary>
/// 取款
/// </summary>
public Account Withdraw(long id, double sum)
{
Account account = GetAccountByID(id);
if (account != null)
{
account.Withdraw(sum);
return account;
}
throw new InvalidOperationException("非法账号!");
return null;
}
/// <summary>
/// 设置透支额度
/// </summary>
public Account SetCeiling(long id, double newCeiling)
{
Account account = GetAccountByID(id);
try
{
(account as CreditAccount).Ceiling = newCeiling;
return account;
}
catch (Exception ex)
{
throw new InvalidOperationException("此账户不是信用账户!");
return account;
}
throw new InvalidOperationException("非法账号!");
return null;
}
/// <summary>
/// 统计银行所有账户余额总数
/// </summary>
/// <returns></returns>
public double GetTotalBalance()
{
double totalBalance = 0;
foreach (Account account in accounts)
{
totalBalance += account.Balance;
}
return totalBalance;
}
/// <summary>
/// 统计所有信用账户透支额度总数
/// </summary>
public double GetTotalCeiling()
{
double totalCeiling = 0;
foreach (Account account in accounts)
{
if (account is CreditAccount)
{
totalCeiling += (account as CreditAccount).Ceiling;
}
}
return totalCeiling;
}
}
}
客户端测试:
view plaincopy to clipboardprint?
using System;
using System.Collections.Generic;
using System.Text;
namespace EXE1
{
class Program
{
/// <summary>
/// Sign when needed.
/// </summary>
/// <returns></returns>
static Account SignIn(Bank icbc)
{
Console.WriteLine("\nPlease input your account ID:");
long id;
try
{
id = long.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Invalid account ID!");
return null;
}
Console.WriteLine("Please input your password:");
string password = Console.ReadLine();
Account account;
try
{
account = icbc.SignIn(id, password);
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
return null;
}
return account;
}
/// <summary>
/// Simulate an ATM
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
Bank icbc = new Bank();
while (true)
{
Console.WriteLine("\nPlease choose the service you need:");
Console.WriteLine("(1) Open a New Account");
Console.WriteLine("(2) Deposit");
Console.WriteLine("(3) Withdraw");
Console.WriteLine("(4) Set Ceiling");
Console.WriteLine("(5) Get Total Balance");
Console.WriteLine("(6) Get Total Ceiling");
Console.WriteLine("(0) Exit");
string choice;
choice = Console.ReadKey().KeyChar.ToString();
switch (choice)
{
case "1":
{
string personId;
int typeOfAccount;
string password;
Console.WriteLine("\nWhich kind of account do you want to open?");
while (true)
{
Console.WriteLine("(1) Saving Account\n(2) Credit Account\n(3) Return to Last Menu");
try
{
typeOfAccount = int.Parse(Console.ReadKey().KeyChar.ToString());
}
catch (FormatException)
{
Console.WriteLine("\nInvalid option, please choose again!");
continue;
}
if (typeOfAccount < 1 || typeOfAccount > 5)
{
Console.WriteLine("\nInvalid option, please choose again!");
continue;
}
break;
}
if (typeOfAccount == 5)
{
break;
}
Console.WriteLine("\nPlese input your name:");
string name = Console.ReadLine();
while (true)
{
Console.WriteLine("Please input your Personal ID:");
personId = Console.ReadLine();
if (personId.Length != 18)
{
Console.WriteLine("Invalid Personal ID, please input again!");
continue;
}
break;
}
Console.WriteLine("Please input your E-mail:");
string email = Console.ReadLine();
while (true)
{
Console.WriteLine("Please input your password:");
password = Console.ReadLine();
Console.WriteLine("Please confirm your password:");
if (password != Console.ReadLine())
{
Console.WriteLine("The password doesn't math!");
continue;
}
break;
}
Account account = icbc.OpenAccount(password, password, name, personId, email, typeOfAccount);
Console.WriteLine("The account opened successfully.");
Console.WriteLine("Account ID: {0}\nAccount Name: {1}\nPerson ID: {2}\nE-mail: {3}\nBalance: {4}", account.ID, account.Name, account.PersonId, account.Email, account.Balance);
break;
}
case "2":
{
Account account = SignIn(icbc);
if (account == null)
{
break;
}
int amount;
while (true)
{
Console.WriteLine("Please input the amount:");
try
{
amount = int.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Invalid input!");
continue;
}
break;
}
try
{
icbc.Deposit(account.ID, amount);
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
break;
}
Console.WriteLine("Deposit successfully, your balance is {0} yuan.", account.Balance);
break;
}
case "3":
{
Account account = SignIn(icbc);
if (account == null)
{
break;
}
int amount;
while (true)
{
Console.WriteLine("Please input the amount:");
try
{
amount = int.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Invalid input!");
continue;
}
break;
}
try
{
icbc.Withdraw(account.ID, amount);
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
break;
}
Console.WriteLine("Deposit successfully, your balance is {0} yuan.", account.Balance);
break;
}
case "4":
{
Account account = SignIn(icbc);
if (account == null)
{
break;
}
double newCeiling;
while (true)
{
Console.WriteLine("Please input the new ceiling:");
try
{
newCeiling = double.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Invalid input!");
continue;
}
break;
}
try
{
icbc.SetCeiling(account.ID, newCeiling);
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
break;
}
Console.WriteLine("Set ceiling successfully, your new ceiling is {0} yuan.", (account as CreditAccount).Ceiling);
break;
}
case "5": Console.WriteLine("\nThe total balance is: " + icbc.GetTotalBalance()); break;
case "6": Console.WriteLine("\nThe total balance is: " + icbc.GetTotalCeiling()); break;
case "0": return;
default: Console.WriteLine("\nInvalid option, please choose again!"); break;
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace EXE1
{
class Program
{
/// <summary>
/// Sign when needed.
/// </summary>
/// <returns></returns>
static Account SignIn(Bank icbc)
{
Console.WriteLine("\nPlease input your account ID:");
long id;
try
{
id = long.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Invalid account ID!");
return null;
}
Console.WriteLine("Please input your password:");
string password = Console.ReadLine();
Account account;
try
{
account = icbc.SignIn(id, password);
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
return null;
}
return account;
}
/// <summary>
/// Simulate an ATM
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
Bank icbc = new Bank();
while (true)
{
Console.WriteLine("\nPlease choose the service you need:");
Console.WriteLine("(1) Open a New Account");
Console.WriteLine("(2) Deposit");
Console.WriteLine("(3) Withdraw");
Console.WriteLine("(4) Set Ceiling");
Console.WriteLine("(5) Get Total Balance");
Console.WriteLine("(6) Get Total Ceiling");
Console.WriteLine("(0) Exit");
string choice;
choice = Console.ReadKey().KeyChar.ToString();
switch (choice)
{
case "1":
{
string personId;
int typeOfAccount;
string password;
Console.WriteLine("\nWhich kind of account do you want to open?");
while (true)
{
Console.WriteLine("(1) Saving Account\n(2) Credit Account\n(3) Return to Last Menu");
try
{
typeOfAccount = int.Parse(Console.ReadKey().KeyChar.ToString());
}
catch (FormatException)
{
Console.WriteLine("\nInvalid option, please choose again!");
continue;
}
if (typeOfAccount < 1 || typeOfAccount > 5)
{
Console.WriteLine("\nInvalid option, please choose again!");
continue;
}
break;
}
if (typeOfAccount == 5)
{
break;
}
Console.WriteLine("\nPlese input your name:");
string name = Console.ReadLine();
while (true)
{
Console.WriteLine("Please input your Personal ID:");
personId = Console.ReadLine();
if (personId.Length != 18)
{
Console.WriteLine("Invalid Personal ID, please input again!");
continue;
}
break;
}
Console.WriteLine("Please input your E-mail:");
string email = Console.ReadLine();
while (true)
{
Console.WriteLine("Please input your password:");
password = Console.ReadLine();
Console.WriteLine("Please confirm your password:");
if (password != Console.ReadLine())
{
Console.WriteLine("The password doesn't math!");
continue;
}
break;
}
Account account = icbc.OpenAccount(password, password, name, personId, email, typeOfAccount);
Console.WriteLine("The account opened successfully.");
Console.WriteLine("Account ID: {0}\nAccount Name: {1}\nPerson ID: {2}\nE-mail: {3}\nBalance: {4}", account.ID, account.Name, account.PersonId, account.Email, account.Balance);
break;
}
case "2":
{
Account account = SignIn(icbc);
if (account == null)
{
break;
}
int amount;
while (true)
{
Console.WriteLine("Please input the amount:");
try
{
amount = int.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Invalid input!");
continue;
}
break;
}
try
{
icbc.Deposit(account.ID, amount);
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
break;
}
Console.WriteLine("Deposit successfully, your balance is {0} yuan.", account.Balance);
break;
}
case "3":
{
Account account = SignIn(icbc);
if (account == null)
{
break;
}
int amount;
while (true)
{
Console.WriteLine("Please input the amount:");
try
{
amount = int.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Invalid input!");
continue;
}
break;
}
try
{
icbc.Withdraw(account.ID, amount);
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
break;
}
Console.WriteLine("Deposit successfully, your balance is {0} yuan.", account.Balance);
break;
}
case "4":
{
Account account = SignIn(icbc);
if (account == null)
{
break;
}
double newCeiling;
while (true)
{
Console.WriteLine("Please input the new ceiling:");
try
{
newCeiling = double.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Invalid input!");
continue;
}
break;
}
try
{
icbc.SetCeiling(account.ID, newCeiling);
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
break;
}
Console.WriteLine("Set ceiling successfully, your new ceiling is {0} yuan.", (account as CreditAccount).Ceiling);
break;
}
case "5": Console.WriteLine("\nThe total balance is: " + icbc.GetTotalBalance()); break;
case "6": Console.WriteLine("\nThe total balance is: " + icbc.GetTotalCeiling()); break;
case "0": return;
default: Console.WriteLine("\nInvalid option, please choose again!"); break;
}
}
}
}
}
Id:账户号码
PassWord:账户密码
Name:真实姓名
PersonId:身份证号码
Email:客户的电子邮箱
Balance:账户余额
方法:
Deposit:存款方法,参数是double型的金额
Withdraw:取款方法,参数是double型的金额
构造方法:
有参和无参,有参构造方法用于设置必要的属性
2、银行的客户分为两类,储蓄账户(SavingAccount)和信用账户(CreditAccount),区别在于储蓄账户不允许透支,而信用账户可以透支,并允许用户设置自己的透支额度(使用ceiling表示).
为这两种用户编写相关的类
3、编写Bank类,属性:
(1)当前所有的账户对象的集合
(2)当前账户数量
方法:
(1)用户开户:
需要的参数:id、密码、姓名、身份证号码、邮箱、账户类型(储蓄账户还是信用账户);
(2)用户登录:
参数:id、密码;返回Account对象
(3)用户存款:
参数:id、存款数额
(4)用户取款:参数:id、取款数额
(5)设置透支额度:参数:id、新的额度;这个方法需要验证账户是否是信用账户
用户会通过调用Bank对象以上的方法来操作自己的账户,请分析各个方法需要的参数
另外,请为Bank类添加几个统计方法
(6)统计银行所有账户余额总数
(7)统计所有信用账户透支额度总数
4、写个主方法测试你写的类
Account.cs
view plaincopy to clipboardPRint?
using System;
using System.Collections.Generic;
using System.Text;
namespace EXE1
{
abstract class Account
{
//账户号码
protected long id;
public long ID
{
get { return id; }
set { id = value; }
}
//账户密码
protected string password;
public string Password
{
get { return password; }
set { password = value; }
}
//户主姓名
protected string name;
public string Name
{
get { return name; }
set { name = value; }
}
//身份证号码
protected string personId;
public string PersonId
{
get { return personId; }
set { personId = value; }
}
protected string email;
public string Email
{
get { return email; }
set { email = value; }
}
protected double balance;
public double Balance
{
get { return balance; }
set { balance = value; }
}
//静态的id号码生成器
private static long idBuilder = 100000;
public static long IdBuilder
{
get { return idBuilder; }
set { idBuilder = value; }
}
/// <summary>
/// 存款
/// </summary>
/// <param name="sum"></param>
public void Deposit(double sum)
{
if(sum<0)
{
throw new InvalidOperationException("输入的金额为负数!");
}
balance += sum;
}
/// <summary>
/// 抽象函数:取款,需要在子类中重写此方法
/// </summary>
/// <param name="sum"></param>
public abstract void Withdraw(double sum);
public Account()
{
}
/// <summary>
/// 有参数构造函数
/// </summary>
public Account(string password,string name,string personId,string email)
{
this.id = ++idBuilder;
this.password = password;
this.name = name;
this.personId = personId;
this.email = email;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace EXE1
{
abstract class Account
{
//账户号码
protected long id;
public long ID
{
get { return id; }
set { id = value; }
}
//账户密码
protected string password;
public string Password
{
get { return password; }
set { password = value; }
}
//户主姓名
protected string name;
public string Name
{
get { return name; }
set { name = value; }
}
//身份证号码
protected string personId;
public string PersonId
{
get { return personId; }
set { personId = value; }
}
protected string email;
public string Email
{
get { return email; }
set { email = value; }
}
protected double balance;
public double Balance
{
get { return balance; }
set { balance = value; }
}
//静态的id号码生成器
private static long idBuilder = 100000;
public static long IdBuilder
{
get { return idBuilder; }
set { idBuilder = value; }
}
/// <summary>
/// 存款
/// </summary>
/// <param name="sum"></param>
public void Deposit(double sum)
{
if(sum<0)
{
throw new InvalidOperationException("输入的金额为负数!");
}
balance += sum;
}
/// <summary>
/// 抽象函数:取款,需要在子类中重写此方法
/// </summary>
/// <param name="sum"></param>
public abstract void Withdraw(double sum);
public Account()
{
}
/// <summary>
/// 有参数构造函数
/// </summary>
public Account(string password,string name,string personId,string email)
{
this.id = ++idBuilder;
this.password = password;
this.name = name;
this.personId = personId;
this.email = email;
}
}
}
CreditAccount.cs
view plaincopy to clipboardprint?
using System;
using System.Collections.Generic;
using System.Text;
namespace EXE1
{
class CreditAccount : Account
{
protected double ceiling;
public double Ceiling
{
get { return ceiling; }
set { ceiling = value; }
}
public CreditAccount(string password, string name, string personId, string email)
: base(password, name, personId, email)
{
}
/// <summary>
/// 信用账户的取款操作
/// </summary>
public override void Withdraw(double sum)
{
if (sum < 0)
{
throw new InvalidOperationException("输入的金额为负数!");
}
if (sum > balance+ceiling)
{
throw new InvalidOperationException("金额已经超出余额和透支度的总数!");
}
balance -= sum;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace EXE1
{
class CreditAccount : Account
{
protected double ceiling;
public double Ceiling
{
get { return ceiling; }
set { ceiling = value; }
}
public CreditAccount(string password, string name, string personId, string email)
: base(password, name, personId, email)
{
}
/// <summary>
/// 信用账户的取款操作
/// </summary>
public override void Withdraw(double sum)
{
if (sum < 0)
{
throw new InvalidOperationException("输入的金额为负数!");
}
if (sum > balance+ceiling)
{
throw new InvalidOperationException("金额已经超出余额和透支度的总数!");
}
balance -= sum;
}
}
}
SavingAccount.cs
view plaincopy to clipboardprint?
using System;
using System.Collections.Generic;
using System.Text;
namespace EXE1
{
class SavingAccount:Account
{
public SavingAccount(string password, string name, string personId, string email)
:base(password,name,personId,email)
{
}
/// <summary>
/// Withdraw from saving account.
/// </summary>
/// <param name="sum"></param>
public override void Withdraw(double sum)
{
if (sum < 0)
{
throw new InvalidOperationException("输入的金额为负数!");
}
if (sum > balance)
{
throw new InvalidOperationException("金额已经超出余额!");
}
balance -= sum;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace EXE1
{
class SavingAccount:Account
{
public SavingAccount(string password, string name, string personId, string email)
:base(password,name,personId,email)
{
}
/// <summary>
/// Withdraw from saving account.
/// </summary>
/// <param name="sum"></param>
public override void Withdraw(double sum)
{
if (sum < 0)
{
throw new InvalidOperationException("输入的金额为负数!");
}
if (sum > balance)
{
throw new InvalidOperationException("金额已经超出余额!");
}
balance -= sum;
}
}
}
Bank.cs
view plaincopy to clipboardprint?
using System;
using System.Collections.Generic;
using System.Text;
namespace EXE1
{
/// <summary>
/// Bank类,对当前银行中的所有账户进行管理
/// </summary>
class Bank
{
//存放账户的集合
private List<Account> accounts;
public List<Account> Accounts
{
get { return accounts; }
set { accounts = value; }
}
//当前银行的账户数量
private uint currentAccountNumber;
public uint CurrentAccountNumber
{
get { return currentAccountNumber; }
set { currentAccountNumber = value; }
}
/// <summary>
/// 构造函数.
/// </summary>
public Bank()
{
accounts=new List<Account>();
}
/// <summary>
/// 开户;
/// typeOfAccount:1 for saving account, 2 for credit account, 3 for loan saving account, 4 for loan credit account..
/// </summary>
public Account OpenAccount(string password, string confirmationPassword, string name, string personId, string email, int typeOfAccount)
{
Account newAccount;
if (!password.Equals(confirmationPassword))
{
throw new InvalidOperationException("两次密码输入的不一致!");
}
switch (typeOfAccount)
{
case 1: newAccount=new SavingAccount(password, name, personId, email); break;
case 2: newAccount=new CreditAccount(password, name, personId, email); break;
default: throw new ArgumentOutOfRangeException("账户类型是1到2之间的整数");
}
//把新开的账户加到集合中
accounts.Add(newAccount);
return newAccount;
}
/// <summary>
/// 根据账户id得到账户对象
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
private Account GetAccountByID(long id)
{
foreach (Account account in accounts)
{
if (account.ID == id)
{
return account;
}
}
return null;
}
/// <summary>
/// 根据账号和密码登陆账户
/// </summary>
public Account SignIn(long id, string password)
{
foreach (Account account in accounts)
{
if (account.ID == id && account.Password.Equals(password))
{
return account;
}
}
throw new InvalidOperationException("用户名或者密码不正确,请重试!");
return null;
}
/// <summary>
/// 存款
/// </summary>
public Account Deposit(long id, double sum)
{
Account account = GetAccountByID(id);
if (account != null)
{
account.Deposit(sum);
return account;
}
throw new InvalidOperationException("非法账号!");
return null;
}
/// <summary>
/// 取款
/// </summary>
public Account Withdraw(long id, double sum)
{
Account account = GetAccountByID(id);
if (account != null)
{
account.Withdraw(sum);
return account;
}
throw new InvalidOperationException("非法账号!");
return null;
}
/// <summary>
/// 设置透支额度
/// </summary>
public Account SetCeiling(long id, double newCeiling)
{
Account account = GetAccountByID(id);
try
{
(account as CreditAccount).Ceiling = newCeiling;
return account;
}
catch (Exception ex)
{
throw new InvalidOperationException("此账户不是信用账户!");
return account;
}
throw new InvalidOperationException("非法账号!");
return null;
}
/// <summary>
/// 统计银行所有账户余额总数
/// </summary>
/// <returns></returns>
public double GetTotalBalance()
{
double totalBalance = 0;
foreach (Account account in accounts)
{
totalBalance += account.Balance;
}
return totalBalance;
}
/// <summary>
/// 统计所有信用账户透支额度总数
/// </summary>
public double GetTotalCeiling()
{
double totalCeiling = 0;
foreach (Account account in accounts)
{
if (account is CreditAccount)
{
totalCeiling += (account as CreditAccount).Ceiling;
}
}
return totalCeiling;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace EXE1
{
/// <summary>
/// Bank类,对当前银行中的所有账户进行管理
/// </summary>
class Bank
{
//存放账户的集合
private List<Account> accounts;
public List<Account> Accounts
{
get { return accounts; }
set { accounts = value; }
}
//当前银行的账户数量
private uint currentAccountNumber;
public uint CurrentAccountNumber
{
get { return currentAccountNumber; }
set { currentAccountNumber = value; }
}
/// <summary>
/// 构造函数.
/// </summary>
public Bank()
{
accounts=new List<Account>();
}
/// <summary>
/// 开户;
/// typeOfAccount:1 for saving account, 2 for credit account, 3 for loan saving account, 4 for loan credit account..
/// </summary>
public Account OpenAccount(string password, string confirmationPassword, string name, string personId, string email, int typeOfAccount)
{
Account newAccount;
if (!password.Equals(confirmationPassword))
{
throw new InvalidOperationException("两次密码输入的不一致!");
}
switch (typeOfAccount)
{
case 1: newAccount=new SavingAccount(password, name, personId, email); break;
case 2: newAccount=new CreditAccount(password, name, personId, email); break;
default: throw new ArgumentOutOfRangeException("账户类型是1到2之间的整数");
}
//把新开的账户加到集合中
accounts.Add(newAccount);
return newAccount;
}
/// <summary>
/// 根据账户id得到账户对象
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
private Account GetAccountByID(long id)
{
foreach (Account account in accounts)
{
if (account.ID == id)
{
return account;
}
}
return null;
}
/// <summary>
/// 根据账号和密码登陆账户
/// </summary>
public Account SignIn(long id, string password)
{
foreach (Account account in accounts)
{
if (account.ID == id && account.Password.Equals(password))
{
return account;
}
}
throw new InvalidOperationException("用户名或者密码不正确,请重试!");
return null;
}
/// <summary>
/// 存款
/// </summary>
public Account Deposit(long id, double sum)
{
Account account = GetAccountByID(id);
if (account != null)
{
account.Deposit(sum);
return account;
}
throw new InvalidOperationException("非法账号!");
return null;
}
/// <summary>
/// 取款
/// </summary>
public Account Withdraw(long id, double sum)
{
Account account = GetAccountByID(id);
if (account != null)
{
account.Withdraw(sum);
return account;
}
throw new InvalidOperationException("非法账号!");
return null;
}
/// <summary>
/// 设置透支额度
/// </summary>
public Account SetCeiling(long id, double newCeiling)
{
Account account = GetAccountByID(id);
try
{
(account as CreditAccount).Ceiling = newCeiling;
return account;
}
catch (Exception ex)
{
throw new InvalidOperationException("此账户不是信用账户!");
return account;
}
throw new InvalidOperationException("非法账号!");
return null;
}
/// <summary>
/// 统计银行所有账户余额总数
/// </summary>
/// <returns></returns>
public double GetTotalBalance()
{
double totalBalance = 0;
foreach (Account account in accounts)
{
totalBalance += account.Balance;
}
return totalBalance;
}
/// <summary>
/// 统计所有信用账户透支额度总数
/// </summary>
public double GetTotalCeiling()
{
double totalCeiling = 0;
foreach (Account account in accounts)
{
if (account is CreditAccount)
{
totalCeiling += (account as CreditAccount).Ceiling;
}
}
return totalCeiling;
}
}
}
客户端测试:
view plaincopy to clipboardprint?
using System;
using System.Collections.Generic;
using System.Text;
namespace EXE1
{
class Program
{
/// <summary>
/// Sign when needed.
/// </summary>
/// <returns></returns>
static Account SignIn(Bank icbc)
{
Console.WriteLine("\nPlease input your account ID:");
long id;
try
{
id = long.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Invalid account ID!");
return null;
}
Console.WriteLine("Please input your password:");
string password = Console.ReadLine();
Account account;
try
{
account = icbc.SignIn(id, password);
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
return null;
}
return account;
}
/// <summary>
/// Simulate an ATM
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
Bank icbc = new Bank();
while (true)
{
Console.WriteLine("\nPlease choose the service you need:");
Console.WriteLine("(1) Open a New Account");
Console.WriteLine("(2) Deposit");
Console.WriteLine("(3) Withdraw");
Console.WriteLine("(4) Set Ceiling");
Console.WriteLine("(5) Get Total Balance");
Console.WriteLine("(6) Get Total Ceiling");
Console.WriteLine("(0) Exit");
string choice;
choice = Console.ReadKey().KeyChar.ToString();
switch (choice)
{
case "1":
{
string personId;
int typeOfAccount;
string password;
Console.WriteLine("\nWhich kind of account do you want to open?");
while (true)
{
Console.WriteLine("(1) Saving Account\n(2) Credit Account\n(3) Return to Last Menu");
try
{
typeOfAccount = int.Parse(Console.ReadKey().KeyChar.ToString());
}
catch (FormatException)
{
Console.WriteLine("\nInvalid option, please choose again!");
continue;
}
if (typeOfAccount < 1 || typeOfAccount > 5)
{
Console.WriteLine("\nInvalid option, please choose again!");
continue;
}
break;
}
if (typeOfAccount == 5)
{
break;
}
Console.WriteLine("\nPlese input your name:");
string name = Console.ReadLine();
while (true)
{
Console.WriteLine("Please input your Personal ID:");
personId = Console.ReadLine();
if (personId.Length != 18)
{
Console.WriteLine("Invalid Personal ID, please input again!");
continue;
}
break;
}
Console.WriteLine("Please input your E-mail:");
string email = Console.ReadLine();
while (true)
{
Console.WriteLine("Please input your password:");
password = Console.ReadLine();
Console.WriteLine("Please confirm your password:");
if (password != Console.ReadLine())
{
Console.WriteLine("The password doesn't math!");
continue;
}
break;
}
Account account = icbc.OpenAccount(password, password, name, personId, email, typeOfAccount);
Console.WriteLine("The account opened successfully.");
Console.WriteLine("Account ID: {0}\nAccount Name: {1}\nPerson ID: {2}\nE-mail: {3}\nBalance: {4}", account.ID, account.Name, account.PersonId, account.Email, account.Balance);
break;
}
case "2":
{
Account account = SignIn(icbc);
if (account == null)
{
break;
}
int amount;
while (true)
{
Console.WriteLine("Please input the amount:");
try
{
amount = int.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Invalid input!");
continue;
}
break;
}
try
{
icbc.Deposit(account.ID, amount);
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
break;
}
Console.WriteLine("Deposit successfully, your balance is {0} yuan.", account.Balance);
break;
}
case "3":
{
Account account = SignIn(icbc);
if (account == null)
{
break;
}
int amount;
while (true)
{
Console.WriteLine("Please input the amount:");
try
{
amount = int.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Invalid input!");
continue;
}
break;
}
try
{
icbc.Withdraw(account.ID, amount);
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
break;
}
Console.WriteLine("Deposit successfully, your balance is {0} yuan.", account.Balance);
break;
}
case "4":
{
Account account = SignIn(icbc);
if (account == null)
{
break;
}
double newCeiling;
while (true)
{
Console.WriteLine("Please input the new ceiling:");
try
{
newCeiling = double.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Invalid input!");
continue;
}
break;
}
try
{
icbc.SetCeiling(account.ID, newCeiling);
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
break;
}
Console.WriteLine("Set ceiling successfully, your new ceiling is {0} yuan.", (account as CreditAccount).Ceiling);
break;
}
case "5": Console.WriteLine("\nThe total balance is: " + icbc.GetTotalBalance()); break;
case "6": Console.WriteLine("\nThe total balance is: " + icbc.GetTotalCeiling()); break;
case "0": return;
default: Console.WriteLine("\nInvalid option, please choose again!"); break;
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace EXE1
{
class Program
{
/// <summary>
/// Sign when needed.
/// </summary>
/// <returns></returns>
static Account SignIn(Bank icbc)
{
Console.WriteLine("\nPlease input your account ID:");
long id;
try
{
id = long.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Invalid account ID!");
return null;
}
Console.WriteLine("Please input your password:");
string password = Console.ReadLine();
Account account;
try
{
account = icbc.SignIn(id, password);
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
return null;
}
return account;
}
/// <summary>
/// Simulate an ATM
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
Bank icbc = new Bank();
while (true)
{
Console.WriteLine("\nPlease choose the service you need:");
Console.WriteLine("(1) Open a New Account");
Console.WriteLine("(2) Deposit");
Console.WriteLine("(3) Withdraw");
Console.WriteLine("(4) Set Ceiling");
Console.WriteLine("(5) Get Total Balance");
Console.WriteLine("(6) Get Total Ceiling");
Console.WriteLine("(0) Exit");
string choice;
choice = Console.ReadKey().KeyChar.ToString();
switch (choice)
{
case "1":
{
string personId;
int typeOfAccount;
string password;
Console.WriteLine("\nWhich kind of account do you want to open?");
while (true)
{
Console.WriteLine("(1) Saving Account\n(2) Credit Account\n(3) Return to Last Menu");
try
{
typeOfAccount = int.Parse(Console.ReadKey().KeyChar.ToString());
}
catch (FormatException)
{
Console.WriteLine("\nInvalid option, please choose again!");
continue;
}
if (typeOfAccount < 1 || typeOfAccount > 5)
{
Console.WriteLine("\nInvalid option, please choose again!");
continue;
}
break;
}
if (typeOfAccount == 5)
{
break;
}
Console.WriteLine("\nPlese input your name:");
string name = Console.ReadLine();
while (true)
{
Console.WriteLine("Please input your Personal ID:");
personId = Console.ReadLine();
if (personId.Length != 18)
{
Console.WriteLine("Invalid Personal ID, please input again!");
continue;
}
break;
}
Console.WriteLine("Please input your E-mail:");
string email = Console.ReadLine();
while (true)
{
Console.WriteLine("Please input your password:");
password = Console.ReadLine();
Console.WriteLine("Please confirm your password:");
if (password != Console.ReadLine())
{
Console.WriteLine("The password doesn't math!");
continue;
}
break;
}
Account account = icbc.OpenAccount(password, password, name, personId, email, typeOfAccount);
Console.WriteLine("The account opened successfully.");
Console.WriteLine("Account ID: {0}\nAccount Name: {1}\nPerson ID: {2}\nE-mail: {3}\nBalance: {4}", account.ID, account.Name, account.PersonId, account.Email, account.Balance);
break;
}
case "2":
{
Account account = SignIn(icbc);
if (account == null)
{
break;
}
int amount;
while (true)
{
Console.WriteLine("Please input the amount:");
try
{
amount = int.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Invalid input!");
continue;
}
break;
}
try
{
icbc.Deposit(account.ID, amount);
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
break;
}
Console.WriteLine("Deposit successfully, your balance is {0} yuan.", account.Balance);
break;
}
case "3":
{
Account account = SignIn(icbc);
if (account == null)
{
break;
}
int amount;
while (true)
{
Console.WriteLine("Please input the amount:");
try
{
amount = int.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Invalid input!");
continue;
}
break;
}
try
{
icbc.Withdraw(account.ID, amount);
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
break;
}
Console.WriteLine("Deposit successfully, your balance is {0} yuan.", account.Balance);
break;
}
case "4":
{
Account account = SignIn(icbc);
if (account == null)
{
break;
}
double newCeiling;
while (true)
{
Console.WriteLine("Please input the new ceiling:");
try
{
newCeiling = double.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Invalid input!");
continue;
}
break;
}
try
{
icbc.SetCeiling(account.ID, newCeiling);
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
break;
}
Console.WriteLine("Set ceiling successfully, your new ceiling is {0} yuan.", (account as CreditAccount).Ceiling);
break;
}
case "5": Console.WriteLine("\nThe total balance is: " + icbc.GetTotalBalance()); break;
case "6": Console.WriteLine("\nThe total balance is: " + icbc.GetTotalCeiling()); break;
case "0": return;
default: Console.WriteLine("\nInvalid option, please choose again!"); break;
}
}
}
}
}
- ››面向对象的JS-私有成员变量实现方式
- ››面向对象的JavaScript (一、对象基础,使用函数来...
- ››面向对象数据库 db4o 之旅,第 4 部分: 使用 dRS
- ››对象存储不给高性能计算添堵
- ››面向 Java Web 应用程序的 OpenID,第 2 部分: 为...
- ››面向 Java 开发人员的 db4o 指南: 简介和概览
- ››面向 Java 开发人员的 db4o 指南: 查询,更新和一...
- ››面向 Java 开发人员的 db4o 指南: db4o 中的数据库...
- ››面向 Java 开发人员的 db4o 指南: 超越简单对象
- ››面向 Java 开发人员的 db4o 指南: 结构化对象和集...
- ››面向 Java 开发人员的 db4o 指南: 事务、分布和安...
- ››面向 Java Web 应用程序的 OpenID,第 1 部分:在...
赞助商链接