All pastes #902025 Raw Edit

Untitled

public text v1 · immutable
·published 2008-02-13 02:41 UTC
rendered paste body
import java.io.PrintWriter;

import javax.swing.*;
public class bankThreads {


	public static void main(String[] args) {
		String bob = "Bob";
		String mary = "Mary";
		String tim = "Tim";
	
		//int balance = 0;
		
		Runnable five = new transactions(mary);
	
		Runnable ten = new transactions(tim);
		
;		Thread thread1 = new Thread(five);
		Thread thread2 = new Thread(ten);
	
		thread1.start();
		//thread1.yield();
		thread2.start();
		
		//int randomInt = random.nextInt(16);
		
		//JOptionPane.showMessageDialog(null, "RAND " + random.nextInt(16));
		

		

	}

}

import java.util.*;
import java.io.PrintWriter;
public class transactions implements Runnable {
	private int maxRange;
	private String who;
	private Random random = new Random();
	int balance, currentAct = 0;
	private char depoWith;
	
	public transactions(String name)
	{
		if (name.equals("Bob"))
		{
			who = "Bob";
			maxRange = 6;
		}
		else if (name.equals("Tim"))
		{
			who = "Tim";
			maxRange = 11;
			depoWith = 'W';
		}
		else if (name.equals("Mary"))
		{
			who = "Mary";
			maxRange = 16;
			depoWith = 'D';
		}
	}
	
	
	public void run() {
		magic();
	}
	
	public synchronized void magic()
	{
		//int balance = 1000; <-- was here
		for(int i = 0; i < 100; i++)
		{
			currentAct = random.nextInt(maxRange);
			
			if (depoWith == 'D')
			{
				balance += currentAct;
				System.out.print(who + " deposited $" + currentAct + " " + balance + "\n");
			}
			else if ((depoWith == 'W') && (currentAct < balance))

			{
				balance -= currentAct;
				System.out.print(who + " withdrew $" + currentAct + " " + balance + "\n");
			}
			
			
		}
	}
	

	
	public void openF() throws Exception {
		PrintWriter writer = new PrintWriter("program1.txt");

	}
	

	
}