Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
stocks.h
Go to the documentation of this file.
1
29#ifndef STOCKS_H_INCLUDED
30#define STOCKS_H_INCLUDED
31
32#include <ctime>
33#include <locale>
34#include <string>
35#include <deque>
36#include <stdexcept>
37#include <iterator>
38#include <sstream>
39
40#include <examples.h>
41
42
43// The stock exchange example displays stock quotes at various exchanges
44// distributed globally.
45
46// The objective of the example is to show monetary, numeric, and date
47// and time values formated using standard and/or user-defined facets.
48
49
50#if defined (_AIX)
51# define US_LOCALE "en_US"
52# define GERMAN_LOCALE "de_DE"
53# define FRENCH_LOCALE "fr_FR"
54# define JAPANESE_LOCALE "ja_JP"
55#elif defined (__hpux)
56# define US_LOCALE "en_US.iso88591"
57# define GERMAN_LOCALE "de_DE.iso88591"
58# define FRENCH_LOCALE "fr_FR.iso88591"
59# define JAPANESE_LOCALE "ja_JP.eucJP"
60#elif defined (__linux__)
61# define US_LOCALE "en_US"
62# define GERMAN_LOCALE "de_DE"
63# define FRENCH_LOCALE "fr_FR"
64# define JAPANESE_LOCALE "ja_JP"
65#elif defined (__osf__)
66# define US_LOCALE "en_US.ISO8859-1"
67# define GERMAN_LOCALE "de_DE.ISO8859-1"
68# define FRENCH_LOCALE "fr_FR.ISO8859-1"
69# define JAPANESE_LOCALE "ja_JP"
70#elif defined (SNI)
71# define US_LOCALE "En"
72# define GERMAN_LOCALE "De"
73# define FRENCH_LOCALE "Fr_FR.850"
74# define JAPANESE_LOCALE "Ja_JP.932"
75#elif defined (_WIN32)
76# define US_LOCALE "us"
77# define GERMAN_LOCALE "german_germany.1252"
78# define FRENCH_LOCALE "french_france.1252"
79# define JAPANESE_LOCALE "japanese_japan.1252"
80#else // POSIX simple names
81# define US_LOCALE "en_US"
82# define GERMAN_LOCALE "de_DE"
83# define FRENCH_LOCALE "fr_FR"
84# define JAPANESE_LOCALE "ja_JP"
85#endif
86
87
88namespace {
89 const int stock_change[10] = { 0, 1, -1, 2, -1, 3, -3, 4, -1, -1 };
90 const int indexSize = 10;
91 struct std::tm tmb;
92}
93
94
95struct Company
96{
97 Company (const std::string &s, double p)
98 : companyName (s), offerPrice (p), stockPrice (0) {}
99
100 //In actuality a company should not manipulate stocks.
101 //For simplicity let the company play with its own stock.
102
103 void updateStock () {
105 if (stockPrice < 0)
107 }
108
109 int stockChange () {
110 return stock_change[changeIndex(true)];
111 }
112
113 int changeIndex (bool increment = false) {
114 static int i = 0;
115 if(increment)
116 ++i;
117 if (i > indexSize-1)
118 i = 0;
119 return i;
120 }
121
122 std::string companyName;
123 double offerPrice; //initial offer price
124 double stockPrice; //current market price
125
126private:
127 std::string companyNews; //Make use of messaging (if at all it works !)
128
129 std::size_t marketOutlook() {
130 return changeIndex();
131 }
132};
133
134
135struct StockXchange: public std::locale::facet
136{
137 typedef std::basic_ostream<char,std::char_traits<char> > outStream;
138 typedef std::ostreambuf_iterator<char, std::char_traits<char> > iter_type;
139 typedef std::deque<Company*,std::allocator<Company*> > database;
140
141 typedef std::locale::facet facet;
142
143 StockXchange(std::size_t refs=0): facet(refs){}
144 virtual ~StockXchange();
145
146 static std::locale::id id;
147 virtual bool put (std::ostream& os) const;
148 virtual void add (const std::string& name, double initPrice);
149 virtual void localTime (std::ostream&) const;
150
151protected:
153
156};
157
158
160{
161 TokyoStockXchange (std::size_t refs = 0)
162 : StockXchange (refs) { }
163
164 virtual void localTime (std::ostream& os) const {
165 // our contrived GMT time is 07:43:00, to use an actual time,
166 // replace the following tmb assignments with:
167 // tmb = std::gmtime (&tm);
168 tmb.tm_sec = 0;
169 tmb.tm_min = 43;
170 tmb.tm_hour = 7;
171 tmb.tm_mday = 4;
172 tmb.tm_mon = 6;
173 tmb.tm_year = 88;
174 tmb.tm_wday = 1;
175 tmb.tm_yday = 185;
176 tmb.tm_isdst = 1;
177
179 }
180
181 virtual bool put (std::ostream& os) const {
182 os << '\n';
183 os << "######## TOKYO STOCK EXCHANGE #########\n";
184 return StockXchange::put (os);
185 }
186};
187
188
190{
191 FrankfurtStockXchange (std::size_t refs = 0)
192 : StockXchange (refs) { }
193
194 virtual void localTime(std::ostream& os) const {
195 // our contrived GMT time is 07:43:00, to use an actual time, replace
196 // the following three tmb assignments with:
197 // tmb = gmtime(&tm);
198 tmb.tm_hour=7;
199 tmb.tm_min=43;
200 tmb.tm_sec=00;
202 }
203
204 virtual bool put (std::ostream& os) const {
205 os << '\n';
206 os << "######## FRANKFURTER WERTPAPIERB\366RSE #########\n";
207 return StockXchange::put (os);
208 }
209};
210
211
213{
214 NewYorkStockXchange (std::size_t refs = 0)
215 : StockXchange (refs) { }
216
217 virtual void localTime(std::ostream& os) const {
218 // our contrived GMT time is 07:43:00, for a GMT-5 of 02:43:00
219 // to use an actual time, replace the following three tmb
220 // assignments with:
221 // tmb = localtime(&tm);
222 tmb.tm_hour=2;
223 tmb.tm_min=43;
224 tmb.tm_sec=00;
226 }
227
228 virtual bool put (std::ostream& os) const {
229 os << '\n';
230 os << "######## NEW YORK STOCK EXCHANGE ########\n";
231 return StockXchange::put (os);
232 }
233};
234
235
237{
238 ParisStockXchange (std::size_t refs = 0)
239 : StockXchange (refs){}
240
241 virtual void localTime(std::ostream& os) const {
242 // our contrived GMT time is 07:43:00, to use an actual time, replace
243 // the following three tmb assignments with:
244 // tmb = gmtime(&tm);
245 tmb.tm_hour=7;
246 tmb.tm_min=43;
247 tmb.tm_sec=00;
249 }
250
251 virtual bool put (std::ostream& os) const {
252 os << '\n';
253 os << "######## BOURSE DE PARIS #########\n";
254 return StockXchange::put (os);
255 }
256};
257
258#endif // STOCKS_H_INCLUDED
#define i
Definition 17.names.cpp:126
#define os
Definition 17.names.cpp:511
#define s
#define p
Definition 17.names.cpp:132
const char * name
Definition codecvt1.cpp:72
int changeIndex(bool increment=false)
Definition stocks.h:113
std::string companyName
Definition stocks.h:122
double offerPrice
Definition stocks.h:123
double stockPrice
Definition stocks.h:124
int stockChange()
Definition stocks.h:109
void updateStock()
Definition stocks.h:103
Company(const std::string &s, double p)
Definition stocks.h:97
virtual bool put(std::ostream &os) const
Definition stocks.h:204
virtual void localTime(std::ostream &os) const
Definition stocks.h:194
FrankfurtStockXchange(std::size_t refs=0)
Definition stocks.h:191
virtual void localTime(std::ostream &os) const
Definition stocks.h:217
NewYorkStockXchange(std::size_t refs=0)
Definition stocks.h:214
virtual bool put(std::ostream &os) const
Definition stocks.h:228
virtual void localTime(std::ostream &os) const
Definition stocks.h:241
virtual bool put(std::ostream &os) const
Definition stocks.h:251
ParisStockXchange(std::size_t refs=0)
Definition stocks.h:238
database companyDatabase
Definition stocks.h:152
static std::locale::id id
Definition stocks.h:146
virtual ~StockXchange()
Definition stocks.cpp:42
std::deque< Company *, std::allocator< Company * > > database
Definition stocks.h:139
std::basic_ostream< char, std::char_traits< char > > outStream
Definition stocks.h:137
virtual bool put(std::ostream &os) const
Definition stocks.cpp:72
virtual void localTime(std::ostream &) const
Definition stocks.cpp:52
std::ostreambuf_iterator< char, std::char_traits< char > > iter_type
Definition stocks.h:138
StockXchange(std::size_t refs=0)
Definition stocks.h:143
virtual void add(const std::string &name, double initPrice)
Definition stocks.cpp:66
std::locale::facet facet
Definition stocks.h:141
friend StockXchange::outStream & operator<<(StockXchange::outStream &, const StockXchange &)
virtual bool put(std::ostream &os) const
Definition stocks.h:181
virtual void localTime(std::ostream &os) const
Definition stocks.h:164
TokyoStockXchange(std::size_t refs=0)
Definition stocks.h:161