文件存储型ATM机-----JAVA
本人小白一枚,java初学者,以此文章记录自己的学习历程。
今天老师要求用java 写一个模拟ATM机,要求机器随机生成账号,并把这些数据对象存储到文件中,以io方式读入读出文件,获取用户信息,以达到文件用户信息的作用。
代码:
package com.nylg.bean;
import java.util.Scanner;
//ATM 工作类
public class ATMTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Menu m = new Menu();
Util util = new Util();
Util2 util2 = new Util2();
User u = null;
//登录注册界面
boolean b = true;
while(b){
int index = m.loginMenu(sc);
if(index1){
//注册
b = util.userReg(sc);
}else{
//登录
u = util.userLogin(sc);
//System.out.println(u);
if(u!=null){
//登录成功
b = false;
}else{
System.out.println(“登录失败,请重新登录”);
}
}
}
//欢迎界面 进行存取查
while(u!=null){
int index = m.welcomeMenu(sc,u);
if(index1){
//存款
u= util2.saveMoney(sc, u);
m.show(sc);
}else if(index2){
//取款
u = util2.moveMoney(sc, u);
m.show(sc);
}else if(index3){
m.showMoney(sc, u);
}
}
}
}
package com.nylg.bean;
//界面类 以及查询金额
import java.util.Scanner;
public class Menu {
public int loginMenu(Scanner sc){
System.out.println("---------------");
System.out.println("|欢迎登录注册ATM |");
System.out.println("|1,注册 |");
System.out.println("|2,登录 |");
System.out.println("---------------");
System.out.println(“请输入整数类型的值:”);
int index = sc.nextInt();
return index;
}
public int welcomeMenu(Scanner sc,User u){
String name="";
String x = u.getName().substring(0, 1); //截取字符串
if(u.getSex().equals("男")){
name=x+"先生";
}else{
name =x+"女士";
}
System.out.println("---------------");
System.out.println("|欢迎"+name+"使用ATM|");
System.out.println("|1,存款 |");
System.out.println("|2,取款 |");
System.out.println("|3,查询 |");
System.out.println("---------------");
System.out.println("请输入整数类型的值:");
int index = sc.nextInt();
return index;
}
public void showMoney(Scanner sc,User u){
System.out.println("当前余额是:"+u.getMoney());
System.out.println("请输入任意符号回到首页");
String str = sc.next();
}
public void show(Scanner sc){
System.out.println("操作成功");
System.out.println("请输入任意符号回到首页");
String str = sc.next();
}
}
package com.nylg.bean;
用户标准类
import java.io.Serializable;
public class User implements Serializable{ //实现序列化
private static final long serialVersionUID = 1L;
private String name;
private String sex;
private String cardNum;
private int pwd;
private double money;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getCardNum() {
return cardNum;
}
public void setCardNum(String cardNum) {
this.cardNum = cardNum;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public int getPwd() {
return pwd;
}
public void setPwd(int pwd) {
this.pwd = pwd;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((cardNum == null) ? 0 : cardNum.hashCode());
long temp;
temp = Double.doubleToLongBits(money);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + pwd;
result = prime * result + ((sex == null) ? 0 : sex.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (cardNum == null) {
if (other.cardNum != null)
return false;
} else if (!cardNum.equals(other.cardNum))
return false;
if (Double.doubleToLongBits(money) != Double
.doubleToLongBits(other.money))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (pwd != other.pwd)
return false;
if (sex == null) {
if (other.sex != null)
return false;
} else if (!sex.equals(other.sex))
return false;
return true;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String name, String sex, String cardNum, int pwd, double money) {
super();
this.name = name;
this.sex = sex;
this.cardNum = cardNum;
this.pwd = pwd;
this.money = money;
}
@Override
public String toString() {
return "User [name=" + name + ", sex=" + sex + ", cardNum=" + cardNum
+ ", pwd=" + pwd + ", money=" + money + "]";
}
}
package com.nylg.bean;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Properties;
import java.util.Scanner;
//注册 登陆类
public class Util {
//注册 获取信息 封装成对象 对象序列化 给properties文件添加值
public boolean userReg(Scanner sc){
System.out.println(“请输入姓名:”);
String name = sc.next();
System.out.println(“请输入性别:”);
String sex = sc.next();
//密码及密码判断
System.out.println(“请输入密码:”);
int pwd = 0;
while(true){
pwd = sc.nextInt();
System.out.println(“请输入确认密码:”);
int pwd1 = sc.nextInt();
if(pwd==pwd1){
break;
}else{
System.err.println(“两次密码不一致,请重新输入:”);
}
}
//获取卡号
String cardNum = getOnlyCard();
User u = new User(name, sex, cardNum, pwd, 0);
//对象序列化 把对象的卡号和路径形成键值对 进行存储
boolean bo = saveObject(u);
//对象序列化以后 我才能把信息存储在properties文件中
if(bo){
//信息存储在properties文件中
bo = saveProperties(cardNum);
}
if(bo){
System.out.println(“注册成功,您的卡号是:”+cardNum);
return true;
}else{
System.err.println(“注册失败,请重新注册”);
return false;
}
}
//存储对象
public boolean saveObject(User u){
ObjectOutputStream out=null;
try {
out = new ObjectOutputStream(new FileOutputStream("E:/fileupload/atm/"+u.getCardNum()+".txt"));
out.writeObject(u);
return true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(out!=null){
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return false;
}
//注册时 添加键值对
public boolean saveProperties(String cardNum){
Properties p = new Properties();
p.setProperty(cardNum, "E:/fileupload/ATm/"+cardNum+".txt");
FileOutputStream out = null;
try {
out = new FileOutputStream("E:/Dong15129/Atm/ATMUser.properties",true);
p.store(out, "key-value");
return true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(out!=null){
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return false;
}
//获取Properties文件的方法
public Properties getProterties(){
//判断该卡号是否已存在 从properties文件中获取已有数据
Properties p = new Properties();
FileInputStream in = null;
try {
in = new FileInputStream("E:/Dong15129/Atm/ATMUser.properties");
p.load(in);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(in!=null){
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return p;
}
//生成唯一卡号
public String getOnlyCard(){
//先得到所有卡号
Properties p = getProterties();
String cardNum = "8888";
while(true){
String card = (int)(Math.random()*10000)+1+"";
cardNum += card;
if(!p.containsKey(cardNum)){
break;
}
}
return cardNum;
}
//登录
//输入账号,查询properties文件 看该账号是否存在
//存在 根据路径反序列化该对象
public User userLogin(Scanner sc){
//得到properties中所有的信息
ObjectInputStream in =null;
User u1 = null;
Properties p = getProterties();
System.out.println("请输入卡号");
boolean b= true;
try {
while(b){
String card = sc.next();
//判断
if(p.containsKey(card)){
//卡号正确 开始反序列化 并读取密码
String txtPath = p.getProperty(card);
//反序列化
in = new ObjectInputStream(new FileInputStream(txtPath));
User u = (User)in.readObject();
//验证密码
System.out.println("请输入密码");
for(int i=0;i<3;i++){
int pwd = sc.nextInt();
if(pwd==u.getPwd()){
//结束
u1=u;
b=false;
break;
}else{
System.out.println("密码错误");
if(i==2){
System.out.println("密码错误超过3次,请明天再来");
System.exit(0);
}
}
}
}else{
//卡号不存在 从新输入卡号
System.out.println("您输入的卡号不存在,请重新输入");
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(in!=null){
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return u1;
}
package com.nylg.bean;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Scanner;
//存款 和取款 类
public class Util2 {
//存入金额 金额只要合理 就序列化到指定文件去
public User saveMoney(Scanner sc,User u){
System.out.println(“请输入存款金额,只能是正整数”);
int inMoney = 0;
while(true){
inMoney = sc.nextInt();
if(inMoney<0){
System.out.println(“金额必须是正整数,请重新输入”);
}else{
break;
}
}
//改变金额
u.setMoney(u.getMoney()+inMoney);
//序列化到内存中去
boolean b = saveObj(u);
if(b){
return u;
}else{
System.out.println(“存款失败”);
return null;
}
}
//操作后序列化的动作
public boolean saveObj(User u){
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(new FileOutputStream("E:/fileupload/ATm/"+u.getCardNum()+".txt"));
out.writeObject(u);
return true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(out!=null){
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return false;
}
//取款
public User moveMoney(Scanner sc,User u){
System.out.println("请输入取款金额,取款金额必须是正整数");
int outMoney =0;
while(true){
outMoney = sc.nextInt();
if(outMoney<0){
System.out.println("金额必须是正整数,请重新输入");
}else{
if(outMoney>u.getMoney()){
System.out.println("卡上余额不足!请重新输入取出金额");
}else{
u.setMoney(u.getMoney()-outMoney);
break;
}
}
}
boolean b = saveObj(u);//序列化
if(b){
return u;
}else{
System.out.println("取款失败");
return null;
}
}
}
以上便是我所写的代码,中间遇到了bug ,进行过很多次封装与更改,水平不够,体力来凑,虽然代码看起来并没有那么漂亮,也算是对自己java基础学习交上一份小小的答卷。
就不一一测试了。