设计步骤:
注意:本篇代码,直接建两个类( Bank_Account、BankManage)在BankManage往里面填方法就行。是为了让和我一样自学朋友可以更快的接受。但是代码优化的空间还是很大的(比如判断ID合法性完全可以写成一个方法,由于是为了学习打的代码,全是用的逻辑语,以后有空再改改吧),望使用的朋友可以融汇贯通下。运行在最后的AccountTest测试类中。
欢迎思考型的白嫖党,拒绝拿来主义的伸手党!
一、总体架构:
(1)设计 Bank_Account类与BankManage类,前者储存数据,后者实现方法
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200106103645119.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dzY3R4,size_16,color_FFFFFF,t_70)
注意:从(3)BankManage类主体之后的代码均为可以直接单独用的方法,每个方法中可以单独拿出来的部分我也没有优化,是最独立的,可以直接看(当然方法调用是离不开方法的,稍微看下,应该能懂吧,都有注释)
目录:
- Bank_Account类:
- BankManage类主体
- 管理员登录方法
- 主界面方法
- 返回主页面的方法
- 显示所有账户的方法
- 添加账户的方法
- 存钱的方法
- 取钱的方法
- 转账的方法
- 修改密码的方法
- 注销账户的方法
- AccountTest测试类
二、实现代码
(2) Bank_Account类:
package com.chen.bankmanager;
public class Bank_Account {
String aname;
float balance;
int Password;
int aid;
Bank_Account[] accounts = new Bank_Account[3];
public void inputaccounts(){
for (int i=0;i<accounts.length;i++)
{
accounts[i]=new Bank_Account();
accounts[i].aname="预存账户";
accounts[i].aid=i+1;
accounts[i].Password=666;
accounts[i].balance=0;
}
}
}
(3)BankManage类主体(其他方法往里面挨个填即可):
在这里插入代码片
package com.chen.bankmanager;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Scanner;
public class BankManager extends Bank_Account {
}
(4)管理员登录方法
public void registerInterface(){
int password = 666;
int i=2;
Scanner input =new Scanner(System.in);
System.out.println("==============");
System.out.println("您好,管理员登陆前请输入密码:");
int inpassword = input.nextInt();
if(inpassword==password){
inputaccounts();
System.out.println("登陆成功");
fristInterface();
}else {
do {
System.out.println("密码错误:还有" + i + "机会");
if(i==0){
System.out.println("系统锁死!");
break;
}
inpassword = input.nextInt();
if(inpassword==password){
break;
}
i--;
}while (i>=0);
if(inpassword==password)
{
inputaccounts();
System.out.println("登陆成功");
fristInterface();
}
}
}
(5)主界面方法:
public void fristInterface(){
Scanner input=new Scanner(System.in);
System.out.println("==============");
System.out.println("0、显示所有账户");
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("==============");
System.out.println("请选择:");
int x=input.nextInt();
if(x==0) {
showAllAccountInfo();
}else if(x==1) {
addAccountInfo();
}else if(x==2){
saveAccountMoney();
}else if(x==3){
getMoney();
}else if(x==4){
transMoney();
}else if(x==5){
updatePwd();
}else if(x==6){
deletePwd();
}
}
(6)返回主页面的方法
public void back(){
Scanner input =new Scanner(System.in);
System.out.println("==========================");
System.out.println("返回用户界面0:");
int x = input.nextInt();
if (x==0)
{
fristInterface();
}
}
(7)显示所有账户的方法
public void showAllAccountInfo(){
for(int i=0;i<accounts.length;i++){
if(accounts[i]!=null) {
System.out.print("编号:" + (i + 1));
System.out.print(" 账户名:" + accounts[i].aname);
System.out.print(" 账户Id:" + accounts[i].aid);
System.out.print(" 账户密码:" + accounts[i].Password);
System.out.print(" 账户余额:" + accounts[i].balance);
System.out.println();
}
}
back();
}
(8)添加账户的方法
public void addAccountInfo(){
Scanner input =new Scanner(System.in);
System.out.println("================");
System.out.println("请输入新账号ID(请勿输入1,2,3):");
int inputID=input.nextInt();
for (int i=0;i<accounts.length;i++)
{
if(inputID==accounts[i].aid){
System.out.println("已存在账户,不允许开户!非法操作,退出程序");
System.exit(0);
}
}
System.out.println("账户合法,继续执行:");
int flag=0;
for (int i=0;i<accounts.length;i++)
{
if(accounts[i]==null)
{
flag=1;
System.out.println("请输入账户名:");
accounts[i].aname=input.next();
accounts[i].aid=inputID;
System.out.println("请输入账户密码:");
accounts[i].Password=input.nextInt();
System.out.println("请输入账户余额:");
accounts[i].balance=input.nextFloat();
break;
}
}
if(flag==0)
{
Bank_Account[] new_accounts= new Bank_Account[accounts.length+20];
System.arraycopy(accounts,0,new_accounts,0,accounts.length);
for (int i=accounts.length;i<new_accounts.length;i++)
{
if (new_accounts[i]==null) {
new_accounts[i] = new Bank_Account();
System.out.println("请输入账户名:");
new_accounts[i].aid = inputID;
new_accounts[i].aname = input.next();
System.out.println("请输入账户密码:");
new_accounts[i].Password = input.nextInt();
System.out.println("请输入账户余额:");
new_accounts[i].balance = input.nextFloat();
break;
}
}
accounts=new_accounts;
}
back();
}
(9)存钱的方法
public void saveAccountMoney(){
Scanner input= new Scanner(System.in);
System.out.println("请输入需要存钱的账户ID:");
int intputID=input.nextInt();
int flag=0;
int j=1;
do {
for (int i = 0; i < accounts.length; i++) {
if (intputID == accounts[i].aid) {
flag = 1;
j = i + 1;
break;
}
}
if (flag == 0) {
System.out.println("不存在此账户,请重输(0)或返回(1):");
int x = input.nextInt();
if (x == 0)
intputID = input.nextInt();
else if (x==1) {
fristInterface();
break;
}
}else {
do {
System.out.println("请输入金额:");
float money = input.nextFloat();
if (money<=0){
System.out.println("输入金额不合法!重新输入:");
}else {
System.out.println("请输入密码:");
int Password=input.nextInt();
if (Password==accounts[j-1].Password){
accounts[j-1].balance+=money;
System.out.println("余额为:"+accounts[j-1].balance);
back();
}else {
System.out.println("密码错误!你还有1次机会:");
Password=input.nextInt();
if(Password!=accounts[j-1].Password){
System.out.println("密码错误!关闭程序!");
System.exit(0);
}else {
accounts[j-1].balance+=money;
System.out.println("余额为:"+accounts[j-1].balance);
back();
}
}
}
}while (j!=0);
}
}while(j!=0);
}
(10)取钱的方法
public void getMoney(){
Scanner input= new Scanner(System.in);
System.out.println("请输入需要取钱的账户ID:");
int intputID=input.nextInt();
int flag=0;
int j=1;
do {
for (int i = 0; i < accounts.length; i++) {
if (intputID == accounts[i].aid) {
flag = 1;
j = i + 1;
break;
}
}
if (flag == 0) {
System.out.println("不存在此账户,请重输(0)或返回(1):");
int x = input.nextInt();
if (x == 0)
intputID = input.nextInt();
else if (x==1) {
fristInterface();
break;
}
}else {
do {
System.out.println("请输入金额:");
float money = input.nextFloat();
if (money>accounts[j-1].balance){
System.out.println("余额仅剩:"+accounts[j-1].balance+" 重新输入:");
}else {
System.out.println("请输入密码:");
int Password=input.nextInt();
if (Password==accounts[j-1].Password){
accounts[j-1].balance-=money;
System.out.println("余额为:"+accounts[j-1].balance);
back();
}else {
System.out.println("密码错误!你还有1次机会:");
Password=input.nextInt();
if(Password!=accounts[j-1].Password){
System.out.println("密码错误!关闭程序!");
System.exit(0);
}else {
accounts[j-1].balance-=money;
System.out.println("余额为:"+accounts[j-1].balance);
back();
}
}
}
}while (j!=0);
}
}while(j!=0);
}
(11)转账的方法
public void transMoney(){
Scanner input=new Scanner(System.in);
int j=1;
int l=1;
int flag01 =0;
int flag02 =0;
System.out.println("请先输入转账账户ID:");
int inputID01=input.nextInt();
do {
for (int i=0;i<accounts.length;i++){
if (accounts[i].aid==inputID01)
{
flag01=1;
j=i+1;
break;
}
}
if(flag01==1){
System.out.println("合法账户,请继续输入对方账户:");
int inputID02=input.nextInt();
for (int i=0;i<accounts.length;i++){
if (accounts[i].aid==inputID02)
{
flag02=1;
l=i+1;
break;
}
}
if(flag02==1){
System.out.println("请输入金额:");
do{
float money=input.nextFloat();
if(money>accounts[j-1].balance){
System.out.println("金额超值,剩余:"+accounts[j-1].balance+"请重新输入:");
}else {
System.out.println("请输入密码:");
int password=input.nextInt();
if(password!=accounts[j-1].Password){
System.out.println("密码错误,你还有一次机会:");
password=input.nextInt();
if(password!=accounts[j-1].Password){
System.out.println("密码错误,程序结束!");
System.exit(0);
}
else {
accounts[j-1].balance-=money;
accounts[l-1].balance+=money;
System.out.println("账户剩余:"+accounts[j-1].balance);
System.out.println("对方账户剩余:"+accounts[l-1].balance);
back();
}
}else {
accounts[j-1].balance-=money;
accounts[l-1].balance+=money;
System.out.println("账户剩余:"+accounts[j-1].balance);
System.out.println("对方账户剩余:"+accounts[l-1].balance);
back();
}
}
}while (j!=0);
}else{
System.out.println("账户不存在,请确认后再次登录:");
back();
}
}else {
System.out.println("账户不存在,请重新输入(0)或者返回(1):");
int x= input.nextInt();
if (x==0){
inputID01=input.nextInt();
}else {
fristInterface();
}
}
}while(j!=0);
}
(12)修改密码的方法
public void updatePwd(){
Scanner input = new Scanner(System.in);
System.out.println("请输入需要改密的账户ID:");
int inputID = input.nextInt();
int flag=0;
int j=1;
do {
for (int i = 0; i < accounts.length; i++) {
if (inputID == accounts[i].aid) {
flag = 1;
j=i+1;
break;
}
}
if(flag==0){
System.out.println("账户有误,重新输入:");
inputID = input.nextInt();
}else{
System.out.println("合法账户,输入原密码:");
int password01=input.nextInt();
if(password01==accounts[j-1].Password){
do {
System.out.println("请输入新密码:");
int password02=input.nextInt();
System.out.println("请再次输入新密码:");
int password03=input.nextInt();
if(password02==password03&&password02!=password01){
accounts[j-1].Password=password02;
System.out.println("密码修改成功!");
back();
}else if(password02==password01){
System.out.println("新密码不能和旧密码相同,请重新输入:");
}else if(password02!=password03){
System.out.println("两次密码不同请重新输入:");
}
}while (j!=0);
}else{
System.out.println("密码错误,程序终止!");
System.exit(0);
}
}
}while (j!=0);
}
(13)注销账户的方法
public void deletePwd(){
System.out.println("请输入需要删除的账户ID:");
Scanner input = new Scanner(System.in);
int inputID=input.nextInt();
int flag = 0;
int j=1;
for (int i = 0; i < accounts.length; i++) {
if (inputID == accounts[i].aid) {
flag = 1;
j=i+1;
break;
}
}
if(flag==1){
System.out.println("合法账户,请输入密码:");
int password = input.nextInt();
if(password==accounts[j-1].Password)
{
for(int i=j-1;i<accounts.length-1;i++){
accounts[i]=accounts[i+1];
}
accounts=Arrays.copyOf(accounts,accounts.length-1);
System.out.println("删除成功"+(j-1));
back();
}else {
System.out.println("密码错误!程序终止!");
System.exit(0);
}
}else {
System.out.println("账户不存在!");
back();
}
}
(14)AccountTest测试类
package com.chen.bankmanager;
public class AccountTest extends BankManager{
public static void main(String[] arg){
BankManager bank = new BankManager();
bank.registerInterface();
}
}