diff options
| author | Franklin Wei <git@fwei.tk> | 2015-05-13 21:46:05 -0400 |
|---|---|---|
| committer | Franklin Wei <git@fwei.tk> | 2015-05-13 21:46:05 -0400 |
| commit | 7586aa241abb26378b7385b43d7596925b67027d (patch) | |
| tree | bcaa099ea4f1f0c43ef36f92d70168dc6a77b2a3 /src | |
| parent | f4dc6ffb90ca05e751e852cb76027399bb503b09 (diff) | |
| download | market-sim-7586aa241abb26378b7385b43d7596925b67027d.zip market-sim-7586aa241abb26378b7385b43d7596925b67027d.tar.gz market-sim-7586aa241abb26378b7385b43d7596925b67027d.tar.bz2 market-sim-7586aa241abb26378b7385b43d7596925b67027d.tar.xz | |
stuff
Diffstat (limited to 'src')
| -rw-r--r-- | src/buy.c | 21 | ||||
| -rw-r--r-- | src/globals.h | 16 | ||||
| -rw-r--r-- | src/main.c | 10 | ||||
| -rw-r--r-- | src/sell.c | 30 | ||||
| -rw-r--r-- | src/util.c | 14 |
5 files changed, 59 insertions, 32 deletions
@@ -50,13 +50,15 @@ void buy_handler(struct player_t *player) { printf("Confirmed.\n"); + struct stock_t *stock; + /* 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 = player->portfolio + i; stock->count += count; stock->current_price.cents = price.cents; goto done; @@ -68,16 +70,21 @@ void buy_handler(struct player_t *player) 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; + stock = player->portfolio + player->portfolio_len - 1; + + memset(stock, 0, sizeof(struct stock_t)); + + stock->symbol = sym; + stock->fullname = name; + stock->count = count; + stock->current_price.cents = price.cents; + done: player->cash.cents -= cost; + add_hist(stock, BUY, count); + /* sort the portfolio alphabetically by ticker symbol */ qsort(player->portfolio, player->portfolio_len, sizeof(struct stock_t), compare_stocks); } diff --git a/src/globals.h b/src/globals.h index cecb62b..5bb26d0 100644 --- a/src/globals.h +++ b/src/globals.h @@ -5,6 +5,8 @@ #include <stdlib.h> #include <string.h> +#include <curl/curl.h> + #define ARRAYLEN(x) (sizeof(x) / sizeof(x[0])) typedef unsigned long long ullong; @@ -18,11 +20,22 @@ struct money_t { ullong cents; }; +enum history_action { BUY, SELL }; + +struct history_item { + enum history_action action; + ullong count; + struct money_t price; + + struct history_item *next; +}; + struct stock_t { char *symbol; char *fullname; ullong count; struct money_t current_price; + struct history_item *history; }; struct player_t { @@ -40,9 +53,12 @@ void all_upper(char*); bool get_stock_info(char *sym, struct money_t*, char **name); uint64_t to_sys64(uint64_t); uint64_t to_be64(uint64_t); +struct stock_t *find_stock(struct player_t*, char*); +void add_hist(struct stock_t*, enum history_action, ullong count); void buy_handler(struct player_t*); void sell_handler(struct player_t*); +void history_handler(struct player_t*); void update_handler(struct player_t*); void save_handler(struct player_t*); void load_handler(struct player_t*); @@ -1,12 +1,5 @@ #include "globals.h" -#include <ctype.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include <curl/curl.h> - /*** utility functions ***/ void update_handler(struct player_t *player) @@ -55,7 +48,7 @@ int main(int argc, char *argv[]) { struct stock_t *stock = player->portfolio + i; ullong total_value = stock->count * stock->current_price.cents; - printf("%6s %30s %5llu * $%5llu.%02llu = $%6llu.%02llu\n", + printf("%6s %40s %5llu * $%5llu.%02llu = $%6llu.%02llu\n", stock->symbol, stock->fullname, stock->count, stock->current_price.cents / 100, stock->current_price.cents % 100, total_value / 100, total_value % 100); @@ -81,6 +74,7 @@ int main(int argc, char *argv[]) { "[B]uy", "buy", buy_handler }, { "[S]ell", "sell", sell_handler }, { "[U]pdate stock prices", "update", update_handler }, + { "Stock [h]istory", "history", history_handler }, { "[W]rite portfolio", "write", save_handler }, { "[L]oad portfolio", "load", load_handler }, { "[Q]uit", "quit", quit_handler }, @@ -10,16 +10,8 @@ void sell_handler(struct player_t *player) printf("Getting stock information...\n"); struct stock_t *stock = NULL; - uint stock_idx; - for(stock_idx = 0; stock_idx < player->portfolio_len; ++stock_idx) - { - if(strcmp(player->portfolio[stock_idx].symbol, sym) == 0) - { - stock = player->portfolio + stock_idx; - break; - } - } + stock = find_stock(player, sym); if(!stock) { @@ -32,25 +24,25 @@ void sell_handler(struct player_t *player) printf("You currently own %llu shares of '%s' (%s) valued at $%llu.%02llu each.\n", stock->count, stock->fullname, stock->symbol, stock->current_price.cents / 100, stock->current_price.cents % 100); - ullong sell = 0; + ullong sell_count = 0; printf("How many shares do you wish to sell? "); - scanf("%llu", &sell); + scanf("%llu", &sell_count); - if(!sell) + if(!sell_count) { printf("Sale cancelled.\n"); return; } - if(stock->count < sell) + if(stock->count < sell_count) { printf("You don't own enough shares!\n"); return; } - ullong sell_total = stock->current_price.cents * sell; + ullong sell_total = stock->current_price.cents * sell_count; - printf("This will sell %llu shares for $%llu.%02llu total. Proceed? ", sell, sell_total / 100, sell_total % 100); + printf("This will sell %llu shares for $%llu.%02llu total.\nProceed? ", sell_count, sell_total / 100, sell_total % 100); char response[16]; scanf("%15s", response); @@ -58,8 +50,9 @@ void sell_handler(struct player_t *player) if(response[0] == 'y') { - stock->count -= sell; + stock->count -= sell_count; +#if 0 if(stock->count == 0) { /* remove this item from the portfolio */ @@ -67,10 +60,13 @@ void sell_handler(struct player_t *player) player->portfolio_len -= 1; player->portfolio = realloc(player->portfolio, sizeof(struct stock_t) * player->portfolio_len); } +#endif player->cash.cents += sell_total; - printf("%llu shares sold for $%llu.%02llu total.\n", sell, sell_total / 100, sell_total % 100); + add_hist(stock, SELL, sell_count); + + printf("%llu shares sold for $%llu.%02llu total.\n", sell_count, sell_total / 100, sell_total % 100); } else { @@ -175,3 +175,17 @@ uint64_t to_sys64(uint64_t n) else return to_be64(n); } + +struct stock_t *find_stock(struct player_t *player, char *sym) +{ + printf("find stock %s\n", sym); + for(int i = 0; i < player->portfolio_len; ++i) + { + if(strcmp(player->portfolio[i].symbol, sym) == 0) + { + return player->portfolio + i; + } + } + + return NULL; +} |