• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Bank account simulation

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

There are two persons in a bank. One person puts 1'000'000 times a certain amount of money in an account, at the same time an other person gets the same amount of money from the same account 1'000'000 times. This is my code so far:

[code=javaclass Account // Konto
{
private float balance; // Kontostand

public void setBalance(float balance)
{
this.balance = balance;
}

public float getBalance()
{
return balance;
}
}

class Bank
{
private Account[] account;

public Bank()
{
account = new Account[100];
for(int i = 0; i < account.length; i++)
{
account[i] = new Account();
}
}

public void getMoney(int accountNumber, float amount)
{
synchronized(account[accountNumber])
{
float oldBalance = account[accountNumber].getBalance();
float newBalance = oldBalance - amount;
account[accountNumber].setBalance(newBalance);
}
}

public void putMoney(int accountNumber, float amount)
{
synchronized(account[accountNumber])
{
float oldBalance = account[accountNumber].getBalance();
float newBalance = oldBalance + amount;
account[accountNumber].setBalance(newBalance);
}
}
}


class Clerk extends Thread
{
private Bank bank;

public Clerk(String name, Bank bank)
{
super(name);
this.bank = bank;
start();
}

public void run()
{
for(int i = 0; i < 1000000; i++)
{
/*
* set account number
*/
int accountNumber = 0;

/*
* set transfer amount
*/
float amount = 100;

bank.putMoney(accountNumber, amount);
bank.getMoney(accountNumber, amount);
}
}
}

public class Banking
{
public static void main(String[] args)
{
Bank myBank = new Bank();
Clerk Andrea = new Clerk("Andrea", myBank);
Clerk Petra = new Clerk("Petra", myBank);

}
}][/code]

Everything is fine so far, the code compiles. But I do not have any idea at the moment, how to continue. Who can help me?

Regards, Urs
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There appears to be a lot of code missing from the code you have provided.
 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this a help? I continued, actually I am in the middle of the development.
 
Alistair J MacDonald
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I could help, but if you are in the middle of development I guess you are not stuck.
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try semaphore with consumer / producer....
 
passwords must contain 14 characters, a number, punctuation, a small bird, a bit of cheese and a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic