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;
	}

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章