最近在用java学习OOP,写了一个模仿ATM的程序。
package atm;
import java.io.*;
/**
* 记录类:记录用户操作,包含存款,取款,转账的相关信息*/
class Record
{
private String cardNum;
private String operType;
private int opAmount;
/*构造方法*/
Record()
{
this.cardNum = null;
this.operType = null;
this.opAmount = 0;
}
void set_cardNum(String carNum)
{
this.cardNum = carNum;
}
void set_opType(String type)
{
this.operType = type;
}
void set_opAmount(int amount)
{
this.opAmount = amount;
}
String get_cardNum()
{
return cardNum;
}
String get_opType()
{
return operType;
}
int get_opAmount()
{
return opAmount;
}
void record_all(String num, String ty, int amo)
{
set_cardNum(num);
set_opType(ty);
set_opAmount(amo);
}
}
class Account
{
private String name;
private String Number;
private String Password;
private double Remain;
Record[] record = new Record[10];/*每个账户拥有10个记录单元*/
public Account()
{
for(int i=0;i<record.length;i++)
{
record[i] = new Record();
record[i].set_cardNum("empty");
record[i].set_opAmount(0);
record[i].set_opType("empty");
}
}
String returnName()
{
return name;
}
String returnNum()
{
return Number;
}
String returnPsw()
{
return Password;
}
double returnRemain()
{
return Remain;
}
void setNum(String num)
{
this.Number = num;
}
void setPsw(String new_psw)
{
this.Password = new_psw;
}
/* 对账户余额的操作。
* 参数为操作金额和操作类型。
* 取款操作时,先比较取出金额和账户剩余金额,
* 操作类型为1:存款,2:取款。
* 在这里,ATM类中的存/取款方法均调用setRemain方法。*/
void setRemain(double amount,int type)
{
/*对输入的数量进行检查*/
if((amount > 9999)||(amount < 0))
{
System.out.println("输入不合法,请重新输入:");
return;
}
/*存款操作*/
if(type == 1)
{
Remain+=amount;
/*操作记录
*记录操作账号,金额,类型*/
record[UserHelper.presentRecord].set_cardNum(UserHelper.get_num());
record[UserHelper.presentRecord].set_opAmount((int)amount);
record[UserHelper.presentRecord].set_opType("存款");
UserHelper.presentRecord++;
}
/*取款操作*/
else if(type == 2)
{
if(Remain < amount)/*检查余额是否充足*/
{
System.out.println("余额不足");
return;
}
else
{
Remain-=amount;
record[UserHelper.presentRecord].set_cardNum(UserHelper.get_num());
record[UserHelper.presentRecord].set_opAmount((int)amount);
record[UserHelper.presentRecord].set_opType("取款");
UserHelper.presentRecord++;
}
}
}
}
/*保持当前登录的用户的信息*/
class UserHelper
{
/*当前登录账户的卡号和密码*/
private static String Tem_num;
private static String Temp_psw;
/*当前账户在账户数组的下标*/
private static int the;
/*当前记录单元的下标*/
public static int presentRecord = 0;
static void set_num(String num)
{
Tem_num = num;
}
static void set_psw(String psw)
{
Temp_psw = psw;
}
static void set_the(int th)
{
the = th;
}
static String get_num()
{
return Tem_num;
}
static String get_psw()
{
return Temp_psw;
}
static int get_the()
{
return the;
}
}
class ATM
{
Account ac[] = new Account[5];
/*构造方法*/
public ATM()
{
for(int i=0;i<ac.length;i++)
{
ac[i] = new Account();
ac[i].setNum("8888"+i);
ac[i].setPsw("0000");
ac[i].setRemain(1000, 1);
}
}
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
/*检查用户身份的合法性
* 要求登录用户输入卡号和密码进行验证
* 合法用户则返回true,并将当前登录用户的信息存入userHelper类中,
* 非法用户返回false*/
boolean check_user(Account ac[])
{
String tem_num = "null";
String tem_psw = "null";
try
{
System.out.println("请输入您的卡号:");
tem_num = reader.readLine();
System.out.println("请输入您的密码:");
tem_psw = reader.readLine();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
/*循环检查用户数组进行匹配*/
for(int i=0;i<ac.length;i++)
{
if((ac[i].returnNum().equals(tem_num)) && (ac[i].returnPsw().equals(tem_psw)))
{
/*保存合法用户的卡号和密码,位置至UserHelper*/
UserHelper.set_num(tem_num);
UserHelper.set_psw(tem_psw);
UserHelper.set_the(i);
return true;
}
continue;
}
return false;
}
/*存款方法:接受用户输入的金额,按照输入金额的数量
* 在账户上增加相应的金额。
* 成功返回true,失败则返回false(未存入或者没有相应的账户)*/
boolean Save_money() throws IOException
{
System.out.println("请输入您想存入金额的数量:(0~9999)");
int save_amount = Integer.parseInt(reader.readLine());
/*调用setRemain方法执行操作*/
ac[UserHelper.get_the()].setRemain(save_amount, 1);
return true;
}
/*取款方法*/
boolean Drew_money() throws IOException
{
System.out.println("请输入您想取出金额的数量:(0~9999)");
int drew_amount = Integer.parseInt(reader.readLine());
/*调用setRemain方法执行操作*/
ac[UserHelper.get_the()].setRemain(drew_amount,2);
return true;
}
/*修改密码方法
*两次输入新密码确认*/
boolean Change_psw() throws IOException
{
boolean ret = false; ;
String psw_input1;
String psw_input2;
System.out.println("请输入新密码:");
do{
psw_input1 = reader.readLine();
/*密码长度限制检查*/
int length_lim = Integer.parseInt(psw_input1);
if((length_lim > 999999)||(length_lim <100000))
{
System.out.println("密码长度为6位,请重新输入:");
continue;
}
System.out.println("请再次输入新密码:");
psw_input2 = reader.readLine();
/*检查两次密码输入是否一致*/
if(!(psw_input1.equals(psw_input2)))
{
System.out.println("两次密码不一致!请重新输入:");
continue;
}
ret = true;
}while(!ret);
/*设置新密码*/
ac[UserHelper.get_the()].setPsw(psw_input1);
return true;
}
/*转账方法:输入要转账的卡号和金额进行转账
*首先根据输入的目标卡号找到目标账户,然后从
*当前账户扣除要转账的金额*/
boolean Transform_money() throws IOException
{
System.out.println("请输入您要转账的卡号");
String dest_num = reader.readLine();
System.out.println("请输入您要转账的金额:");
int Transform_num = Integer.parseInt(reader.readLine());
/*根据用户输入的卡号进行查找*/
for(int j=0;j<ac.length;j++)
{
if(ac[j].returnNum().equals(dest_num))
{
/*调用ac[i]的setRemain方法扣除当前账户余额*/
ac[UserHelper.get_the()].setRemain(Transform_num, 2);
/*调用ac[j]的setRemain方法*/
ac[j].setRemain(Transform_num, 1);
return true;
}
continue;
}
return false;
}
/*余额查询方法
**/
void Remain_check()
{
System.out.println("您的账户余额为:"+ac[UserHelper.get_the()].returnRemain());
}
boolean Continue() throws IOException
{
System.out.println("是否继续?(y/n)");
String answer = reader.readLine();
if(answer.equalsIgnoreCase("y"))
{
return true;
}
return false;
}
/*打印明细方法*/
void Print_detil()
{
System.out.println("当前账户的操作记录:");
for(int k=0;k<ac[UserHelper.get_the()].record.length;k++)
{
System.out.println("****************");
System.out.println("卡号:"+ac[UserHelper.get_the()].record[k].get_cardNum());
System.out.println("操作类型:"+ac[UserHelper.get_the()].record[k].get_opType());
System.out.println("操作数量:"+ac[UserHelper.get_the()].record[k].get_opAmount());
}
}
/*显示主界面*/
void showMain() throws IOException
{
int choice = 0;
boolean ret;
System.out.println("**********************");
System.out.println("欢迎使用ICBC自动柜员机");
System.out.println("**********************");
/*进行用户合法性验证*/
if((!check_user(ac)))/*非法用户*/
{
System.out.println("非法身份!无法登陆");
System.exit(0);
}
/*若是合法用户则继续*/
do{
System.out.println("请您选择服务种类:");
System.out.println("1.存款 2.取款");
System.out.println("3.修改密码 4.转账");
System.out.println("5.余额查询 6.退出");
System.out.println("7.打印明细");
try
{
choice = Integer.parseInt(reader.readLine());
}
catch (NumberFormatException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
/*根据用户输入执行相应操作
**/
switch(choice)
{
case 1:
Save_money();
break;
case 2:
Drew_money();
break;
case 3:
Change_psw();
break;
case 4:
Transform_money();
break;
case 5:
Remain_check();
break;
case 6:
System.exit(0);
break;
case 7:
Print_detil();
break;
}
ret = Continue();
}while(ret);
}
}
/*测试类*/
public class Text
{
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException
{
ATM atm = new ATM();
atm.showMain();
}
}