package YinHang;
//User类
public class User {
private String cardNo;//卡号
private String idcard;//身份证
private String Name;//用户名
private String pawd;//密码
private String phone;//电话
private double balance;//余额
//构造
public User(){}
@Override
public String toString() {
return "User{" +
"cardNo='" + cardNo + '\'' +
", idcard='" + idcard + '\'' +
", Name='" + Name + '\'' +
", pawd='" + pawd + '\'' +
", phone='" + phone + '\'' +
", balance=" + balance +
'}';
}
public User(String cardNo, String idcard, String name, String pawd, String phone, double balance) {
this.cardNo = cardNo;
this.idcard = idcard;
Name = name;
this.pawd = pawd;
this.phone = phone;
this.balance = balance;
}
public String getCardNo() {
return cardNo;
}
public void setCardNo(String cardNo) {
this.cardNo = cardNo;
}
public String getIdcard() {
return idcard;
}
public void setIdcard(String idcard) {
this.idcard = idcard;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getPawd() {
return pawd;
}
public void setPawd(String pawd) {
this.pawd = pawd;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
Bank
package YinHang;
//银行类
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Scanner;
public class Bank {
//创建数组
private User[] users = new User[5];
//保存个数
private int size;
//1.初始化方法
public void Initial(){
//创建user1
User user1 = new User();
user1.setCardNo("1001");
user1.setIdcard("410422");
user1.setName("用户A");
user1.setPawd("123456");
user1.setPhone("10086");
user1.setBalance(10000);
//创建user2
User user2 = new User("1002", "410423", "用户B", "123456", "110", 2000);
//创建user3
User user3 = new User("1003", "410424", "用户C", "123456", "120", 3000);
//添加元素
users[0] = user1;
users[1] = user2;
users[2] = user3;
size = 3;
System.out.println("用户初始化成功");
}
//用户登录
public void login(){
Scanner sc = new Scanner(System.in);
System.out.println("卡号:");
String cardNo = sc.next();
System.out.println("密码:");
String pawd = sc.next();
//遍历数组
User u = null;//保存找到的用户
for (int i = 0; i < size; i++) {
if (users[i].getCardNo().equals(cardNo) && users[i].getPawd().equals(pawd)) {
u = users[i];
break;
}
}
if (u != null) {
//显示菜单
showMenu(u);
}else {
System.out.println("卡号或者密码错误");
}
}
//显示菜单
private void showMenu(User u) {
Scanner sc = new Scanner(System.in);
System.out.println("=====" + u.getName() + "welcome to the tansk-bank system ====");
//循环
boolean b = true;
while (b = true){
System.out.println("---- 存款 -- 取款 -- 转账 -- 查询余额 -- 修改密码 -- 退出 ----");
int c = sc.nextInt();
switch (c){
case 1:
this.save(u);
break;
case 2:
this.withDraw(u);
break;
case 3:
this.trans(u);
break;
case 4:
this.query(u);
break;
case 5:
this.alter(u);
break;
case 6:
return;
default:
System.out.println("输入错误");
b = false;
break;
}
}
}
//存钱
public void save(User u){
Scanner sc = new Scanner(System.in);
System.out.println("输入存款金额");
double h = sc.nextDouble();
if (h > 0) {
u.setBalance(u.getBalance() + h);
System.out.println("存钱成功,余额:" + u.getBalance());
}else {
System.out.println("存钱失败");
}
}
//取钱
public void withDraw(User u){
Scanner sc = new Scanner(System.in);
System.out.println("输入取款金额");
double h = sc.nextDouble();
if (h>0 && u.getBalance() >= h) {
u.setBalance(u.getBalance() - h);
System.out.println("取钱成功,余额:" + u.getBalance());
}else {
System.out.println("取钱失败");
}
}
//转账
public void trans(User u){
Scanner sc = new Scanner(System.in);
System.out.println("输入卡号:");
String cardNo = sc.next();
System.out.println("输入转账金额:");
double h = sc.nextDouble();
//判断卡号是否存在
User to = null;
for (int i = 0; i < size; i++) {
try {
if (users[i].getCardNo().equals(cardNo)) {
to = users[i];
break;
}
}catch (Exception e){
break;
}
}
if (to != null) {
if (u.getBalance() >= h) {
u.setBalance(u.getBalance() - h);
to.setBalance(to.getBalance() + h);
System.out.println("转账成功");
}else {
System.out.println("余额不足,转账失败");
}
}else {
System.out.println("转账卡号不存在");
return;
}
}
//查余额
private void query(User u) {
System.out.println("卡号:" + u.getCardNo() + "余额:" + u.getBalance());
}
//改密码
public void alter(User u){
Scanner sc = new Scanner(System.in);
System.out.println("输入新的密码");
String pawd = sc.next();
if (pawd.length() == 6) {
u.setPawd(pawd);
System.out.println("修改成功");
}else {
System.out.println("密码不符合要求");
}
}
}
Realize
package YinHang;
//实现类
public class Realize {
public static void main(String[] args) throws Exception {
Bank b = new Bank();
b.Initial();
b.login();
}
}
ATM机银行柜台升级版
最新推荐文章于 2024-09-07 12:59:11 发布