C#ATM

时间:2022-01-04 06:14:02

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
public class Account//定义一个类Account
{
private int accountnumber;//定义一个变量卡号
private int pin;//定义一个变量密码
public int Balance;//定义一个变量余额
public Account(int number, int password, int userbalance)
{
accountnumber = number;
pin = password;
Balance = userbalance;
}//构造函数Account,输入姓名,密码和余额
public int Number
{
get { return accountnumber; }
}//设置账户为只读属性
public int Userbalance
{
get { return Balance; }
}//设置余额为只读属性
public bool ValidatePIN(int userPIN)
{
return (userPIN == pin);
}//验证输入密码是否正确
public void Credit(int amount)
{
Balance += amount;
}//存款
public void Debit(int amount)
{
Balance -= amount;
}//取款
public bool toDeposit(int numOfDeposit)
{
if (numOfDeposit >= 0) //存储金额为正数
{
Balance = Balance + numOfDeposit;
return true;
}
else
{
return false;
}
}

//取钱
public bool toTakeOut(int numOfTakeOut)
{
if ((numOfTakeOut >= 0) && (numOfTakeOut <= Balance)) //要保证取钱数为正数,且要小于余额
{
Balance = Balance - numOfTakeOut;
return true;
}
else
{
Console.WriteLine("你的余额不足!");
return false;
}
}
public void zhanghao(int number,int k)
{
Console.WriteLine("请输入账号:");
k = 1;
while (k>0)
{
int str = Convert.ToInt32(Console.ReadLine());
if (str != number)
{
Console.WriteLine("账号输入错误,请重新输入。");
k = k;

}
else k = 0;
}
}
public void mima(int password,int y)
{
Console.WriteLine("请输入密码:");
y=1;
while (y > 0)
{
int s = Convert.ToInt32(Console.ReadLine());
if (s != password)
{
Console.WriteLine("密码输入错误,请重新输入。");
y = y;
}
else y = 0;
}
}
}
public class role//create a class named role
{
private string administrators;
private string administratorpin;
public role(string Admin, string Adminpin)
{
administrators = Admin;
administratorpin = Adminpin;
}//构造一个函数,输入管理员的账号,密码
public string Administratorsnumber
{
get { return administrators; }
}//设置管理员账号只读
public bool Apin(string ADMpin)
{
return (administratorpin == ADMpin);
}//验证密码是否正确

}
public class Keypad
{
public int GetInput()
{
return Convert.ToInt32(Console.ReadLine());
} //根据用户输入,返回一个整型
}
class program
{
static void Main(string[] args)
{
Account user = new Account(12345, 12345, 10000);
role roleone = new role("roleone", "impassword");
user.zhanghao(12345,1);
user.mima(12345,1);
Keypad pad = new Keypad();
pad.GetInput();
Console.WriteLine("你想?1.存钱 2.取钱 3.离开");
int a = pad.GetInput();
while (a > 0)
{
if (a == 1)
{
Console.WriteLine("请输入你存入的金额");
int b = pad.GetInput();
user.toDeposit(b);
Console.WriteLine("你的余额为{0}元", user.Balance);
}
else
{
if (a == 2)
{
Console.WriteLine("请输入你取出的金额");
int b = pad.GetInput();
user.toTakeOut(b);
Console.WriteLine("你的余额为{0}元", user.Balance);
}
else
{
if (a == 3)
{
Console.WriteLine("再见!");
a = -1;
break;
}
else
{
Console.WriteLine("非法输入,请重新输入。");
}
}

}
Console.WriteLine("你想?1.存钱 2.取钱 3.离开");
int d = pad.GetInput();
a = d;
}
Console.ReadKey();
}
}

}

相关文章