Ⅰ 利用java線程技術模擬銀行中用戶的存取款的設計
肯定是可視化程序,不然怎麼可以有用戶數據,每個用戶銀行數據。
就跟用銀行卡取錢一樣,你把你的磁卡插入了,讀取你的信息,才可以查詢自己信息,取錢
即你用戶登陸後就可以查看自己的信息什麼的。具體怎麼實現 就需要看你用什麼技術了
Ⅱ JAVA 編程創建Account 類模擬銀行賬戶。
我幫你一共寫了四個類,一個賬戶Account類,兩個賬戶類的子類(信用卡賬戶CreditCardAccount類和借記卡DebitCardAccount類),另外再加上一個演示透支效果測試Test類。 代碼貼在下面: 
/**
 * 賬戶類。
 * 
 * @author CodingMouse
 * @version 1.0
 */
public abstract class Account { protected String accountNumber;       // 賬號
 protected double overage;             // 余額
 protected double annualInterestRate;  // 年利率
 
 /**
  * 參數化構造器方法。
  * 
  * 用於開戶。
  * @param accountNumber 預設賬號
  * @param overage 初始余額
  * @param annualInterestRate 預設年利率
  */
 public Account(String accountNumber, double overage, double annualInterestRate) {
  super();
  // 設定賬號。
  this.accountNumber = accountNumber;
  // 設定初始余額,至少為零。
  this.overage = overage >= 0 ? overage : 0;
  // 設定年利率,至少為零。
  this.annualInterestRate = annualInterestRate >= 0 ? annualInterestRate : 0;
 }
 
 /**
  * 查詢賬號。
  * @return 返回賬號。
  */
 public String getAccountNumber() {
  return this.accountNumber;
 }
 
 /**
  * 設置賬號。
  * @param accountNumber 新賬號。
  */
 public void setAccountNumber(String accountNumber) {
  this.accountNumber = accountNumber;
 } /**
  * 查詢余額方法。
  * @return 返回賬戶余額。
  */
 public double getOverage() {
  return this.overage;
 } /**
  * 存款方法。
  * @param money 要存入的金額。
  * @return 返回true時存款成功,否則存款失敗。
  */
 public boolean depositMoney(double money) {
  // 如果金額小於零,則不能存款
  if (money <= 0)
   return false;
  // 否則將相應的金額累加到賬戶余額中
  this.overage += money;
  return true;
 }
 
 /**
  * 取款方法。
  * 
  * 默認不能透支。
  * @param money 要取出的金額。
  * @return 返回true時取款成功,否則取款失敗。
  */
 public boolean drawMoney(double money) {
  // 如果賬戶余額不足,則不能取款
  if (this.overage < money)
   return false;
  // 否則從賬戶余額中扣除相應的金額
  this.overage -= money;
  return true;
 } /**
  * 查詢年利率。
  * @return 返回年利率。
  */
 public double getAnnualInterestRate() {
  return this.annualInterestRate;
 } /**
  * 設置年利率。
  * @param annualInterestRate 新的年利率。
  */
 public void setAnnualInterestRate(double annualInterestRate) {
  this.annualInterestRate = annualInterestRate;
 }
 
}
 -------------------------------------------------- 
/**
 * 借記卡賬戶。
 * 
 * 不能透支。
 * @author CodingMouse
 * @version 1.0
 */
public class DebitCardAccount extends Account { /**
  * 重寫父類構造器。
  * @param accountNumber 預設賬號
  * @param overage 初始余額
  * @param annualInterestRate 預設年利率
  */
 public DebitCardAccount(String accountNumber, double overage,
   double annualInterestRate) {
  super(accountNumber, overage, annualInterestRate);
 }}
 -------------------------------------------------
/**
 * 信用卡賬戶。
 * 
 * 可以透支。
 * @author CodingMouse
 * @version 1.0
 */
