1. 程式人生 > >java 實現簡單的銀行登入註冊退出功能(未儲存資料)

java 實現簡單的銀行登入註冊退出功能(未儲存資料)

RegisterAndLogin.java:

package bao1;

import java.util.Scanner;
import java.util.regex.Pattern;
import  bao1.Login;
public class RegisterAndLogin {
    static Scanner sc= new Scanner(System.in); //個人資訊
    static String name;
    static int age;
    static String sex;
    static String pass;
    static String id;
    static long cardId;
    
    //控制檯 主體邏輯
    public static void main(String[] args) throws InterruptedException {
        Control();
    }
    public static void Control() throws InterruptedException{
        menu();
        System.out.println("請選擇功能");
        int oper = sc.nextInt();
        switch(oper){
        case 1:
            register();
            break;
        case 2:
            //登入
            login();
            break;
        case 3:
            System.exit(0);
            //退出
            break;
        default:
            System.out.println("功能鍵選擇錯誤,請重新輸入:");
            Control();
        }
    }
    public static void menu(){
        System.out.println("1.註冊");
        System.out.println("2.登入");
        System.out.println("3.退出");
    }
    public static void register() throws InterruptedException{
        String pass_next;
        System.out.println("請輸入姓名:");
        name = sc.next();
        Pattern pattern_name = Pattern.compile("[0-9]*");
        while(pattern_name.matcher(name).matches()){
            System.out.println("請確認姓名格式正確");
            name = sc.next();
        }
        System.out.println("請輸入年齡:");
        age = sc.nextInt();
        while(age < 18 || age >60){
            System.out.println("年齡不滿足,重新輸入");
            age = sc.nextInt();
        }
        System.out.println("請輸入性別(男或女):");
        sex = sc.next();
        while(!(sex.equals("男")) & !(sex.equals("女"))){
            System.out.println("請重新輸入性別(男或女):");
            sex = sc.next();
        }
        System.out.println("請輸入密碼:");
        pass = sc.next();
        Pattern pattern_pass = Pattern.compile("[0-9]*");
        while(!(pattern_pass.matcher(pass).matches()) || pass.length()!=6){
            System.out.println("請確認密格式正確!(六位數字)");
            pass = sc.next();
        }
        System.out.println("請確認密碼:");
        pass_next = sc.next();
        while(!(pass_next.equals(pass))){
            System.out.println("請重新確認密碼");
            pass_next = sc.next();
        }
        System.out.println("請輸入身份證號:");
        id = sc.next();    
        while(id.length()!=18){//判斷不準確
            System.out.println("請重新輸入身份證號");
            id = sc.next();    
        }
        cardId = (long)((Math.random()*9+1)*100000);
        System.out.println("註冊成功,您的卡號為:"+ cardId);
        login();
        
    }
    public static void login() throws InterruptedException{
        System.out.println("請輸入卡號");
        long cardId1 = sc.nextLong();
        while( cardId1 != cardId){
            System.out.println("請重新輸入卡號");
            cardId1 =  sc.nextLong();
        }
        System.out.println("請輸入密碼");
        String pa = sc.next();
        while(!(pa.equals(pass))){
            System.out.println("請重新輸入密碼");
            pa =  sc.next();
        }
        
        System.out.println("登入成功!");
        Login.loginView();
        
    }
    public static String getName() {
        return name;
    }
    public static void setName(String name) {
        RegisterAndLogin.name = name;
    }
    public static int getAge() {
        return age;
    }
    public static void setAge(int age) {
        RegisterAndLogin.age = age;
    }
    public static String getSex() {
        return sex;
    }
    public static void setSex(String sex) {
        RegisterAndLogin.sex = sex;
    }
    public static String getPass() {
        return pass;
    }
    public static void setPass(String pass) {
        RegisterAndLogin.pass = pass;
    }
    public static String getId() {
        return id;
    }
    public static void setId(String id) {
        RegisterAndLogin.id = id;
    }
    public static long getCardId() {
        return cardId;
    }
    public static void setCardId(long cardId) {
        RegisterAndLogin.cardId = cardId;
    }

}

Login.java:

package bao1;
import java.util.Scanner;
import java.util.regex.Pattern;

