Skip to content

Instantly share code, notes, and snippets.

@vaughan0
Forked from anonymous/gist:4011954
Created November 4, 2012 13:44
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save vaughan0/4011973 to your computer and use it in GitHub Desktop.
Save vaughan0/4011973 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
public class BankAccount {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
BankAccount account = new BankAccount(1000);
account.deposit(500);
account.withdraw(50);
System.out.println("BankAccount " + account.getNumber());
System.out.println("Has a balance of " + account.getBalance());
}
// These are different for each account
private double balance;
private int accountNumber;
// This is shared by all accounts, so it's static
private static int lastAccountNumber = 0;
// This is a constructor: no return type (void, boolean etc) and has the same name as the class.
public BankAccount(double intialBalance)
{
balance = intialBalance;
accountNumber = lastAccountNumber + 1;
lastAccountNumber = accountNumber;
}
public void deposit(double depositAmount)
{
balance += depositAmount;
}
public boolean withdraw(double withdrawAmount)
{
if (withdrawAmount > balance){
System.out.println("Insufficient Funds!!!");
return false;
} else {
balance -= withdrawAmount;
return true;
}
}
public int getNumber()
{
return accountNumber;
}
public double getBalance()
{
return balance;
}
}
@steven555555
Copy link

请问并发安全是怎么保证的?Account代码中没看到并发措施

@steven555555
Copy link

Hi, is there any policy for concurrency issues? What about two threads call the transfer method of the same account instance. Did I miss something?

@mlimbika
Copy link

mlimbika commented Nov 2, 2020

Screenshot (21)

HELP ME TO COME UP WITH THE CODE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment