我是靠谱客的博主 孝顺菠萝,最近开发中收集的这篇文章主要介绍java做一个简单的银行账户演示程序_JAVA初学(七):银行账户演示程序,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

一、设计梗概

做一个简单的银行、账户演示程序。

(1)定义一个账户类,包含用户名、密码、余额等属性、存钱、取钱等方法。

(2)定义一个银行类,包含银行名、账户列表等属性,开账户、查账户的功能。账户列表可以使用集合相关的类,如ArrayList,最好同时使用泛型。

(3)定义一个测试类及main函数,在其中创建一个银行,开设一些账号,模拟存钱、取钱、显示余额等。

二、内容完善

代码中第二部分的泛型尚未完成,后期会继续完善

三、代码内容

package practise;

import java.util.*;

import java.util.Scanner;

public class bank {

public static void main(String[]arg){

System.out.println("欢迎来到小猪银行!");

boolean out = true;boolean out0=true; //out和out0用于退出循环,回到初始界面

Welcome welcome = new Welcome(); //新建一个欢迎类

Count count = new Count(); //新建一个Count类(用户账户)

WorkerCount workerCount=new WorkerCount(); //新建一个WorkerCount类(工作人员账户)

workerCount.countContent.add(new java.lang.String("账号:佩奇,密码:1314")) ;

System.out.println("请选择你的账户类型:输入数字0:进入普通账户|输入数字1:进入工作人员账户");

Scanner scan = new Scanner(System.in); //用于扫描键盘输入,以做出下一步操作,下同

int judgment = scan.nextInt();

boolean rightInput=false;//rightInput判断用户输入是否正确,如果错误则提示重新输入

while (true) {

if (judgment == 0) { //judgment==0时,进入客户服务端

rightInput=true; //将rightInput设置成ture

welcome.welcomeCustomer(); //触发顾客账号登录程序

Scanner Input = new Scanner(System.in);

System.out.print("n请输入如下数字进入客户服务:");

while (out) {

System.out.println("输入数字0:查询账户余额" +

" | 输入数字1:提取现金" +

" | 输入数字2:存入现金" +

" | 输入数字3:退卡");

int input = Input.nextInt();

switch (input) {

case 0:

System.out.print("账户余额为:" + count.banlance + "nn请继续下一步操作:n");

break;

case 1:

count.WithdrawMoney();

break;

case 2:

count.deposit();

break;

case 3:

out = false;

}

}

System.out.println("感谢您的使用,欢迎下次光临!");

}

if (judgment == 1) {

rightInput=true;

welcome.welcomeWorker(); //触发工作人员登录程序

Scanner Input = new Scanner(System.in);

System.out.print("n请输入如下数字进行不同操作:");

while (out0) {

System.out.println("输入数字0:查询银行名" +

" | 输入数字1:查询账户列表" +

" | 输入数字2:开账户" +

" | 输入数字3:关账户"+

" | 输入数字4:退出系统");

int input0 = Input.nextInt();

switch (input0) {

case 0:

workerCount.bankname();

break;

case 1:

workerCount.countlist();

break;

case 2:

workerCount.opencount();

break;

case 3:

workerCount.closecount();

break;

case 4:

out0=false;

}

}

}

if (rightInput==true)System.out.println("请重新开始您的操作");

else System.out.println("输入错误,请重新输入");

}

}

}

class Welcome{ //输入客户的账号密码,并测试信息是否正确

public void welcomeCustomer(){

while(true) {

boolean sign = false; //该标志用于退出循环,账号密码正确则break

Scanner scanner_login = new Scanner(System.in);

System.out.println("请输入您的账号名:");

String CountName = scanner_login.next();

Scanner scanner_Password = new Scanner(System.in);

System.out.println("请输入您的数字密码:");

int CountPassword = scanner_login.nextInt();

Count count_test = new Count(CountName, CountPassword, sign); //Count类reture sign,if true则退出循环,继续执行main函数

if (count_test.CountTest()) break;

}

}

public void welcomeWorker(){ //执行方法与顾客类似

while(true) {

boolean sign = false;

Scanner Workerscanner_login = new Scanner(System.in);

System.out.println("请输入您的账号名:");

String WorkerName = Workerscanner_login.next();

Scanner Workerscanner_Password = new Scanner(System.in);

System.out.println("请输入您的数字密码:");

int WorkerPassword = Workerscanner_login.nextInt();

WorkerCount Workercount_test = new WorkerCount(WorkerName, WorkerPassword, sign);

if (Workercount_test.WorkerCountTest()) break;

}

}

}

