日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

模拟银行转账信息

發(fā)布時(shí)間:2023/12/10 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 模拟银行转账信息 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1、從本地 data.txt 文本中讀取賬戶信息,模擬銀行不同賬戶之間的轉(zhuǎn)賬。

2、實(shí)現(xiàn)代碼如下:

package com.tiger.thread;import java.io.*; /*** 模擬銀行轉(zhuǎn)賬信息* 從本地 data.txt 文本中讀取賬戶信息* @author tiger* @Date 2017年7月26日 */ public class BankTest extends Thread{public BankTest(String name) {super(name);}public static void main(String[] args) {Bank bank = new Bank();Thread th1 = new Thread(bank,"1號(hào)轉(zhuǎn)賬線路-》");Thread th2 = new Thread(bank,"2號(hào)轉(zhuǎn)賬線路-》");Thread th3 = new Thread(bank,"3號(hào)轉(zhuǎn)賬線路-》");th1.start();th2.start();th3.start();} } /*** @author tiger* @Date 2017年7月26日*/ class Bank implements Runnable{String path = "D:\\Javac\\data.txt";int oneAccount;int twoAccount;int transfer = 100;BufferedReader reader;BufferedWriter writer;//在構(gòu)造器初始化,為賦值。public Bank() {try {reader = new BufferedReader(new FileReader(path));//讀取 比爾蓋茨 賬戶信息String oneAccount = reader.readLine();//讀取 巴菲特 賬戶信息String twoAccount = reader.readLine();//截取字符數(shù)字oneAccount = oneAccount.replaceAll("\\D", "");twoAccount = twoAccount.replaceAll("\\D", "");//將字符串解釋為 int。this.oneAccount = Integer.parseInt(oneAccount);this.twoAccount = Integer.parseInt(twoAccount);} catch (IOException e) {e.printStackTrace();}}@Overridepublic void run() {//同步業(yè)務(wù)邏輯synchronized ("1") {String name = Thread.currentThread().getName();//模擬 one 給 two 轉(zhuǎn)錢 turnoneAccount = oneAccount - transfer;twoAccount = twoAccount + transfer; //將轉(zhuǎn)賬后更新的數(shù)據(jù)(int類型的轉(zhuǎn)為String類型)存入data.txt中 String onestr = String.valueOf(oneAccount); String twostr = String.valueOf(twoAccount);try {//writer需要同步writer = new BufferedWriter(new FileWriter(path));writer.write("比爾蓋茨 = " + onestr + " (億美元);");writer.newLine();writer.write("巴菲特 = " + twostr + " (億美元);");writer.close();} catch (IOException e) {e.printStackTrace();}}} }



總結(jié)

以上是生活随笔為你收集整理的模拟银行转账信息的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。