JAVA-银行账户系统学习笔记
本帖最后由 songshao 于 2022-10-11 10:55 编辑最近上编程课学习都挺晚的,没时间逛论坛了,今天休息正好也赶上线上,所以来发点学习笔记,一起讨论学习.(看我这个标点符号就知道我学编程的了吧)
先看这个银行系统
一:代码实现步骤
1:新建账号类:
package domin;
public class Account {
private String name;
private String id;
private String password;
private double money;
private double quotaMoney;
public Account() {
}
public Account(String name, String id, String password, double money, double quotaMoney) {
this.name = name;
this.id = id;
this.password = password;
this.money = money;
this.quotaMoney = quotaMoney;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public double getQuotaMoney() {
return quotaMoney;
}
public void setQuotaMoney(double quotaMoney) {
this.quotaMoney = quotaMoney;
}
}
2:建立一个main入口
package system;
import domin.Account;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class Enter {
//主方法界面
public static void main(String[] args) {
ArrayList<Account> list = new ArrayList<>();
Account zhangsan = new Account("张三", "12345678", "123456", 90000.0, 90000.0);
list.add(zhangsan);
Account lisi = new Account("李四", "12345", "123456", 90000.0, 90000.0);
list.add(lisi);
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("欢迎来到吾爱破解银行");
System.out.println("1. 登录账号");
System.out.println("2. 注册账号");
int type = sc.nextInt();
switch (type) {
case 1:
System.out.println("欢迎来到登录界面");
login(list, sc);
break;
case 2:
System.out.println("欢迎来到注册界面");
register(list, sc);
break;
default:
System.out.println("您的输入有误,请重新输入");
break;
}
}
}
//账号注册
private static void register(ArrayList<Account> list, Scanner sc) {
System.out.println("请输入您的姓名");
String name = sc.next();
String password;
while (true) {
System.out.println("请输入您的密码:");
password = sc.next();
System.out.println("请输入再次确认您的密码:");
String password1 = sc.next();
if (password.equals(password1)) {
break;
}
System.out.println("两次密码输入不一致,请重新输入");
}
System.out.println("请输入当次提现额度:");
double quotaMoney = sc.nextDouble();
String cardID = getcardID(list);
Account temp = new Account(name, cardID, password, 0.0, quotaMoney);
list.add(temp);
System.out.println("开户成功" + temp.getName() + "\t" + temp.getId() + "\t" + temp.getMoney() + "\t" + temp.getQuotaMoney());
}
//卡号生成,返回卡号
public static String getcardID(ArrayList<Account> list) {
Random random = new Random();
String cardID = "";
while (true) {
for (int i = 0; i < 8; i++) {
cardID += random.nextInt(10);
}
Account account = isExtened(list, cardID);
if (account == null) {
break;
} else {
System.out.println("卡号重复");
}
}
return cardID;
}
//账号查重,返回对象账户
public static Account isExtened(ArrayList<Account> list, String id) {
for (int i = 0; i < list.size(); i++) {
Account account = list.get(i);
if ((account.getId()).equals(id)) {
return account;
}
}
return null;
}
//登录界面
private static void login(ArrayList<Account> list, Scanner sc) {
System.out.println("============欢迎您进入登录界面===============");
Account account;
while (true) {
System.out.println("请输入您的账户:");
String tempid = sc.next();
account = isExtened(list, tempid);
if (account == null) {
System.out.println("您的账号输入错误!重新输入");
} else {
break;
}
}
for (int i = 0; i < 3; i++) {
System.out.println("请输入您的密码:");
String tempwd = sc.next();
if ((account.getPassword()).equals(tempwd)) {
System.out.println("登陆成功");
break;
} else {
if (2 - i == 0) {
System.out.println("您的账户已被锁定,请联系管理员解锁");
return;
}
System.out.println("密码输入错误,您还有" + (2 - i) + "次机会");
}
}
useroperation(account, list, sc);
return;
}
//用户操作界面
private static void useroperation(Account account, ArrayList<Account> list, Scanner sc) {
while (true) {
System.out.println("==============欢迎来到用户操作界面==============");
System.out.println("1. 查询");
System.out.println("2. 存款");
System.out.println("3. 取款");
System.out.println("4. 转账");
System.out.println("5. 修改密码");
System.out.println("6. 退出账户");
System.out.println("7. 注销账户");
int type = sc.nextInt();
switch (type) {
case 1:
checkAccount(account);
System.out.println("查询成功");
break;
case 2:
saveMoney(account, sc);
break;
case 3:
withDrawal(account, sc);
break;
case 4:
transMoney(list, account, sc);
break;
case 5:
reSet(account, sc);
return;
case 6:
System.out.println("退出账户成功");
return;
case 7:
boolean flag = logOff(list, account, sc);
if (flag) {
return;
}
break;
default:
System.out.println("您的输入有误");
break;
}
}
}
//账户查看界面
public static void checkAccount(Account account) {
System.out.println("姓名\t\t账号\t\t账户余额\t\t当次提现额度");
System.out.println(account.getName() + "\t\t" + account.getId() + "\t\t" + account.getMoney() + "\t\t" + account.getQuotaMoney());
}
//用户存款界面
private static void saveMoney(Account account, Scanner sc) {
System.out.println("请输入您存入的金额:");
double savemoney = sc.nextInt();
if (savemoney % 100 != 0) {
System.out.println("存入的金额仅能为100的整数倍");
return;
}
if (savemoney == 0) {
System.out.println("您尚未存入金额,请查验,如有疑问请联系银行客服");
return;
}
account.setMoney(account.getMoney() + savemoney);
System.out.println("存款成功,您的账户余额为:" + account.getMoney());
}
//用户取款界面
public static void withDrawal(Account account, Scanner sc) {
System.out.println("请输入您要取出的金额:");
double outMoney = sc.nextDouble();
if (outMoney % 100 != 0) {
System.out.println("取出的金额仅能为100的整数倍");
return;
}
if (outMoney > account.getMoney() || outMoney > account.getQuotaMoney() || outMoney == 0) {
System.out.println("您的账户余额不足或当次取款限额不足或取款金额为0,请重新输入:");
return;
} else {
account.setMoney(account.getMoney() - outMoney);
account.setQuotaMoney(account.getQuotaMoney() - outMoney);
}
System.out.println("取款成功,您的账户余额为:" + account.getMoney());
return;
}
//用户转账界面
public static void transMoney(ArrayList<Account> list, Account account, Scanner sc) {
double outMoney;
if (list == null || list.size() < 2) {
System.out.println("当前暂无可用转账账户");
return;
}
while (true) {
System.out.println("请输入您要转账的账号");
String inAccountId = sc.next();
Account intemp = isExtened(list, inAccountId);
if (intemp == null || intemp.getId().equals(account.getId())) {
System.out.println("暂无该账户或不能给自己转账,请检查后再次输入");
} else {
while (true) {
System.out.println("请输入您的转账金额:");
outMoney = sc.nextDouble();
if (outMoney > account.getMoney() || outMoney == 0) {
System.out.println("您的账户余额不足或转账金额为0,请重试");
} else {
break;
}
}
while (true) {
String name = intemp.getName();
char tempch = name.charAt(0);
String newname = name.replace(tempch, '*');
System.out.println("您要给账户为" + intemp.getId() + "转账:" + outMoney + "元,请输入该账户户主姓氏确认转账:" + newname);
String inch1 = sc.next();
char inch = inch1.charAt(0);
boolean flag = flag(inch, tempch);
if (flag) {
System.out.println("信息对比有误 ,请重新输入");
} else {
account.setMoney(account.getMoney() - outMoney);
intemp.setMoney(intemp.getMoney() + outMoney);
System.out.println("转账成功");
return;
}
}
}
}
}
//账户户主姓氏对比
public static boolean flag(char inch, char tempch) {
if (inch == tempch) {
return false;
}
return true;
}
//账户密码修改界面
public static void reSet(Account account, Scanner sc) {
while (true) {
System.out.println("请输入原密码:");
String oldPwd = sc.next();
if (oldPwd.equals(account.getPassword())) {
System.out.println("请输入新设的密码");
String newPwd1 = sc.next();
System.out.println("请第二次输入新设的密码");
String newPwd2 = sc.next();
if (newPwd1.equals(newPwd2)) {
account.setPassword(newPwd1);
return;
} else {
System.out.println("两次密码输入不一致");
}
} else {
System.out.println("密码输入错误,请重新输入");
}
}
}
//账户注销界面
public static boolean logOff(ArrayList<Account> list, Account account, Scanner sc) {
System.out.println("请输入账户密码:");
String pwd = sc.next();
if (pwd.equals(account.getPassword())) {
System.out.println("确认注销?(请确保账户余额为0,否则注销失败) 确认请按1. 取消注销请按其他键 ");
int num = sc.nextInt();
if (num == 1 && account.getMoney() == 0) {
list.remove(account);
System.out.println("注销账户成功");
return true;
}
System.out.println("已取消注销账户操作或者余额不为0元");
return false;
}
System.out.println("密码输入错误,已取消注销账户操作");
return false;
}
}
https://static.52pojie.cn/static/image/hrline/1.gif
二:代码实现具体截图
其他的内容我就不截图了,大家有兴趣的可以自己复制到运行器中看看(新手一个,不好,可以留下指导意见,勿喷,感谢嘴下留情).
用spring框架写一个web给他 感谢感谢,最近也在学 谢谢分享,关系 金额最好用bigdecimal类型 吾爱葡萄 发表于 2022-10-11 14:10
金额最好用bigdecimal类型
确实,这个学过后老师有讲过.bigdecimal的精度不会丢失,谢谢 楼主继续加油, lcg2014 发表于 2022-10-11 14:35
楼主继续加油,
Thanks♪(・ω・)ノ
是在黑马学的吗?黑马我们写的是学生管理系统 Ahuage98 发表于 2022-10-11 16:02
是在黑马学的吗?黑马我们写的是学生管理系统
是的哈哈,银行的学生的等等都写,你没写这个的话,可能是我们老师自己添加的
页:
[1]
2