public static synchronized Collection purchaseStock(String tickername,
			User username, int no, ArrayList<User> users,
			ArrayList<Stock> stocks) {
		stocks = DataReader.getStocks();
		users = DataReader.getUsers();
		tickername = tickername.toUpperCase();
		// the collection that is returned
		Collection results = new ArrayList();

		// check if that no of stocks are available
		Stock purchasedStock = DataReader.getStockByTickername(tickername,
				stocks);
		
		if(purchasedStock == null) {
			return null;
		}
		
		if (purchasedStock.getNo() < no) {
			System.out
					.println("The amount of stocks remaining is less that what you want. The amount remaining is "
							+ purchasedStock.getNo());
			return null;
		}

		// calculate what it would cost from the amount and the price
		double costPrice = 0;
		try {
			costPrice = no * PriceUpdater.price(tickername);
		} catch (Exception e) {
			System.out.println("Invalid tickername. Please try again");
			return null;
		}
		// check if the user has enough balance
		double userBalance = DataReader.getUserBalance(username.getUsername(),
				users);
		if (userBalance < costPrice) {
			System.out.println("You do not have sufficient funds. You have $"
					+ userBalance + ". Please review your purchase");
			return null;
		}

		// add the stock to the userStocks arraylist and write the values
		ArrayList<UserStocks> userStocks = DataReader.getUserStocks();
		int index = 0;
		boolean flag = false;
		// check to see if the user already has some of that stock
		for (UserStocks us : userStocks) {
			if (us.getTickername().equalsIgnoreCase(tickername)
					&& us.getUsername()
							.equalsIgnoreCase(username.getUsername())) {
				// user already has some of those stocks
				us.setNo(us.getNo() + no);
				userStocks.set(index, us);
				flag = true;
				break;
			}
			index++;
		}
		// user does not have any of those stocks
		if (!flag) {
			UserStocks newUs = new UserStocks();
			newUs.setNo(no);
			newUs.setTickername(tickername);
			newUs.setUsername(username.getUsername());
			userStocks.add(newUs);
			results.add(userStocks); // add the user stocks to the collection.
										// index 0
		}
		// write updated userstocks values to file
		writeAllUserStocks(userStocks); // write the stock values to the file

		// update and write the no of stocks of the stock
		int indexOfPurchasedStock = DataReader.getIndexOfPS(purchasedStock, stocks);
		purchasedStock.setNo(purchasedStock.getNo() - no);
		stocks.set(indexOfPurchasedStock, purchasedStock);
		results.add(stocks); // index 1
		writeStockValues(stocks);

		// update the user balance
		int userIndex = DataReader.getIndexOfUser(username, users);
		username.setBalance(username.getBalance() - costPrice);
		username.setUserStock(userStocks);
		users.set(userIndex, username);

		results.add(users); // index 2 is the updated list of all the users
		writeAllUsers(users); // write all the users to file
		results.add(username); // index 3 is the updated value of the user
		// return the collection that was just purchased
		return results;
	}