Aggiunta fantastica classe TCurrency per la mistica gestione valutaria
git-svn-id: svn://10.65.10.50/trunk@6614 c028cbd2-c16b-5b4b-a496-9718f37d4682
This commit is contained in:
parent
e153f64143
commit
233593b7fb
include
217
include/currency.cpp
Executable file
217
include/currency.cpp
Executable file
@ -0,0 +1,217 @@
|
||||
#include <currency.h>
|
||||
#include <recarray.h>
|
||||
|
||||
///////////////////////////////////////////////////////////
|
||||
// DowJones
|
||||
///////////////////////////////////////////////////////////
|
||||
|
||||
class TDowJones : public TFile_cache
|
||||
{
|
||||
struct TExchangeData : public TObject
|
||||
{
|
||||
real _num;
|
||||
real _den;
|
||||
int _dec;
|
||||
|
||||
public:
|
||||
const TExchangeData& operator=(const TExchangeData& d)
|
||||
{ _num = d._num; _den = d._den; _dec = d._dec; return d; }
|
||||
TExchangeData() : _num(1.0), _den(1.0), _dec(0) { }
|
||||
};
|
||||
|
||||
char _defval[4];
|
||||
|
||||
protected:
|
||||
virtual TObject* rec2obj(const TRectype& rec) const;
|
||||
|
||||
void test_cache();
|
||||
const TExchangeData& get(const char* key);
|
||||
|
||||
public:
|
||||
const char* get_defval();
|
||||
|
||||
real exchange(const real& num, const char* fromval, const char* toval);
|
||||
int get_dec(const char* val);
|
||||
|
||||
TDowJones() : TFile_cache("%VAL") { }
|
||||
virtual ~TDowJones() { }
|
||||
} DowJones;
|
||||
|
||||
TObject* TDowJones::rec2obj(const TRectype& rec) const
|
||||
{
|
||||
TExchangeData* data = new TExchangeData;
|
||||
data->_num = rec.get_real("R12");
|
||||
if (data->_num < 1.0)
|
||||
data->_num = 1.0;
|
||||
data->_den = rec.get_real("R13");
|
||||
if (data->_den > ZERO)
|
||||
{
|
||||
data->_dec = rec.get_int("I0");
|
||||
}
|
||||
else
|
||||
{
|
||||
data->_den = rec.get_real("R10");
|
||||
data->_num = 1.0;
|
||||
data->_dec = rec.get("CODTAB").empty() ? 0 : 2;
|
||||
}
|
||||
if (data->_den <= ZERO)
|
||||
{
|
||||
NFCHECK("Cambio nullo: %s = %s",
|
||||
(const char*)rec.get("CODTAB"),
|
||||
data->_den.string());
|
||||
data->_den = 1.0;
|
||||
data->_num = 1.0;
|
||||
data->_dec = 0;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
void TDowJones::test_cache()
|
||||
{
|
||||
if (_cache.items() == 0)
|
||||
{
|
||||
fill();
|
||||
|
||||
*_defval = '\0';
|
||||
FOR_EACH_ASSOC_OBJECT(_cache, hash, key, obj)
|
||||
{
|
||||
const TExchangeData* data = (const TExchangeData*)obj;
|
||||
if (data->_num == 1.0 && data->_den == 1.0)
|
||||
{
|
||||
strcpy(_defval, key);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (*_defval == '\0')
|
||||
{
|
||||
strcpy(_defval, "LIT");
|
||||
_cache.add(_defval, new TExchangeData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const TDowJones::TExchangeData& TDowJones::get(const char* val)
|
||||
{
|
||||
test_cache();
|
||||
if (val == NULL || *val == '\0')
|
||||
val = _defval;
|
||||
return (const TExchangeData&)query(val);
|
||||
}
|
||||
|
||||
const char* TDowJones::get_defval()
|
||||
{
|
||||
test_cache();
|
||||
return _defval;
|
||||
}
|
||||
|
||||
real TDowJones::exchange(const real& num, // Importo da convertire
|
||||
const char* fromval, // Dalla valuta
|
||||
const char* toval) // Alla valuta
|
||||
{
|
||||
real n = num;
|
||||
if (n != ZERO)
|
||||
{
|
||||
const TExchangeData& datafrom = get(fromval);
|
||||
const TExchangeData& datato = get(toval);
|
||||
n *= datafrom._den * datato._num;
|
||||
n /= datafrom._num * datato._den;
|
||||
n.round(datato._dec);
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
int TDowJones::get_dec(const char* val)
|
||||
{
|
||||
const TExchangeData& data = get(val);
|
||||
return data._dec;
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////
|
||||
// TCurrency
|
||||
///////////////////////////////////////////////////////////
|
||||
|
||||
void TCurrency::change_value(const char* val)
|
||||
{
|
||||
if (_num != ZERO)
|
||||
_num = DowJones.exchange(_num, _val, val);
|
||||
strncpy(_val, val, 3);
|
||||
}
|
||||
|
||||
void TCurrency::read(const TRectype& rec, const char* field, const char* val)
|
||||
{
|
||||
_num = rec.get_real(field);
|
||||
if (val == NULL || *val == '\0')
|
||||
val = "_VALUTA";
|
||||
if (rec.exist(val))
|
||||
strcpy(_val, rec.get(val));
|
||||
else
|
||||
*_val = '\0';
|
||||
if (*_val == '\0')
|
||||
strcpy(_val, DowJones.get_defval());
|
||||
}
|
||||
|
||||
void TCurrency::write(TRectype& rec, const char* field, const char *val, bool forceval) const
|
||||
{
|
||||
if (val == NULL || *val == '\0')
|
||||
val = "_VALUTA";
|
||||
if (forceval || rec.exist(val))
|
||||
{
|
||||
const TString& recval = rec.get(val);
|
||||
if (recval != _val)
|
||||
{
|
||||
if (forceval || recval.empty())
|
||||
rec.put(val, _val);
|
||||
else
|
||||
NFCHECK("Scrittura della valuta %s su di un record con valuta %s",
|
||||
_val, (const char*)recval);
|
||||
}
|
||||
}
|
||||
rec.put(field, _num);
|
||||
}
|
||||
|
||||
|
||||
int TCurrency::compare(const TSortable& s) const
|
||||
{
|
||||
const TCurrency& cur = (const TCurrency&)s;
|
||||
if (stricmp(cur._val, _val) == 0)
|
||||
{
|
||||
return _num == cur._num ? 0 : (_num > cur._num ? +1 : -1);
|
||||
}
|
||||
TCurrency curr(cur);
|
||||
curr.change_value(_val);
|
||||
return _num == curr._num ? 0 : (_num > curr._num ? +1 : -1);
|
||||
}
|
||||
|
||||
const TCurrency& TCurrency::operator=(const TCurrency& cur)
|
||||
{
|
||||
_num = cur._num;
|
||||
strcpy(_val, cur._val);
|
||||
return cur;
|
||||
}
|
||||
|
||||
const char* TCurrency::string(bool dotted) const
|
||||
{
|
||||
TString16 picture;
|
||||
if (dotted)
|
||||
{
|
||||
picture << '.';
|
||||
const int dec = DowJones.get_dec(_val);
|
||||
if (dec > 0)
|
||||
picture << dec;
|
||||
}
|
||||
return _num.string(picture);
|
||||
}
|
||||
|
||||
TCurrency::TCurrency(const TCurrency& cur)
|
||||
: _num(cur._num)
|
||||
{
|
||||
strcpy(_val, cur._val);
|
||||
}
|
||||
|
||||
TCurrency::TCurrency(const real& num, const char* val)
|
||||
: _num(num)
|
||||
{
|
||||
strncpy(_val, val, 3);
|
||||
}
|
||||
|
39
include/currency.h
Executable file
39
include/currency.h
Executable file
@ -0,0 +1,39 @@
|
||||
#ifndef __CURRENCY_H
|
||||
#define __CURRENCY_H
|
||||
|
||||
#ifndef __REAL_H
|
||||
#include <real.h>
|
||||
#endif
|
||||
|
||||
#ifndef __RECTYPES_H
|
||||
class TRectype;
|
||||
#endif
|
||||
|
||||
class TCurrency : public TSortable
|
||||
{
|
||||
real _num;
|
||||
char _val[4];
|
||||
|
||||
protected:
|
||||
virtual int compare(const TSortable& s) const;
|
||||
|
||||
public:
|
||||
void change_value(const char* newval);
|
||||
const char* get_value() const { return _val; }
|
||||
|
||||
void set_num(const real& num) { _num = num; }
|
||||
const real& get_num() const { return _num; }
|
||||
|
||||
const TCurrency& operator=(const TCurrency& cur);
|
||||
|
||||
const char* string(bool dotted = FALSE) const;
|
||||
void read(const TRectype& rec, const char* field, const char *val = NULL);
|
||||
void write(TRectype& rec, const char* field, const char *val = NULL, bool forceval = FALSE) const;
|
||||
|
||||
TCurrency() { _val[0] = '\0'; }
|
||||
TCurrency(const TCurrency& cur);
|
||||
TCurrency(const real& num, const char* val = "");
|
||||
virtual ~TCurrency() { }
|
||||
};
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user