这篇文章主要为大家详细介绍了java模拟实现银行ATM机操作,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
java模拟银行ATM机操作(基础版),供大家参考,具体内容如下
实现的功能需求:
修改密码之后,就会自动退出登录,再重新登录,若登录成功才能验证修改密码成功,这里用到 【跳出指定循环】, 其他功能都是基本操作,作为入门入手程序。
准备两个实体类(一个银行类,一个用户类),一个测试类,注意,这里暂且存储了两个用户,这里可以优化,暂且不优化了
Blank.java
package com.demo2;
public class Blank {
/*数组模拟数据库后台,并初始化*/
private User[] users = new User[20];
int size;
/*接收当前登录的用户*/
User user;
public User getUser() {
return user;
}
public Blank (){ }
/*初始化用户*/
public void init(){
User user1= new User("890012","43042419990201","刘军","7519","15116497809","18000");
User user2= new User("890013","43042419990821","一凡","1314","118842320","13000");
/*初始化*/
users[0] = user1;
users[1] =user2;
size=2;
}
/*用户登录*/
public boolean login(String cardNo, String passwd){
for(int i=0; i<size; i++){
if(users[i].getCardNo().equals(cardNo) && users[i].getPasswd().equals(passwd) ){
user = users[i];
return true;
}
}
return false;
}
/*存款*/
public void save(String balance){
int money =Integer.parseInt(user.getBalance());
money += Integer.parseInt(balance);
user.setBalance((money+""));
System.out.println("存款成功");
}
/*取款*/
public void widthDraw(String balance){
int money =Integer.parseInt(user.getBalance());
money -= Integer.parseInt(balance);
user.setBalance((money+""));
}
/*转账*/
public void trans(String cardNo,String balance){
int i=0;
for(i=0; i<size;i++){
if(users[i].getCardNo().equals(cardNo)){
/*保存没转账之前的数*/
int oldmoney =Integer.parseInt(user.getBalance()) ;
int oldanothermoney = Integer.parseInt(users[i].getBalance());
/*转账,调用当前登录用户的取款方法*/
widthDraw(balance);
/*目标账户的余额变化*/
int money =Integer.parseInt(users[i].getBalance());
money += Integer.parseInt(balance);
users[i].setBalance((money+""));
/*这里做转账成功的判断*/
int userseflmoney =oldmoney - Integer.parseInt(user.getBalance());
int useraothermoney = Integer.parseInt(users[i].getBalance())-oldanothermoney;
if(userseflmoney == useraothermoney){
System.out.println("转账成功");
}
break;
}
}
if(i>=size){
System.out.println("输入的卡号有误");
}
}
/*查询余额*/
public void query(){
System.out.println("【账户余额】: "+user.getBalance());
}
/*修改密码*/
public void modifyPassword(String passwd){
user.setPasswd(passwd);
System.out.println("修改密码成功,请重新登录");
}
}
User.java
package com.demo2;
public class User {
private String cardNo ;
private String identity;
private String username;
private String passwd;
private String phone;
private String balance;
public User(){}
public User(String cardNo, String identity, String username, String passwd, String phone, String balance) {
this.cardNo = cardNo;
this.identity = identity;
this.username = username;
this.passwd = passwd;
this.phone = phone;
this.balance = balance;
}
public String getCardNo() {
return cardNo;
}
public void setCardNo(String cardNo) {
this.cardNo = cardNo;
}
public String getIdentity() {
return identity;
}
public void setIdentity(String identity) {
this.identity = identity;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getBalance() {
return balance;
}
public void setBalance(String balance) {
this.balance = balance;
}
}
BlankTest.java
package com.demo2;
import java.util.Scanner;
public class BankTest {
public static void main(String[] args) {
Blank blank = new Blank();
/*初始化*/
blank.init();
Scanner sc =new Scanner(System.in);
while(true) {
System.out.println("========== 中国银行欢迎你 ========= ");
System.out.println(" 【请先登录】 ");
System.out.print("【请输入卡号】:");
String carNo = sc.next();
System.out.print("【请输入密码】:");
String passwd = sc.next();
if (blank.login(carNo, passwd)) {
System.out.println("登录成功: 【账号:欢迎】:"+ blank.getUser().getUsername());
// User user = blank.getUser();
prof: while(true) {
System.out.println("========== 1,【存款】 2,【取款】 3,【转账】 ========= ");
System.out.println("========== 4,【查询余额】 5,【修改密码】 6,【退出】 ========= ");
System.out.print("【请选择业务功能】:");
int num = sc.nextInt();
if (num == 1) {
System.out.println("【进入存款操作】");
System.out.print("【输入存款金额】:");
int money = sc.nextInt();
while (true) {
if (money <= 0) {
System.out.println("【请输入正确的存款金额】");
} else {
break;
}
}
blank.save(money + "");
} else if (num == 2) {
System.out.println("【进入取款操作】");
System.out.print("【输入取款金额】:");
int money = sc.nextInt();
blank.widthDraw(money+"");
System.out.println("取款成功");
} else if (num == 3) {
System.out.println("【进入转账操作】");
System.out.print("【输入转账卡号】:");
String cardNo =sc.next();
System.out.print("【输入转款金额】");
int money = sc.nextInt();
blank.trans(cardNo, money+"");
} else if (num == 4) {
blank.query();
} else if (num == 5) {
System.out.println("【进入改密操作】");
System.out.print("【输入重置密码】:");
String newpasswd =sc.next();
blank.modifyPassword(newpasswd);
break prof;
} else if (num == 6) {
System.out.println("【账号】: 退出成功");
break prof;
}
}
} else {
System.out.println("卡号或者密码输入不正确");
}
}
}
}
程序运行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程学习网。
沃梦达教程
本文标题为:java模拟实现银行ATM机操作
基础教程推荐
猜你喜欢
- springboot自定义starter方法及注解实例 2023-03-31
- Java并发编程进阶之线程控制篇 2023-03-07
- JDK数组阻塞队列源码深入分析总结 2023-04-18
- java实现多人聊天系统 2023-05-19
- java基础知识之FileInputStream流的使用 2023-08-11
- Java数据结构之对象比较详解 2023-03-07
- Java实现线程插队的示例代码 2022-09-03
- ConditionalOnProperty配置swagger不生效问题及解决 2023-01-02
- Java实现查找文件和替换文件内容 2023-04-06
- Java文件管理操作的知识点整理 2023-05-19