概述
1.程序入口
package com.fy.day11.bank;
/**
* 银行案例的测试类(程序的起始)
* @author fangyuan
*
*/
public class TestBank {
public static void main(String[] args) {
Bank bank =new Bank();//创建了一家银行网点
bank.welcomeMenu();
bank.getRandomCardNo();
bank.showAll();
}
}
2. Bank类
package com.fy.day11.bank;
/**
* 银行类()
* @author fangyuan
*
*/
import java.util.Scanner;
import java.util.Random;
public class Bank {//中国建设银行
Scanner input = new Scanner(System.in);//实例变量(实例方法共享)
//保存已注册的用户数据
User[] users = new User[5];
int size = 3;
int currentIndex;
public Bank() {
//第一行逻辑 初始化实例变量
User user = new User();
user.setCardNo("6222020200001234567");
user.setIdentify("11111111111111111");
user.setUsername("张三");
user.setPassword("123456");
user.setPhone("12399999999");
user.setBalance(2000.0);
users[0] = user;
users[1] = new User();
users[1].setCardNo("6222020200001234568");
users[1].setIdentify("11111111111111111");
users[1].setUsername("李四");
users[1].setPassword("123456");
users[1].setPhone("13999994444");
users[1].setBalance(3000.0);
users[2] = new User("6222020200001234560","11111111111111111","王五","123456","13666666666",5000.0);
}
//欢迎页
public void welcomeMenu() {
System.out.println("-----------------------------------------------------------------------");
System.out.println("1.登录
2.注册");
System.out.println("请输入操作码:");
int choice = input.nextInt();
switch(choice) {
case 1:
this.login();
break;
case 2:
this.register();
this.login();
break;
default:
System.out.println("输入有误,请重新输入!");
}
}
//登录
public void login() {
System.out.println("请输入卡号:");
String no = input.next();
System.out.println("请输入密码:");
String pwd = input.next();
User u = check(no,pwd);
if(u != null) {
this.showMenu(u);
}else {
System.out.println("用户名或者密码错误");
}
}
//注册
public void register() {
System.out.println("请输入姓名:");
String uname = input.next();
String id;
do {
System.out.println("请输入身份证号:");
id = input.next();
}while(id.length() !=18);
String phone;
do {
System.out.println("请输入电话号码:");
phone = input.next();
}while(phone.length() != 11);
System.out.println("请输入密码:");
String pwd = input.next();
System.out.println("请输入预存金额:");
Double money = input.nextDouble();
//根据用户输入的信息 + 随机卡号
完成对象的封装
String myCardNo = this.getRandomCardNo();
User user = new User();
user.setCardNo(myCardNo);
user.setUsername(uname);
user.setIdentify(id);
user.setPhone(phone);
user.setPassword(pwd);
user.setBalance(money);
//将该对象存储到users数组中
//
if() {}//扩容判断
users[size] = user;
size++;
System.out.println("注册成功,您的卡号是:" + myCardNo);
}
public String getRandomCardNo() {
String prefix = "622202020000";
Random random = new Random();
int r = random.nextInt(9000000);
System.out.println(prefix + (r + 1000000));
return null;
}
//验证
public User check(String no,String pwd) {
for(int i = 0; i < size; i++) {
//验证卡号和密码
//
if(users[i] != null) {//非空判断
if( no.equals(users[i].getCardNo()) && pwd.equals(users[i].getPassword())) {
//查找到该用户,用户存在
//存在
return users[i];
}
}
//}
return null;//不存在
}
//显示菜单
public void showMenu(User u) {
int choice;
do {
System.out.println("-----------------------------------------------------------------------");
System.out.println("---1.修改预留手机号 2.存款 3.取款 4.转账 5.查询余额 6.修改密码 7.注销用户
0.退出---");
System.out.println("请输入操作码:");
choice = input.nextInt();
switch(choice) {
case 1:
System.out.println("执行开户");
break;
case 2:
System.out.println("执行存款");
break;
case 3:
this.withdrawal(u);;
break;
case 4:
System.out.println("执行转账");
break;
case 5:
System.out.println("执行查询余额");
break;
case 6:
System.out.println("执行修改密码");
break;
case 7:
this.cancel(u);
break;
case 0:
return;
default:
System.out.println("输入有误,请重新输入!");
break;
}
}while(true);
}
//注销
public void cancel(User user) {
int index = size + 1;
//查找该用户所在的数组下标
for(int i = 0; i < size; i++) {
if(users[i] != null) {//非空判断
index = i;
break;
}
}
//移动该元素之后的每个元素
for(int i = index; i < size - 1; i++) {
users[i] = users[i+1];
}
size--;
}
//取款
public void withdrawal(User mine) {
System.out.println("请输入取款金额:");
double money = input.nextDouble();
if(money < mine.getBalance()) {
//可以取款
mine.setBalance(mine.getBalance() - money);
System.out.println("取款成功,当前余额为:" + mine.getBalance());
}else {
System.out.println("余额不足");
}
}
public void showAll() {
for(int i = 0; i < size; i++) {
System.out.println(users[i].getUsername());
}
}
}
3. User类
package com.fy.day11.bank;
/**
* 类的组成、私有属性、公共访问方法、构造方法
* 实体类:Entity
作用:存储数据
* @author fangyuan
*
*/
public class User {
private String cardNo;//卡号
private String identify;//身份证号
private String username;//用户名
private String password;//密码
private String phone;//手机号码
private double balance;//余额
public User() {}
public User(String cardNo,String identity,String username,String password,String phone,double balance, String identify) {
this.cardNo = cardNo;
this.identify = identify;
this.username = username;
this.password = password;
this.phone = phone;
this.balance = balance;
}
public void setCardNo(String cardNo) {
this.cardNo = cardNo;
}
public String getCardNo() {
return this.cardNo;
}
public void setIdentify(String identify) {
this.identify = identify;
}
public String getIdentify() {
return this.identify;
}
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return this.username;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return this.password;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPhone() {
return this.phone;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getBalance() {
return this.balance;
}
}
最后
以上就是优美豆芽为你收集整理的java开发——银行项目基本实现功能(基础)的全部内容,希望文章能够帮你解决java开发——银行项目基本实现功能(基础)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复