public class CreditCardAccount extends Account { private double overdraftLimit;  // 透支限度
/**
  * 重載構造器。
  * 
  * 便於構建可透支的信用卡賬戶實例。
  * @param accountNumber 預設賬號
  * @param overage 初始余額
  * @param annualInterestRate 預設年利率
  * @param overdraftLimit 透支限度
  */
 public CreditCardAccount(String accountNumber, double overage,
   double annualInterestRate, double overdraftLimit) {
  super(accountNumber, overage, annualInterestRate);
  this.overdraftLimit = overdraftLimit;
 } /**
  * 查詢透支限度的方法。
  * @return 透支限度金額。
  */
 public double getOverdraftLimit() {
  return this.overdraftLimit;
 } /**
  * 設置透支限度的方法。
  * @param overdraftLimit 新的透支限度金額。
  */
 public void setOverdraftLimit(double overdraftLimit) {
  // 透支限度必須為零和正數,否則為零。
  this.overdraftLimit = overdraftLimit >= 0 ? overdraftLimit : 0;
 } /**
  * 重寫父類構造器。
  * @param accountNumber 預設賬號
  * @param overage 初始余額
  * @param annualInterestRate 預設年利率
  */
 public CreditCardAccount(String accountNumber, double overage,
   double annualInterestRate) {
  super(accountNumber, overage, annualInterestRate);
 } /**
  * 重寫父類取款方法。
  * 
  * 將默認不能透支的取款改為可以透支的取款。
  * @param money 要取出的金額。
  * @return 返回true時取款成功,否則取款失敗。
  */
 @Override
 public boolean drawMoney(double money) {
  // 如果賬戶余額 + 透支限度的總金額仍不足,則不能取款
  if (this.overage + this.overdraftLimit < money)
   return false;
  // 否則從賬戶余額中扣除相應的金額
  this.overage -= money;
  return true;
 }}
 ------------------------------------------ 
/**
 * 測試賬戶使用。
 * 
 * @author CodingMouse
 * @version 1.0
 */
public class Test { /**
  * 主程序方法。
  * @param args 入口參數。
  */
 public static void main(String[] args) {
  
  // 創建一個不能透支的借記卡賬戶。
  System.out.println("------------  借記卡賬戶  ------------");
  DebitCardAccount debitCardAccount = new DebitCardAccount("CHK20100117001", 100, 0.02);
  // 初始余額有100元,調用並列印取90元和取120元的結果。
  System.out.println("取90元的結果:" + debitCardAccount.drawMoney(90));
  // 重新存入90元
  debitCardAccount.depositMoney(90);
  System.out.println("取120元的結果:" + debitCardAccount.drawMoney(120));
  
  // 創建一個可以透支的信用卡賬戶。
  System.out.println("------------  信用卡賬戶  ------------");
  CreditCardAccount crebitCardAccount = new CreditCardAccount("CHK20100117002", 100, 0.03, 50);
  // 初始余額有100元,並且透支限度為50元,調用並列印取90元、取120元和取160元的結果。
  System.out.println("取90元的結果:" + crebitCardAccount.drawMoney(90));
  // 重新存入90元
  crebitCardAccount.depositMoney(90);
  System.out.println("取120元的結果:" + crebitCardAccount.drawMoney(120));
  // 重新存入120元
  crebitCardAccount.depositMoney(120);
  System.out.println("取160元的結果:" + crebitCardAccount.drawMoney(160));
  
 }
 
}
 -------------------------------------- 在我機器上的測試列印結果為: ------------  借記卡賬戶  ------------
