使用面向对象来完成一个银行系统的功能
- 账户类,首页设计
- 用户开户功能实现
- 用户登录功能实现
- 用户操作页面设计,查询账户,退出账户功能
- 用户存款取款功能设计
- 用户转账功能设计
- 用户密码修改功能,销户功能
package com.zxx.test;
public class Account {
private String cardId;
private String userName;
private String password;
private double money;
private double quoteMoney;
public Account(String cardId, String userName, String password, double quoteMoney) {
this.cardId = cardId;
this.userName = userName;
this.password = password;
this.quoteMoney = quoteMoney;
}
public Account() {
}
public String getCardId() {
return cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
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 double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public double getQuoteMoney() {
return quoteMoney;
}
public void setQuoteMoney(double quoteMoney) {
this.quoteMoney = quoteMoney;
}
}
package com.zxx.test;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class ATMSystem {
public static void main(String[] args) {
ArrayList<Account> accounts=new ArrayList<>();
showMain(accounts);
}
private static void showMain(ArrayList<Account> accounts) {
while (true) {
System.out.println("欢迎您进入黑马ATM系统");
System.out.println("1.登录账户");
System.out.println("2.注册开户");
System.out.println("请输入命令1,2选择对应的操作");
Scanner sc=new Scanner(System.in);
int command=sc.nextInt();
switch (command){
case 1://登录
login(accounts,sc);
break;
case 2://注册
reginster(accounts,sc);
break;
default:
System.out.println("输入有误,");
break;
}
}
}
private static void login(ArrayList<Account> accounts, Scanner sc) {
if (accounts.size()==0){
System.out.println("系统中暂无任何账户");
return;
}
System.out.println("===============欢迎来到登录页面=================");
while (true) {
System.out.println("请您输入您的卡号");
String cardId=sc.next();
Account acc=getAccountById(accounts,cardId);///////////////////////////////从这传的acc
//一定要去判断一下这个用户是不是空的,否则会出现空指针异常
if (acc != null) {
if (acc.getCardId().equals(cardId)){
System.out.println("请输入您的密码");
String password=sc.next();
if (acc.getPassword().equals(password)){
System.out.println(acc.getUserName()+"先生/女士,欢迎您进入系统!");
showAccount(accounts,acc,sc);
return;
}else {
System.out.println("密码有误!!");
}
}else {
System.out.println("卡号输入错误");
}
}else {
System.out.println("无该卡号的信息,请重新输入");
}
}
}
private static void showAccount(ArrayList<Account> accounts,Account acc, 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.注销账户");
System.out.println("您可以继续选择功能进行操作:");
int type=sc.nextInt();
switch (type){
case 1://查询
find(acc,sc);
break;
case 2://存款
depositMoney(acc,sc);
break;
case 3://取款
drawMoney(acc,sc);
break;
case 4://转账
transfromMoney(acc,sc,accounts);
break;
case 5://修改密码
updatePassword(acc,sc);
return;
case 6://退出
System.out.println("感谢您的信赖和光临");
return;
case 7://注销
accounts.remove(acc);
return;
default:
System.out.println("输入有误");
break;
}
}
}
private static void transfromMoney(Account acc, Scanner sc, ArrayList<Account> accounts) {
System.out.println("===============欢迎来到转账页面=================");
if (accounts.size()<2){
System.out.println("当前系统中无其它用户!");
return;
}
if (acc.getMoney()<0){
System.out.println("穷逼,自己都没钱还转啥转");
return;
}
while (true) {
System.out.println("请输入转账用户的ID");
String cardId=sc.next();
Account account= getAccountById(accounts,cardId);
if (acc.getCardId().equals(cardId)){
System.out.println("调皮,不能给自己转账!");
}else {
if (cardId==null){
System.out.println("卡号输入错误");
}else {
String name="*"+acc.getUserName().substring(1);
System.out.println("您当前要为"+name+"转账!");
System.out.println("请输入姓氏来进一步确认");
String userName=sc.next();
if (acc.getUserName().startsWith(userName)){
System.out.println("请输入转账金额:");
double money=sc.nextDouble();
if (money>acc.getMoney()){
System.out.println("账户余额不足");
}else {
acc.setMoney(acc.getMoney()-money);
account.setMoney(account.getMoney()+money);
System.out.println("转账成功!账户当前余额为:"+acc.getMoney());
return;
}
}else {
System.out.println("姓氏验证有误");
}
}
}
}
}
private static void updatePassword(Account acc, Scanner sc) {
System.out.println("===============欢迎来到修改密码页面=================");
while (true) {
System.out.println("请输入原来密码");
String password=sc.next();
if (acc.getPassword().equals(password)){
System.out.println("请输入修改后的密码");
String upPassword=sc.next();
System.out.println("请确认修改后的密码");
String okPassword=sc.next();
if (upPassword.equals(okPassword)){
acc.setPassword(upPassword);
System.out.println("修改密码成功!");
return;
}else {
System.out.println("两次密码不一致~~");
}
}else {
System.out.println("密码错误!");
}
}
}
private static void drawMoney(Account acc, Scanner sc) {
System.out.println("===============欢迎来到取钱页面=================");
if (acc.getMoney()<100){
System.out.println("兄弟先存钱吧,余额不足100");
return;
}
while (true) {
System.out.println("请输入取款的金额");
double money=sc.nextDouble();
if (money>acc.getQuoteMoney()){
System.out.println("您当前取款金额大与当次限额");
}else {
if (money>acc.getMoney()){
System.out.println("余额不足!");
}else {
acc.setMoney(acc.getMoney()-money);
System.out.println("取钱成功!当前余额为:"+acc.getMoney());
return;
}
}
}
}
private static void depositMoney(Account acc, Scanner sc) {
System.out.println("===============欢迎来到存款页面=================");
System.out.println("请输入存款的金额");
double money=sc.nextDouble();
acc.setMoney(acc.getMoney()+money);
System.out.println("存款成功!当前余额为:"+acc.getMoney());
}
private static void find(Account acc, Scanner sc) {
System.out.println("===============欢迎来到查询页面=================");
System.out.println("您的账户信息如下:");
System.out.println("卡号:"+acc.getCardId());
System.out.println("姓名:"+acc.getUserName());
System.out.println("余额:"+acc.getMoney());
System.out.println("取现额度:"+acc.getQuoteMoney());
}
private static void reginster(ArrayList<Account> accounts, Scanner sc) {
System.out.println("===============欢迎来到注册页面=================");
System.out.println("请输入您的账户名");
String name=sc.next();
String password;
while (true) {
System.out.println("请输入您的密码");
password=sc.next();
System.out.println("请再次确认密码");
String okPassword=sc.next();
if (password.equals(okPassword)){
break;
}else {
System.out.println("两次密码不一致~~~");
}
}
System.out.println("请设置当次的取现额度");
double quoteMoney=sc.nextDouble();
String cardId=creatCardId(accounts);
accounts.add(new Account(cardId,name,password,quoteMoney));
System.out.println(name+"先生/女士,您的账户开卡成功,您的卡号是:"+cardId);
}
private static String creatCardId(ArrayList<Account> accounts) {
while (true) {
Random r=new Random();
String cardId="";
for (int i = 0; i < 8; i++) {
cardId+=r.nextInt(10);
}
Account acc=getAccountById(accounts,cardId);
if (acc==null){
return cardId;
}
}
}
private static Account getAccountById(ArrayList<Account> accounts, String cardId) {
for (int i = 0; i < accounts.size(); i++) {
Account acc=accounts.get(i);
if (cardId.equals(acc.getCardId())){
return acc;
}
}
return null;
}
}