class Count{

String name;int password;boolean sign;

int banlance=10000;

Count(){};

Count(String name,int password,boolean sign){ //构造方法,sign标记用于跳出循环

this.name=name;

this.password=password;

this.sign=sign;

}

public boolean CountTest(){

if (name.equals("佩奇")){

if (password==1314){

System.out.println("账号名及密码输入正确");

sign=true;

}else System.out.println("密码输入错误,请重新输入");

}else System.out.println("账号名输入错误,请重新输入");

return sign; //此处reture sign

}

public void deposit(){ //用于存入现金

Scanner scanner=new Scanner(System.in);

System.out.println("How mach do you want to deposit?");

int deposit_money=scanner.nextInt();

this.banlance=this.banlance+deposit_money;

System.out.println("已存入:"+deposit_money+"元,剩余金额为:"+this.banlance +"元"+"nn请继续下一步操作:");

}

public void WithdrawMoney(){ //用于提取现金

Scanner scanner=new Scanner(System.in);

System.out.println("How mach do you want to withdraw?");

int withdraw_money=scanner.nextInt();

if (this.banlance>=withdraw_money){

this.banlance=this.banlance-withdraw_money;

System.out.println("已取出:"+withdraw_money+"元,剩余金额为:"+this.banlance +"元"+"nn请继续下一步操作:");

}

}

}

class WorkerCount{ //工作人员账户

List countContent= new ArrayList();

String name;int password;boolean sign;String bankName="小猪银行";

WorkerCount(){};

WorkerCount(String name,int password,boolean sign){ //构造方法,同时判断用户名是否输入正确

this.name=name;

this.password=password;

this.sign=sign;

}

public boolean WorkerCountTest(){

if (name.equals(bankName)){

if (password==520520){

System.out.println("账号名及密码输入正确");

sign=true;

}else System.out.println("密码输入错误,请重新输入");

}else System.out.println("账号名输入错误,请重新输入");

return sign;

}

public void bankname(){

System.out.println("银行名为:"+this.bankName);

while (true) {

Scanner scanner1 = new Scanner(System.in);

System.out.println("修改银行名请按:1,退出请按0");

int inputNumber = scanner1.nextInt();

if (inputNumber==0)break;

else {

Scanner scanner2 = new Scanner(System.in);

System.out.println("请输入您要修改的银行名:");

String bankName = scanner2.next();

this.bankName=bankName;

System.out.println("新的银行名为:"+this.bankName+",请继续下一步操作:"); //此处功能任有缺陷,新修改的银行名在重启系统时无法仍旧会变成“小猪银行”,暂时没想到办法完善

}

}

}

public void countlist(){

for (int i = 0 ; i < WorkerCount.this.countContent.size() ; i++ )

{

System.out.println(WorkerCount.this.countContent.get(i));

}

}

public void opencount(){

String[]opendArray=new String[2];

while (true) {

Scanner scanner0 = new Scanner(System.in);

System.out.println("请输入设置的账号名");

opendArray[0] = scanner0.next();

Scanner scanner1 = new Scanner(System.in);

System.out.println("请输入设置的密码");

opendArray[1] = scanner1.next();

countContent.add(new String("账号:"+opendArray[0]+",密码:"+opendArray[1]));

System.out.println("您的账号为:"+opendArray[0]+"密码为:"+opendArray[1]+"确认请按1,重新设置请按0");

Scanner scanner2 = new Scanner(System.in);

int judge = scanner2.nextInt();

if (judge==1)break;

}

}

public void closecount(){

while (true) {

Scanner scanner0 = new Scanner(System.in);

System.out.println("您要关闭的账号名为:");

String closename = scanner0.next();

Scanner scanner1 = new Scanner(System.in);

System.out.println("请输入密码:");

int password = scanner0.nextInt();

int i=countContent.indexOf(new String("账号:"+closename+","+"密码:"+password));

Scanner scanner2 = new Scanner(System.in);

System.out.println("您关闭的账号序号为:"+closename+";确认请按1,重新设置请按0");

int judge = scanner2.nextInt();

if (judge==1){this.countContent.remove(i); break;}

}

}

}

注:鉴于作者水平有限,如有更好的算法欢迎评价。

最后

以上就是孝顺菠萝为你收集整理的java做一个简单的银行账户演示程序_JAVA初学(七):银行账户演示程序的全部内容,希望文章能够帮你解决java做一个简单的银行账户演示程序_JAVA初学(七):银行账户演示程序所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(43)

评论列表共有 0 条评论

立即
投稿
返回
顶部