案例ATM系统
大致思路:
实现功能:
案例代码:
Account类:
packageatmDemo;
publicclassAccount{
privateStringcardId;
privateStringuserName;
privatecharsex;
privateStringpassWord;
privatedoublemoney;
privatedoublelimit;
publicStringgetCardId(){
returncardId;
}
publicvoidsetCardId(StringcardId){
this.cardId=cardId;
}
publicStringgetUserName(){
returnuserName;
}
publicvoidsetUserName(StringuserName){
this.userName=userName;
}
publicchargetSex(){
returnsex;
}
publicvoidsetSex(charsex){
this.sex=sex;
}
publicStringgetPassWord(){
returnpassWord;
}
publicvoidsetPassWord(StringpassWord){
this.passWord=passWord;
}
publicdoublegetMoney(){
returnmoney;
}
publicvoidsetMoney(doublemoney){
this.money=money;
}
publicdoublegetLimit(){
returnlimit;
}
publicvoidsetLimit(doublelimit){
this.limit=limit;
}
}
ATM类:
packageatmDemo;
importjava.util.ArrayList;
importjava.util.Random;
importjava.util.Scanner;
publicclassATM{
privateArrayList<Account>accounts=newArrayList<>();
privateScannersc=newScanner(System.in);
AccountloginAccount;
//欢迎界面
publicvoidstart(){
System.out.println("======欢迎来到天地银行======");
while(true){
System.out.println("请输入您想办理的业务:");
System.out.println("1.用户登录");
System.out.println("2.办理开户");
Stringcommand=sc.next();
switch(command){
case"1":
loginAcc();
break;//登录
case"2":
createAcc();
break;//开户
default:
System.out.println("您输入的业务编号有误,请重新输入!!!");
}
}
}
privatevoidcreateAcc(){
Accountacc=newAccount();
accounts.add(acc);//将创建的对象添加到集合里
System.out.println("请您输入您的姓名:");
Stringname=sc.next();
acc.setUserName(name);
while(true){
System.out.println("请您输入您的性别:");
Stringsex=sc.next();
if(sex.charAt(0)=='男'||sex.charAt(0)=='女')
{acc.setSex(sex.charAt(0));
break;
}
else{
System.out.println("您的输入有误~~~~");
}
}
while(true){
System.out.println("请您设置您的密码:");
Stringpassword=sc.next();
System.out.println("请您确认您的密码:");
StringokPassword=sc.next();
if(password.equals(okPassword)){
acc.setPassWord(password);
System.out.println("密码设置完成!!");
break;
}else{
System.out.println("请您重新输入~~");
}
}
while(true){
System.out.println("请您设置您的每次提现额度:");
doublelimit=sc.nextDouble();
if(limit>=0){
acc.setLimit(limit);break;
}
{
System.out.println("输入错误,请您重新输入!");
}
}
Randomr=newRandom();
while(true){
StringcardNum="";
for(inti=0;i<8;i++){
intran=r.nextInt(9);
cardNum+=ran;
}
if(acc==check(cardNum)){
}
else{
acc.setCardId(cardNum);
System.out.println("开户成功,您的卡号为:"+cardNum);
break;
}
}
}
//查找用户
privateAccountcheck(StringcardNum){
for(inti=0;i<accounts.size();i++){
if(cardNum.equals(accounts.get(i).getCardId())){
returnaccounts.get(i);
}
}
returnnull;
}
privatevoidloginAcc(){
Accountaccount=newAccount();
while(true){
if(accounts.size()==0){
System.out.println("当前银行无账户信息,请您先去开户!");return;
}
System.out.println("请您输入卡号:");
StringcardNum=sc.next();
if(check(cardNum)==null){
System.out.println("输入错误,请您检查!并重新输入。");
}
else{
account=check(cardNum);
System.out.println("请您输入密码:");
Stringpass=sc.next();
if(pass.equals(account.getPassWord())){
System.out.println("恭喜您!登陆成功."+account.getUserName()+"用户,您的卡号为:"+cardNum);
System.out.println(account.getUserName()+"用户,您已经成功进入系统,您可以回复编号选择办理以下业务:");
loginAccount=account;
showSer();
return;//跳出登录页面
}
break;}
}
}
privatevoidshowSer(){
while(true){
System.out.println("1.查询帐户信息");
System.out.println("2.存款");
System.out.println("3.取款");
System.out.println("4.转账");
System.out.println("5.修改密码");
System.out.println("6.退出");
System.out.println("7.注销账户");
intcom=sc.nextInt();
switch(com){
case1:checkInfo();
break;
case2:addMoney();
break;
case3:takeMOney();
break;
case4:switchMonry();
break;
case5:changePass();
break;
case6:exit();
return;
case7:if(desACC()){
return;
}
break;
default:
System.out.println("您输入的指令有误,请重新输入~~");
}
}
}
privatevoidcheckInfo(){
System.out.println("======当前账户信息如下======");
System.out.println("用户名为:"+loginAccount.getUserName());
System.out.println("卡号为:"+loginAccount.getCardId());
System.out.println("当前账户余额为:"+loginAccount.getMoney());
System.out.println("支付限额为:"+loginAccount.getLimit());
}
privatevoidexit(){
System.out.println(loginAccount.getUserName()+"您已经成功退出系统~~");
}
privatevoidaddMoney(){
System.out.println("请您输入您要存款的金额:");
doublemoney=sc.nextDouble();
loginAccount.setMoney(loginAccount.getMoney()+money);
System.out.println("存款成功,您的账户现有余额为:"+loginAccount.getMoney());
}
privatevoidtakeMOney(){
System.out.println("请您输入要取款的金额:");
while(true){
doublemoney=sc.nextDouble();
if(money>loginAccount.getMoney()){
System.out.println("余额不足,您最多可以取出"+loginAccount.getMoney()+"元");
System.out.println("请您重新输入:");
}elseif(money>loginAccount.getLimit()){
System.out.println("对不起,您输入的金额超过了您的存取限额"+loginAccount.getLimit());
System.out.println("请您重新输入:");
}else{
loginAccount.setMoney(loginAccount.getMoney()-money);
System.out.println("取款成功,您的账户现有余额为:"+loginAccount.getMoney());
break;
}
}
}
privatevoidswitchMonry()
{
if(accounts.size()<2){
System.out.println("账户过少无法转账,请去开户");
return;
}
if(loginAccount.getMoney()==0){
System.out.println("余额不足,无法转账!");
}
else{
System.out.println("请您输入对方账户卡号:");
StringcardNum=sc.next();
if(check(cardNum)==null){
System.out.println("对不起,您输入的卡号不存在,请您重新输入!");
}
else{
Accountacc=check(cardNum);
Stringname="*"+acc.getUserName().substring(1);
System.out.println("请您输入对方的姓氏:"+name);
while(true){
StringpreName=sc.next();
if(preName.charAt(0)==acc.getUserName().charAt(0))
{System.out.println("请您输入转账金额:");
while(true){
doublemoney=sc.nextDouble();
if(money>loginAccount.getMoney()){
System.out.println("余额不足,您最多可以转出"+loginAccount.getMoney()+"元");
System.out.println("请您重新输入:");
}elseif(money>loginAccount.getLimit()){
System.out.println("对不起,您输入的金额超过了您的存取限额"+loginAccount.getLimit());
System.out.println("请您重新输入:");
}
else{
loginAccount.setMoney(loginAccount.getMoney()-money);
acc.setMoney(acc.getMoney()+money);
System.out.println("转账已完成!!");
return;
}
}
}
else{
System.out.println("信息输入错误,认证失败,请重新认证!");
}
}
}
}
}
privatevoidchangePass(){
System.out.println("请您输入当前账户密码:");
while(true){
Stringpass=sc.next();
if(pass.equals(loginAccount.getPassWord())){
while(true){
System.out.println("请您输入修改后的密码:");
StringokPass=sc.next();
System.out.println("请您再次输入修改后的密码:");
StringokPass1=sc.next();
if(okPass.equals(okPass1)){
System.out.println("密码修改成功!");
return;
}
else{
System.out.println("两次输入不一致,请您重新输入!");
}
}
}
else{
System.out.println("当前密码错误!请您重新输入");
}
}
}
privatebooleandesACC(){
//销户操作
System.out.println("您真的打算销户吗?是/否");
Stringh=sc.next();
if(h.equals("是")){
if(loginAccount.getMoney()==0){
System.out.println("销户成功!");
returntrue;
}else{
System.out.println("您的账户还有余额,无法执行该操作!");
returnfalse;
}
}else{
returnfalse;
}
}
}
atmTest类:
packageatmDemo;
publicclassatmTest{
publicstaticvoidmain(String[]args){
ATMatm=newATM();
atm.start();
}
}