?? ex1.java
字號:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package chenchao;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() { 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("Enter your choose:"); 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 character 'q' to exit! *"); while(!(sc.nextInt() == (int) 'q')) System.out.println("* You must input 'q' 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 o = new Log(this, opType, amount, new Date()); log.add(o); isSuccess = true; break; case DEBIT: this.setBalance(beforeOperate - amount); 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 + -