项目场景:

java实现简单的银行存款取款系统

本次代码适合初学者小白,连数据库都没有运用。一次性程序哈哈。


备学内容:

你得先了解java中以下基本内容。
1、 数组
2、 方法
3、 类
4、 Scanner扫描器


学习产果:


在写这段代码的过程中,我学到了以下几个知识点:
1、 类和方法的灵活应用。
2、类既然可以数组来使用!!!
3、 数据的相互传递:


系统描述:

当我学完java中的基本知识点之后想实现一个银行存取款系统,就简单实现:
登录,注册,退出,存款,取款转账,查询个人信息等功能。

首先写一个main方法

import java.util.Scanner; //扫描器public class bank{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); } }

main方法创建好之后再 创建一个geren1类,就是保存用户各类信息。

class geren1{private String zhanghao;private String mima;private int zijin=0;private String name;private int xuhao;private String phone;//构造方法public geren1(String zhanghao,String mima,String name,String phone,int xuhao){this.zhanghao = zhanghao;this.mima = mima;this.name = name;this.phone = phone;this.xuhao = xuhao;}//遍历个人信息public void print(){System.out.println("姓名:"+this.getName()+"\t\t\t账号:"+this.getZhanghao()+" \n密码:"+this.getMima()+"\t\t\t账户资金:"+this.getZijin()+"\n练习电话:"+this.getPhone());}public String getPhone(){return phone;}public void setPhone(String phone){this.phone = phone;}public int getXuhao(){return xuhao;}public void setXuhao(int xuhao){this.xuhao = xuhao;}public String getName(){return name;}public void setName(String name){this.name = name;}public String getZhanghao(){return zhanghao;}public void setZhanghao(String zhanghao){this.zhanghao = zhanghao;}public String getMima(){return mima;}public void setMima(String mima){this.mima = mima;}public int getZijin(){return zijin;}public void setZijin(int zijin){this.zijin = zijin;}}

类创建好了之后在main方法里创建一个geren1类型数组。

意思就是在geren1数组中就放geren1类。最多也就能放10个,如果想多方几个就把10改成100或者更多就OK了。
 public static void main(String[] args){ Scanner sc = new Scanner(System.in); geren1[] user = new geren1[10]; }

然后就输出一个登录页面,不用认真,简单输出就好!我在main方法外面创建一个xtjiemian()方法,顾名思义就是专门用来显示登录界面的。

public static void xtjiemian(Scanner sc,geren1[] user){while (true){System.out.println("===银行存取款系统===");System.out.println();System.out.println("1,登录\t\t2,注册\t\t3,退出");int xuanze = sc.nextInt();if (xuanze == 1){//print(user);denglu1(sc,user);}else if(xuanze == 2){zhuce(sc,user);//print(user);}else if(xuanze == 3){break;}else{System.out.println("输入有误!");}}}

然后分别再写两个登录和注册判断的两个方法

public static void denglu1(Scanner sc,geren1[] user){System.out.println("==== 登录 =====");System.out.println("请输入账号:");String zh = sc.next();System.out.println("请输入密码:");String mm = sc.next();for (int i = 0;i<user.length;i++){System.out.println(user[i]);if (user[i] != null){geren1 gr = user[i];System.out.println(gr.getZijin());if (zh.equals(gr.getZhanghao()) && mm.equals(gr.getMima())){System.out.println("登录成功!");grjiemian(sc,user,gr);return;}}}System.out.println("无此账号或密码错误!");}public static void zhuce(Scanner sc, geren1[] user){System.out.println("==== 注册 ====");for (int i=0;i<user.length;i++){if (user[i] == null){System.out.println("请输入注册账号:");//user[i][0] = sc.next();String zhanghao = sc.next();System.out.println("请输入注册密码:");//user[i][1] = sc.next();String mima = sc.next();System.out.println("请输入您的真实姓名:");String name = sc.next();System.out.println("请输入银行卡号:(手机号即可)");//zijin[i][2] = sc.nextInt();String phone = sc.next();geren1 gr = new geren1(zhanghao,mima,name,phone,i);user[i] = gr;System.out.println("注册成功");return;}}}

最后就是登录后的个人界面

public static void grjiemian(Scanner sc,geren1[] user,geren1 gr){while(true){System.out.println("==== 个人页面 =====");System.out.println("请选择您要办理的业务:");System.out.println("1,存款\t\t\t2,取款");System.out.println("3,查询余额\t\t4,转账");System.out.println("5,查询个人信息\t6,退出登录");int xuanze = sc.nextInt();if (xuanze == 1){System.out.println("请输入存款金额:");gr.setZijin(gr.getZijin()+sc.nextInt());System.out.println("存款成功,当前余额:"+gr.getZijin()+"元");}else if (xuanze == 2){System.out.println("请输入取款金额:");int qk = sc.nextInt();if(qk>gr.getZijin()){System.out.println("余额不足。。。");}else{gr.setZijin(gr.getZijin()-qk);System.out.println("取款成功,当前余额:"+gr.getZijin()+"元");}}else if(xuanze == 3){System.out.println("当前余额:"+gr.getZijin()+"元");}else if(xuanze == 4){System.out.println("请输入转账账号:");String zh = sc.next();System.out.println("请输入转账金额:");int jine = sc.nextInt();for (int zhao = 0;zhao<user.length;zhao++){if (user[zhao] !=null){geren1 gr1 = user[zhao];if (zh.equals(gr1.getZhanghao())){gr1.setZijin(gr1.getZijin()+jine);gr.setZijin(gr.getZijin() - jine);}}}}else if(xuanze == 5){gr.print();}else if(xuanze == 6){return;}}}

把xtjiemian()方法写在main()方法中,大功告成,以下是最后的成品代码。

import java.util.Scanner;public class denglu2 { public static void main(String[] args){ Scanner sc = new Scanner(System.in); geren1[] user = new geren1[10]; //System.out.println(user[1]); xtjiemian(sc,user); } public static void xtjiemian(Scanner sc,geren1[] user){ while (true) { System.out.println("===优就业银行系统==="); System.out.println(); System.out.println("1,登录\t\t2,注册\t\t3,退出"); int xuanze = sc.nextInt(); if (xuanze == 1) { //print(user); denglu1(sc,user); }else if(xuanze == 2){ zhuce(sc,user); //print(user); }else if(xuanze == 3){ break; } else{System.out.println("输入有误!");} } } public static void zhuce(Scanner sc, geren1[] user){ System.out.println("==== 注册 ===="); for (int i=0;i<user.length;i++) { if (user[i] == null) { System.out.println("请输入注册账号:"); //user[i][0] = sc.next(); String zhanghao = sc.next(); System.out.println("请输入注册密码:"); //user[i][1] = sc.next(); String mima = sc.next(); System.out.println("请输入您的真实姓名:"); String name = sc.next(); System.out.println("请输入手机号:"); //zijin[i][2] = sc.nextInt(); String phone = sc.next(); geren1 gr = new geren1(zhanghao,mima,name,phone,i); user[i] = gr; System.out.println("注册成功"); return; } } } public static void denglu1(Scanner sc,geren1[] user){ System.out.println("==== 登录 ====="); System.out.println("请输入账号:"); String zh = sc.next(); System.out.println("请输入密码:"); String mm = sc.next(); for (int i = 0;i<user.length;i++){ //System.out.println(user[i]); if (user[i] != null){ geren1 gr = user[i]; //System.out.println(gr.getZijin()); if (zh.equals(gr.getZhanghao()) && mm.equals(gr.getMima())) { System.out.println("登录成功!"); grjiemian(sc,user,gr); return; } } } System.out.println("无此账号或密码错误!"); } public static void grjiemian(Scanner sc,geren1[] user,geren1 gr){ while(true){ System.out.println("==== 个人页面 ====="); System.out.println("请选择您要办理的业务:"); System.out.println("1,存款\t\t\t2,取款"); System.out.println("3,查询余额\t\t4,转账"); System.out.println("5,查询个人信息\t6,退出登录"); int xuanze = sc.nextInt(); if (xuanze == 1){ System.out.println("请输入存款金额:"); gr.setZijin(gr.getZijin()+sc.nextInt()); System.out.println("存款成功,当前余额:"+gr.getZijin()+"元"); }else if (xuanze == 2){ System.out.println("请输入取款金额:"); int qk = sc.nextInt(); if(qk>gr.getZijin()){ System.out.println("余额不足。。。"); }else{ gr.setZijin(gr.getZijin()-qk); System.out.println("取款成功,当前余额:"+gr.getZijin()+"元"); } }else if(xuanze == 3){ System.out.println("当前余额:"+gr.getZijin()+"元"); } else if(xuanze == 4){ System.out.println("请输入转账账号:"); String zh = sc.next(); System.out.println("请输入转账金额:"); int jine = sc.nextInt(); for (int zhao = 0;zhao<user.length;zhao++){ if (user[zhao] !=null){ geren1 gr1 = user[zhao]; if (zh.equals(gr1.getZhanghao())){ gr1.setZijin(gr1.getZijin()+jine); gr.setZijin(gr.getZijin() - jine); } } } } else if(xuanze == 5){ gr.print(); } else if(xuanze == 6){ return; } } }}

代码其实可以再优化以下,有一些代码可以合并。


效果图: