编写一个程序,用来模拟银行帐户的基本操作,如帐户开户的话,则最低存款额为100、存取现金操作以及在使用任意修改余额后都可以随时查看帐户余额。请使用重载的带参数的构造函数。
提示:要实现此问题的解决方案,请创建一个名为Account类。该类包含一个名为saving的成员变量。编写一个默认的构造函数,使用值100 初始化该成员变量(如果用户开户时的钱少于100,则不能开户;也就是不能实例化该类。)。创建一个参数化构造函数,它可以传递任何大于100的整数以初始化saving变量。
//本程序由王赟 计算机092编写 2011,11,5 编写,仅供交流
//做完C#作业,便把东西传了上来。
///wangyun 2011,11,05 23:34 at CIEE Computer Room
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace S3_3
{
class Account
{
private decimal saving;
/// <summary>
/// 默认构造函数
/// </summary>
private Account()
{
this.saving = 100;
}
/// <summary>
/// 带一个参数的构造函数
/// </summary>
/// <param name="money"></param>
public Account(decimal money)
{
this.saving = money;
}
/// <summary>
/// 存款
/// </summary>
/// <param name="Num">存款数</param>
public void depositMoney(decimal Num)
{
if (Num <= 0)
{
Console.WriteLine("存款数必须大于0!");
}
else
{
saving = saving + Num;
Console.WriteLine("存款成功!本次存款{0},余额为{1}", Num, saving);
}
}
/// <summary>
/// 取款
/// </summary>
/// <param name="Num">取款数</param>
public void drawMoney(decimal Num)
{
if (Num>saving)
{
Console.WriteLine("抱歉,取款失败!\n当前存款为:{0}", saving);
}
else if (Num == 0)
{
Console.WriteLine("操作失败,取款数不能为0!");
}
else
{
saving=saving-Num;
Console.WriteLine("取款成功!本次取款{0},余额为{1}", Num, saving);
}
}
/// <summary>
/// 账户信息查询
/// </summary>
public decimal Saving
{
get { return saving; }
private set { }
}
static void Main(string[] args)
{
string choice;
decimal Num;
Account ac;
Begin:
Console.WriteLine("请放入存款以便开户");
Num = Convert.ToDecimal(Console.ReadLine());
if(Num<100)
{
Console.WriteLine("开户失败,最低为100!");
Console.WriteLine("输入任意键继续!");
Console.ReadKey();
Console.Clear();
goto Begin;
}
else
{
ac = new Account(Num);
Console.WriteLine("开户成功,当前账户余额为{0}", ac.Saving);
}
while (true)
{
Console.WriteLine(" 欢迎光临");
Console.WriteLine(" 1.存款");
Console.WriteLine(" 2.取款");
Console.WriteLine(" 3.查询");
Console.WriteLine(" 4.退出");
choice = Console.ReadLine();
switch (choice)
{
case "1":
Console.WriteLine("请放入存款数");
Num = Convert.ToDecimal(Console.ReadLine());
ac.depositMoney(Num);
break;
case "2":
Console.WriteLine("请输入取款数");
Num = Convert.ToDecimal(Console.ReadLine());
ac.drawMoney(Num);
break;
case "3":
Console.WriteLine("当前存款余额是{0}", ac.Saving);
break;
case "4":
goto Exit;
default:
Console.WriteLine("指令输入错误!");
break;
}
Console.WriteLine("输入任意键继续!");
Console.ReadKey();
Console.Clear();
}
Exit:
Console.ReadKey();
}
}
}