java atm项目实战_本文实例为大家分享了Java实现模拟ATM机的具体代码,供大家参考,具体内容如下项目名称模拟ATM机项目描述简单实现ATM机功能代码实现测试类...

本文介绍了一个使用Java编写的模拟ATM机系统,包括登录、注册、查询余额、取款、存款和转账等功能。用户可以进行选择操作,银行类负责管理多个账户,银行卡类实现了账户的具体操作。代码中通过Scanner获取用户输入,并使用switch语句进行流程控制。
摘要由CSDN通过智能技术生成

本文实例为大家分享了Java实现模拟ATM机的具体代码,供大家参考,具体内容如下

项目名称

模拟ATM机

项目描述

简单实现ATM机功能

代码实现

测试类

public class Test {

//模拟多功能ATM机

public static void main(String[] args) {

ATM atm = new ATM();

atm.opearte();

}

}

主类:实现主方法

public class ATM {

private Bank bank;

public ATM(){

bank = new Bank();

}

private void success(BankCard bankCard) {

while (true) {

System.out.println("请选择:1、查询余额,2、取款,3、存款,4、转账,5、退卡");

Scanner scanner = new Scanner(System.in);

int choice = scanner.nextInt();

if (choice == 5) {

return;

}

switch (choice) {

case 1:

int money = bank.printf(bankCard);

System.out.println("余额:" + money);

break;

case 2:

System.out.println("请输入取款金额:");

money = scanner.nextInt();

bankCard.withdraw(money);

break;

case 3:

System.out.println("请输入存款金额:");

money = scanner.nextInt();

bankCard.deposit(money);

break;

case 4:

System.out.println("请输入对方账户id:");

int id = scanner.nextInt();

BankCard card = bank.transfer(id);

if(card != null){

System.out.println("请输入转账金额:");

money = scanner.nextInt();

if(money <= bankCard.getMoney()){

bankCard.withdraw(money);

card.deposit(money);

}else {

System.out.println("转账失败");

}

}else {

System.out.println("未找到该账户");

}

break;

default:

}

}

}

private void login(){ //登录

System.out.println("请输入账号,密码");

Scanner scanner = new Scanner(System.in);

int id = scanner.nextInt();

int password = scanner.nextInt();

BankCard bankCard = bank.search(id,password);

if(bankCard != null){

System.out.println("登录成功");

success(bankCard);

}else {

System.out.println("登录失败,请注册");

}

}

private void register(){ //注册

System.out.println("请输入账号,密码");

Scanner scanner = new Scanner(System.in);

int id = scanner.nextInt();

int password = scanner.nextInt();

BankCard bankCard = bank.search(id,password);

if(bankCard != null){

System.out.println("该账号已存在,请直接登录");

}else {

bank.addcard(id,password);

System.out.println("注册成功");

}

}

public void opearte() {

while (true) {

System.out.println("请选择:1、登录,2、注册,3、退出");

Scanner scanner = new Scanner(System.in);

int choice = scanner.nextInt();

if (choice == 3) {

return;

}

switch (choice) {

case 1:

login();

break;

case 2:

register();

break;

default:

}

}

}

}

银行类:一个银行对应多个ATM机

public class Bank {

private BankCard[] bankCards;

private final int INITSIZE = 10;

private int size;

public Bank(){

bankCards = new BankCard[INITSIZE];

size = 0;

}

public BankCard search(int id,int password){ //搜索

BankCard[] bankCard = bankCards;

for(int i=0;i

if(bankCards[i].getId() == id && bankCards[i].getPassword() == password){

return bankCard[i];

}

}

return null;

}

public void addcard(int id,int password){ //注册

BankCard bankCard = new BankCard(id, password);

if (size == bankCards.length){

bankCards = Arrays.copyOf(bankCards,bankCards.length * 2);

}

bankCards[size++] = bankCard;

}

public int printf(BankCard bankCard){ //显示余额

return bankCard.getMoney();

}

public BankCard transfer(int id){

for(int i=0;i

if(bankCards[i].getId() == id){

return bankCards[i];

}

}

return null;

}

}

银行卡类:不同账户的操作

public class BankCard {

private int id;

private int password;

private int money;

private int size = 0;

public BankCard(int id,int password){

this.id = id;

this.password = password;

money = 0;

}

public int getId(){

return id;

}

public int getPassword(){

return password;

}

public int getMoney(){

return money;

}

public void withdraw(int money){ //取款

if(this.money >= money){

this.money -= money;

}else {

System.out.println("取款失败");

}

}

public void deposit(int money){ //存款

this.money += money;

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值