aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFranklin Wei <git@fwei.tk>2015-05-16 14:27:58 -0400
committerFranklin Wei <git@fwei.tk>2015-05-16 14:27:58 -0400
commitf1b434fd0e76d44056868ea4d2d8fd52138a663b (patch)
tree37e38db0ae52c16fc27a26793993426274d9f867
parent2bb97375378c24bc62fea9445f5b445291e0a9ee (diff)
downloadmarket-sim-f1b434fd0e76d44056868ea4d2d8fd52138a663b.zip
market-sim-f1b434fd0e76d44056868ea4d2d8fd52138a663b.tar.gz
market-sim-f1b434fd0e76d44056868ea4d2d8fd52138a663b.tar.bz2
market-sim-f1b434fd0e76d44056868ea4d2d8fd52138a663b.tar.xz
add missing files
-rw-r--r--src/history.c71
-rw-r--r--src/info.c21
2 files changed, 92 insertions, 0 deletions
diff --git a/src/history.c b/src/history.c
new file mode 100644
index 0000000..324633b
--- /dev/null
+++ b/src/history.c
@@ -0,0 +1,71 @@
+#include "globals.h"
+
+void add_hist(struct stock_t *stock, enum history_action action, ullong count)
+{
+ /* add a history item for this transaction */
+ struct history_item *newhist = malloc(sizeof(struct history_item));
+ newhist->action = action;
+ newhist->count = count;
+ newhist->price.cents = stock->current_price.cents;
+ newhist->next = NULL;
+
+ time_t timer = time(0);
+ struct tm *tm = localtime(&timer);
+
+ newhist->action_time.year = tm->tm_year - 100;
+ newhist->action_time.month = tm->tm_mon;
+ newhist->action_time.day = tm->tm_mday;
+ newhist->action_time.hour = tm->tm_hour;
+ newhist->action_time.minute = tm->tm_min;
+ newhist->action_time.second = tm->tm_sec;
+
+ if(!stock->history)
+ {
+ stock->history = newhist;
+ }
+ else
+ {
+ struct history_item *last = stock->history;
+ while(last)
+ {
+ if(last->next)
+ last = last->next;
+ else
+ break;
+ }
+ last->next = newhist;
+ }
+
+ ++stock->history_len;
+}
+
+void print_history(struct stock_t *stock)
+{
+ assert(stock);
+
+ struct history_item *hist = stock->history;
+
+ while(hist)
+ {
+ ullong total = hist->count * hist->price.cents;
+
+ printf("[%d-%d-%d %d:%02d:%02d] ", hist->action_time.year + 2000, hist->action_time.month + 1, hist->action_time.day + 1,
+ hist->action_time.hour, hist->action_time.minute, hist->action_time.second);
+
+ switch(hist->action)
+ {
+ case BUY:
+ printf("[BUY] %llu shares for $%llu.%02llu each (+$%llu.%02llu).\n", hist->count, hist->price.cents / 100, hist->price.cents % 100,
+ total / 100, total % 100);
+ break;
+ case SELL:
+ printf("[SELL] %llu shares for $%llu.%02llu each (-$%llu.%02llu).\n", hist->count, hist->price.cents / 100, hist->price.cents % 100,
+ total / 100, total % 100);
+ break;
+ default:
+ printf("unknown history enum (%d).\n", hist->action);
+ break;
+ }
+ hist = hist->next;
+ }
+}
diff --git a/src/info.c b/src/info.c
new file mode 100644
index 0000000..ea79dd7
--- /dev/null
+++ b/src/info.c
@@ -0,0 +1,21 @@
+#include "globals.h"
+
+void info_handler(struct player_t *player)
+{
+ char *sym;
+ printf("Enter the ticker symbol of the stock to get information for: ");
+ sym = get_ticker();
+
+ struct stock_t *stock = find_stock(player, sym);
+
+ free(sym);
+
+ if(!stock)
+ {
+ printf("Couldn't find '%s' in portfolio.\n", sym);
+ return;
+ }
+
+ printf("Transaction history:\n");
+ print_history(stock);
+}