import bao1.RegisterAndLogin;
public class Login {
    static Scanner sc= new Scanner(System.in); //個人資訊
    static long saveMoney;
    static long getMoney;
    static double moneyInBank = 500;
    public static void main(String[] args) {
        
    }
    public static void loginView() throws InterruptedException{
        System.out.println("1.存款");
        System.out.println("2.取款");
        System.out.println("3.修改密碼");
        System.out.println("4.返回上一級");
        System.out.println("5.退出");
        System.out.println("6.顯示餘額");
        System.out.println("請輸入功能編號");
        int open = sc.nextInt();
        switch(open){
        case 1:
            saveMoney();
            break;
        case 2:
            getMoney();
            break;
        case 3:
            modify();
            break;
        case 4:
            backLogin();
            break;
        case 5:
            quit();
            break;
        case 6:
            bankMoney();
            break;
        default:
            System.out.println("請重新輸入功能編號");
            loginView();
            
        }
    }
    public static void quit() throws InterruptedException{
        System.out.println("退出!");
        System.exit(0);
    }
    public static void modify() throws InterruptedException{
         System.out.println("請輸入舊密碼:");
         String old = sc.next();
         while(!(old.equals(RegisterAndLogin.getPass()))){
             System.out.println("密碼輸入錯誤,請重新輸入:");
             old = sc.next();
         }
         System.out.println("請輸入新密碼:");
         String newpass = sc.next();
         Pattern pattern_pass = Pattern.compile("[0-9]*");
        while(!(pattern_pass.matcher(newpass).matches()) || newpass.length()!=6){
            System.out.println("請確認密格式正確!(六位數字)");
            newpass = sc.next();
        }
        RegisterAndLogin.setPass(newpass);
        RegisterAndLogin.login();
    }
    public static void backLogin() throws InterruptedException{
        RegisterAndLogin.login();
    }
    public static void back() throws InterruptedException{
        loginView();
    }
    public static void saveMoney() throws InterruptedException{
        System.out.println("輸入儲存金額(只能存整百)");
        saveMoney = sc.nextLong();
        while(saveMoney % 100 != 0 || saveMoney < 0){
            System.out.println("請重新輸入儲存金額(只能存整百)");
            saveMoney = sc.nextLong();
        }
        moneyInBank+=saveMoney;
        System.out.println("餘額:" + moneyInBank);
        Thread.sleep(1000);
        System.out.println("跳轉頁面:");
        System.out.println("1.退出");
        System.out.println("2.返回上一級");
        System.out.println("請輸入功能編號");
        int number = sc.nextInt();
        while(true){
            if(number == 1){
                Login.quit();
                break;
            }
            else if(number == 2){
                Login.back();
                break;
            }
            else{
                System.out.println("請重新輸入功能編號");
                number = sc.nextInt();
            }
            
        }
        
    }
    public static void getMoney() throws InterruptedException{
        System.out.println("您的餘額:"+moneyInBank);
        System.out.println("輸入取款金額:");
        getMoney = sc.nextLong();
        while(getMoney > moneyInBank){
            System.out.println("餘額不足,請重新輸入取款金額:");
            getMoney = sc.nextLong();
        }
        moneyInBank-=getMoney;
        System.out.println("您的餘額為:" + moneyInBank);
        System.out.println("跳轉頁面:");
        System.out.println("1.退出");
        System.out.println("2.返回上一級");
        System.out.println("請輸入功能編號");
        int number = sc.nextInt();
        while(true){
            if(number == 1){
                Login.quit();
                break;
            }
            else if(number == 2){
                Login.back();
                break;
            }
            else{
                System.out.println("請重新輸入功能編號");
                number = sc.nextInt();
            }
            
        }
                
    }
    public static void bankMoney() throws InterruptedException{
        System.out.println("您的餘額為:"+ moneyInBank);
        System.out.println("跳轉頁面:");
        System.out.println("1.退出");
        System.out.println("2.返回上一級");
        System.out.println("請輸入功能編號");
        int number = sc.nextInt();
        while(true){
            if(number == 1){
                Login.quit();
                break;
            }
            else if(number == 2){
                Login.back();
                break;
            }
            else{
                System.out.println("請重新輸入功能編號");
                number = sc.nextInt();
            }
            
        }
        
    }
   
}