aboutsummaryrefslogtreecommitdiff
path: root/src/buy.c
diff options
context:
space:
mode:
authorFranklin Wei <git@fwei.tk>2015-05-12 19:59:44 -0400
committerFranklin Wei <git@fwei.tk>2015-05-12 19:59:44 -0400
commitb05123ca8b967a31cd074792ca920a3a74f61bc4 (patch)
tree0053b98f2815243b881e477787a98326dbde1f0e /src/buy.c
parent9082eb2a10f935a23e140e0089fae26570081883 (diff)
downloadmarket-sim-b05123ca8b967a31cd074792ca920a3a74f61bc4.zip
market-sim-b05123ca8b967a31cd074792ca920a3a74f61bc4.tar.gz
market-sim-b05123ca8b967a31cd074792ca920a3a74f61bc4.tar.bz2
market-sim-b05123ca8b967a31cd074792ca920a3a74f61bc4.tar.xz
split into multiple files
Diffstat (limited to 'src/buy.c')
-rw-r--r--src/buy.c88
1 files changed, 88 insertions, 0 deletions
diff --git a/src/buy.c b/src/buy.c
new file mode 100644
index 0000000..7419bbf
--- /dev/null
+++ b/src/buy.c
@@ -0,0 +1,88 @@
+#include "globals.h"
+
+void buy_handler(struct player_t *player)
+{
+ char *sym = malloc(16);
+ printf("Enter the ticker symbol of the stock you wish to purchase: ");
+ scanf("%15s", sym);
+ all_upper(sym);
+
+ struct money_t price;
+
+ printf("Getting stock information...\n");
+
+ char *name;
+
+ if(!get_stock_info(sym, &price, &name))
+ {
+ printf("Failed to get query information for '%s'.\n", sym);
+ return;
+ }
+
+ printf("Stock name: %s\n", name);
+ printf("Price per share: $%llu.%02llu\n", price.cents / 100, price.cents % 100);
+ printf("Enter the number of shares to be purchased (maximum %llu): ", player->cash.cents / price.cents);
+
+ ullong count = 0;
+
+ scanf("%llu", &count);
+
+ if(count <= 0)
+ {
+ printf("Purchase cancelled.\n");
+ return;
+ }
+
+ ullong cost = price.cents * count;
+
+ if(cost > player->cash.cents)
+ {
+ printf("Not enough money!\n");
+ return;
+ }
+
+ printf("This will cost $%llu.%02llu. Are you sure? ", cost / 100, cost % 100);
+ char response[16];
+ scanf("%15s", response);
+ all_lower(response);
+
+ if(response[0] == 'y')
+ {
+ printf("Confirmed.\n");
+
+ /* add the stock to the portfolio or increase the count of a stock */
+
+ for(uint i = 0; i < player->portfolio_len; ++i)
+ {
+ if(strcmp(player->portfolio[i].symbol, sym) == 0)
+ {
+ struct stock_t *stock = player->portfolio + i;
+ stock->count += count;
+ stock->current_price.cents = price.cents;
+ goto done;
+ }
+ }
+
+ /* not found, add a new one */
+ player->portfolio_len += 1;
+ player->portfolio = realloc(player->portfolio, player->portfolio_len * sizeof(struct stock_t));
+ player->need_to_free_portfolio = true;
+
+ printf("sym: %s\n", sym);
+ printf("name: %s\n", name);
+ player->portfolio[player->portfolio_len - 1].symbol = sym;
+ player->portfolio[player->portfolio_len - 1].fullname = name;
+ player->portfolio[player->portfolio_len - 1].count = count;
+ player->portfolio[player->portfolio_len - 1].current_price.cents = price.cents;
+ done:
+
+ player->cash.cents -= cost;
+
+ /* sort the portfolio alphabetically by ticker symbol */
+ qsort(player->portfolio, player->portfolio_len, sizeof(struct stock_t), compare_stocks);
+ }
+ else
+ {
+ printf("Not confirmed.\n");
+ }
+}