亲们!您们好!
讲一下Bank系统的做法:
01.首先创建一个Card类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace VS1 { //账号类 public class Card { //姓名 public string name; //账号 public string account; //密码 public string password; //身份证号 public string id; //余额 public double balance; } }
02.创建一个Bank类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace VS1 { //银行类 public class Bank { //定义卡数组 Card[] cards=new Card[100]; #region 01.给原有2卡赋值 public void InitialData() { Console.WriteLine("==========================================="); Console.WriteLine("=================现有卡有==================>>"); //卡1 Card card1 = new Card(); card1.name = "阿乐";//用户名 card1.account = "111111";//卡号 card1.password = "123456";//mima card1.id = "342225199504262055";//身份证号 card1.balance = 1000200;//余额 cards[0] = card1;//把卡值赋值数组 //卡2 Card card2 = new Card(); card2.name = "阿龙"; card2.account = "222222"; card2.password = "654321"; card2.id = "34222519514562115"; card2.balance = 1000200; cards[1] = card2; } #endregion #region 02.欢迎页面 public void Welcome() { Console.WriteLine("欢迎进入天尽头ATM系统,亲"); Console.WriteLine("功能:1.开户 2.存款 3.取款 4.查询余额 5.转账 6.修改密码 7.退卡"); Console.WriteLine("请选择您要操作的功能模块,亲"); string choice = Console.ReadLine(); switch (choice) { case "1": //开户 CreateAccount(); break; case "2": //存款 bool flag= MoneyIn(); if (flag==false) { Console.WriteLine("对不起,您输入信息(密码和卡号)有误!,登录失败...."); Console.WriteLine("====>>自动转回主页面"); Welcome(); } break; case "3": //取款 bool flag1 = MoneyOut(); if (flag1 == false) { Console.WriteLine("对不起,您输入信息(密码和卡号)有误!"); Console.WriteLine("====>>自动转回主页面"); Welcome(); } break; case "4": //查询余额 bool flag2 = SelectMoney(); if (flag2 == false) { Console.WriteLine("对不起,您输入信息(密码和卡号)有误!,登录失败...."); Console.WriteLine("====>>自动转回主页面"); Welcome(); } break; case "5": //转账 bool flag3 = TransferMoney(); if (flag3 == false) { Console.WriteLine("对不起,您输入信息(密码和卡号)有误!,登录失败...."); Console.WriteLine("====>>自动转回主页面"); Welcome(); } break; case "6": //修改密码 bool flag6 = ModifyPwd(); if (flag6 == false) { Console.WriteLine("对不起,您输入信息(密码和卡号)有误!,登录失败...."); Console.WriteLine("====>>自动转回主页面"); Welcome(); } break; default: Console.WriteLine("成功退卡!"); break; } } #endregion #region 03.输出原卡信息 public void PrintCustomerInfo() { foreach(Card item in cards){ if(item!=null){ Console.WriteLine("用户名:{0},卡号:{1},密码:{2},身份证号:{3},余额:{4}", item.name, item.account,item.password,item.id,item.balance); Console.WriteLine("==========================================="); } } } #endregion #region 04.开户功能 public void CreateAccount() { Console.WriteLine("============这里是开户==================="); Console.WriteLine("===================>>>>>>>>>"); Console.WriteLine("请输入身份证号:"); string id = Console.ReadLine(); Console.WriteLine("请输入用户名"); string name = Console.ReadLine(); Console.WriteLine("请输入密码"); string password = Console.ReadLine(); Console.WriteLine("请输入开户金额"); double balance = double.Parse(Console.ReadLine()); //先将四个零散的属性装配成一个Card对象 Card card = new Card(); card.id = id; card.name = name; card.password = password; card.balance = balance; card.account = "987654"; for (int i = 0; i < cards.Length; i++) { if(cards[i]==null){ cards[i] = card; Console.WriteLine("您好!{0},您开户成功!您的卡号为:{1},您卡余额为:{2},请牢记您的密码!",cards[i].name,cards[i].account,cards[i].balance); Console.WriteLine("=====>>自动转回主页面"); break; } } Console.WriteLine("=============================="); Welcome(); } #endregion #region 05.存款功能 public bool MoneyIn() { bool flag = false; Console.WriteLine("============这里是存款==================="); Console.WriteLine("===================>>>>>>>>>"); Console.WriteLine("请输入卡号:"); string account = Console.ReadLine(); Console.WriteLine("请输入密码:"); string password = Console.ReadLine(); for (int i = 0; i < cards.Length;i++ ) { if(cards[i]!=null){ if (cards[i].account == account&&cards[i].password==password) { flag = true; Console.WriteLine("您好!{0},请输入您要存的金额:", cards[i].name); double balance=double.Parse(Console.ReadLine()); cards[i].balance = cards[i].balance + balance; Console.WriteLine("{0},您存款成功,现在您的卡总余额为:{1}", cards[i].name, cards[i].balance); Console.WriteLine("====>>自动转回主页面"); Welcome(); break; } } } return flag; } #endregion #region 06.取款功能 public bool MoneyOut() { bool flag1 = false; Console.WriteLine("============这里是取款==================="); Console.WriteLine("===================>>>>>>>>>"); Console.WriteLine("请输入卡号:"); string account = Console.ReadLine(); Console.WriteLine("请输入密码:"); string password = Console.ReadLine(); for (int i = 0; i < cards.Length;i++ ) { if(cards[i]!=null){ if (cards[i].account == account&&cards[i].password==password) { flag1 = true; Console.WriteLine("您好!{0},请输入您要取的金额:", cards[i].name); double balance=double.Parse(Console.ReadLine()); if(cards[i].balance>balance){ cards[i].balance = cards[i].balance - balance; Console.WriteLine("{0},您取款成功,现在您的卡总余额为:{1}",cards[i].name,cards[i].balance); Console.WriteLine("====>>自动转回主页面"); Welcome(); break; }else{ Console.WriteLine("可能不要瞎输,你卡里有这么多钱吗?"); Console.WriteLine("====>>自动转回主页面"); Welcome(); break; } } } } return flag1; } #endregion #region 07.查询余额 public bool SelectMoney(){ bool flag2 = false; Console.WriteLine("============这里是查询==================="); Console.WriteLine("===================>>>>>>>>>"); Console.WriteLine("请输入卡号:"); string account = Console.ReadLine(); Console.WriteLine("请输入密码:"); string password = Console.ReadLine(); for (int i = 0; i < cards.Length; i++) { if(cards[i]!=null){ if (cards[i].account == account && cards[i].password == password) { flag2 = true; Console.WriteLine("{0},您好,现在您的卡总余额为:{1}", cards[i].name, cards[i].balance); Console.WriteLine("====>>自动转回主页面"); Welcome(); break; } } } return flag2; } #endregion #region 08.转账 public bool TransferMoney() { bool flag3 = false; Console.WriteLine("============这里是转账==================="); Console.WriteLine("===================>>>>>>>>>"); Console.WriteLine("请输入卡号:"); string account = Console.ReadLine(); Console.WriteLine("请输入密码:"); string password = Console.ReadLine(); for (int i = 0; i < cards.Length; i++) { if (cards[i] != null) { if (cards[i].account == account && cards[i].password == password) { bool flag4 = false; bool flag5 = false; flag3 = true; Console.WriteLine("您好!{0},请输入您要转账的卡号:", cards[i].name); string account1=Console.ReadLine(); for (int j = 0; j < cards.Length;j++ ) { if(cards[j]!=null){ if (cards[j].account == account1) { flag4 = true; Console.WriteLine("您好!{0},您输入要转账的卡号为:{1},请您输入要转账的金额:", cards[i].name,account1); double balance=double.Parse(Console.ReadLine()); if(cards[j].balance>balance){ flag5 = true; cards[j].balance = cards[j].balance - balance; cards[i].balance = cards[i].balance + balance; Console.WriteLine("您好!{0},转账后,您的余额:{1}", cards[i].name, cards[i].balance); Console.WriteLine("您好!{0},转账后,您的余额:{1}", cards[j].name, cards[j].balance); Console.WriteLine("====>>自动转回主页面"); Welcome(); break; } } } } if (flag4 == false) { Console.WriteLine("您输入的未找到相匹配的卡号!!请核对!!"); Console.WriteLine("====>>自动转回主页面"); Welcome(); } if (flag5 == false) { Console.WriteLine("您的卡里没有这么多钱!!请核对!!"); Console.WriteLine("====>>自动转回主页面"); Welcome(); } } } } return flag3; } #endregion #region 09.修改密码 public bool ModifyPwd() { bool flag6 = false; Console.WriteLine("============这里是修改密码==================="); Console.WriteLine("===================>>>>>>>>>"); Console.WriteLine("请输入卡号:"); string account = Console.ReadLine(); Console.WriteLine("请输入密码:"); string password = Console.ReadLine(); for (int i = 0; i < cards.Length; i++) { if (cards[i] != null) { if (cards[i].account == account && cards[i].password == password) { flag6 = true; Console.WriteLine("您好!{0},请再次输入您的原始密码:", cards[i].name); string password1 = Console.ReadLine(); if (password == password1) { Console.WriteLine("您好!{0},请输入您的新密码:", cards[i].name); string password2 = Console.ReadLine(); Console.WriteLine("您好!{0},请再次输入您的新密码:", cards[i].name); string password3 = Console.ReadLine(); if (password2 == password3) { Console.WriteLine("{0},您改密码成功,现在您的卡新密码为:{1}", cards[i].name, password2); cards[i].password = password3; Console.WriteLine("====>>自动转回主页面"); Welcome(); } else { Console.WriteLine("您好!{0},您输入的两次新密码密码不一致", cards[i].name); Console.WriteLine("====>>自动转回主页面"); Welcome(); } } else { Console.WriteLine("您好!{0},您输入的两次原始密码不一致", cards[i].name); Console.WriteLine("====>>自动转回主页面"); Welcome(); } break; } } } return flag6; } #endregion #region 10.退卡 #endregion } }
其中08.转账也可以写为:
//转账 public void TransferMoney() { Console.WriteLine("请输入转出卡卡号"); string outCardNo = Console.ReadLine(); //003 Console.WriteLine("请输入转出卡密码"); string outCardPwd = Console.ReadLine(); //判定用户名和密码是否正确 //循环对比 //定义一个标记,标记指示转出卡号默认是不存在 bool flag = false;//默认转出卡不存在 bool outCardFlag = false;//默认转入卡不存在 int type = 0; foreach (Card card in cards) { if (card!=null) { if (card.CardNo.Equals(outCardNo)) //卡号一致 { flag = true; //判定密码是否一致 if (card.CardPwd.Equals(outCardPwd)) //密码一致 { //用户名 密码均正确,可以执行后续内容 Console.WriteLine("请输入转入卡卡号"); string inCardNo = Console.ReadLine(); #region 内层循环,转入卡 foreach (Card card1 in cards) { if (card1 != null) { if (card1.CardNo.Equals(inCardNo)) //转入卡存在 { type = 1; //存在,可以提示输入转账金额 Console.WriteLine("请输入转账金额"); int money = Convert.ToInt32(Console.ReadLine()); if (money <= card.Balance) //合法 { while (money % 100 != 0) //不是面值100的 { Console.WriteLine("您输入的不合法"); Console.WriteLine("请输入转账金额"); money = Convert.ToInt32(Console.ReadLine()); } //代码执行到这里,证明转账金额合法 //100元 //改变双方的余额 card.Balance -= money; card1.Balance += money; Console.WriteLine("转账成功!"); outCardFlag = true; Console.WriteLine("转出卡余额{0}\t转入卡余额{1}", card.Balance, card1.Balance); } else { Console.WriteLine("余额不足,退出"); } break; } } } #endregion if (outCardFlag == false&&type==0) { Console.WriteLine("转入卡不存在!"); } } else { Console.WriteLine("转出卡密码错误!"); } break; } } } //出了循环,如果flag还是false,就证明没有该卡! if (flag==false) { Console.WriteLine("没有该卡"); } }
03.最后使用main方法调用输出:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Bank { class Program { static void Main(string[] args) { Bank bank=new Bank(); bank.InitialData();//保证数组中前3项有值 bank.PrintCustomerInfo(); bank.Welcome(); Console.ReadKey(); } } }
是不是好难看,我也是这么认为的!初次使用,请多关照!!
时间有限! 到此再见! 后会有期!
时间: 2024-10-13 15:52:20