本文目錄一覽:
- 1、用java編寫多線程銀行ATM 模擬程序
- 2、使用JAVA編寫一個簡單的銀行存取款程序
- 3、銀行運行模擬 設計一個Java應用程序,模擬顧客到銀行存取款的現象。 謝謝
- 4、求兩道編程題程序! 1.模擬銀行取款,製造卡和折同時取款時出現的線程安全問題,分別用同步代碼塊和同步方法
- 5、用JAVA語言編寫程序,模擬銀行賬戶功能。要有: 屬性:賬號、儲戶姓名、地址、存款餘額、最小餘額。
用java編寫多線程銀行ATM 模擬程序
先構建一個客戶端,再構建一個服務器端,其實做一個簡單的界面,建立一個數據庫,調用SQl 語句,,實現單機,模擬多線程的就可以了,服務器部分不做也可以模擬出來。。
PS:這不會是程序專題訓練一吧。。。
使用JAVA編寫一個簡單的銀行存取款程序
package com.lw.thread;
/*
銀行賬戶類Account(不能透支),
包含賬號id(10~16位數字),密碼password(6位數字),戶主姓名name,餘額balence
*/
public class Account {
private String id;
private int password;
private String name;
private double balence;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getPassword() {
return password;
}
public void setPassword(int password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalence() {
return balence;
}
public void setBalence(double balence) {
this.balence = balence;
}
/*
* 默認構造賬戶信息為:1111111111111111,666666,錢三多,888888.88。
*/
public Account() {
super();
this.id = “1111111111111111”;
this.password = 666666;
this.name = “錢三多”;
this.balence = 888888.88;
}
/*
* 另一個構造方法帶4個參數分別初始化4個屬性(帶數據有效性驗證)。
*/
public Account(String id, int password, String name, double balence) {
this.id = id;
this.password = password;
this.name = name;
this.balence = balence;
}
/*
* 查詢餘額
*/
public static double selectMoney(Account account) {
return account.getBalence();
}
/*
* 存錢
*/
public static String setMoney(Account account, double balence) {
if (balence 0) {
return “存錢失敗,請正確放入!”;
}
double d = balence + account.getBalence();
account.setBalence(d);
return “您存入了” + balence + “元,現賬戶餘額為+” + d;
}
/*
* 取錢
*/
public static String getMoney(Account account, double balence) {
double d = account.getBalence();
if (balence d) {
return “您的餘額不足!”;
}
account.setBalence(d – balence);
return “您取出了” + balence + “元,現賬戶餘額為+” + account.getBalence();
}
}
銀行運行模擬 設計一個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;當S0時,表示已無可用資源,請求者必須等待別的進程釋放了該類資源,它才能運行下去。所以它要排隊。而執行一次SIGNAL操作意味着釋放一個單位資源,因此S值加1;若S(0時,表示有某些進程正在等待該資源,因而要把隊列頭上的進程喚醒,釋放資源的進程總是可以運行下去的。
—————
/**
* 生產者
*
*/
public class Producer implements Runnable{
private Semaphore mutex,full,empty;
private Buffer buf;
String name;
public Producer(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 product 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 product 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(semValue0){
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 Producer(“p001”,mutex,full,empty,bf)).start();
Producer p=new Producer(“p001”,mutex,full,empty,bf);
new Thread(new Producer(“p002”,mutex,full,empty,bf)).start();
new Thread(new Producer(“p003”,mutex,full,empty,bf)).start();
new Thread(new Producer(“p004”,mutex,full,empty,bf)).start();
new Thread(new Producer(“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();
}
}
——————————————–
求兩道編程題程序! 1.模擬銀行取款,製造卡和折同時取款時出現的線程安全問題,分別用同步代碼塊和同步方法
你好樓主,看看我的代碼行不
第一題 同步代碼塊實現:
class TB implements Runnable {
int i = 100;
int money = 11000;
boolean flag = true;
public void run() {
while(flag) {
synchronized (this){
if(i=money) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
money -= i;
System.out.println(Thread.currentThread().getName()+”取款”+i+”元。餘額為:”+money);
}else flag = false;
}
}
}
}
public class Bank {
public static void main(String[] args) {
TB2 tt = new TB2();
Thread t1 = new Thread(tt);
Thread t2 = new Thread(tt);
t1.setName(“銀行卡”);
t2.setName(“存摺用戶”);
t1.start();
t2.start();
}
}
第一題同步方法實現,主方法的那個類還是一樣,看看TB:
class TB implements Runnable {
int i = 100;
int money = 1100;
boolean flag = true;
public void run() {
while(flag) {
QuKuan();
}
}
public synchronized void QuKuan(){
if(i=1100) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
money -= i;
System.out.println(Thread.currentThread().getName()+”取款”+i+”元。餘額為:”+money);
}
}
}
=========================================================================
第二題
==========================================================================
public class Bank {
public static void main(String[] args) {
TB2 tt = new TB2();
Thread saveThed = new Thread(tt);
Thread payThed = new Thread(tt);
saveThed.setName(“saveMoney”);
payThed.setName(“payMoney”);
saveThed.start();
payThed.start();
}
}
class TB2 implements Runnable {
int i = 50;
int money = 100;
boolean flag = true;
int count=0;
public void run() {
while(count100) {//存取操作100次就可以了
if(Thread.currentThread().getName().equals(“saveMoney”)){
CunKuan();
}else if(Thread.currentThread().getName().equals(“payMoney”)){
QuKuan();
}
//為了方便觀看,程序跑得慢一點,這裡我們休眠一段時間
//去掉後效率更高,並且結果正確
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
count++;
}
}
public void QuKuan(){
if(money=50) {
money -= i;
System.out.println(Thread.currentThread().getName()+”取款”+i+”元。餘額為:”+money);
}else{
Thread.yield();//停止當前線程,執行其線程
}
}
public void CunKuan(){
if(money50) {
money += i;
System.out.println(Thread.currentThread().getName()+”存款”+i+”元。餘額為:”+money);
}else{
Thread.yield();
}
}
}
怎麼樣???
用JAVA語言編寫程序,模擬銀行賬戶功能。要有: 屬性:賬號、儲戶姓名、地址、存款餘額、最小餘額。
package com.cshr.test;
public class bank {
private String card;//賬號
private String uname;//儲戶姓名
private String address;//地址
private double money;//存款餘額
public static final double minMoney=0;//最小餘額
public bank(){}
public String getCard() {
return card;
}
public void setCard(String card) {
this.card = card;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public static double getMinmoney() {
return minMoney;
}
}
package com.cshr.test;
import java.util.List;
import org.hibernate.Session;
import com.utils.HibernateSessionFactory;
public class bankDao {
//存款
public void addMoney(double money,double Sqlmoney,String card){
Session session=HibernateSessionFactory.getSession();
try{
session.beginTransaction();
String hql=”update bank b set b.money+=”+money+Sqlmoney+” where b.card='”+card+”‘”;
session.createQuery(hql).executeUpdate();
session.beginTransaction().commit();
}catch(Exception e){
e.printStackTrace();
session.beginTransaction().rollback();
}finally{
HibernateSessionFactory.closeSession();
}
}
//取款
public void delMoney(double money,double Sqlmoney,String card){
Session session=HibernateSessionFactory.getSession();
try{
session.beginTransaction();
String hql=”update bank b set b.money+=”+(Sqlmoney-money)+” where b.card='”+card+”‘”;
session.createQuery(hql).executeUpdate();
session.beginTransaction().commit();
}catch(Exception e){
e.printStackTrace();
System.out.println(“取款失敗”);
session.beginTransaction().rollback();
}finally{
HibernateSessionFactory.closeSession();
}
}
//查詢所有
public Listbank selectfind(){
Session session=HibernateSessionFactory.getSession();
session.beginTransaction();
String hql=”select b from bank b “;
Listbank list=session.createQuery(hql).list();
return list;
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/258108.html