45 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Haskell
		
	
	
	
	
	
			
		
		
	
	
			45 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Haskell
		
	
	
	
	
	
| 
 | |
| module BasicTypes
 | |
| where
 | |
| 
 | |
| import Debug.Trace
 | |
| import Text.Printf
 | |
| import Text.Regex
 | |
| import Data.List
 | |
| 
 | |
| import Utils
 | |
| 
 | |
| 
 | |
| type Date = String
 | |
| type Status = Bool
 | |
| 
 | |
| -- amounts
 | |
| -- amount arithmetic currently ignores currency conversion
 | |
| 
 | |
| data Amount = Amount {
 | |
|                       currency :: String,
 | |
|                       quantity :: Double
 | |
|                      } deriving (Eq,Ord)
 | |
| 
 | |
| instance Num Amount where
 | |
|     abs (Amount c q) = Amount c (abs q)
 | |
|     signum (Amount c q) = Amount c (signum q)
 | |
|     fromInteger i = Amount "$" (fromInteger i)
 | |
|     (+) = amountAdd
 | |
|     (-) = amountSub
 | |
|     (*) = amountMult
 | |
| Amount ca qa `amountAdd` Amount cb qb = Amount ca (qa + qb)
 | |
| Amount ca qa `amountSub` Amount cb qb = Amount ca (qa - qb)
 | |
| Amount ca qa `amountMult` Amount cb qb = Amount ca (qa * qb)
 | |
| 
 | |
| instance Show Amount where show = amountRoundedOrZero
 | |
| 
 | |
| amountRoundedOrZero :: Amount -> String
 | |
| amountRoundedOrZero (Amount cur qty) =
 | |
|     let rounded = printf "%.2f" qty in
 | |
|     case rounded of
 | |
|       "0.00"    -> "0"
 | |
|       "-0.00"   -> "0"
 | |
|       otherwise -> cur ++ rounded
 | |
| 
 |