取90元的結果:true
取120元的結果:false
------------  信用卡賬戶  ------------
取90元的結果:true
取120元的結果:true
取160元的結果:false ----------------------------------------- 這個題目只是簡單考查了下封裝、繼承、多態三大面向對象特性,我提供的代碼僅作參考。 如果你懶得復制,想要程序源代碼文件或有不明白的地方,可以發郵件到我QQ郵箱,我再回給你。
Ⅲ 用JAVA編程設計一個銀行賬戶類,其中包括以下內容,並用字元界面模擬存款和取款過程。
import java.util.Scanner;
public class ZH {
private String zh;//賬戶
private String password;//密碼
private String openTime;//開戶時間
private String sfz;//身份證號
private double je;//存款金額
public String getZh() {
return zh;
}
public void setZh(String zh) {
this.zh = zh;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getOpenTime() {
return openTime;
}
public void setOpenTime(String openTime) {
this.openTime = openTime;
}
public String getSfz() {
return sfz;
}
public void setSfz(String sfz) {
this.sfz = sfz;
}
public double getJe() {
return je;
}
public void setJe(double je) {
this.je = je;
}
//存款方法
public void ck(double je){
this.je=this.je+je;//存入的金額加上原有的金額
}
//取款方法
public void qk(double je){
if(je>this.je){//取款金額大於余額
System.out.println("存款余額不足");
}else{
this.je=this.je-je;//原有的金額減去取出的金額
}
}
public static void main(String[] args) {
ZH zh = new ZH();//初始化一個賬戶信息
zh.setJe(10000.0);//向zh賬戶添加余額
zh.setOpenTime("2013.12.3");//向zh賬戶添加開發時間
zh.setPassword("123456");//向zh賬戶添加密碼
zh.setSfz("123456789");//向zh賬戶添加身份證
zh.setZh("zhangsan");//向zh賬戶添加賬號
System.out.println("歡迎光臨模擬銀行");
Scanner scan = new Scanner(System.in);
int count=0;//記錄輸入錯誤的次數
while(1==1){//循環
System.out.println("請輸入賬號");
String zhm=scan.next();
System.out.println("請輸入密碼");
String mm=scan.next();
if(zhm.equals(zh.getZh()) && mm.equals(zh.getPassword())){//輸入的信息與zh賬戶信息的密碼和賬號一致
while(1==1){
System.out.println("當前余額為:"+zh.getJe()+"元。請選擇操作:1.存款;2.取款;3.退出(只能輸入數字)");
String cz=scan.next();
switch (Integer.parseInt(cz)) {
case 1:
System.out.println("請輸入存款金額(輸入小數)");
double ckje=scan.nextDouble();
zh.ck(ckje);
System.out.println("實施存款:"+ckje+"元,當前余額為"+zh.getJe()+"元");
break;
case 2:
System.out.println("請輸入取款金額(輸入小數)");
double qkje=scan.nextDouble();
zh.qk(qkje);
System.out.println("實施取款:"+qkje+"元,當前余額為"+zh.getJe()+"元");
break;
case 3:
break;
default:
System.out.println("暫無此功能");//輸入1或者2、3以外的操作
break;
}
if("3".equals(cz)){
break;
}
}
System.out.println("退出操作");
break;
}else{
if(count>=3){
System.out.println("已輸入錯誤三次,賬號被鎖");
break;//結束循環
}else{
System.out.println("賬號或密碼錯誤,請重新輸入");
count++;//錯誤一次count+1
continue;//進入下次循環
}
}
}
}
}

Ⅳ JAVA編寫銀行賬戶程序摸擬銀行賬戶的存\取款操作
public class ATM {
    public static void main(String[] args) {
        // 開立帳號
        Account account = new Account();
        // 在 account 中存 10,000 元
        account.setBalance(10000); 
        // 檢查 account 中的存款
        System.out.println("帳戶原始金額 : " + account.getBalance() + " 元");
        
        // 小明, 小華與小英一起對 account 進行提款的動作
        WithDraw s1 = new WithDraw("小明", account, 5000); // 小明 在 account 中提 5000 元
        WithDraw s2 = new WithDraw("小華", account, 2000); // 小華 在 account 中提 2000 元
        WithDraw s3 = new WithDraw("小英", account, 4000); // 小英 在 account 中提 4000 元
        s1.start();
        s2.start();
        s3.start();
        
    }
}
//帳戶
class Account {
    private int balance; // 帳戶餘額
    
    public int getBalance() { // 取得帳戶餘額
        return balance;
    }
    
    public void setBalance(int money) { // 設定帳戶餘額
        balance = money;
    }
    
