Prepared documentation for release 0.4
[AXE.git] / demos / timesheet.cpp
1 #include <AXE/date.h>
2
3 class activity
4 {
5    String description;
6    hour   spent_in_month[12];
7
8 public:
9
10    activity(String desc)
11    {
12       description = desc;
13    }
14
15    int operator!=(String &s);
16
17    void addtime(hour spent, unsigned month);
18
19    void print(void);
20 };
21
22 struct act_lst
23 {
24    activity *act;
25    act_lst  *next;
26
27 };
28
29 main()
30 {
31
32    act_lst *timesheet, *a;
33    act_lst *Total;
34
35    timesheet = new act_lst;
36    timesheet->act = new activity("TOTAL");
37    timesheet->next = 0;
38    Total = timesheet;
39
40    date d1, d2;
41    hour t1, t2;
42    String action1, action2, datestring;
43
44    std::cin >> datestring;
45    std::cin >> action1;
46    d1 = date(datestring);
47    t1 = hour(datestring);
48
49    while (std::cin)
50    {
51       std::cin >> datestring;
52       d2 = date(datestring);
53       t2 = hour(datestring);
54       std::cin >> action2;
55
56       //   process the activity from nr 1 to nr 2.
57
58       if (!action1)
59       {
60          // No action: ends the previous action.
61       }
62       else
63       {
64          // Find the action in the list. If not found add a new activity.
65
66          for (a = timesheet; a && *a->act != action1; a=a->next);
67          if (!a)
68          {
69             a = new act_lst;
70             a->act = new activity(action1);
71             a->next = timesheet;
72             timesheet = a;
73          }
74
75          if (d2 != d1)
76          {
77             cerr << "More than a day at " << datestring << "\n";
78          }
79          a->act->addtime(t2 - t1, d1.Month());
80          Total->act->addtime(t2 - t1, d1.Month());
81
82       }
83       //   shift the log entry one place.
84
85       d1 = d2;
86       t1 = t2;
87       action1 = action2;
88    }
89
90    std::cout << "Activity\tJan\tFeb\tMar\tApr\tMay\tJun\tJul\tAug\tSep\tOct\tNov\tDec\tTOTAL\n\n";
91
92    for (a = timesheet; a; a = a->next)
93    {
94       a->act->print();
95    }
96 }
97
98 int activity::operator!=(String &s)
99 {
100    return description != s;
101 }
102
103 void activity::addtime(hour spent, unsigned month)
104 {
105    spent_in_month[month - 1] += spent;
106 }
107
108 void activity::print(void)
109 {
110    hour total;
111
112    std::cout << description << "\t";
113    for (int i=0; i<12; i++)
114    {
115       std::cout << spent_in_month[i] << "\t";
116       total += spent_in_month[i];
117    }
118    std::cout << total << "\t" << description << "\n";
119 }