# Depreciation Calculator / asset.cpp

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160``` ```/* * Asset.hpp * Created: 18/05/2010 * Last updated: 19/05/2010 * Author: Arun.K.R */ #ifndef ASSET_CPP #define ASSET_CPP #include #include #include #include #include class Assets{ //Data Members private: int code; //asset code char name[30]; //asset name char type[1]; //asset type (either M/F for Mobile/FIxed) int quantity; //quantity int usefulLifeYears; //Useful Life years of the asset double assetValue; //Value of the asset double salvageValue; //Value of asset after a period //Member Functions private: int sumUptoN(int); //To calculate sum of the disgits of useful Life Years. double* straightLine(); //to calculate the depreciation of fixed assets. double* sumOfYearsDigits(); //to calculate the depreciation of mobile assets. public: double* createDepriciationTable(); //to create depriciation table based upon asset type. void getAssets(); // read data into the data members void showAssets(); //print data into screen int getCode(){ //returns code return code; } //returns usefulLifeYears. It's useful since, it's the same as depriciation table's size. // In c++ finding size of dynamic array is not possible when, array is actually a pointer. int getUsefulLifeYears(){ return usefulLifeYears; } }; // Function Definitions /* * Calculates depriciation in straight-ine method * returns : Depriciation array of double values */ double* Assets::straightLine(){ double depriciationPerYear = assetValue/usefulLifeYears; double *depriciation = new double[usefulLifeYears]; for(int i = 0; i < usefulLifeYears; ++i){ depriciation[i] = depriciationPerYear; /* using calculated value over and over has a speed advantage over * doing calculation each time it loops. */ } return depriciation; } /* * Calculates depriciation in sum-of-years-digit method * returns : Depriciation array of double values */ double* Assets::sumOfYearsDigits(){ double tmpYr = usefulLifeYears; double sumYr = sumUptoN(tmpYr); double *depriciation = new double[usefulLifeYears]; for(int i = 0; i < usefulLifeYears; ++i){ depriciation[i] = assetValue * (tmpYr-- / sumYr); } return depriciation; } /* * Calculates sum upto N * ie., sum upto 3 is 1+2+3. ie., 6 * returns : Sum upto N */ int Assets::sumUptoN(int N){ int result = 0; for(int i = 0; i <= N; --N){ result += N; } return result; } /* * Call appropriate depriciation method based on asset type. * straight-ine method for Fixed assets. * sum-of-years-digit method for Mobile assets. * returns : Depriciation array of double values */ double* Assets::createDepriciationTable(){ double *depriciationTbl; if(type[0] == 'F' || type[0] == 'f'){ depriciationTbl = straightLine(); }else if(type[0] == 'M' || type[0] == 'm'){ depriciationTbl = sumOfYearsDigits(); }else{ //something wrong happened somewhere exit(-1); } return depriciationTbl; } /* * Read data into class * returns : nothing */ void Assets::getAssets(){ clrscr(); cout<<"\n\n"<<" Asset Code : "; cin>>code; cout<<" Asset name : "; gets(name); cout<<" Type - [M]obile / [F]ixed : "; gets(type); cout<<" Quantity : "; cin>>quantity; cout<<" Useful life (years) : "; cin>>usefulLifeYears; cout<<" Asset value (QR) : "; cin>>assetValue; cout<<" Salvage value (QR) : "; cin>>salvageValue; //Each entry is not checked for correctness since it's tiresome //instead at main class input is shown to user for verification //if found error get input once again } /* * Print data into screen. It's for testing validity of data in the developing stage & no use there after. * returns : nothing */ void Assets::showAssets(){ cout<<"\n\n"<<" Asset Code : "<