Java-银行案例-学习十二天Java的知识应用


功能上实现了:开户、登录、验证、存款、取款、转账、修改密码、修改手机号、注销账户。
因目前所有知识有限,并未实现数据的有效存储,只能在代码内部实现数据流通。

TestBank.java 测试Bank类

package com.qf.bank;

/*
 * 测试银行的类
 * */

public class TestBank {

	public static void main(String[] args) {
		
		//0.完成开户的操作(卡号、身份证号、用户名、密码、手机号、余额)
		
		//1.输入卡号、密码
		
		//2.卡密的校验,成功展示菜单,失败重新输入
		
		//3.显示showMenu()菜单
		
		//4.执行功能
		
		//程序的起始
		Bank bank = new Bank();
		
		bank.index();
	}

}

Bank.java 银行类

package com.qf.bank;

import java.util.Scanner;

/*
 * 银行类
 * */
public class Bank {
	Scanner input = new Scanner(System.in);//声明在类中,全类共享
	
	User[] users = new User[5];//存用户
	
	//初始化信息
	public void initial() {
		users[0] = new User("622202020001" , "413026197101035322" , "张三" , "123456" , "13345677654" , 5000.0);
		users[1] = new User("622202020002" , "413026196803139564" , "李四" , "123456" , "13445677654" , 6000.0);
		users[2] = new User("622202020003" , "413026199912255463" , "王五" , "123456" , "13545677654" , 7000.0);
	}
	
	//首页
	public void index() {
		this.initial();//初始化数据
		do {
			System.out.println("----欢迎使用ATM自动银行系统----");
			System.out.print("请选择1.登录  2.开户 0.退出系统:");
			int choice = input.nextInt();
			switch(choice){
				case 1:
					login();
					break;
				case 2:
					this.openAccount();//开户功能
					break;
				case 0:
					exit();
					return;
				default:
					System.out.println("输入错误,请重新输入!\n");
					break;
			}
		}while(true);
	
	
	}
	
	//登录
	public void login() {
		
		//用户输入卡号、密码
		do {
			System.out.print("请输入卡号:");
			String no = input.next();
			
			System.out.print("请输入密码:");
			String pwd = input.next();
			
			User u = check(no , pwd);//验证卡密
			
			if(u != null) {
				System.out.println("登录成功,欢迎您"+ u.getUsername() +"\n正在进入菜单。。。");
				this.showMenu(u);//展示菜单
				return;
			}else {
				System.out.println("卡号或密码输入错误,请重新输入!\n");
			}
		}while(true);
	}
	
