一共只写了三个类分别是Personal,CRS,BankSystem,个人文档数据存在项目下文件里,和src同级。每个人创建一个txt,有一个用户集.txt,里面存储着所有人的ID。
个人信息只有三个:ID,密码和余额。
只有CRS里有主函数,运行CRS类即可。三类要在同一包里。查找个人信息文档请在项目文件第一级下查找。个人文档用个人ID命名,格式是txt。
------------------------------------------以下是Personal类,可以直接复制使用------------------------------------
/*
* Personal类主要负责在后台存取个人数据并且写入个人文档里,个人文档用txt格式,文件名为ID名,
* 文档就存入项目文件下。
* 个人数据的读写通过删除个人文档所有数据,再重新写入,简单粗暴,想要更改某个人文档数据可以简单查找文件,修改内部数据。
* 个人银行账户信息没有名字,没有身份证,没有年龄,没有性别,只有账户名(ID)和密码(password)以及余额(accountBalance)。
* 添加其他个人信息只是增加代码重复量,所以没有增加,只写了必要的。
* 创建个人信息时会在账户集.txt里写入相应的ID。这个账户集通过收集所有账户的ID,来达到方便读入ID查找密码,余额信息。
* 2021-6-10-9:10
*/
package com.itheima.bank;
import java.io.*;
public class Personal {
//当用户登录时需判断银行卡号和银行卡密码,当输入的卡号和密码都正确时,登录成功,提示当前登录的账户名,并进入下一步选择操作类型。
String ID;
String password;
double accountBalance = 0.0;
public void put(String ID,String password) {
this.ID = ID;
this.password = password;
}
public void add(double nums) throws IOException {
this.accountBalance += nums;
File file = new File(this.ID+".txt");
file.delete();
FileWriter f1 = new FileWriter(ID+".txt");
f1.write("账户名"+this.ID+"||");
f1.write("密码"+this.password+"||");
f1.write("余额"+this.accountBalance+"||");
f1.close();
}
public double get() {
return this.accountBalance;
}
public void decline(double nums) throws IOException {
this.accountBalance -= nums;
File file = new File(this.ID+".txt");
file.delete();
FileWriter f1 = new FileWriter(ID+".txt");
f1.write("账户名"+this.ID+"||");
f1.write("密码"+this.password+"||");
f1.write("余额"+this.accountBalance+"||");
f1.close();
}
public String getID() {
return ID;
}
public String getPW() {
return password;
}
public void ResetPW(String password) throws IOException {
this.password = password;
File file = new File(this.ID+".txt");
file.delete();
FileWriter f1 = new FileWriter(ID+".txt");
f1.write("账户名"+this.ID+"||");
f1.write("密码"+this.password+"||");
f1.write("余额"+this.accountBalance+"||");
f1.close();
}
public Personal(String ID,String password) throws IOException {
//创建文档,写入ID和password数据
FileWriter f1 = new FileWriter(ID+".txt");
FileWriter f2 = new FileWriter("账户集.txt",true);
f1.write("账户名"+ID+"||");
f1.write("密码"+password+"||");
f1.write("余额0.0||");
f1.close();
f2.write("ID"+ID+"|");
f2.close();
}
public Personal() {
//空。
}
}
------------------------------------------以上是Personal类,可以直接复制使用------------------------------------
---------------------------------------------以下是CRS类,可以直接复制使用---------------------------------------
/*
* CRS类主要就是提供自动存款机界面的基本功能,只有CRS有主函数main。
* CRS里面写了很多静态方法,这些静态方法在其方法体内又调用了BankSystem接上的接口:inFunctional接口的方法。
* 通过这个流程,逐步实现自动存款机的基本功能,表面看起来多此一举,不如直接调用BankSystem的方法。
* 实际上并非如此,通过接口,写代码的效率更高,并且思路清晰,起到了很大的辅助作用,并且有助于理解代码。
* 通过调用相应的方法可以进行存款,取款,查询(余额),重设密码,新建账户,通过传参ID返回密码(FindPW()等),通过传参ID返回对应对象(FindP()等)。
* 实际的进行操作的代码在BankSystem类里,因为CRS只是提供一个界面方便用户进行使用。
* 2021-6-10-9:44
*/
package com.itheima.bank;
import java.io.*;
import java.util.*;
interface inFunctional{
String Save(double nums,Personal p) throws IOException;
String Draw(double nums,Personal p) throws IOException;
String Check(Personal p);
String Reset(Personal p,String password) throws IOException;
String Set(String key,String password,Personal p);
String findPW(String ID);
Personal findP(String ID);
void getData() throws IOException;
}
public class CRS {
public static inFunctional f = new BankSystem() ;
public static void main(String[] args) throws Exception{
/*这是用的中间变量*/
Scanner s = new Scanner(System.in);
String str1 = new String();
String str2 = new String();
int i = 0;
/* */
System.out.println("loading data...");
Getting();
System.out.println("over!");
System.out.println("--------------------------");
System.out.println("--------------------------");
System.out.println("--------------------------");
System.out.println("--------------------------");
System.out.println("--------欢迎使用CRS-------"); /*开始界面*/
System.out.println("登录: ----------- A");
System.out.println("注册: ----------- B");
System.out.println("关闭: ----------- !");
System.out.println("--------------------------");
System.out.println("-----------------=======---");
System.out.println("-----------------| |--");
System.out.println("-----------------=======---");
System.out.println("--------------------------");
System.out.println("请输入:");
for(;i<100;i++) {
if (i==0) str1 = s.nextLine();
if (str1.equals("!")) {
System.out.println("谢谢您的使用。");
System.exit(0);
}
if (str1.equals("A")) {
//登录
Login();
s.close();
break;
}
if (str1.equals("B")) {
//注册
Register(s);
Scanner ss = new Scanner(System.in);
System.out.println("是否选择直接登录?请输入Y或N:");
str2 = ss.nextLine();
if (str2.equals("Y")) {
Login();
}
if (str2.equals("N")) {
System.out.println("选择关闭程序或继续注册?请输入C或B:");
str1 = ss.nextLine();
if (str1.equals("C")) {
System.out.println("谢谢您的使用。");
System.exit(0);
}
if (str1.equals("B")) {
continue;
}
}
ss.close();
s.close();
break;
}
else {
System.out.println("您输入有误,请重新输入:");
continue;
}
}
}
//静态方法调用传参方法
public static void Getting() throws IOException {
forGetData();
}
public static void Save(double nums,inFunctional f,Personal p) throws IOException {
Saving(nums,f,p);
}
public static void Set(String ID,String password,inFunctional f) throws IOException {
Personal p1 = new Personal(ID,password);
forSet(ID,password,f,p1);
}
public static void Draw(double nums,inFunctional f,Personal p) throws IOException {
Drawing(nums,f,p);
}
public static void Check(Personal p,inFunctional f) {
Checking(p,f);
}
public static void Reset(Personal p,String password,inFunctional f) throws IOException {
forReset(p,password,f);
}
public static String FindPW(String ID,inFunctional f) {
return forFindPW(ID,f);
}
public static Personal FindP(String ID,inFunctional f) {
return forFindP(ID,f);
}
public static void Login() throws IOException {
Scanner s = new Scanner(System.in);
String str1 = new String();
String str2 = new String();
String str3 = new String();
System.out.println("请输入您的ID:");
str1 = s.nextLine();
str2 = FindPW(str1,f);
if (str2.equals("1")) {
System.out.println("您输入的ID未注册。");
String[] args = {"1"};
try {
main(args);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else {
System.out.println("请输入您的密码:");
str3 = s.nextLine();
int i = 0;
for(;i<10000;i++) {
i = 0;
if (str3.equals(str2)) {
String str = new String();
Double nums = 0.0;
System.out.println("---------------CRS自动存取款机---------------");
System.out.println("-------------------------------------------");
System.out.println("------------------登录成功-----------------");//存款:1 取款:2 余额:3 修改个人密码:4 退出:0
System.out.println("-------------------------------------------");
System.out.println("欢迎您的使用!尊敬的"+str1+"用户。");
System.out.println("存款: ------------------------- 1");
System.out.println("取款: ------------------------- 2");
System.out.println("查询余额:------------------------- 3");
System.out.println("修改密码:------------------------- 4");
System.out.println("退出: ------------------------- 0");
System.out.println("-------------------------------------------");
System.out.println("---------------请注意交易安全---------------");
System.out.println("-------------------------------------------");
System.out.println("------最近诈骗行为屡屡发生,请谨慎存取款------");
System.out.println("-------------------------------------------");
System.out.println("-------------------------------------------");
System.out.println("请输入您需要的服务后面的数字:");
str = s.nextLine();
if (str.equals("1")) {
System.out.println("请输入您想存入的款数(小数形式),并将现金和信用卡放进机口内:");
nums = s.nextDouble();
Save(nums,f,FindP(str1,f));
str = null;
continue;
}
if (str.equals("2")) {
System.out.println("请输入您想取出的款数(小数形式),并将信用卡放进口内:");
nums = s.nextDouble();
Draw(nums,f,FindP(str1,f));
str = null;
continue;
}
if (str.equals("3")) {
Check(FindP(str1,f),f);
str = null;
continue;
}
if (str.equals("4")) {
System.out.println("您想重设的密码为:");
str = s.nextLine();
Reset(FindP(str1,f),str,f);
str = null;
continue;
}
if (str.equals("0")) {
str = null;
System.out.println("谢谢您的使用。");
s.close();
System.exit(0);
}
}
}
}
}
public static void Register(Scanner s) throws IOException {
String str1 = new String();
String str2 = new String();
String str3 = new String();
System.out.println("请设置您的账户名(任意形式,除去特殊符号):");
str1 = s.nextLine();
System.out.println("请设置您的密码(任意形式,除去特殊符号):");
str2 = s.nextLine();
System.out.println("请重新确认您的密码:");
str3 = s.nextLine();
if (str3.equals(str2)) {
Set(str1,str3,f);
}
//s.close(); 不能关闭,一旦关闭,所有的Scanner都会没用。
}
//向接口传参
public static void forGetData() throws IOException {
f.getData();
}
public static void Saving(double nums,inFunctional f,Personal p) throws IOException {//存款
System.out.println(f.Save(nums,p));
}
public static void Drawing(double nums,inFunctional f,Personal p) throws IOException {//取款
System.out.println(f.Draw(nums,p));
}
public static void Checking(Personal p,inFunctional f) {
System.out.println(f.Check(p));
}
public static void forReset(Personal p,String password,inFunctional f) throws IOException {
System.out.println(f.Reset(p,password));
}
public static void forSet(String ID,String password,inFunctional f,Personal p) {
//--->调用BankSystem,在hashmap里存储一个新对象
System.out.println(f.Set(ID,password,p));
}
public static String forFindPW(String ID,inFunctional f) {
return f.findPW(ID);
}
public static Personal forFindP(String ID,inFunctional f) {
return f.findP(ID);
}
}
---------------------------------------------以上是CRS类,可以直接复制使用---------------------------------------
---------------------------------------以下是BankSystem类,可以直接复制使用----------------------------------
/*
* BankSystem是连接CRS和Personal的一个中介,但是实际操作代码基本上都在BankSystem里。
* 最难阅读的模块(方法)是读入数据的模块,即getData()。
* 读入模块的原理就是简单粗暴的遍历,通过很多循环来获取个人信息,程序启动时就先获取这些信息,并且依次创建对象,下面介绍具体实现原理。
* 首先程序启动时首先读入数据。因为个人文档的命名都是由ID+.txt的格式,所以很容易查找个人信息,问题是,查找个人文档需要ID,怎么获取ID?
* 于是我就加入了“账户集”的概念,里面存储了所有账户的ID信息,里面可能有重复的ID,但是实际上个人文档没有重复的,对于使用毫不影响。
* 通过对账户集的遍历,来获取所有人的ID。账户集记录数据统一用“ID”+个人ID+“|”的格式来写。
* 遍历到“D”时,一直到下一个“|”,中间是具体的ID,通过字符数组来获取,再将字符数组转换为字符串。于是得到第一个ID。
* 得到第一个ID后,遍历停止,通过字符流的缓冲区来根据ID去查找个人信息文档。再通过同样的原理,查找到相应的密码和余额信息。
* 因为个人信息文档的命名方式都是个人ID+.txt的格式,所以可以通过以下代码就读到个人信息文档:
* FileReader f = new FileReader(ID+".txt");
* 得到第一个ID所对应的密码和余额信息后,也就是得到了第一个ID所有个人信息,创建对应对象,并且放入BankSystem的HashMap里。
* 然后继续进行遍历,获取下一个ID。以此类推。
* 这样,就完成了数据的读入。
* 共大约一百行代码,来实现数据的读入。
* 2021-6-10-9:58
*/
package com.itheima.bank;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class BankSystem implements inFunctional{
HashMap<String,Personal> M = new HashMap<>();
//HashMap桶,key是个人ID,value是ID对应的Personal对象。
@Override
public String Save(double nums,Personal p) throws IOException {
// TODO Auto-generated method stub
p.add(nums);
return "存款成功!数量为"+nums+"元,余额为"+p.get()+"元,感谢您的使用。";
}
//存款
@Override
public String Draw(double nums,Personal p) throws IOException {
// TODO Auto-generated method stub
p.decline(nums);
return "取款成功!数量为"+nums+"元,余额为"+p.get()+"元,感谢您的使用";
}
//取款
@Override
public String Check(Personal p) {
// TODO Auto-generated method stub
return "账户ID:"+p.getID()+"。您的余额为"+p.get()+"元。";
}
//查询余额
@Override
public String Reset(Personal p,String password) throws IOException {
// TODO Auto-generated method stub
p.ResetPW(password);
return "重新设置成功!您的密码为:"+password;
}
//重设密码
@Override
public String Set(String key,String password,Personal p) {
// TODO Auto-generated method stub
this.M.put(key,p);//哈希map里面存储key为ID,value为personal对象
p.put(key,password);//建立新账户时,传参给personal对应personal的ID和password。
return "银行新账户建立成功!感谢您的使用。";
}
//把ID和密码写入对应的Personal对象里并放入HashMap桶里
public String findPW(String ID) {
Set<String> key = M.keySet();
Iterator<String> i = key.iterator();
while(i.hasNext()) {
Object k = i.next();
if (ID.equals((String)k)) {
Object v = M.get(k);
Personal p = (Personal)v;
return p.getPW();
}
}
return "1";
}
//实现根据ID查找密码的功能,这个方法主要用于其他方法调用使用。
public Personal findP(String ID) {
Set<String> key = M.keySet();
Iterator<String> i = key.iterator();
while(i.hasNext()) {
Object k = i.next();
if (ID.equals((String)k)) {
return (Personal)M.get(k);
}
}
return null;
}
//根据ID查找Personal对象,这个方法也用于其他方法调用使用。
@Override
public void getData() throws IOException {
FileReader Fr = new FileReader("账户集.txt");
int leng = 0;
char chg = '-';
int ig = 0;
while ((leng = Fr.read()) != -1) {
chg = (char)leng;
ig++;
if(ig == 1) break;
}
if (chg != '-') {
FileReader r = new FileReader("账户集.txt");
int len = 0;
int i = 0,j = 0,k = 0,ii = 0;
int t = 0,p = 0;
int temp = 0;
while ((len = r.read()) != -1) {
k++;
}
char s[] = new char[k];
len = 0;
FileReader rr = new FileReader("账户集.txt");
while ((len = rr.read()) != -1) {
s[ii] = (char)len;
ii++;
};
for(i = 0;i<k;i++) {
if (s[i] == 'D') {
t = i+1;
p = i;//i下标停止地方是D
for(j = 0;j<100;j++) {
i++;//得到j为id数量,i为|下标。
if (s[i] == '|') break;
}
char str[] = new char[j];//创建对应数量的字符数组
for(p = 0;p<j;p++) {//获取ID
str[p] = s[t];
t++;
}
//此时t指向|
i = t;
temp = t;
String ss = new String();
ss = String.valueOf(str);//ID
//获取ID成功
//获取了ID,还没有获取密码
FileReader ff = new FileReader(ss+".txt");//找到对应id的文档,开始获取密码。
int l = 0;
int i1 = 0,i2 = 0,i3 = 0,i4 = 0,i5 = 0;
while ((l = ff.read()) != -1) {
i1++;
}
char s1[] = new char[i1];//创建对应数量的字符数组
l = 0;
FileReader ffff = new FileReader(ss+".txt");
while ((l = ffff.read()) != -1) {
s1[i2] = (char)l;//获取所有信息
i2++;
}
//开始查找获取密码
for(l = 0;l<i1;l++) {
if (s1[l] == '码') {
i4 = l+1;//密码第一个字符的下标是i4;
//统计密码字符数量
for(i3 = 0;i3<100;i3++) {
l++;
if (s1[l] == '|') break;//获取了密码字符数量,i3是密码字符数量
}
break;
}
}
char s2[] = new char[i3];
for(i5 = 0;i5<i3;i5++) {
s2[i5] = s1[i4];
i4++;
}
//----测试用的 ss是ID s2是密码
//----测试
String s3 = new String();
s3 = String.valueOf(s2);//密码是s3
//到此为止,读取完毕了账户和密码,接下来读取余额。
FileReader fff = new FileReader(ss+".txt");
len = 0;
l = 0;
i = 0;
ii = 0;
i1 = 0;
i2 = 0;
while ((len = fff.read()) != -1) {
i++;//i获取了所有字数量。
if ((char)len == '额') l = i-1;//l下标为"额"
}
fff.close();
char c1[] = new char[i];//创建相应字符数组。
len = 0;
FileReader fffff = new FileReader(ss+".txt");
while((len = fffff.read()) != -1) {
c1[ii] = (char)len;//获取了所有信息
ii++;
}
fffff.close();
//---测试成功,c1是所有单个账户所有信息,l下标是"额"
//---测试
for(i1 = l+1;i1<100;i1++) {//从余额第一个字符开始遍历
i2++;
if(c1[i1] == '|') {
i2--;//i2是余额字符数量
break;
}
}
char c3[] = new char[i2];
len = 0;
l++;//l下标为余额第一个字符
for(;len<i2;len++) {
c3[len] = c1[l];
l++;
}
//获取余额完毕
String s4 = new String();
s4 = String.valueOf(c3);
double nums = Double.valueOf(s4);
//放到bankSystem里的hashmap里。
Personal p0 = new Personal();
Set(ss,s3,p0);//新建hashmap
Save(nums, p0);//存入余额
r.close();
rr.close();
i = temp;
continue;
}
}
}
Fr.close();
}
//读入数据模块
}
---------------------------------------以上是BankSystem类,可以直接复制使用----------------------------------
有意以各种方式使用本文者,请加本人QQ2435693791。学生党随意。