啊哈这次是用的老师给的框架,老师用的是二维数组
我前两天发的是用HashMap写的,感觉HashMap更简单哈哈哈哈
不过这个只有一个类
下面是项目结构
下面是代码
package ATMbag;
import java.util.HashMap;
import java.util.Scanner;
public class atmDemo {
static int MAX = 256;
static String[][] users = new String[MAX][MAX];
static double[] balance = new double[MAX];
static int id = -1;
private static void init() {
System.out.println("ATM机正在初始化......");
String intial = "1:Jack:pwd1:1000.0|2:Jones:pwd2:3000.0|3:Mark:pwd3:5000.0";
int i;
for (i = 0; i < 3; i++) { //i<n i指id,0,1,2,3依次存放名字,密码金额
users[i][0] = intial.split("\\|")[i].split(":")[0];
users[i][1] = intial.split("\\|")[i].split(":")[1];
users[i][2] = intial.split("\\|")[i].split(":")[2];
users[i][3] = intial.split("\\|")[i].split(":")[3];
double value = Double.parseDouble(users[i][3]);
balance[i] = value;// balance[i]是金额
}
System.out.println("ATM机初始化完成");
for (int k = 0; k < 3; k++) {
for (int j = 0; j < 4; j++) {
System.out.println(users[k][j]);
}
}
for(int m=0;m<3;m++){
System.out.println(balance[m]);
}
}
public static void main(String[] args) {
mainMenu();
}
private static void mainMenu() { //主菜单
int choice;
while (true) {
Scanner s = new Scanner(System.in);
System.out.println("");
System.out.println("---欢迎使用SH Bank ATM机");
System.out.println(" ****1.系统初始化\t*****");
System.out.println(" ****2.用户操作\t****");
System.out.println(" ****3.系统管理\t****");
System.out.println(" ****0.退出\t****");
System.out.println("");
System.out.println("请选择您的操作:");
choice = s.nextInt();
switch (choice) {
case 1:
System.out.println("请稍等,正在为您初始化");
init();
break;
case 2:
Scanner sc = new Scanner(System.in);
System.out.println("请输入账号:");
String yh = sc.nextLine();
System.out.println("请输入密码:");
String passward = sc.nextLine();
if (checkIn(yh, passward)) {
System.out.println("登录成功");
subMenu();
} else {
System.out.println("账号或密码错误,请重新登录!");
}
break;
case 3:
System.out.println("正在建设中......");
break;
case 0:
System.out.println("谢谢使用,再见!");
return;
default:
System.out.println("您的输入有误,请重新输入:");
break;
}
}
}
private static void subMenu() { //存取操作
while (true) {
System.out.println(users[id-1][1]);
System.out.println("--欢迎使用SH Bank ATM机--");
System.out.println(" **** 1.查询余额 ****");
System.out.println(" **** 2.取款 ****");
System.out.println(" **** 3.存款 ****");
System.out.println(" **** 0.退卡 ****");
System.out.println("");
Scanner s1 = new Scanner(System.in);
System.out.println("请选择您的操作:");
int choice = s1.nextInt();
switch (choice) {
case 1:
System.out.println("您账户余额为:" + Double.parseDouble(users[id-1][3]));
break;
case 2:
Scanner s2=new Scanner(System.in);
System.out.println("请输入您要取出的金额:");
double amount = s2.nextDouble();
if (withDraw(amount)) {
System.out.println("支取成功");
System.out.println("您账户余额为:" + Double.parseDouble(users[id-1][3]));
} else {
System.out.println("您的金额不足!");
}
break;
case 3:
System.out.println("请输入您要存入的金额:");
double depAmount = s1.nextDouble();
dePosit(depAmount);
System.out.println("存款成功,您的余额为:" + query(id));
break;
case 0:
System.out.println("谢谢使用," + users[id-1][1] + "请取出您的银行卡");
return;
default:
System.out.println("您的输入有误,请重新输入");
break;
}
}
}
private static boolean checkIn(String yh, String passward) {//登录
boolean flag = false;
for (int i = 0; i < users.length; i++) {
if (yh.equals(users[i][0])) {
int k = i;
if (passward.equals(users[k][2])) {
id=Integer.parseInt(yh);
flag = true;
}
}
}
return flag;
}
private static boolean withDraw(double amount) {//取款
boolean flag = false;
if (amount <= balance[id-1]) {
balance[id-1] -= amount;
users[id-1][3]=String.valueOf(balance[id-1]);
flag = true;
}
return flag;
}
private static void dePosit(double amount){//存款
balance[id-1]+=amount;
users[id-1][3]=String.valueOf(balance[id-1]);
}
private static double query(int accountID) {//查询余额
id=accountID;
return balance[id-1];
}
}
写在一个类里面真的好容易弄混,改了好多次,如果有用的话就点个赞叭,过两天更新arraylist做的ATM机。