emmm

因为一个人可以有多个账户。。所以实现起来还是有点麻烦的。。

还是有点BUG。。。

而且没有写注释。。。

慢慢来吧QwQ

UML图
package base;

public class BankAccount {
    private double balance;
    private String Uname,Pwd;
    public BankAccount(String Uname,String Pwd){
        this.balance=0;
        this.Uname=Uname;
        this.Pwd=Pwd;
    }
    public boolean isValid(){
        return !Uname.equals("");
    }
    boolean Login(String Uname,String Pwd){
        return this.Uname.equals(Uname)&&this.Pwd.equals(Pwd);
    }
    double GetBalance(){
        return balance;
    }
    void Deposit(double amount){
        balance+=amount;
    }
    void Withdraw(double amount){
        balance-=amount;
    }
    public String toString(){
        return Uname+" "+balance;
    }
}
package base;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
class Failed extends Exception{
    String message;
    public Failed(String error) {
        message = error;
    }
    @Override
    public String getMessage() {
        return message;
    }
}
public class Person {

    private ArrayList<BankAccount>BA;
    private String ID;
    private BankAccount B;

    private static  BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
    private static PrintWriter stdOut = new PrintWriter(System.out, true);
    private static PrintWriter stdErr = new PrintWriter(System.err, true);

    public Person(String ID){this.ID=ID;BA=new ArrayList<BankAccount>();}
    public String GetID(){
        return ID;
    }
    private BankAccount AddAccount(String Uname,String Pwd) {
        BankAccount B=new BankAccount(Uname, Pwd);
        BA.add(B);
        return B;
    }
    public BankAccount Login(String Uname,String Pwd){
        for (BankAccount x:BA)
            if (x.Login(Uname,Pwd))
                return x;
        return new BankAccount("","");
    }
    public void Login()throws IOException{
        stdErr.println("Please Input Your Username and Password\n");
        while (true) {
            try {
                String Uname = stdIn.readLine();
                String Pwd = stdIn.readLine();
                B = Login(Uname, Pwd);
                if (B.isValid()) return;
                else throw new Failed("Wrong Username Or Password");
            }
            catch(Failed e){
                stdErr.println(e);
            }
        }
    }
    private void Create()throws IOException{
        stdErr.println("Please Input Your Username and Password\n");
        String Uname = stdIn.readLine();
        String Pwd = stdIn.readLine();
        B=AddAccount(Uname,Pwd);
        stdErr.printf("Success!\n");
    }
    private int GetChoice()throws IOException {
        while (true){
            try{
                stdOut.print("[0]  Quit\n"
                        + "[1]  Show Your Accounts\n"
                        + "[2]  Choose an ID card\n"
                        + "[3]  Create an ID card\n"
                        + "choice>");
                stdOut.flush();
                int choice=Integer.parseInt(stdIn.readLine());
                if (0<=choice&&choice<=3)return choice;
                else {
                    stdErr.println("Invalid Input\n");
                }
            }
            catch(NumberFormatException nfe){
                stdErr.println(nfe);
            }
        }
    }
    private int GetWork()throws IOException {
        while (true){
            try{
                stdOut.print("[0]  Quit\n"
                        + "[1]  Show Your Balance\n"
                        + "[2]  Deposit\n"
                        + "[3]  WithDraw\n"
                        + "choice>");
                stdOut.flush();
                int choice=Integer.parseInt(stdIn.readLine());
                if (0<=choice&&choice<=3)return choice;
                else {
                    stdErr.println("Invalid Input\n");
                    stdErr.flush();
                }
            }
            catch(NumberFormatException nfe){
                stdErr.println(nfe);
                stdErr.flush();
            }
        }
    }
    private void Work()throws IOException{
        while (true){
            int ch=GetWork();double amount;
            switch (ch){
                case 0:
                    return;
                case 1:
                    stdOut.println(B.GetBalance());
                    stdOut.flush();
                    break;
                case 2:
                    amount=Double.parseDouble(stdIn.readLine());
                    B.Deposit(amount);
                    break;
                case 3:
                    amount=Double.parseDouble(stdIn.readLine());
                    try {
                        if (B.GetBalance()<amount)throw new Failed("Insufficient Balance!");
                        B.Withdraw(amount);
                    }
                    catch(Failed e){
                        stdErr.println(e);
                        stdErr.flush();
                    }
                    break;
            }
        }
    }
    public void run()throws IOException{
        while (true){
            int ch=GetChoice();
            switch(ch){
                case 0:return;
                case 1:
                    stdOut.println(this);
                    stdOut.flush();
                    break;
                case 2:
                    Login();
                    stdOut.println("Success");
                    stdOut.println(B);
                    stdOut.flush();
                    Work();
                    break;
                case 3:
                    Create();
                    break;
            }
        }
    }
    public void Deposit(BankAccount BA,double num){
        BA.Deposit(num);
    }
    public void WithDraw(BankAccount BA,double num) {
        BA.Withdraw(num);
    }
    public double GetBalance(BankAccount BA){
        return BA.GetBalance();
    }
    public String toString(){
        String S="";
        for (BankAccount x:BA)
            S=S+x.toString()+"\n";
        return S;
    }
}
package Application;
import base.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
class Failed extends Exception{
    String message;
    public Failed(String error) {
        message = error;
    }
    @Override
    public String getMessage() {
        return message;
    }
}
public class Client {
    ArrayList<Person>Persons;
    Person P;
    private static BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
    private static PrintWriter stdOut = new PrintWriter(System.out, true);
    private static PrintWriter stdErr = new PrintWriter(System.err, true);
    public static void main(String []args)throws IOException{
        Client client=new Client();
        client.run();
    }
    private void PersonIdentity()throws IOException {
        stdOut.println();
        stdOut.print("Welcome!\nLogin Or Register?(0:Login/1:Register)\n");
        stdOut.flush();
        try {
            int Input = Integer.parseInt(stdIn.readLine());
            if (Input == 0) {
                while (true) {
                    String ID = stdIn.readLine();
                    try {
                        for (Person PP : Persons)
                            if (PP.GetID().equals(ID)) {
                                P = PP;
                                stdOut.println("Success!");
                                stdOut.flush();
                                return;
                            }
                        throw new Failed("ID wrong!");
                    } catch (Failed e) {
                        stdErr.println(e.message);
                        stdErr.flush();
                    }
                }
            }
            else if (Input==1){
                String ID=stdIn.readLine();
                P=new Person(ID);
                Persons.add(P);
                stdOut.println("Success!");
                stdOut.flush();
            }
            else{
                stdErr.println("Invalid Input!");
                stdErr.flush();
            }
        }
        catch (NumberFormatException nfe) {
            stdErr.println(nfe);
            stdErr.flush();
        }
    }
    private int GetChoice()throws IOException {
        while (true){
            try{
                stdOut.print("[0]  Quit\n"
                        + "[1]  TransAction\n"
                        + "choice>");
                stdOut.flush();
                int choice=Integer.parseInt(stdIn.readLine());
                if (0<=choice&&choice<=1)return choice;
                else {
                    stdErr.println("Invalid Input\n");
                    stdErr.flush();
                }
            }
            catch(NumberFormatException nfe){
                stdErr.println(nfe);
                stdErr.flush();
            }
        }
    }
    private void run()throws IOException{
        Persons=new ArrayList<Person>();
        while (true) {
            int ch=GetChoice();
            if (ch==0)return;
            PersonIdentity();
            P.run();
        }
    }
}

告别纷扰,去寻找生活的宝藏。