	//验证
	public User check(String no , String pwd) {
		for(int i = 0 ; i < users.length;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 mine) {
		
		do {
			System.out.println("\n--------------------欢迎使用ATM自动银行系统--------------------");
			System.out.println("1.存款  2.取款  3.转账  4.查询余额  5.修改密码  6.修改预留手机号  7.注销账户    0.退出");
			System.out.println("-----------------------------------------------------------");
			System.out.print("请输入操作编号:");
			int choice = input.nextInt();
			switch(choice){
				case 1:
					this.deposit(mine);//存款功能
					System.out.println();
					break;
				case 2:
					this.withdrawal(mine);//取款功能
					System.out.println();
					break;
				case 3:
					this.transfer(mine);//转账功能
					System.out.println();
					break;
				case 4:
					this.query(mine);//查询余额
					System.out.println();
					break;
				case 5:
					this.changePwd(mine);//修改密码
					System.out.println();
					break;
				case 6:
					this.changePhone(mine);
					System.out.println();
					break;
				case 7:
					this.logout(mine);
					System.out.println();
					return;
				case 0:
					System.out.println();
					return;//退出整个方法
				default:
					System.out.println("输入错误,请重新输入!\n");
			}
		}while(true);
	}
	
	//开户
	public void openAccount(){
		int i;
		for(i = 0; i < users.length;i++) {
			if(users[i] == null) {
				break;
			}
		}
		System.out.print("请输入身份证号:");
		String id = input.next();
		System.out.print("请输入姓名:");
		String name = input.next();
		System.out.print("请输入密码:");
		String pwd = input.next();
		System.out.print("请输入预留手机号:");
		String phone = input.next();
		double balance = 0D;
		
		users[i] = new User(this.generateCordNo() , id , name , pwd , phone , balance);
		
		System.out.println("您的账户信息为:\n卡号:"+users[i].getCardNo()+"\n身份证号:"+users[i].getIdentity()+"\n姓名:"+users[i].getUsername()+"\n密码:"+users[i].getPassword()+"\n预留手机号:"+users[i].getPhone()+"\n余额:"+users[i].getBalance() );
	}
	
	//自动生成卡号
	public String generateCordNo(){
		String num1 = "62220202";
		int num2 = (int)(Math.random() * 10000);
		String nums = num1 + num2;
		return nums;
	}
	
	//存款功能
	public void deposit(User mine) {
		do {
			System.out.print("请输入存款金额:");
			double money = input.nextDouble();
			mine.setBalance( mine.getBalance() + money );
			System.out.println("存款成功,余额为:"+ mine.getBalance());
			boolean flag = this.judge();//判断是否继续存款
			if(flag == false) {
				return;
			}
		}while(true);
	}

	//取款功能
	public void withdrawal(User mine) {
		do {
			System.out.print("请输入取款金额:");
			double money = input.nextDouble();
			if( money <= mine.getBalance() ) {
				mine.setBalance( mine.getBalance() - money );
				System.out.println("取款成功,余额为:"+ mine.getBalance());
			}else {
				System.out.println("余额不足!");
			}
			boolean flag = this.judge();//判断是否继续取款
			if(flag == false) {
				return;
			}
		}while(true);
	}
	
	//转账功能
	public void transfer(User mine) {
		do {
			System.out.print("请输入对方卡号:");
			String no = input.next();
			System.out.print("请输入对方姓名:");
			String name = input.next();
			User u = this.checkCardNo(no, name);//判断对方账户是否输入正确
			if(u != null) {
				System.out.print("请输入转账金额:");
				double money = input.nextDouble();
				if( money <= mine.getBalance() ) {
					mine.setBalance( mine.getBalance() - money );
					u.setBalance( u.getBalance() + money );
					System.out.println("转账成功,余额为:"+ mine.getBalance());
				}else {
					System.out.println("余额不足!");
				}
			}else {
				System.out.println("账户或姓名错误");
			}
			boolean flag = this.judge();//判断是否继续输入账户
			if(flag == false) {
				return;
			}
		}while(true);
	}
	
	//验证对方账户
	//验证对方账户
	public User checkCardNo(String no , String name) {
		for(int i = 0 ; i < users.length;i++) {
			if(users[i] != null) {
				if( no.equals( users[i].getCardNo() ) && name.equals( users[i].getUsername() ) ) {
					return users[i];
				}
			}
		}
		return null;
	}
	
	//查询余额
	public void query(User mine) {
		System.out.println("当前余额为:"+mine.getBalance());
	}
	
	//修改密码
	public void changePwd(User mine) {
		System.out.print("请输入旧密码:");
		String oldpwd = input.next();
		if( oldpwd.equals( mine.getPassword() ) ) {
			System.out.print("请输入新密码:");
			String newpwd = input.next();
			mine.setPassword( newpwd );
			System.out.println("密码修改成功!\n");
		}else {
			System.out.println("密码输入错误!\n");
		}
	}
	
	//修改预留手机号
	public void changePhone(User mine) {
		System.out.print("请输入密码:");
		String pwd = input.next();
		if( pwd.equals( mine.getPassword() ) ) {
			System.out.print("请输入新手机号:");
			String newphone = input.next();
			mine.setPhone( newphone );
			System.out.println("手机号修改成功!\n");
		}else {
			System.out.println("密码输入错误!\n");
		}
	}
		
	//注销账户
	public void logout(User mine) {
		System.out.println("请确认是否注销账户(y/n):");
		char choice = input.next().charAt(0);
		if(choice == 'n') {
			System.out.println("退出注销\n");
			return;
		}else if(choice != 'y') {
			System.out.println("输入有误,退出注销\n");
			return;
		}
		int i;
		for(i = 0 ; i < users.length;i++) {//找到当前账户在数组中的下标
			if(mine.getCardNo() == users[i].getCardNo()) {
				break;
			}
		}
		if(i != users.length - 1) {
			for(int j = i ; j < users.length - 1;j++) {//通过赋值注销账户
				users[j] = users[j+1];
			}
		}else {
			users[i] = new User();
		}
		System.out.println("注销完成!");
	}
		
	//判断是否继续
	public boolean judge() {
		System.out.print("\n是否继续(y/n):");
		char choice = input.next().charAt(0);
		if(choice == 'y') {
			return true;
		}else if(choice == 'n'){
			return false;
		}else {
			System.out.println("输入错误,请重新输入!");
			this.judge();//调用本身,达到循环
		}
		return false;
	}
	
	//退出提示
	//退出功能
	public void exit() {
		System.out.println("\n欢迎再次使用本系统,再见!");
	}
}

User.java 用户类

package com.qf.bank;

/*用户类*/

public class User {
	private String cardNo;//卡号
	private String identity;//身份证号
	private String username;//用户名
	private String password;//密码
	private String phone;//预留手机号
	private double balance;//余额
	
	//无参的构造方法
	public User() {}
	
	
	//6参的构造方法
	public User(String cardNo , String identity , String username , String password , String phone , double balance) {
		
		this.cardNo = cardNo;
		this.identity = identity;
		this.username = username;
		this.password = password;
		this.phone = phone;
		this.balance = balance;
	}
	
	//cardNo的存取方法
	public void setCardNo(String cardNo) {
		this.cardNo = cardNo;
	}
	public String getCardNo() {
		return this.cardNo;
	}
	
	//identity的存取方法
	public void setIdentity(String identity) {
		this.identity = identity;
	}
	public String getIdentity() {
		return identity;
	}

	//username的存取方法
	public void setUsername(String username) {
		this.username = username;
	}
	public String getUsername() {
		return this.username;
	}
	
	//password的存取方法
	public String getPassword() {
		return this.password;
	}
	public void setPassword(String password) {
		this.password = password;
	}

	//phone的存取方法
	public String getPhone() {
		return this.phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}

	//balance的存取方法
	public double getBalance() {
		return this.balance;
	}
	public void setBalance(double balance) {
		this.balance = balance;
	}

}
发布了34 篇原创文章 · 获赞 7 · 访问量 1304

猜你喜欢

转载自blog.csdn.net/weixin_44257082/article/details/104381708