⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex1.java

📁 讲述各种各样的java初始编程 了解编程
💻 JAVA
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package chenchao2;import java.util.*;/** * * @author qjxy */public class Ex1 {    Account testAccount;    ArrayList<Log> log;    Scanner sc = new Scanner(System.in);    public Ex1() {        log = new ArrayList<Log>();        testAccount = new Account("0001", "Test", new Date(), "123456789111111111", 500, log);    }    public void doTest() {        while (true) {            printWelcome(testAccount);            switch (operateChooser()) {                case 1:                    float amount1 = inputAmount();                    testAccount.doTransaction(amount1, Log.OperateType.CREDIT);                    break;                case 2:                    float amount2 = inputAmount();                    testAccount.doTransaction(amount2, Log.OperateType.DEBIT);                    break;                default:                    break;            }            sayBye();        }    }    private void printWelcome(Account account) {        System.out.println("******** Welcome to Test Bank System ********");        System.out.println("* Your Account is:                          *");        System.out.println(account.toString());        System.out.println("*********************************************");    }    private int operateChooser() {        System.out.println("******** Please Choose your Operation ********");        System.out.println("* (1) CREDIT                     (2) DEBIT   *");        System.out.println();        int choose;        while (true) {            choose = sc.nextInt();            if (choose > 2 || choose < 1) {                System.out.println("* Error: only 1 or 2 can be input, plaease reinput!*");            } else {                break;            }        }        return choose;    }    private float inputAmount() {        System.out.println("******** Please Input the amount you want to operate ********");        System.out.println();        float amount = sc.nextFloat();        return amount;    }    private void sayBye() {        System.out.println("******** Thank you for choose our Service System ********");        System.out.println(testAccount.toString());        System.out.println("* Please input word [quit] to exit!                   *");        String isQuit = sc.next();        while (true) {            if (isQuit.matches("quit")) {                System.exit(0);            } else {                System.out.println("* You must input [quit] to exit!                           *");            }        }    }    public static void main(String[] args) {        Ex1 test = new Ex1();        test.doTest();    }}class Account {    private String number;    private String name;    private Date createDate;    private String creditNo;    private float balance;    private ArrayList<Log> log;    public Account(String number, String name, Date createDate, String creditNo, float balance, ArrayList<Log> log) {        this.number = number;        this.name = name;        this.createDate = createDate;        this.creditNo = creditNo;        this.balance = balance;        this.log = log;    }    public boolean doTransaction(float amount, Log.OperateType opType) {        boolean isSuccess = false;        float beforeOperate = this.getBalance();        switch (opType) {            case CREDIT:                this.setBalance(beforeOperate + amount);                Log o1 = new Log(this, opType, amount, new Date());                log.add(o1);                isSuccess = true;                break;            case DEBIT:                this.setBalance(beforeOperate - amount);                Log o2 = new Log(this, opType, amount, new Date());                isSuccess = true;                break;            default:                isSuccess = false;        }        return isSuccess;    }    public float getBalance() {        return balance;    }    public void setBalance(float balance) {        this.balance = balance;    }    public Date getCreateDate() {        return createDate;    }    public void setCreateDate(Date createDate) {        this.createDate = createDate;    }    public String getCreditNo() {        return creditNo;    }    public void setCreditNo(String creditNo) {        this.creditNo = creditNo;    }    public ArrayList<Log> getLog() {        return log;    }    public void setLog(ArrayList<Log> log) {        this.log = log;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getNumber() {        return number;    }    public void setNumber(String number) {        this.number = number;    }    @Override    public String toString() {        String s = new String();        s = s + "账号:\t" + this.getNumber() + '\n';        s = s + "户名:\t" + this.getName() + '\n';        s = s + "余额:\t" + this.getBalance() + '\n';        s = s + "创建日期:\t" + this.getCreateDate() + '\n';        s = s + "身份证号:\t" + this.getCreditNo() + '\n';        s = s + "操作日志:\t" + this.getLog() + '\n';        return s;    }}class Log {    private Date operateDate;    private float amount;    private Account account;    private OperateType operateType;    enum OperateType {        CREDIT, DEBIT    }    public Log(Account account, OperateType operateType, float amount, Date operateDate) {        this.operateDate = operateDate;        this.amount = amount;        this.account = account;        this.operateType = operateType;    }    public OperateType getOperateType() {        return operateType;    }    public void setOperateType(OperateType operateType) {        this.operateType = operateType;    }    public Account getAccount() {        return account;    }    public void setAccount(Account account) {        this.account = account;    }    public float getAmount() {        return amount;    }    public void setAmount(float amount) {        this.amount = amount;    }    public Date getOperateDate() {        return operateDate;    }    public void setOperateDate(Date operateDate) {        this.operateDate = operateDate;    }    @Override    public String toString() {        String s = new String();        s = s + "操作金额:\t" + this.getAmount() + '\n';        s = s + "操作类型:\t" + this.getOperateType() + '\n';        s = s + "操作日期:\t" + this.getOperateDate() + '\n';        return s;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -