package com.myhomework;
//陈天祥
//时间:2016.10.13
//功能:ATM机
import java.util.Scanner;
public class Atmprogram {
public static void main(String[] args) {
String[][] id = { { "001", "002", "003" } };
String[][] password = { { "123", "456", "789" } };
double[][] money = { { 100, 1000, 10000 } };
manage(id,password,money);
}
// 登录账户
public static void login(String[][] id, String[][] password,double[][] money) {
Scanner s1 = new Scanner(System.in);
System.out.println("请输入您的卡号");
String idnumber = s1.nextLine();
int index = -1;
//
for (int i = 0; i < id[0].length; i++) {
if (idnumber.equals(id[0][i])) {
index = i;
System.out.println("卡号正确");
}
}
if(index==-1){
System.out.println("卡号错误\n1:重新输入\n2:退出登录系统");
int input=s1.nextInt();
switch(input){
case 1:
login(id,password,money);
break;
case 2:
System.out.println("已退出登录系统");
break;
default:
System.out.println("输入错误,请重新输入");
login(id,password,money);
break;
}
}
System.out.println("请输入您的密码");
String psw = s1.nextLine();
if (psw.equals(password[0][index])) {
System.out.println("密码正确");
administrator(id,password,money,index);
} else {
System.out.println("密码错误!\n1:重新输入\n2:退出登录系统");
int input=s1.nextInt();
switch(input){
case 1:
login(id,password,money);
break;
case 2:
System.out.println("已退出登录系统");
break;
default:
System.out.println("输入错误,请重新输入");
login(id,password,money);
break;
}
}
}
// 开通账户
public static void regist(String[][] id, String[][] password,double[][] money) {
Scanner s1 = new Scanner(System.in);
System.out.println("请输入新的ID");
String idname = s1.nextLine();
for (int i = 0; i < id[0].length; i++) {
while (id[0][i].equals(idname)) {
System.out.println("该用户已存在,请输入重新输入一个ID");
idname = s1.nextLine();
i=0;
}
}
System.out.println("该ID可以使用,请输入密码");
String newpwd = s1.nextLine();
String[] newid = id[0];
String[] newpassword = password[0];
double[] newmoney = money[0];
id[0] = new String[newid.length + 1];
password[0] = new String[newid.length + 1];
money[0] = new double[newmoney.length + 1];
for (int j = 0; j < id[0].length; j++) {
if (j == id[0].length - 1) {
id[0][j] = idname;
password[0][j] = newpwd;
money[0][j] = 0;
}else{
id[0][j] = newid[j];
password[0][j] = newpassword[j];
money[0][j] = newmoney[j];
}
}
System.out.println("开户成功!\n1:登录\n2:退出开户系统");
int input = s1.nextInt();
switch (input) {
case 1:
login(id, password,money);
break;
case 2:
System.out.println("退出注册");
break;
default:
System.out.println("输入错误,退出开户系统!");
}
}
// 用户管理系统
public static void manage(String[][] id,String[][] password,double[][]money){
System.out.println("欢迎进入ATM机!\n1:登录\n2:开通用户\n3:退出系统");
Scanner s1=new Scanner(System.in);
int input=s1.nextInt();
switch(input){
case 1:
login(id,password,money);
break;
case 2:
regist(id,password,money);
break;
case 3:
System.out.println("已退出系统");
break;
default:
System.out.println("输入错误,请重新输入");
break;
}
manage(id,password,money);
}
// 用户操作系统
public static void administrator(String[][] id,String [][]password,double[][] money,int index) {
Scanner s1 = new Scanner(System.in);
System.out
.println("登录成功!请输入您需要进行的操作:\n1:存款\n2:取款\n3:转账\n4:查询余额\n5:修改密码\n6:退出当前用户");
int input = s1.nextInt();
switch (input) {
case 1:
depositmoney(id,password,money,index);
break;
case 2:
drawmoney(id,password,money,index);
break;
case 3:
transfer(id,password,money,index);
break;
case 4:
lookupmoney(id,password,money,index);
break;
case 5:
changepassword(id,password,index);
break;
case 6:
System.out.println("已经退出当前用户");
manage(id,password,money);
break;
default:
System.out.println("输入错误,请重新输入");
}
administrator(id,password,money,index);
}
// 存款
public static void depositmoney(String[][] id, String[][] password,double[][] money,int index) {
Scanner s1 = new Scanner(System.in);
System.out.println("请输入你想存入的数目:");
double dmoney = s1.nextDouble();
if(dmoney>=0){
money[0][index] += dmoney;
System.out.println("存款成功,您目前的余额为:" + money[0][index]);
}
}
// 取款
public static void drawmoney(String[][] id, String[][] password,double[][] money,int index) {
Scanner s1=new Scanner(System.in);
System.out.println("请输入你想取出的数目:");
double dmoney = s1.nextDouble();
if (dmoney > money[0][index]) {
System.out.println("你卡里余额不足,取款失败!\n1:继续取款\n2:结束取款 ");
int select = s1.nextInt();
switch (select) {
case 1:
drawmoney(id, password, money,index);
break;
case 2:
System.out.println("退出取款系统");
default:
System.out.println("输入错误,退出取款系统");
break;
}
} else {
money[0][index] -= dmoney;
System.out.println("取款成功,您目前的余额为:" + money[0][index]);
}
}
// 查询余额
public static void lookupmoney(String[][] id, String[][] password,double[][] money,int index) {
System.out.println("您的余额为:"+money[0][index]);
}
//计算下标
public static int countindex(String[][] id, String idnumber) {
int index = -1;
for (int i = 0; i < id[0].length; i++) {
if (idnumber.equals(id[0][i])) {
index = i;
}
}
return index;
}
// 修改ID的密码
public static void changepassword(String[][] id, String[][] password,int index) {
Scanner s1 = new Scanner(System.in);
System.out.println("请输入您的原密码");
String idname = s1.nextLine();
if(idname.equals(password[0][index])){
System.out.println("密码正确!\n请输入新的密码");
String newpwd=s1.nextLine();
System.out.println("请再次输入新密码:");
String newpwd1=s1.nextLine();
if(newpwd.equals(newpwd1)){
password[0][index] = newpwd;
System.out.println("修改成功,您的新密码为:" + password[0][index]);
}
}
}
//转账
public static void transfer(String[][] id,String[][] password,double[][] money,int index){
System.out.println("请输入你需要转入的账户");
Scanner s1=new Scanner(System.in);
String transferuser=s1.nextLine();
int indexuser=countindex(id,transferuser);
if(indexuser==-1){
System.out.println("账户不存在!\n1:重新输入\n2:退出转账");
int input=s1.nextInt();
switch(input){
case 1:
transfer(id,password,money,index);
break;
case 2:
System.out.println("已退出转账系统");
administrator(id,password,money,index);
break;
default:
System.out.println("输入错误,退出转账");
administrator(id,password,money,index);
break;
}
}else{
System.out.println("请输入您需要转账的金额:");
double transfermoney=s1.nextDouble();
if(transfermoney>=0&&transfermoney
money[0][index]-=transfermoney;
money[0][indexuser]+=transfermoney;
System.out.println("转账完成,您目前的余额为:"+money[0][index]);
}else{
System.out.println("转账金额无效!\n1:重新转账\n退出转账");
int input=s1.nextInt();
switch(input){
case 1:
transfer(id,password,money,index);
break;
case 2:
System.out.println("已退出转账系统");
administrator(id,password,money,index);
break;
default:
System.out.println("输入错误,退出转账");
administrator(id,password,money,index);
break;
}
}
}
}
}