利用Java代码实现银行的 查询 存储 取款 转账 等功能
首先需要创建如下结构:
用户信息
package com.gyx;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* 用户表 用来保存用户信息
*/
public class Entity {
//用户1
private static String account ="10086";
private static String password ="10086";
private static double balance =1000;
private static String name ="张三";
//用户2
private static String account1 ="146204";
private static String password1 ="146204";
private static double balance1 =2000;
private static String name1 = "李四";
public static String getAccount() {
return account;
}
public static void setAccount(String account) {
Entity.account = account;
}
public static String getPassword() {
return password;
}
public static void setPassword(String password) {
Entity.password = password;
}
public static double getBalance() {
return balance;
}
public static void setBalance(double balance) {
Entity.balance = balance;
}
public static String getName() {
return name;
}
public static void setName(String name) {
Entity.name = name;
}
public static String getAccount1() {
return account1;
}
public static void setAccount1(String account1) {
Entity.account1 = account1;
}
public static String getPassword1() {
return password1;
}
public static void setPassword1(String password1) {
Entity.password1 = password1;
}
public static double getBalance1() {
return balance1;
}
public static void setBalance1(double balance1) {
Entity.balance1 = balance1;
}
public static String getName1() {
return name1;
}
public static void setName1(String name1) {
Entity.name1 = name1;
}
}
登录类(Logic):
package com.gyx;
import java.io.IOException;
import java.util.Scanner;
/**
* 登录页面
* 逻辑分析: 1、显示一段欢迎的信息。 2、提示输入卡号 3、输入密码 4、显示主菜单。
*
*/
public class Logic {
public static void main(String[] args) throws IOException {
System.out.println("=====================");
System.out.println("<<<----欢迎使用ATM------>>>>");
System.out.println("=====================");
//调用登录方法
login();
}
//登录方法
private static void login() throws IOException {
for (int i = 0; i < 3; i++) {
//提示输入账账户
System.out.println("<<<---请输入账户--->>>");
Scanner scanner = new Scanner(System.in);
String acc = scanner.next();
//提示用户输入密码
System.out.println("<<<---请输入密码--->>>");
String pwd = scanner.next();
//判断用户输入信息是否和后台相同
if (Entity.getAccount().equals(acc) && Entity.getPassword().equals(pwd)){
//如果相同 则进入成功界面
Succed succed = new Succed();
succed.showMenu();
break;
}else {
System.out.println("账户或密码错误,重新输入");
if (i==2){
//错误三次 则退出循环
System.err.println("error-账户已被锁定");
}
}
}
}
}
登录成功
package com.gyx;
import org.junit.Test;
import java.io.IOException;
import java.util.Scanner;
//登录成功页面
public class Succed {
public void showMenu() throws IOException {
while (true){
//选择服务类型
System.out.println("==============================");
System.out.println("<<<---请选择服务项目--->>");
System.out.println("**1.查询余额** **2.取款**");
System.out.println("**3.存 款** **4.转账**");
System.out.println("**5.打 印** **6.退卡**");
//选择服务项目
Scanner scanner =new Scanner(System.in);
//键盘输入
int id = scanner.nextInt();
//选择项目1
switch (id){
case 3:
Deposit deposit = new Deposit();
deposit.depos();
break;
case 4:
Turn turn = new Turn();
turn.tur();
break;
case 5:
System.out.println("您的账户信息已打印");
Print print = new Print();
print.pri();
break;
case 6:
System.out.println("欢迎下次使用");
System.exit(0);
break;
default:
}
Withdrawal withdrawal = new Withdrawal();
withdrawal.select(id);
}
}
}
查询余额及取款(Withdrawal)
package com.gyx;
import java.io.IOException;
import java.util.Scanner;
/**
* 余额 及存款方法
*/
public class Withdrawal {
public void select(int id) throws IOException {
switch (id){
case 1:
//h获得余额
double balance = Entity.getBalance();
System.out.println("您的余额为:" + balance);
break;
case 2:
System.out.println("<<<---请选择取款金额--->>>");
System.out.println("**1.300** **2.800**");
System.out.println("**3.1500** **4.3000**");
//获取键盘输入
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
fetch(i);
break;
default:;
}
}
//取款方法
private void fetch(int i) throws IOException {
switch (i){
case 1:
//获取余额减去取款金额
if(Entity.getBalance() <300){
System.out.println("余额不足,请重新选择");
Succed succed = new Succed();
succed.showMenu();
}
double bala = Entity.getBalance() - 300;
Entity.setBalance(bala);
System.out.println("取款成功,余额为"+ Entity.getBalance());
break;
case 2:
//获取余额减去取款金额
if(Entity.getBalance() <800) {
System.out.println("余额不足,请重新选择");
Succed succed = new Succed();
succed.showMenu();
}
//获取余额减去取款金额
double bala1 = Entity.getBalance() - 800;
System.out.println("取款成功,余额为"+ bala1);
break;
case 3:
//获取余额减去取款金额
if(Entity.getBalance() <1500) {
System.out.println("余额不足,请重新选择");
Succed succed = new Succed();
succed.showMenu();
}
//获取余额减去取款金额
double bala2 = Entity.getBalance() - 1500;
System.out.println("取款成功,余额为"+ bala2);
break;
case 4:
//获取余额减去取款金额
if(Entity.getBalance() <3000) {
System.out.println("余额不足,请重新选择");
Succed succed = new Succed();
succed.showMenu();
}
//获取余额减去取款金额
double bala3 = Entity.getBalance() - 3000;
System.out.println("取款成功,余额为"+ bala3);
break;
default:;
}
}
}
存款(Deposit)
package com.gyx;
import java.util.Scanner;
/**
* 存款
*/
public class Deposit {
public void depos(){
System.out.println("========================");
System.out.println("<<<---请选择存款金额--->>>");
System.out.println("**1.300** **2.800**");
System.out.println("**3.1500** **4.3000**");
Scanner scanner = new Scanner(System.in);
//选择金额
int i = scanner.nextInt();
in(i);
}
private void in(int i) {
double balance = Entity.getBalance();
switch (i){
case 1:
balance = balance +300;
Entity.setBalance(balance);
System.out.println("存款成功,余额为:"+ Entity.getBalance());
break;
case 2:
balance = balance +800;
Entity.setBalance(balance);
System.out.println("存款成功,余额为:"+ Entity.getBalance());
break;
case 3:
balance = balance +1500;
Entity.setBalance(balance);
System.out.println("存款成功,余额为:"+ Entity.getBalance());
break;
case 4:
balance = balance +3000;
Entity.setBalance(balance);
System.out.println("存款成功,余额为:"+ Entity.getBalance());
break;
default:;
}
}
}
转账(Turn)
package com.gyx;
import java.io.IOException;
import java.util.Scanner;
/**
* 转账功能
*/
public class Turn {
public void tur() throws IOException {
System.out.println("<<<---请输入转账用户--->>>");
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
System.out.println("<<<---请输入转账金额--->>>");
double i1 = scanner.nextInt();
//调用用户余额
double balance = Entity.getBalance();
if (balance < i1){
System.out.println("余额不足,转账失败");
Succed succed = new Succed();
succed.showMenu();
}else {
balance = balance - i1;
Entity.setBalance(balance);
System.out.println("转账成功,余额为" + Entity.getBalance());
}
}
}
打印(Print)
package com.gyx;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
/**
* 打印用户信息
*/
public class Print {
public void pri() throws IOException {
FileWriter writer = new FileWriter("bill.txt",true);
writer.write("姓名:" + Entity.getName());
writer.write("卡号:" + Entity.getAccount());
writer.write("密码:" + Entity.getPassword());
writer.write("余额:" + Entity.getBalance());
writer.close();
}
}
最好写一个功能运行测试一遍,以免造成不必要的错误
有任何问题都可私聊博主,我会及时解决