之前,写银行贷款系统之前,在优快云上发表过一个论坛,是想问问别人该怎样写,现在自己写好了,发表出来。(哪里有不懂的,或是大佬想指点我的尽管在底下评论)
言归正传接下来展示一下包是啥样的:
一定要记得建一个:①User.txt用来储存用户名和密码,以及用户信息
②Data.txt用来储存用户进行增删查改时的数据信息
下面这个User类是定义用户信息的成员变量,和构造方法的:
package Fuction;
public class User {
public String username;//用户名
public String password;//密码
public String sex;//性别
public int age;//年龄
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String phonenumber;//电话号码
public String id;//身份证
public String address;//家庭地址
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhonenumber() {
return phonenumber;
}
public void setPhonenumber(String newphonenumber) {
this.phonenumber = newphonenumber;
}
public String getId() {
return id;
}
public void setId(String newid) {
this.id = newid;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {//用集合给用户呈现出的数据信息
return "用户名:" + username + ", 密码" + password + ",性别:" + sex + ",年龄:" + age
+ ", 电话号码:" + phonenumber + ", 身份证号码:" + id + ",地址:" + address;
}
public String totring() {//写入文档中用的方法
return username + "," + password + "," + sex + "," + age
+ "," + phonenumber + "," + id + "," + address+"\n";
}
public User(String username, String password, String sex,//有参构造方法
int age, String phonenumber, String id, String address) {
this.username = username;
this.password = password;
this.sex = sex;
this.age = age;
this.phonenumber = phonenumber;
this.id = id;
this.address = address;
}
public User() {//无参构造方法
}
}
下面这个Bank类是定义一些银行系统中应该有的成员变量和构造方法的:
package Fuction;
import java.text.DecimalFormat;
public class Bank {
public String account ; // 用户账号
public double loanmoney;//贷款金额
public double loanrate;//贷款利率
public String loanday;//贷款的时间
public int loanduration;//贷款期限
public double repaymoney;//还款金额
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public double getLoanmoney() {
return loanmoney;
}
public void setLoanmoney(double loanmoney) {
this.loanmoney = loanmoney;
}
public int getLoanduration() {
return loanduration;
}
public void setLoanduration(int loanduration) {
this.loanduration = loanduration;
}
public String getLoanDay() {
return loanday;
}
public void setLoanDay(String loanDay) {
this.loanday = loanDay;
}
public double getRepaymoney() {
if( 0 < this.loanduration && this.loanduration< 12) {
this.loanrate=0.0435/12;
}else if( this.loanduration< 60) {
this.loanrate=0.0475/12;
}else if( this.loanduration> 60) {
this.loanrate=0.049/1;
}
double temp =this.loanmoney+this.loanmoney*this.loanrate;
DecimalFormat df = new DecimalFormat("0.00");
df.format(temp);
return temp;
}
public double getLoanrate() {
if( 0 < this.loanduration && this.loanduration< 12) {
this.loanrate=0.0435/12;
}else if( this.loanduration< 60) {
this.loanrate=0.0475/12;
}else if( this.loanduration> 60) {
this.loanrate=0.049/12;
}
return loanrate;
}
public Bank(String account, double loanmoney,String loanDay,
int loanduration,double loanrate ,double repaymoney) {//构造方法bank
this.account = account;
this.loanmoney = loanmoney;
this.loanday = loanDay;
this.loanduration = loanduration;
this.loanrate=loanrate;
this.repaymoney=repaymoney;
}
public Bank() {//空构造方法bank
}
public String toString() {//用集合给用户呈现出的数据信息
return "用户名:" + account + ", 贷款金额:" + loanmoney + ", 贷款利率:" + loanrate + ",贷款时间:"
+ loanday + ", 贷款期限(按月计算):" + loanduration + ",还款金额:" + repaymoney + "";
}
public String toString1() {//写入文档中用的方法//用集合给用户呈现出的数据信息
return account + "," + loanmoney + "," + loanday + "," + loanduration
+ ","+loanrate + "," + repaymoney+"\n" ;
}
}
下面这个Enterio类是一个有关于输入输出流的(简单来说,第一个是判断输入的用户名和密码,是否文档中储存的用户名和密码是否一致,第二个是注册的时候用来存储用户信息用的):
package Fuction;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Enterio {// 判断输入的密码和用户名与文档中储存的是否相等
public boolean reader(String username, String password) throws IOException {
File file = new File("User.txt");
boolean flag = false;
BufferedReader br = new BufferedReader(new FileReader(file));
String line = null;
while ((line = br.readLine()) != null) {
String[] datas = line.split(",");// 用逗号隔开
if (datas[0].equals(username) && datas[1].equals(password)) {// 判断
flag = true;
break;
}
}
br.close();
return flag;
}
public void writer(User user) throws IOException {// 将贷款信息储存进user里
File file = new File("User.txt");
BufferedWriter bw = null;
bw = new BufferedWriter(new FileWriter(file, true));
bw.write(user.getUsername() + "," + user.getPassword() + "," + user.getSex() + ","
+ user.getAge() + ","+ user.getPhonenumber() + "," + user.getId() + "," + user.getAddress());
bw.newLine();//// 写入一个行分隔符
bw.flush();// 刷新缓冲区
bw.close();
}
}
接下来这个Login类是用户登录和注册用的:
package Fuction;
import java.io.IOException;
import java.util.Scanner;
public class Login {
Scanner sc = new Scanner(System.in);
public void welcome() {//开始界面
System.out.println("------------欢迎光临--------------");
System.out.println();
System.out.println("***************");
System.out.println("1.登录贷款系统");
System.out.println("***************");
System.out.println("2.注册贷款账户");
System.out.println("***************");
System.out.println("3.退出系统");
System.out.println("***************");
System.out.println("请输入你的选择:");
}
public String login() throws IOException {//
Enterio enterio = new Enterio();
System.out.println("---------欢迎进入登录界面-----------");
boolean en=false;
while(!en) {
System.out.println("请输入用户名:");
String username = sc.nextLine();
System.out.println("请输入密码:");
String password = sc.nextLine();
boolean flag = enterio.reader(username, password);
if (flag) {
System.out.println("登录成功!");
en=true;
return username;
}else {
System.out.println("用户名或者密码有误,登录失败,请重新输入");
}
}
return "";
}
public void register() throws Exception{
Enterio enterio = new Enterio();
System.out.println("--------------贷款系统注册界面--------------");
System.out.println("请设置用户名:");
String newUsername = sc.nextLine();
System.out.println("请设置密码:");
String newPassword = sc.nextLine();
System.out.println("确认密码:");
String newPassword1 = sc.nextLine();
boolean en1=false;
while(!en1) {
if(newPassword.equals(newPassword1)) {
en1=true;
}else {
System.out.println("两次密码不一致,请再次设置密码:");
newPassword = sc.nextLine();
System.out.println("确认密码:");
newPassword1 = sc.nextLine();
}
}
System.out.println("请填写性别:");
String sex = sc.nextLine();
System.out.println("请填写年龄:");
int age = sc.nextInt();
sc.nextLine();
System.out.println("请填写手机号:");
String newphonenumber = sc.nextLine();
System.out.println("请填写身份证号:");
String newid = sc.nextLine();
System.out.println("请填写地址:");
String newaddress = sc.nextLine();
User user = new User();
user.setUsername(newUsername);
user.setPassword(newPassword);
user.setSex(sex);
user.setAge(age);
user.setPhonenumber(newphonenumber);
user.setId(newid);
user.setAddress(newaddress);
enterio.writer(user);
System.out.println("注册成功!");
System.out.println("按1键进行服务,按任意键退出!!");
}
}
下面这个Service 类是用户进行服务时的一个类(简单说就是增删查改的功能):
package Fuction;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
public class Service {
Scanner sc = new Scanner(System.in);
// 贷款界面
public void servicewelcome(String account) throws IOException {
ArrayList<Bank> bank = new ArrayList<Bank>();// 一个以bank为范型的集合
List<Bank> list = new ArrayList<Bank>();// 储存需要处理的信息的集合
System.out.println("-----欢迎来到贷款系统------");
System.out.println();
System.out.println("***************");
System.out.println("4.申请贷款");
System.out.println("***************");
System.out.println("5.申请还款");
System.out.println("***************");
System.out.println("6.查看我的贷款");
System.out.println("***************");
System.out.println("7.修改我的账户信息");
System.out.println("***************");
System.out.println("请输入你的选择:");
char choice = sc.nextLine().charAt(0);// 选择需要的服务,是字符串类型的变量
boolean en6 = false;
while (!en6) {
if (choice == '4' || choice == '5' || choice == '6' || choice == '7') {
// 输入4567,进行操作
en6 = true;
switch (choice) {
case '4':
loan(account);// 进行贷款功能
System.out.println("返回上一层,请按8,退出系统,请按9,");
char chie = sc.nextLine().charAt(0);// 输入选择
chi(chie, choice, account);// 选择是否返回上一层
en6 = true;// 跳出while循环
break;
case '5':
Repayment(account, list, bank);// 进行还款
System.out.println("返回上一层,请按8,退出系统,请按9,");
chie = sc.nextLine().charAt(0);// 输入选择
chi(chie, choice, account);// 选择是否返回上一层
en6 = true;// 跳出while循环
break;
case '6':
check(account, list);// 进行查看功能
System.out.println("返回上一层,请按8,退出系统,请按9,");
chie = sc.nextLine().charAt(0);// 输入选择
chi(chie, choice, account);// 选择是否返回上一层
en6 = true;// 跳出while循环
break;
case '7':
change(account);// 修改用户信息
System.out.println("返回上一层,请按8,退出系统,请按9,");
chie = sc.nextLine().charAt(0);// 输入选择
chi(chie, choice, account);// 选择是否返回上一层
en6 = true;// 跳出while循环
break;
}
} else {
System.out.println("输入错误,请重新输入:");
choice = sc.nextLine().charAt(0);
;// 重新输入进行循环
}
}
}
public void writer(Bank loaner) throws IOException {// 将贷款记录存起来
File file = new File("Data.txt");
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(file, true));// true是将录入很多行行信息存入文档
bw.write(loaner.account + "," + loaner.getLoanmoney() + "," + loaner.getLoanDay() + ","
+ loaner.getLoanduration() + "," + loaner.getLoanrate() + "," + loaner.getRepaymoney());// 将数据写入到BufferedWriter中
bw.newLine();// 写入一个行分隔符
bw.flush();// 刷新缓冲区
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void loan(String account) throws IOException {// 增加贷款
boolean en3 = false;
while (!en3) {// 贷款成功,en3=true;贷款成功
System.out.println("****欢迎贷款****");
System.out.println("请输入您要贷款的金额:");
double newloanmoney = sc.nextDouble();
boolean en = false;
while (!en) {// 贷款>0,跳出循环
if (newloanmoney > 0) {// 贷款大于0
en = true;
break;
} else {
newloanmoney = sc.nextDouble();
}
}
Date d = new Date();
SimpleDateFormat ss = new SimpleDateFormat("yyyy-MM-dd ");
String loanDay = ss.format(d);// 输出当前时间信息
System.out.println("当前的日期为:" + loanDay);
System.out.println("请输入贷款的期限(单位:月):");
int newloanduration = sc.nextInt();
sc.nextLine();
Bank loaner = new Bank();// 创建贷款还款记录的对象
loaner.setLoanDay(loanDay);
loaner.setLoanmoney(newloanmoney);
loaner.setLoanduration(newloanduration);
loaner.setAccount(account);
double repaymoney = loaner.getRepaymoney();// 将信息存入
System.out.println("需要还款:" + repaymoney);
System.out.println("贷款金额为:" + newloanmoney + ",确认贷款请按1,返回上一层请按任意键");
// 输入1,贷款成功,
char i = sc.nextLine().charAt(0);
if (i == '1') {
System.out.println("贷款成功");
writer(loaner);
en3 = true;// 贷款成功,en3=true;贷款成功
break;
} else {
servicewelcome(account);
}
}
}
public void reader(String account, List<Bank> list) throws IOException {// 读信息的,
File file = new File("Data.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
String[] datas = line.split(",");
if (datas[0].equals(account)) {
double loanmoney = Double.parseDouble(datas[1]);
String loanDay = datas[2];
int loanduration = Integer.parseInt(datas[3]);
double loanrate = Double.parseDouble(datas[4]);
double repaymoney = Double.parseDouble(datas[5]);
Bank bank = new Bank(account, loanmoney, loanDay, loanduration, loanrate, repaymoney);
list.add(bank);
System.out.println(bank.toString());
}
}
br.close();
}
public void Repayment(String account, List<Bank> list, ArrayList<Bank> bank) throws IOException {
// 还款,并删除信息
System.out.println("****欢迎还款****");
reader(account, list);
System.out.println("确定还款请按1,放回上一层请任意按任意键");
char r = sc.nextLine().charAt(0);
if (r == '1') {
File file = new File("Data.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
String[] datas = line.split(",");
double loanmoney = Double.parseDouble(datas[1]);// 将贷款金额存为bank集合中banks对象的第二个
String loanDay = datas[2];
int loanduration = Integer.parseInt(datas[3]);
double loanrate = Double.parseDouble(datas[4]);
double repaymoney = Double.parseDouble(datas[5]);
Bank banks = new Bank(datas[0], loanmoney, loanDay, loanduration, loanrate, repaymoney);
bank.add(banks);// 用户名 贷款金额 贷款时间 贷款期限 贷款利率 还款金额
for (int i = 0; i < bank.size(); i++) {
Bank temp = new Bank();// new一个新对象
temp = bank.get(i);// 得到
if (temp.getAccount().equals(account)) {
bank.remove(i);
}
}
}
br.close();
BufferedWriter bw = new BufferedWriter(new FileWriter("data.txt"));
for (Bank b : bank) {// 遍历集合
bw.write(b.toString1());// 写数据
bw.newLine();
bw.flush();// 释放资源
}
bw.close();
System.out.println("还款成功");
} else {
servicewelcome(account);
}
}
public void check(String account, List<Bank> list) throws IOException {// 查看用户贷款信息,调用reader方法
reader(account, list);
System.out.println("用户暂无其他贷款项");
}
public void change(String account) throws IOException {// 修改用户信息
System.out.println("****欢迎修改信息****");
File file = new File("User.txt");
List<User> userlist = new ArrayList<User>();
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
String[] data = line.split(",");
String username = data[0];
String password = data[1];
String sex = data[2];
int age = Integer.parseInt(data[3]);
String phonenumber = data[4];
String id = data[5];
String address = data[6];
User use = new User(username, password, sex, age, phonenumber, id, address);
userlist.add(use);// 将所有用户信息存入集合
}
br.close();
System.out.println("修改密码请按1,不需要修改,请任意按任意键");
char i = sc.nextLine().charAt(0);
String newPassword = "";
if (i == '1') {// 修改密码,请按1
sc.nextLine();
System.out.println("请设置密码:");
newPassword = sc.nextLine();
System.out.println("确认密码:");
String newPassword1 = sc.nextLine();
boolean en1 = false;
while (!en1) {
if (newPassword.equals(newPassword1)) {// 判断密码与输入的是否一样
en1 = true;
} else {
System.out.println("两次密码不一致,请再次设置密码:");
newPassword = sc.nextLine();
System.out.println("确认密码:");
newPassword1 = sc.nextLine();
}
}
}
System.out.println("请修改年龄:");
String age = sc.nextLine();
System.out.println("请修改手机号:");
String newphonenumber = sc.nextLine();
System.out.println("请修改地址:");
String newaddress = sc.nextLine();
System.out.println("确认修改,请按1,放回上一层请按任意键");
char k = sc.nextLine().charAt(0);
System.out.println("请修改地址:");
if (k == '1') {
for (int j = 0; j < userlist.size(); j++) {
User temp = new User();// 未修改前的对象
temp = userlist.get(j);// 得到对象
if (temp.getUsername().equals(account)) {
if (!newPassword.equals(""))// 输入回车
temp.setPassword(newPassword);
if (!age.equals(""))
temp.setAge(Integer.parseInt(age));
if (!newphonenumber.equals(""))
temp.setPhonenumber(newphonenumber);
if (!newaddress.equals(""))
temp.setAddress(newaddress);
userlist.set(j, temp);
}
}
File file1 = new File("User.txt");
BufferedWriter bw = null;
bw = new BufferedWriter(new FileWriter(file1));
for (User u : userlist) {// 便利集合,存入文档
bw.write(u.totring());
bw.newLine();
bw.flush();
}
if (bw != null) {
bw.close();
}
System.out.println("修改成功!");
} else {
servicewelcome(account);// 点击任意键放回服务界面
}
}
public void chi(char chie, char choice, String account) throws IOException {
// 主要适用于用户操作完之后,例如贷款完之后,想进行其他操作,可以按8.退出系统按9
if (chie == '8') {
servicewelcome(account);
chie = sc.nextLine().charAt(0);
} else if (chie == '9') {
System.out.println("欢迎再次光临");
} else {
System.out.println("输入错误,请重新输入");
chie = sc.nextLine().charAt(0);
}
}
}
这个类是主类,Main类(实现类):
package Main;
import java.util.Scanner;
import Fuction.Login;
import Fuction.Service;
public class Main {
static String account;//静态账户
public static void main(String[] args) throws Exception {
Login loginer = new Login();//new一个新的登陆对象
Service servicer=new Service();//new一个新的进行服务的对象
loginer.welcome();//开始的界面
Scanner sc = new Scanner(System.in);
char choie = sc.nextLine().charAt(0);////选择需要的服务,登陆,注册,或是退出。它是字符串类型的变量
boolean en5 = false;
while (!en5) {
if (choie == '1' || choie == '2' || choie == '3') {//输入123,进行操作
en5 = true;
switch (choie) {
case '1':
account = loginer.login();//登陆
servicer.servicewelcome(account);
break;
case '2':
loginer.register();//注册
char login= sc.nextLine().charAt(0);
if(login=='1') {//注册完输入1,
servicer.servicewelcome(account);//进行服务,贷款或是或还款。。。。。。
}else {
System.out.println("欢迎再次光临");//
}
break;
case '3':
System.out.println("欢迎再次光临");
sc.close();
break;
}
} else {//输入的不是123,重新操作
System.out.println("输入错误,请重新输入:");
choie = sc.nextLine().charAt(0);
}
}
}
}
这样一个简单的银行贷款系统就做完了,(该系统运用了io流和抛异常的简单知识,)
这是我学习Java一段时间后写的第一个系统,虽然bug特别多,但是我做出来已经都很高兴了。。