Java实现简单银行ATM功能
本文实例为大家分享了Java实现简单银行ATM功能的具体代码,供大家参考,具体内容如下
实现功能
1、用户需要通过输入银行卡号和密码才能进入ATM系统
2、用户可以在ATM中实现取款、存款、转账、余额查询、退出系统等功能
简单分析
1、创建User类(cardNo,identity,phone,username,password,balance(余额))
2、创建Bank类,主要实现初始化用户、用户登录、显示菜单、取款、存款、转账、余额查询、退出系统等功能。
代码实现
User.java
publicclassUser{
privateStringusername;
privateintpassword;
privateStringcardNo;
privateStringidentity;
privateStringphone;
privatedoubleblance;
publicUser(){
}
publicUser(Stringusername,intpassword,StringcardNo,Stringidentity,Stringphone,doubleblance){
this.username=username;
this.password=password;
this.cardNo=cardNo;
this.identity=identity;
this.phone=phone;
this.blance=blance;
}
publicStringgetUsername(){
returnusername;
}
publicvoidsetUsername(Stringusername){
this.username=username;
}
publicintgetPassword(){
returnpassword;
}
publicvoidsetPassword(intpassword){
this.password=password;
}
publicStringgetCardNo(){
returncardNo;
}
publicvoidsetCardNo(StringcardNo){
this.cardNo=cardNo;
}
publicStringgetIdentity(){
returnidentity;
}
publicvoidsetIdentity(Stringidentity){
this.identity=identity;
}
publicStringgetPhone(){
returnphone;
}
publicvoidsetPhone(Stringphone){
this.phone=phone;
}
publicdoublegetBlance(){
returnblance;
}
publicvoidsetBlance(doubleblance){
this.blance=blance;
}
}
Bank.java
publicclassBank{
//创建用户数组
privateUser[]users=newUser[100];
//用户个数
privateintsize=0;
//当前登录的用户
privateUserloginuser;
publicBank(){
initial();//初始化用户
}
/**
*初始化用户
*/
publicvoidinitial(){
Useruser1=newUser("钟爱",12346,"6226789234023434","130324192309123074","13133565435",2312313);
Useruser2=newUser("冷冬",12354,"6226789234023567","1305472309123074","13446745675",2333);
Useruser3=newUser("小龙",9893,"6226789234023564","13032414575467457","13145745435",255553);
Useruser4=newUser("赵丽颖",6342,"6226789234029324","130324192647456774","13145675435",288883);
Useruser5=newUser("徐三哥",8445,"6226789234025487","1303241923456744","1457785435",28989);
users[0]=user1;
users[1]=user2;
users[2]=user3;
users[3]=user4;
users[4]=user5;
size=5;
}
/**
*用户登录
*/
publicUserlogin(StringcardNo,intpassword){
for(inti=0;i0){
loginuser.setBlance(loginuser.getBlance()+money);
System.out.println("存款成功");
System.out.println("您的当前余额为"+loginuser.getBlance());
}else{
System.out.println("输入错误!");
}
}else{
System.out.println("您还未进行账户登录");
}
}
/**
*取款操作
*/
publicvoidwithdraw(){
Scannerinput=newScanner(System.in);
System.out.println("请输入你要取出的金额为:");
intmoney=input.nextInt();
if(loginuser!=null){
if(money>0&&money<=loginuser.getBlance()){
loginuser.setBlance(loginuser.getBlance()-money);
System.out.println("取款成功");
System.out.println("您的当前余额为:"+loginuser.getBlance());
}else{
System.out.println("输入错误");
}
}else{
System.out.println("您还未进行账户登录");
}
}
/**
*转账操作
*/
publicvoidtrans(){
Scannerinput=newScanner(System.in);
System.out.println("请输入要转入账户的卡号:");
StringcardNo=input.next();
Usernuser=null;//对方账户
booleanIsExit=false;//判断对方账户是否存在
for(inti=0;i0&&money<=loginuser.getBlance()){
loginuser.setBlance(loginuser.getBlance()-money);
nuser.setBlance(nuser.getBlance()+money);
System.out.println("转账成功");
System.out.println("您当前余额为"+loginuser.getBlance());
System.out.println(nuser.getBlance());
}else{
System.out.println("输入错误");
}
}else{
System.out.println("请登录账户");
}
}
/**
*查询余额
*/
publicvoidquery(){
System.out.println("您的余额为:"+loginuser.getBlance());
}
/**
*修改密码
*/
publicvoidrevise(){
Scannerinput=newScanner(System.in);
System.out.println("请输入您的新密码");
intnewpassword=input.nextInt();
for(inti=0;i
BankSystem.java
publicclassBankSystem{
publicstaticvoidmain(String[]args){
Scannerinput=newScanner(System.in);
Bankbank=newBank();
System.out.println("请输入您的卡号:");
StringcardNo=input.next();
System.out.println("请输入您的密码:");
intpassword=input.nextInt();
Useruser=bank.login(cardNo,password);//用户登录
if(user!=null){
System.out.println(user.getUsername()+"登陆成功");
bank.showMenus();
}else{
System.out.println("登陆失败");
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。