有点些许无聊,写了个ATM系统 锻炼一下脑子
=XX银行ATM系统登陆= (menu1)
1.插卡
2.拔卡
=XX银行ATM系统操作页面== (menu2)
1.存款
2.取款
3.转账
4.查询余额
5.退卡//回到menu1()
Customer类
package com.huan;
/**
* 银行客户实体类
*
*
*/
public class Customer {
private int id;// 卡号
private String name;// 姓名
private String pass;// 密码
private double money;// 金钱
private boolean state;// 客户状态 true:激活 false:冻结
public Customer() {
}
public Customer(int id, String name, String pass, double money, boolean state) {
this.id = id;
this.name = name;
this.pass = pass;
this.money = money;
this.state = state;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public boolean isState() {
return state;
}
public void setState(boolean state) {
this.state = state;
}
@Override
public String toString() {
return this.id + "\t" + this.name + "\t\t" + this.money;
}
}
Service类 (重点)
package com.huan;
import java.util.Scanner;
public class Service {
//定义程序Scanner
Scanner input = new Scanner(System.in);
//定义客户信息
Customer[] customers =new Customer[100];
//定义初始化数据
public void init(){
customers[0] = new Customer( 1001,"欢欢","123456",200000,true );
customers[1] = new Customer( 1002,"昊哥","234567",100000,true );
}
//定义一个功能模块
public void menu1(){
System.out.println("1.插卡");
System.out.println("2.拔卡");
int choose = input.nextInt();
switch (choose){
case 1:
customersLogin();
break;
case 2:
System.out.println("正在退卡........");
System.out.println("请收好你的银行卡........");
break;
default:
System.out.println("输入有误....");
break;
}
menu1();
}
public void customersLogin() {
System.out.println("请输入你的卡号");
int id = input.nextInt();
int index =getCustomerIndex(id);
if (index < 0){
System.out.println("无效的银行卡.....");
System.out.println("正在退卡中.....");
System.out.println("请收好的你卡片.....");
return;
}
if (!customers[index].isState()){
System.out.println("此账户已经冻结,请联系工作人员佟欢");
return;
}
for(int i = 0;i<=3;i++){
System.out.println("请输入密码!");
String password = input.nextLine();
//如果输入的密码和银行的密码相等,则输入成功
if(customers[index].getPass().equals( password )){
System.out.println("登陆成功!");
menu2(index);
break;
}
//密码只有三次输入机会
if (i == 3){
System.out.println("机会已经用尽,账户已冻结,请联系管理员");
customers[index].setState( false );
}else{
System.out.println("你还有" + (3-i) + "次机会");
}
}
}
//定义id唯一性,如果客户输入的数字id和数据库中的id不相等,则返回 -1,如果相等,则返回客户输入的数字id
public int getCustomerIndex(int id) {
int index = -1;
for (int i = 0; i <customers.length; i++) {
if (customers[i] != null && customers[i].getId() == id){
index = i;
break;
}
}
return index;
}
public void menu2(int customerIndex){
System.out.println("====XX银行系统欢迎【"+customers[customerIndex].getName()+"】");
System.out.println("1.存款");
System.out.println("2.取款");
System.out.println("3.转账");
System.out.println("4.查询");
System.out.println("5.退卡");
int choose = input.nextInt();
switch (choose){
case 1:
addCustomerMoney(customerIndex);
break;
case 2:
dropCustomerMoney(customerIndex);
break;
case 3:
pastCustomerMoney(customerIndex);
break;
case 4:
showCustomerMoney(customerIndex);
break;
case 5:
menu1();
break;
default:
System.out.println("输入有误!!");
}
menu2(customerIndex);
}
//转账功能实现
public void pastCustomerMoney(int customerIndex) {
System.out.println("请输入对方的银行卡号");
int otherID = input.nextInt();
//获取对方卡号
int otherIndex = getCustomerIndex( otherID );
if (otherIndex < 0){
System.out.println("该卡号不存在");
return;
}
System.out.println("请输入转账金额");
double pastMoney = input.nextDouble();
//得到自己金额
double money = customers[customerIndex].getMoney();
//做比较,如果自己金额小于转账金额,则余额不足
if (money < pastMoney){
System.out.println("余额不足");
System.out.println("请重试");
return;
}
//展示对方信息
System.out.println("卡号\t姓名");
System.out.println(customers[otherIndex].getId() + "\t" + customers[otherIndex].getName());
System.out.println("是否确认转账");
String choose = input.next();
if (choose.equals( "n" )){
System.out.println("转账失败");
return;
}else if (choose.equals( "y" )) {
//做转账操作
//自己的余额减少
money -= pastMoney;
//得到自己的余额
customers[customerIndex].setMoney( money );
//获取对方的余额
double otherMoney = customers[otherIndex].getMoney();
//对方的余额增加
otherMoney += pastMoney; //otherMoney + pastMoney = otherMoney
customers[otherIndex].setMoney( otherMoney );
System.out.println( "转账成功" );
return;
}else{
System.out.println("无效的操作");
System.out.println("请重试");
return;
}
}
public void dropCustomerMoney(int customerIndex) {
System.out.println( "请输入取款金额【面值100元的钞票】" );
double dropMoney = input.nextDouble();
//判断输入的金额是否能被100整除
if (dropMoney % 100 != 0) {
System.out.println( "金额不合法" );
return;
}
//得到原有存款
double money = customers[customerIndex].getMoney();
//如果原有存款小于要取款金额,则余额不足
if (money < dropMoney) {
System.out.println( "余额不足!" );
return;
}
//累减
money -= dropMoney; //money = money - dropMoney
//剩余金额
customers[customerIndex].setMoney( money );
System.out.println("正在出钞..");
System.out.println("取款成功!");
}
//将所有金额展示出来
public void showCustomerMoney(int customerIndex) {
System.out.println("卡号\t姓名\t金额");
System.out.println(customers[customerIndex].toString());
}
//存款操作
public void addCustomerMoney(int customerIndex) {
System.out.println("请放入现金【面值100元的钞票】");
double addMoney = input.nextDouble();
//判断输入的金额是否能被100整除
if (addMoney % 100 != 0){
System.out.println("金额不合法");
return;
}
//开始存钱
double money = customers[customerIndex].getMoney();
money += addMoney;
customers[customerIndex].setMoney( money );
System.out.println("存款成功!");
}
public void start(){
init();
menu1();
}
}
Test测试类
package com.huan;
public class Test {
public static void main(String[] args) {
new Service().start();
}
}