序列化,反序列化,模拟ATM机

时间:2020-12-16 03:51:20
package com.bank.unionpay;

//银行卡的接口
public interface I_yinhangka
{ //抽象方法
//public abstract默认修饰抽象的 public abstract void cxyue();//查询余额 void cunkuan();//存款 void qukuan();//取款 void accound();//账户 }

  

package com.bank.unionpay;

public abstract class Atm implements I_yinhangka
{
String leixing;//银行卡类型
String accound;//账户
double qukuan;//取款
double cunkuan;//存款
double cxyue ;//余额
String name;
String sfzheng;//省份证号 public Atm(String name,String sfzheng,String accound, double cxyue ) {
super();
this.accound = accound;
this.cxyue = cxyue;
this.name = name;
this.sfzheng = sfzheng;
} public Atm() {
super();
} @Override
public void cxyue()
{
// 查询余额
cxyue-=qukuan;
cxyue+=cunkuan; System.out.println("您的余额="+cxyue);
} @Override
public void cunkuan()
{
// 存款
cxyue+=cunkuan; System.out.println("存款成功,您的存款金额为"+cunkuan);
System.out.println("本次操作后您的余额为:"+cxyue);
} @Override
public void qukuan()
{
// 取款
cxyue-=qukuan;
if(cxyue>=0)
{
System.out.println("取款成功,本次取款金额= "+qukuan);
System.out.println("本次操作后您的余额= "+cxyue);
}
else
{
System.out.println("余额不足,无法取款"); } } @Override
public void accound() {
// 账户 } //声明成员方法
public double qukuan(double qukuan)//取款
{
cxyue-=qukuan;
if(cxyue>=0)
{
System.out.println("取款成功,本次取款金额= "+qukuan);
System.out.println("本次操作后您的余额= "+cxyue);
}
else
{
System.out.println("余额不足,无法取款"); }
return qukuan; } public double cunkuan(double cunkuan)//存款
{ cxyue+=cunkuan; System.out.println("存款成功,您的存款金额为"+cunkuan);
System.out.println("本次操作后您的余额为:"+cxyue);
return cunkuan;
} public double cxyue(double cxyue)
{
cxyue-=qukuan;
cxyue+=cunkuan; System.out.println("您的余额="+cxyue);
return cxyue;
} //添加开户的抽象方法
public abstract void kaihu(); }

  

package com.bank.unionpay;

import java.util.ArrayList;
import java.util.List; public class Zhujiemian { public void zhujiemian()
{
System.out.println("银行自助终端系统");
System.out.println();
System.out.println("************************");
List<String> li=new ArrayList<>();
li.add("开户");
li.add("查询");
li.add("存款");
li.add("取款");
li.add("退出"); for(int i=0;i<5;i++)
{
System.out.println(i+"------"+li.get(i));
}
System.out.println();
System.out.println("请选择您要执行的功能");
} }

  

package com.bank.unionpay;

//银行枚举类型
public enum Leixing
{ ABC("农行"),ICBC("工行"),CCB("建行"),BOC("中行"); private final String leixing; public String getLeixing()
{
return this.leixing;
} //构造方法
private Leixing(String leixing)
{
this.leixing=leixing;
} }

  

package com.bank.abc;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import com.bank.unionpay.Leixing; //单利模式
public class Bank implements Serializable
{
private Bank()//禁止外部访问构造方法
{ }
private static Bank bank=new Bank();//内部产生唯一实例
public static Bank getBank()//开放一个方法给调用者返回这个实例
{
return bank;
} final Leixing lx=Leixing.ABC;//农行类型 private String yhName;//银行名称 public List <String>card=new ArrayList<>();//银行卡列表 public Map<String, CxCard > account=new HashMap<>();//账号储蓄卡集合 }

  

package com.bank.abc;

import java.util.Random;
import java.util.Scanner; import com.bank.unionpay.Atm;
import com.bank.unionpay.Leixing; public class Atm2 extends Atm
{ public CxCard cx=new CxCard();// public Atm2(String name, String sfzheng, String accound, double cxyue) {
super(name, sfzheng, accound, cxyue);
// TODO 自动生成的构造函数存根
} public Atm2() {
// TODO 自动生成的构造函数存根
super();
} final Leixing lx = Leixing.ABC; @Override
public void kaihu()// 实现开户
{
System.out.println("您选择的为开户功能");
Scanner sc0=new Scanner(System.in);
System.out.println("请输入姓名");
cx.name=sc0.nextLine(); for(int i=1;i>0;i++)
{
System.out.println("请输入身份证号");
cx.sfzheng=sc0.nextLine(); if(cx.sfzheng.length()==18)//判断身份证
{ for(int j=1;j>0;j++)
{ System.out.println("请输入密码");
String mm="\\d{6}";//6位数字正则表达式
cx.password=sc0.nextLine();
if(cx.password.matches(mm))
{ cx.setPassword(cx.password);//写入密码 Random ra=new Random();//随机生成6位卡号
int[] ch=new int[6];
StringBuilder str=new StringBuilder();
String s;//用于下面拼接
for(int k=0;k<6;k++)
{
ch[k]=ra.nextInt(9);//0-9
} for(Integer c:ch)
{
str.append(c.toString());//遍历 拼接6位数字
}
s=str.toString();
cx.setCairNumber(s);//写入卡号 Bank.getBank().card.add(cx.getCairNumber());//将生成的储蓄卡号存到银行的银行卡列表中 Bank.getBank().account.put(cx.getCairNumber(), cx); //id+储蓄卡对象
System.out.println("开户成功");
System.out.println("您的账号为: "+cx.getCairNumber()); break;
} else
{
System.out.println("请输入6位纯数字密码");
}
} break;
} else
{
System.out.println("请重新输入身份证号"); }
} } }

  

package com.bank.abc;

import java.io.Serializable;
import java.util.Scanner; import com.bank.unionpay.Leixing; //储蓄卡
//只有实现了Serializable和Externalizable接口的类的对象才能被序列化。
public class CxCard implements Serializable {
//属性
final Leixing lx=Leixing.ABC;
String name;//姓名
String sfzheng;//身份证号
String cairNumber;//卡号
String password;//密码
double cxyue ;//余额 //生成卡号的get set方法 public String getCairNumber() {
return cairNumber;
} public void setCairNumber(String cairNumber) {
this.cairNumber = cairNumber;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getSfzheng() {
return sfzheng;
} public void setSfzheng(String sfzheng) {
this.sfzheng = sfzheng;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public double getCxyue() {
return cxyue;
} public void setCxyue(double cxyue) {
this.cxyue = cxyue;
} public CxCard(String name, String sfzheng, String cairNumber, String password, double cxyue) {
super();
this.name = name;
this.sfzheng = sfzheng;
this.cairNumber = cairNumber;
this.password = password;
this.cxyue = cxyue;
} @Override
public String toString() {
return "姓名:" + name + "\n"+" 身份证:" + sfzheng + "\n"+"卡号: " + cairNumber +"\n"+ " 余额:" + cxyue;
} public CxCard() {
// TODO 自动生成的构造函数存根
} //声明成员方法
public void qukuan()//取款
{
if(true)
{
System.out.println("您选择的为取款功能");
for(int i=1;i>0;i++)
{
Scanner sc1=new Scanner(System.in); System.out.println("请输入您要取款的卡号");
cairNumber=sc1.nextLine();
if(Bank.getBank().account.containsKey(getCairNumber()))//判断卡号是否存在
{
for(int h=0;h<4;h++)
{
h=h+1; System.out.println("请输入密码"); String passwordshuru=sc1.nextLine(); if(passwordshuru.equals(Bank.getBank().account.get(cairNumber).getPassword()))//判断密码
{
System.out.println("您要取款的账号信息为:");
System.out.println("****************"); //遍历卡号
for(String k:Bank.getBank().account.keySet())
{
System.out.println("账号: "+k);
//通过k便利values的账户信息
System.out.println(Bank.getBank().account.get(k));
}
System.out.println("请输入您要取款的金额");
Scanner qukuan=new Scanner(System.in);
double qk=qukuan.nextDouble(); if(qk>=0)
{
//通过k获取v里面的余额,减去取款金额生成余额
cxyue=Bank.getBank().account.get(cairNumber).getCxyue() -qk; System.out.println(cxyue); if(cxyue>=0)
{
Bank.getBank().account.get(cairNumber).setCxyue(cxyue);
System.out.println("取款成功,本次取款金额= "+qk); System.out.println("本次操作后您的余额= "+Bank.getBank().account.get(cairNumber).getCxyue());
} }
else
{
System.out.println("余额不足,无法取款"); } break;
}
else
{
System.out.println("请重新输入密码,还剩"+(4-h)+"次");
} } break;
} } } } public void cunkuan()//存款
{ if(true)
{
System.out.println("您选择的为存款功能");
for(int i=1;i>0;i++)
{
Scanner sc1=new Scanner(System.in); System.out.println("请输入您要存款的卡号");
cairNumber=sc1.nextLine();
if(Bank.getBank().account.containsKey(getCairNumber()))//判断卡号是否存在
{
for(int h=0;h<4;h++)
{
h=h+1; System.out.println("请输入密码"); String passwordshuru=sc1.nextLine(); //判断如果输入的密码和通过k找到v里面存的密码一致,输出信息
if(passwordshuru.equals(Bank.getBank().account.get(cairNumber).getPassword()))//判断密码
{
System.out.println("您要存款的账号信息为:");
System.out.println("****************"); //遍历卡号
for(String k:Bank.getBank().account.keySet())
{
System.out.println("账号: "+k);
System.out.println(Bank.getBank().account.get(k));
}
System.out.println("请输入您要存入的金额");
Scanner cunkuan1=new Scanner(System.in);
double ck=cunkuan1.nextDouble();
if(ck>=0)
{
cxyue+=ck; System.out.println("存款成功,您的存款金额为"+ck);
//把操作后的余额写进集合
Bank.getBank().account.get(cairNumber).setCxyue(cxyue);
System.out.println("本次操作后您的余额为:"+Bank.getBank().account.get(cairNumber).getCxyue()); }
else
{
System.out.println("你输入的数据无效,系统将自动退出!谢谢使用");
} break;
}
else
{
System.out.println("请重新输入密码,还剩"+(4-h)+"次");
}
} break;
}
else
{
System.out.println("该卡号不存在,请重新输入要查询的卡号");
} }
} } public void cxyue()//查询
{ if(true)
{
System.out.println("您选择的为查询功能");
for(int i=1;i>0;i++)
{
Scanner sc1=new Scanner(System.in); System.out.println("请输入您要查询的卡号");
cairNumber=sc1.nextLine();
if(Bank.getBank().account.containsKey(getCairNumber()))//判断卡号是否存在
{
for(int h=0;h<3;h++)
{
h=h+1; System.out.println("请输入密码"); String passwordshuru=sc1.nextLine(); //判断密码
if(passwordshuru.equals(Bank.getBank().account.get(cairNumber).getPassword()))
{
System.out.println("您查询的账号信息为:");
System.out.println("****************"); //遍历卡号
for(String k:Bank.getBank().account.keySet())
{
System.out.println("账号: "+k);
//通过k遍历values的信息
System.out.println(Bank.getBank().account.get(cairNumber));
}
break;
}
else
{
System.out.println("请重新输入密码,还剩"+(4-h)+"次");
} }
//System.out.println("密码输入超过三次,自动退出系统");
break;
}
else
{
System.out.println("该卡号不存在,请重新输入要查询的卡号");
} } } } }

  

package com.bank.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner; import com.bank.abc.Atm2;
import com.bank.abc.Bank;
import com.bank.abc.CxCard;
import com.bank.unionpay.Atm;
import com.bank.unionpay.Leixing;
import com.bank.unionpay.Zhujiemian; public class A
{ public static void main(String[] args) throws Exception
{
// TODO 自动生成的方法存根 Leixing[] l= Leixing.values(); for(Leixing x:l)
{
System.out.println("银行卡类型的枚举="+x);
} System.out.println("*************************"); try
{
ObjectInputStream in1=new ObjectInputStream(new FileInputStream("d:/abc122/abc.obj"));
Map<String, CxCard > account1=(Map<String, CxCard>) in1.readObject();
Bank.getBank().account=account1;
//System.out.println(Bank.getBank().account);
}
catch(Exception e)
{ } for(int i=1;i>0;i++)
{
Zhujiemian zjm=new Zhujiemian();
zjm.zhujiemian(); Scanner sc=new Scanner(System.in);
int in=sc.nextInt();
if(in==0||in==1||in==2||in==3||in==4)
{ if(in==0) {//测试开户
Atm2 a2=new Atm2();
a2.kaihu();
continue;
}
//查询
if(in==1)
{
Atm2 a2=new Atm2();
a2.cx.cxyue();
continue;
} if(in==2)
{
//存款
Atm2 a2=new Atm2();
a2.cx.cunkuan();
continue;
}
if(in==3)
{
//取款
Atm2 a2=new Atm2();
a2.cx.qukuan();
continue;
}
if(in==4)
{
//退出
System.out.println("系统即将退出"); File fi=new File("d:/abc122");
if(!fi.exists())
{
fi.mkdirs();
}
ObjectOutputStream obj=new ObjectOutputStream(new FileOutputStream("d:/abc122/abc.obj"));
obj.writeObject(Bank.getBank().account);
System.out.println("数据保存完成");
obj.close(); break; } }
else
{
System.out.println("该菜单不存在");
} } } }