本文实例为大家分享了java实现atm取款机程序的具体代码,供大家参考,具体内容如下
对象说明:
功能: 该程序的功能为实现模拟银行atm自动取款机取款,存款,查询余额,转账等功能,只是完成了基本的功能。
思路:第一、登录判断,密码限制三次,使用for循环。第二、成功登录,选择相应的功能,使用switch语句。第四、实现功能的反复循环操作,因为次数不确定,使用while结构。第五、对每个功能模块进行填充完善。
代码展示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
import java.util.scanner;
public class bankatm { public static void main(string[] args) { scanner input = new scanner(system.in);
string cardnum = "6228123123" ; // 卡号 int pwd = 888888 ; // 密码 boolean flag = true ; // 声明布尔类型变量 double surplus = 1000 ; // 余额
// 界面 system.out.println( "---------欢迎使用工商银行atm机---------" );
/** 用于进行登录的次数限制止 **/ for ( int i = 1 ; i <= 3 ; i++) { system.out.println( "请插入您的银行卡:" ); string inputcard = input.next(); system.out.println( "请输入您的密码:" ); int inputpwd = input.nextint();
// 验证账号和密码 if (inputcard.equals(cardnum) && inputpwd == pwd) { flag = true ; break ; } else { if (i <= 2 ) { system.out.println( "对不起,密码输入不正确,你还有" + ( 3 - i) + "次机会!" ); } else { system.out.println( "对不起,您的卡已被锁定!" ); break ; } flag = false ; } }
/** 登录成功后选择功能 */ if (flag) { char answer = 'y' ; while (answer == 'y' ) { system.out.println( "请选择功能:1.取款 2.存款 3.查询余额 4.转账 5.退出" ); int choice = input.nextint(); switch (choice) { case 1 : // 执行取款操作 system.out.println( "--->取款" ); system.out.println( "请输入取款金额:" ); double getmoney = input.nextdouble(); if (getmoney > 0 ) { if (getmoney <= surplus) { if (getmoney % 100 == 0 ) { system.out.println( "请取走您的钞票!余额为¥" + (surplus - getmoney)); } else { system.out.println( "对不起,不能取零钱!" ); } } else { system.out.println( "对不起,余额不足!" ); } } else { system.out.println( "请输入正确的金额:" ); }
break ; case 2 : // 执行存款操作 system.out.println( "--->存款" ); system.out.println( "请把钞票整理后放入存钞口:" ); double savemoney = input.nextdouble(); if (savemoney > 0 && savemoney <= 10000 ) { if (savemoney % 100 == 0 ) { surplus += savemoney; system.out.println( "存款成功!余额为¥" + surplus); } else {
double backmoney = savemoney % 100 ; surplus = savemoney + surplus - backmoney; system.out.println( "存款成功!余额为¥" + surplus); system.out.println( "请取走零钱¥" + backmoney); } } else if (savemoney > 10000 ) { system.out.println( "一次最多存入一万元,请分批存入!" ); } else { system.out.println( "存入的钞票是假钞,无效没收!" ); } break ; case 3 : // 执行查询余额 system.out.println( "--->查询余额" ); system.out.println( "您卡上的余额是:" + surplus); break ; case 4 : // 执行转账操作 system.out.println( "--->转账" ); system.out.println( "请输入转账金额:" ); double gomoney = input.nextdouble(); // 转账金额 if (gomoney > 0 ) { if (gomoney <= surplus) { system.out.println( "转账成功!余额为¥" + (surplus - gomoney)); } else { system.out.println( "对不起,请确保卡上有足够的余额!" ); }
} else { system.out.println( "转账失败!请输入正确的金额:" ); } break ; case 5 : // 执行退出操作 // system.out.println("--->退出"); system.out.println( "谢谢您的使用!" ); return ; default : system.out.println( "对不起,您选择的功能有误!" ); break ; } // switch end system.out.println( "继续吗?y/n" ); answer = input.next().charat( 0 );
} // while end system.out.println( "谢谢您的使用!" );
}
} } |
效果截图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
原文链接:https://blog.csdn.net/weixin_42517667/article/details/82049249