    // 提款方法
    public void withDraw(Account account, int withdrawMoney) {
        
    	String tName = Thread.currentThread().getName(); // tName=提款人
        
        System.out.println(tName + " 開始提款 ... ");
        
        boolean withDrawStatus; // 提款狀態 說明:false=提款失敗, true=提款成功
        
        synchronized(ATM.class) {
            int tmpBalabce = account.getBalance(); // 取得最新帳戶餘額
            //用 for-loop 模擬提款時系統所花的時間
            for(double delay=0;delay<99999999;delay++) {
                    // ... 提款進行中 ...
            }
            tmpBalabce = tmpBalabce - withdrawMoney; // 最新帳戶餘額 - 欲提款金額 (用來判斷是否餘額足夠的依據)
            if (tmpBalabce < 0) { // 判斷是否餘額足夠
                withDrawStatus = false;
                System.out.println("....................");
                System.out.println("   帳戶餘額不足!");
                System.out.println("....................");
            } else {
                withDrawStatus = true;
                account.setBalance(tmpBalabce); // 回存account最後剩餘金額
            }
        }
        
        System.out.println(tName + "的交易單:");
        System.out.println("\t欲提款金額:" + withdrawMoney + "元");
        System.out.println("\t帳戶餘額:" + account.getBalance());
        
        if(withDrawStatus == true){
            System.out.println(tName + " 完成提款 ... ");
        } else {
            System.out.println(tName + " 提款失敗 ... ");
        }
        
        System.out.println("-------------------------------");
}
}
// 提款類別
class WithDraw extends Thread {
    private Account account;      // 帳號
    private int withdrawMoney; // 欲提款的金額
    
    // tName:執行緒名稱, account:Account物件名稱, withdrawMoney:欲提款金額
    public WithDraw(String tName, Account account, int withdrawMoney) {
        setName(tName);
        this.account = account;
        this.withdrawMoney= withdrawMoney;
    }
    
