黑马程序员技术交流社区
标题:
C#基础实战——自动取款机模拟程序
[打印本页]
作者:
贡宗新
时间:
2013-4-14 01:05
标题:
C#基础实战——自动取款机模拟程序
Account.cs类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ATM
{
class Account //源文件
{
protected string name;
protected string password;
protected decimal balance;
public decimal Balance //balance属性 账户金额初始化
{
get
{
return balance;
}
}
public string Name //属性 用户名初始化
{
get
{
return name;
}
}
public Account(string name, string password) //构造函数
{
this.balance = 0;
this.name = name;
this.password = password;
}
public bool Deposit(decimal amount) //存款方法
{
if (amount <= 0)
return false;
balance += amount;
return true;
}
public bool Deposit(double amount) //重载
{
return Deposit((decimal)amount);
}
public bool Deposit(int amount) //重载方法
{
return Deposit((decimal)amount);
}
public bool Deposit(decimal amount,out decimal balance) //输出参数out
{
bool succeed = Deposit(amount);
balance = this.balance;
return succeed;
}
public bool Withdraw(decimal amount) //取钱方法
{
if (amount > balance || amount <= 0)
{
return false;
}
balance -= amount;
return true;
}
public bool Withdraw(double amount) //取钱方法重载
{
return Withdraw((decimal)amount);
}
public bool Withdraw(int amount)
{
return Withdraw((decimal)amount);
}
public bool Withdraw(decimal amount, out decimal balance)
{
bool succeed = Withdraw(amount);
balance = this.balance;
return succeed;
}
public bool ChangePassword(string oldPassword,string newPassword) //改密码方法
{
if (oldPassword != password)
{
return false;
}
password = newPassword;
return true;
}
public bool Login(string name, string password) //登陆判断
{
return (this.name == name && this.password == password);
}
}
}
复制代码
Bank.cs类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ATM
{
class Bank
{
protected const int MaxAccountNum = 2048; //什么意思
protected string name;
protected int usedAccountNum;
protected Account[] accounts;
public string Name //属性
{
get
{
return name;
}
}
public Bank(string name)
{
this.name = name;
this.usedAccountNum = 0; //初始化用户金额
accounts = new Account[MaxAccountNum]; //这个又是什么意思,索引器吗???
}
public bool LoginAccount(string name, string password, out Account account)
{
account = null;
for (int i = 0; i < usedAccountNum; ++i)
{
if (accounts[i].Login(name, password)) //有几个账户,遍历账户判断是否存在匹配账户密码
{
account = accounts[i];
return true;
}
}
return false;
}
public bool OpenAccount(string name, string password, out Account account) //开户
{
account = null;
for (int i = 0; i < usedAccountNum; i++)
{
if (accounts[i].Name == name)
{
return false;
}
}
account = new Account(name, password);
accounts[usedAccountNum++] = account;
return true;
}
}
}
复制代码
可能其中也有些不懂的地方,请大家讲解一下咯,总的来看对学完基础非常有好处
作者:
贡宗新
时间:
2013-4-14 01:06
三个类太多了超过了限制了输入数量了,所以就多发了一个
ATM.cs类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ATM
{
class ATM
{
private const string quitCode = "20060824"; //设置的退出程序的条件
private Bank bank;
public ATM(Bank bank) //这个ATM构造函数中参数初始化银行,至于参数Bank bank,为什么拿Bank类来当参数类型呢
{
this.bank = bank;
}
public void Start()
{
while (true)
{
//主界面
PrintLogo();
Console.WriteLine(" 1.开户 ");
Console.WriteLine(" 2.登陆 ");
Console.WriteLine("------------------------------");
Console.WriteLine("");
Console.WriteLine("请选择(回车结束):");
string code = Console.ReadLine();
//退出系统
if (code == quitCode)
{
return;
}
if (code == "1") //开户
{
OpenAccount();
}
else if (code == "2") //登陆选择
{
LoginAccount();
}
}
} //开始方法
private void LoginAccount() //登陆
{
PrintLogo();
Console.WriteLine(" 请输入你的账号的用户名和密码 ");
Console.WriteLine("-----------------------------------------------");
Console.WriteLine("");
string name = Input("用户名(回车结束):");
string password = Input("密码(回车结束):");
//登陆账号
Account account; //用类名来当类型,着实不懂,求指教
if (!bank.LoginAccount(name,password, out account))
{
Console.Write("登陆错误,请检查用户名和密码是否正确。按Enter键继续....");
Console.Read();
}
else
{
ManageAccount(ref account); //新的技术点,引用参数
}
}
private void OpenAccount() //开户
{
PrintLogo();
Console.WriteLine(" 请输入你的账号的用户名和密码 ");
Console.WriteLine("");
string name = Input("用户名(回车结束):");
string password = Input("密码(回车结束):");
//开户
Account account;
if (!bank.OpenAccount(name, password, out account))
{
Console.Write("开户错误,用户名可能已经存在。按Enter键继续.....");
Console.Read();
}
else
{
Print("开户",0,account);
Pause(); //这个不晓得干嘛的哈
ManageAccount(ref account);
}
}
private void ManageAccount(ref Account account) //账户操作
{
while (true)
{
PrintLogo();
Console.WriteLine(" 1.存款 ");
Console.WriteLine(" 2.取款 ");
Console.WriteLine(" 3.查询余额 ");
Console.WriteLine(" 4.修改密码 ");
Console.WriteLine(" 5.退出 ");
Console.WriteLine("----------------------------------");
Console.WriteLine("");
Console.WriteLine("你的选择(回车结束):");
string code = Console.ReadLine();
// string s;
decimal amount;
bool succeed;
switch (code)
{
case "1":
amount = InputNumber("\n输入存款数目:");
succeed = account.Deposit(amount);
if (succeed)
{
Print("存入", amount, account);
}
else
{
Console.WriteLine("存款失败!");
}
Pause();
break;
case "2":
amount = InputNumber("\n输入存款数目:");
succeed = account.Withdraw(amount);
if (succeed)
{
Print("取出", amount, account);
}
else
{
Console.WriteLine("取款失败!");
}
Pause();
break;
case "3":
Print(account);
Pause();
break;
case "4":
string oldPassword = Input("当前密码(回车结束):");
string newPassword = Input("新密码(回车结束):");
succeed = account.ChangePassword(oldPassword,newPassword);
if (succeed)
Console.WriteLine("密码修改成功!");
else
Console.WriteLine("密码修改失败!");
Pause();
break;
case "5":
return;
default:
break;
}
}
} //账户管理
private void PrintLogo() //显示Logo
{
Console.WriteLine("\n--------------------------------------");
Console.WriteLine("{0}自动取款机 用户第一 服务至上",bank.Name);
Console.WriteLine("----------------------------------------- ");
}
private string Input(string prompt) //输入金额参数控制
{
Console.Write(prompt);
string str = Console.ReadLine();
while (str == "")
{
Console.WriteLine("不能为空,{0}",prompt);
str = Console.ReadLine();
}
return str;
}
private decimal InputNumber(string prompt) //输入参数控制重载
{
Console.WriteLine(prompt);
string s = Console.ReadLine();
//decimal amount = Decimal.Parse(s); //此处会有异常如果输入字母,字符键的时候,改成如下
decimal amount = 0;
try //异常处理
{
amount = Decimal.Parse(s);
}
catch
{
Console.Write("输入的数值格式不正确,请输入!");
amount = InputNumber(prompt); //用递归调用InputNumber
}
return amount;
}
private void Pause() //暂停方法
{
Console.WriteLine("按Enter键继续.....");
Console.Read();
}
private void Print(string operation, decimal amount, Account account) //操作显示
{
Console.WriteLine("--------------------------------");
Console.WriteLine("姓名:{0}",account.Name);
Console.WriteLine("{0}: {1}",operation,amount);
Console.WriteLine("余额:{0}",account.Balance);
Console.WriteLine("---------------------------------");
Console.WriteLine("{0}成功!",operation);
}
public void Print(Account account) //查看显示
{
Console.WriteLine("---------------------------------");
Console.WriteLine("姓名:{0}",account.Name);
Console.WriteLine("余额:{0}",account.Balance);
Console.WriteLine("----------------------------------");
}
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2