2021/9/26ATM机的存取款

Posted yifaer_chen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2021/9/26ATM机的存取款相关的知识,希望对你有一定的参考价值。

1.ATM(和用户交互)属性:银行        行为:展示菜单

代码:

package com.stx.oop.feature;

import java.util.Scanner;

public class ATM {
Bank bank = new Bank();
public static void main(String[] args) {
ATM atm = new ATM();
atm.display();
}
public void display() {
Scanner scanner = new Scanner(System.in);
while(true){
System.out.println("1开户");
System.out.println("2转账");
System.out.println("3查询");
System.out.println("0退出");
String input=scanner.next();
if ("0".equals(input)){
System.out.println("bye");
break;
}
switch (input){
case "1":
System.out.println("输入卡号");
String id=scanner.next();
System.out.println("输入用户名");
String name=scanner.next();
System.out.println("放入纸币");
Double balance=scanner.nextDouble();
bank.createAccount(id,name,balance);
break;
case "2":
System.out.println("请输入付款账号");
String fromId = scanner.next();
System.out.println("请输入转账卡号");
String toId = scanner.next();
System.out.println("请输入转账金额");
Double money=scanner.nextDouble();
bank.transfer(fromId,toId, money);
break;
case "3":
bank.queryAll();
break;
default:
System.out.println("输入错误,请重新输入!");
break;
}
}
}

}

2.银行 属性:所有账户 行为:开户、转账、查询所有用户
代码:
package com.stx.oop.feature;

public class Bank {
private Account[] accounts = new Account[10];
private int count = 0;
public void createAccount(String id, String name, double balance) {
// 创建账户对象
Account account = new Account();
account.setBalance(balance);
account.setName(name);
account.setId(id);
accounts[count] = account;
count++;
}
// 转账
public void transfer(String fromId, String toId, double money) {
// 找到对应的账号
Account from = findByAccount(fromId);
// 找到付款用户信息
Account to = findByAccount(toId);
// 账号余额变化
double fromBalance = from.getBalance();
if (money > 0 && fromBalance - money > 0) {
// 转账成功
from.setBalance(fromBalance - money);
double toBalance = to.getBalance();// 钱存进收款账户
to.setBalance(toBalance + money);
} else {
System.out.println("转账失败");
return;
}
}
// 查询
public void queryAll(){
for (int i = 0; i < accounts.length; i++) {
Account account = accounts[i];
if (account != null) {
account.show();
}
}
}
//查询用户
private Account findByAccount(String id) {
for (int i = 0; i < accounts.length; i++) {
Account account = accounts[i];
if (account != null && account.getId().equals(id)) {
return account;
}
}
return null;
}

}

package com.stx.oop.feature;

import java.util.Scanner;

public class ATM {
Bank bank = new Bank();
public static void main(String[] args) {
ATM atm = new ATM();
atm.display();

}

public void display() {
Scanner scanner = new Scanner(System.in);
while(true){
System.out.println("1开户");
System.out.println("2转账");
System.out.println("3查询");
System.out.println("0退出");
String input=scanner.next();
if ("0".equals(input)){
System.out.println("bye");
break;
}
switch (input){
case "1":
System.out.println("输入卡号");
String id=scanner.next();
System.out.println("输入用户名");
String name=scanner.next();
System.out.println("放入纸币");
Double balance=scanner.nextDouble();
bank.createAccount(id,name,balance);
break;
case "2":
System.out.println("请输入付款账号");
String fromId = scanner.next();
System.out.println("请输入转账卡号");
String toId = scanner.next();
System.out.println("请输入转账金额");
Double money=scanner.nextDouble();
bank.transfer(fromId,toId, money);
break;
case "3":
bank.queryAll();
break;

default:
System.out.println("输入错误,请重新输入!");
break;
}
}
}

}

3.账户 属性:卡号、名称、余额 行为:展示信息、设置余额
代码:
package com.stx.oop.feature;

public class Account {
private String id;
private String name;
private double balance;
// 访问途径就是方法
public void show(){
// 语法格式
System.out.printf("卡号:%s\\t姓名:%s\\t余额:%f\\r\\n",id,name,balance);
}
// 存钱取钱
public void setBalance(double balance){
this.balance=balance;
}
public Double getBalance(){
return balance;
}
// 开户
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
// 卡号
public void setId(String id){
this.id = id;
}
public String getId(){
return id;
}
}

以上是关于2021/9/26ATM机的存取款的主要内容,如果未能解决你的问题,请参考以下文章

一个用java模拟ATM机的代码且数据库为mysql,

简单的ATM机的取款过程

使用JavaScript实现ATM取款机

实战案例:场景测试之ATM机取款业务测试

考试感受

ATM与支付宝