    public void run() {
    	// 執行提款動作(account:帳號, withdrawMoney 欲提款金額)
    	account.withDraw(account, withdrawMoney); // 執行提款動作
    }    
}
Ⅳ java 模擬實現銀行自動存取一體機~~求程序指導~~
1.創建資料庫test,在test中創建一張表,用戶id,用戶名,登錄密碼,用戶狀態(0為普通用戶,大於0則為vip),用戶余額(BigDecimal簡單點就用int類型)
2.寫一個用戶的javaBean
3.封裝用戶的存,取,查詢功能(存取,取出用戶余額做加減,再把值更新回去)
4.編譯用戶的登錄界面,獲取用戶輸入的id和密碼,把這個值會傳入所寫的查詢方法的參量中去,如果id和密碼都符合,那麼再判斷一下用戶狀態,進入到相應的管理界面
5.編譯管理界面,先編譯普通用戶的管理界面
6.編譯vip用戶的管理界面可以繼承普通用戶的管理界面,在面板上再加兩個按鈕就可以了。
7.至於透支和轉賬的功能,轉賬,可以要求vip用戶輸入所要轉賬用戶的id,並顯示所轉入用戶的相應信息,確認後,把原用戶的用戶余額取出來減去後,更新到資料庫,在輸入用戶的相應的加數據,這個就可以直接調用之前寫的存取方法,最後提示成功後返回原用戶的管理界面中
Ⅵ 用Java編寫銀行賬戶存取款業務,急求!!
publicclassAccount{
	
	protectedStringaccId;
	protectedStringname;
	protecteddoublemoney;
	
	publicAccount(StringaccId,Stringname){
		(accId,name,0);
	}
	
	publicAccount(StringaccId,Stringname,doublemoney){
		this.accId=accId;
		this.name=name;
		this.money=money;
	}
	
	publicvoidsaveMoney(doublemoney){
		if(money<=0){
			System.out.println("存款金額必須大於0");
		}
		this.money+=money;
		System.out.println("存款成功");
	}
	
	publicdoublegetMoney(doublemoney){
		if(money<=0){
			System.out.println("取款金額必須大於0");
			return0;
		}
		if(this.money<=money){
			System.out.println("余額不足,無法取款");
			return0;
		}
		this.money-=money;
		System.out.println("取款成功");
		returnmoney;
	}
	
	publicdoublegetBalance(){
		returnthis.money;
	}
	
	protecteddoublegetOverdraft(){
		return0;
	}
	
	//實現了equals方法,list比較時才能正確
	@Override
	publicbooleanequals(Objectobj){
		if(obj==null){
			returnfalse;
		}
		if(this==obj){
			returntrue;
		}
		if(objinstanceofAccount){
			returnthis.accId.equals(((Account)obj).accId);
		}
		returnfalse;
	}
	
	@Override
	publicStringtoString(){
		return"賬戶="+accId+",名字="+name+",余額="+money;
	}
	
}
publicclassBank{
	//Account實現了equals方法,list查找時才能正確
	privateList<Account>usersAccounts;
	publicBank(){
		usersAccounts=newArrayList<Account>();
	}
	publicvoidaddAccount(Accountaccount){
		if(usersAccounts.contains(account)){
			System.out.println("添加失敗,不能添加同樣的賬戶");
			return;
		}
		usersAccounts.add(account);
	}
	publicbooleandelAccount(Accountaccount){
		returnusersAccounts.remove(account);
	}
	publicbooleandelAccount(StringaccId){
		returndelAccount(newAccount(accId,null));
	}
	publicbooleanexistAccount(Accountaccount){
		returnusersAccounts.contains(account);
	}
	publicbooleanexistAccount(StringaccId){
		returnexistAccount(newAccount(accId,null));
	}
	
	publicAccountgetAccount(StringaccId){
		returnusersAccounts.get(usersAccounts.indexOf(newAccount(accId,null)));
	}
	publicdoublegetAllMoney(){
		//不考慮是否溢出,只是把所有用戶余額相加
		doubleresult=0;
		for(Accountaccount:usersAccounts){
			result+=account.getBalance();
		}
		returnresult;
	}
	publicdoublegetAllOverdraft(){
		//不考慮是否溢出
		doubleresult=0;
		for(Accountaccount:usersAccounts){
			result+=account.getOverdraft();
		}
		returnresult;
	}
	publicintgetAccountNum(){
		returnusersAccounts.size();
	}
	publicintgetCreditAccountNum(){
		intnum=0;
		for(Accountaccount:usersAccounts){
			//instanceof性能沒有簡單的方法快。
			if(){
				num++;
			}
		}
		returnnum;
	}
	publicintgetSavingAccountNum(){
		intnum=0;
		for(Accountaccount:usersAccounts){
			if(){
				num++;
			}
		}
		returnnum;
	}
	publicList<Account>getAllAccount(){
		returnusersAccounts;
	}
}
{
	
	privatedoubleoverdraft;
	
	publicCreditAccount(StringaccId,Stringname){
		super(accId,name);
		this.overdraft=1000;
	}
	
	publicCreditAccount(StringaccId,Stringname,doublemoney){
		this(accId,name,money,1000);
	}
	
	publicCreditAccount(StringaccId,Stringname,doublemoney,doubleoverdraft){
		super(accId,name,money);
		this.overdraft=overdraft;
	}
	
	@Override
	publicdoublegetMoney(doublemoney){
		if(money<=0){
			System.out.println("取款金額必須大於0");
			return0;
		}
		if(this.money+overdraft<=money){
			System.out.println("余額不足,無法取款");
			return0;
		}
		this.money-=money;
		System.out.println("取款成功");
		returnmoney;
	}
	
	@Override
	publicdoublegetOverdraft(){
		returnoverdraft;
	}
	
	@Override
	publicStringtoString(){
		return"賬戶="+accId+",名字="+name+",余額="+money+",透支="+overdraft;
	}
}
{
	publicSavingAccount(StringaccId,Stringname){
		super(accId,name);
	}
	publicSavingAccount(StringaccId,Stringname,doublemoney){
		super(accId,name,money);
	}
	@Override
	publicdoublegetMoney(doublemoney){
		returnsuper.getMoney(money);
	}
	@Override
	publicdoublegetOverdraft(){
		returnsuper.getOverdraft();
	}
	
}
publicclassTest{
	
	privatestaticBankbank=newBank();
	publicstaticvoidmain(String[]args){
		Test.genAccount();
		//開戶
		Accounta1=newCreditAccount("1","1",200,2000);
		Accounta2=newSavingAccount("2","2",300);
		Accounta3=newSavingAccount("3","3",400);
		Accounta4=newCreditAccount("4","4",500,2000);
		Accounta5=newCreditAccount("4","5",600,2000);//帳號4重
		bank.addAccount(a1);
		bank.addAccount(a2);
		bank.addAccount(a3);
		bank.addAccount(a4);
		bank.addAccount(a5);
		showNowAccount();
		//銷戶
		bank.delAccount("1");
		bank.delAccount("2");
		showNowAccount();
		//存款
		if(bank.existAccount("3")){
			Accountacc=bank.getAccount("3");
			acc.saveMoney(100);
		}
		showNowAccount();
		//取款
		if(bank.existAccount("3")){
			Accountacc=bank.getAccount("3");
			System.out.println("余額="+acc.getBalance());
			acc.getMoney(100);
			System.out.println("余額="+acc.getBalance());
			acc.getMoney(1000);
			System.out.println("余額="+acc.getBalance());
		}
		if(bank.existAccount("4")){
			Accountacc=bank.getAccount("4");
			System.out.println("余額="+acc.getBalance());
			acc.getMoney(100);
			System.out.println("余額="+acc.getBalance());
			acc.getMoney(1000);
			System.out.println("余額="+acc.getBalance());
			acc.getMoney(10000);
			System.out.println("余額="+acc.getBalance());
		}
		System.out.println(bank.getAccountNum());
		System.out.println(bank.getAllMoney());
		System.out.println(bank.getAllOverdraft());
		System.out.println(bank.getCreditAccountNum());
		System.out.println(bank.getSavingAccountNum());
	}
	
	publicstaticvoidgenAccount(){
		Strings="100000000000000";
		Accounta=null;
		for(inti=1;i<11;i++){
			if((i&2)==0){
				a=newCreditAccount(s+String.valueOf(i),"賬戶"+String.valueOf(i));
			}else{
				a=newSavingAccount(s+String.valueOf(i),"賬戶"+String.valueOf(i));
			}
			bank.addAccount(a);
		}
	}
	
	(){
		for(Accountaccount:bank.getAllAccount()){
			System.out.println(account);
		}
	}
}// 面向對象,看多寫多了就會了,簡單的隨便寫的,有錯誤先不用太糾結。
Ⅶ Java課程設計,模擬銀行存取業務,按照這個流程圖去做,其實最主要的是求畫圈的部分怎麼寫和它的方法。
請點擊輸入圖片描述
package com.greatwall.business.controller;
import java.math.BigDecimal;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author xysddjyt
* @since 2020/6/16 15:06
*/
public class BankTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// 余額(單位:分)
Long BALANCE = 10000000L;
// 卡號
String card = "001";
// 密碼
String password = "123456";
String inputCard = new String();
String inputPassword = new String();
String quit = new String();
while (true) {
System.out.println(" 歡迎來到網上銀行辦理存取款業務!");
System.out.println("請輸入銀行卡號和銀行卡密碼進行登錄!");
while (true) {
System.out.print("請輸入銀行卡號(按q退出): ");
inputCard = scan.nextLine();
quit = inputCard;
if (inputCard.equals("q")) {
break;
}
if (!inputCard.equals(card)) {
System.out.print("您輸入銀行卡號不正確,請重新輸入 ");
continue;
}
break;
}
if (quit.equals("q")) {
continue;
}
while (true) {
System.out.print("請輸入銀行卡密碼(按q退出): ");
inputPassword = scan.nextLine();
quit = inputPassword;
if (inputPassword.equals("q")) {
break;
}
if (!inputPassword.equals(password)) {
System.out.print("您輸入銀行卡密碼不正確,請重新輸入 ");
continue;
}
break;
}
if (quit.equals("q")) {
continue;
}
System.out.print("登錄成功,當前登錄的賬戶名:" + inputCard);
String type = "4";
while (!type.equals("0")) {
System.out.print(" 您當前的余額為:" + money(BALANCE) + "元");
System.out.print(" 請選擇操作類型。(存款:1;取款:2 ;余額:3;退出:0) ");
type = scan.nextLine();
switch (type) {
case "1": {
System.out.print("請輸入您的存款金額(元):");
String chageNumber = scan.nextLine();
if (!isPositiveInteger(chageNumber)) {
System.out.print("請輸入正確的存款金額!");
continue;
}
BALANCE = Long.valueOf(chageNumber) * 100 + BALANCE;
continue;
}
case "2": {
System.out.print("請輸入您的取款金額(元):");
String chageNumber = scan.nextLine();
if (!isPositiveInteger(chageNumber)) {
System.out.print("請輸入正確取款金額!");
continue;
}
BALANCE = BALANCE - Long.valueOf(chageNumber) * 100;
continue;
}
case "3": {
System.out.print("您當前的余額為:" + money(BALANCE) + "元 ");
continue;
}
default: {
continue;
}
}
}
}
}
private static boolean isMatch(String regex, String orginal) {
if (orginal == null || orginal.trim().equals("")) {
return false;
}
Pattern pattern = Pattern.compile(regex);
Matcher isNum = pattern.matcher(orginal);
return isNum.matches();
}
// 判斷數據是否為正整數
public static boolean isPositiveInteger(String orginal) {
return isMatch("^\+{0,1}[1-9]\d*", orginal);
}
// 分轉元,轉換為bigDecimal在toString
public static String money(Long money) {
return BigDecimal.valueOf(money).divide(new BigDecimal(100)).toString();
}
}
Ⅷ 銀行運行模擬 設計一個Java應用程序,模擬顧客到銀行存取款的現象。 謝謝
使用的生產者和消費者模型具有如下特點:
(1)本實驗的多個緩沖區不是環形循環的,也不要求按順序訪問。生產者可以把產品放到目前某一個空緩沖區中。
(2)消費者只消費指定生產者的產品。
(3)在測試用例文件中指定了所有的生產和消費的需求,只有當共享緩沖區的數據滿足了所有關於它的消費需求後,此共享緩沖區才可以作為空閑空間允許新的生產者使用。
(4)本實驗在為生產者分配緩沖區時各生產者間必須互斥,此後各個生產者的具體生產活動可以並發。而消費者之間只有在對同一產品進行消費時才需要互斥,同時它們在消費過程結束時需要判斷該消費對象是否已經消費完畢並清除該產品。
Windows 用來實現同步和互斥的實體。在Windows 中,常見的同步對象有:信號量(Semaphore)、
互斥量(Mutex)、臨界段(CriticalSection)和事件(Event)等。本程序中用到了前三個。使用這些對象都分
為三個步驟,一是創建或者初始化:接著請求該同步對象,隨即進入臨界區,這一步對應於互斥量的
上鎖;最後釋放該同步對象,這對應於互斥量的解鎖。這些同步對象在一個線程中創建,在其他線程
中都可以使用,從而實現同步互斥。當然,在進程間使用這些同步對象實現同步的方法是類似的。
1.用鎖操作原語實現互斥
為解決進程互斥進人臨界區的問題,可為每類臨界區設置一把鎖,該鎖有打開和關閉兩種狀態,進程執行臨界區程序的操作按下列步驟進行:
①關鎖。先檢查鎖的狀態,如為關閉狀態,則等待其打開;如已打開了,則將其關閉,繼續執行步驟②的操作。
②執行臨界區程序。
③開鎖。將鎖打開,退出臨界區。
2.信號量及WAIT,SIGNAL操作原語
信號量的初值可以由系統根據資源情況和使用需要來確定。在初始條件下信號量的指針項可以置為0,表示隊列為空。信號量在使用過程中它的值是可變的,但只能由WAIT,SIGNAL操作來改變。設信號量為S,對S的WAIT操作記為WAIT(S),對它的SIGNAL操作記為SIGNAL(S)。
WAIT(S):順序執行以下兩個動作:
①信號量的值減1,即S=S-1;
②如果S≥0,則該進程繼續執行;
如果 S(0,則把該進程的狀態置為阻塞態,把相應的WAITCB連人該信號量隊列的末尾,並放棄處理機,進行等待(直至其它進程在S上執行SIGNAL操作,把它釋放出來為止)。
SIGNAL(S):順序執行以下兩個動作
①S值加 1,即 S=S+1;
②如果S)0,則該進程繼續運行;
如果S(0則釋放信號量隊列上的第一個PCB(既信號量指針項所指向的PCB)所對應的進程(把阻塞態改為就緒態),執行SIGNAL操作的進程繼續運行。
在具體實現時注意,WAIT,SIGNAL操作都應作為一個整體實施,不允許分割或相互穿插執行。也就是說,WAIT,SIGNAL操作各自都好像對應一條指令,需要不間斷地做下去,否則會造成混亂。
從物理概念上講,信號量S)時,S值表示可用資源的數量。執行一次WAIT操作意味著請求分配一個單位資源,因此S值減1;當S<0時,表示已無可用資源,請求者必須等待別的進程釋放了該類資源,它才能運行下去。所以它要排隊。而執行一次SIGNAL操作意味著釋放一個單位資源,因此S值加1;若S(0時,表示有某些進程正在等待該資源,因而要把隊列頭上的進程喚醒,釋放資源的進程總是可以運行下去的。
---------------
/**
 * 生產者
 *
 */
public class Procer implements Runnable{
 private Semaphore mutex,full,empty;
 private Buffer buf;
 String name;
 public Procer(String name,Semaphore mutex,Semaphore full,Semaphore empty,Buffer buf){
  this.mutex  = mutex;
  this.full  = full;
  this.empty = empty;
  this.buf = buf;
  this.name = name;
 }
 public void run(){
  while(true){
   empty.p();
   mutex.p();
   System.out.println(name+" inserts a new proct into "+buf.nextEmptyIndex);
   buf.nextEmptyIndex = (buf.nextEmptyIndex+1)%buf.size;
   mutex.v();
   full.v(); 
   try {
    Thread.sleep(1000);
   } catch (InterruptedException e) {    
    e.printStackTrace();
   }
  }
 }
}
---------------
/**
 * 消費者
 *
 */
public class Customer implements Runnable{
 private Semaphore mutex,full,empty;
 private Buffer buf;
 String name;
 public Customer(String name,Semaphore mutex,Semaphore full,Semaphore empty,Buffer buf){
  this.mutex  = mutex;
  this.full  = full;
  this.empty = empty;
  this.buf = buf;
  this.name = name;
 }
 public void run(){
  while(true){
   full.p();
   mutex.p();
   System.out.println(name+" gets a  proct from "+buf.nextFullIndex);
   buf.nextFullIndex = (buf.nextFullIndex+1)%buf.size;
   mutex.v();
   empty.v(); 
   try {
    Thread.sleep(1000);
   } catch (InterruptedException e) {    
    e.printStackTrace();
   }
  }
 }
}
-------------------------
/**
 * 緩沖區
 *
 */
public class Buffer{
 public Buffer(int size,int nextEmpty,int nextFull){
  this.nextEmptyIndex = nextEmpty;
  this.nextFullIndex = nextFull;
  this.size = size; 
 }
 public int size; 
 public int nextEmptyIndex;
 public int nextFullIndex;
}
-----------------
/**
 * 此類用來模擬信號量
 *
 */
public class Semaphore{
 private int semValue;
 public Semaphore(int semValue){
  this.semValue = semValue;   
 }
 public synchronized void p(){
  semValue--;
  if(semValue<0){
   try {
    this.wait();
   } catch (InterruptedException e) {    
    e.printStackTrace();
   }
  }
 }
 public synchronized void v(){
  semValue++;
  if(semValue<=0){
   this.notify();
  }  
 }
}
------------------------
public class Test extends Thread
{
 public static void main(String[] args)
 {
  Buffer bf=new Buffer(10,0,0);
  Semaphore mutex=new Semaphore(1);
  Semaphore full=new Semaphore(0);
  Semaphore empty=new Semaphore(10);
  //new Thread(new Procer("p001",mutex,full,empty,bf)).start();
  Procer p=new Procer("p001",mutex,full,empty,bf);
  new Thread(new Procer("p002",mutex,full,empty,bf)).start();
  new Thread(new Procer("p003",mutex,full,empty,bf)).start();
  new Thread(new Procer("p004",mutex,full,empty,bf)).start();
  new Thread(new Procer("p005",mutex,full,empty,bf)).start();
  try{
   sleep(3000);
  }
  catch(Exception ex)
  {
   ex.printStackTrace();
  }
  new Thread(new Customer("c001",mutex,full,empty,bf)).start();
  new Thread(new Customer("c002",mutex,full,empty,bf)).start();
  new Thread(new Customer("c003",mutex,full,empty,bf)).start();
  new Thread(new Customer("c004",mutex,full,empty,bf)).start();
  new Thread(new Customer("c005",mutex,full,empty,bf)).start();
 }
}
--------------------------------------------
Ⅸ 用JAVA語言編寫程序,模擬銀行賬戶功能。要有: 屬性:賬號、儲戶姓名、地址、存款余額、最小余額。 方法
很明顯你缺少main方法啊。java程序的主入口是main方法。
你應該在main裡面寫
public static void main(String args[]){
 //請選擇你需要的服務
   1 查詢
   2 存款
   3 取款
 //根據選擇的服務調用你的方法
 }
調用你的業務方法