java系统系统异常处理,银行系统(Java)异常处理
package analog_ATM;
import java.io.IOException;
//import java.sql.*;
import java.util.*;
/**
* 1.賬戶類,模擬ATM機,即讀取了用戶的個人信息
* 2.查詢余額
* 3.交易記錄
* 4.存/取
* 5.其他功能
*/
class InsufficientFundsException extends Exception{//自定義異常類
Account account; //貌似有危險
private double dAmount;
InsufficientFundsException(){} //無參
InsufficientFundsException(Account account, double dAmount){
this.account = account;
this.dAmount = dAmount;
}
public String getMessage(){ //錯誤信息
String str = "取款失敗!賬戶余額:" + account.getBalance()+ ",取款額:" + dAmount + ",余額不足";
return str;
}
}
class Account{
boolean isUseful = true; //卡是否可用
String cardNumber; //4位數的卡號,由銀行按照規定分配
String userName; //用戶名
String password; //密碼
double balance; //余額
Account(){
this.isUseful = true;
this.balance = 0.0;
}
Account(String cardNumber, String userName, String password){
this.isUseful = true;
this.balance = 0.0;
this.cardNumber = cardNumber;
this.userName = userName;
this.password = password;
}
//
//public void setCardNumber() {
//Random rnd = new Random();
//rnd.setSeed(9999);
//}
//
public void setUserName() throws IOException{//cin exception
Scanner sc = new Scanner(System.in);
System.out.println("請輸入您的用戶名:");
this.userName = sc.nextLine();
}
//
public void setPassword() throws IOException {//cin exception
Scanner sc = new Scanner(System.in);
System.out.println("請輸入您的密碼:");
this.password = sc.nextLine();
}
//查余額
public double getBalance(){
return this.balance;
}
//取
public void withdraw(double money) throws InsufficientFundsException{
if (balance < money) {
throw new InsufficientFundsException(this, money);
}
balance = balance - money;
System.out.println("取款成功!賬戶余額為" + balance);
}
//查記錄
public void recordsInfo() {
}
//存
public void saveMoney(double money) {
this.balance += money;
System.out.println("存款成功!新余額為:"+this.balance);
}
}
//銀行系統
class Bank{
Account card[];
static int current_account_index = 0;
static int accountNumber = 0; //賬戶數量
static int illegalCardNumber = 0; //凍結數量
Bank(){
card = new Account[1];
card[0] = new Account("1000", "root", "password");
accountNumber ++;
}
void addAccount() throws IOException, InsufficientFundsException {
Account [] temp = card;
card = new Account[accountNumber + 1];
for(int i = 0; i
總結
以上是生活随笔為你收集整理的java系统系统异常处理,银行系统(Java)异常处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 循环do while,Java
- 下一篇: matlab做计算器纯代码,**matl