# Calculate Monthly Depreciation and Accumulation

• 10-29-2013, 03:28 PM
BankTechy
Calculate Monthly Depreciation and Accumulation
i want to write a function which calculates the Monthly depreciation and accumulation of an asset but i am having difficulties writing it to calculate and insert it into mysql database.I will appreciate the help

Code:

private double getAccumulation(){
double Accumulation = 0;
double Monthly_Depreciation;
int Estimated_Useful_Life = Integer.parseInt(eul.getText());
double Cost_Of_Acquisition = Integer.parseInt(txt_coa.getText());
double Estimated_Residual_Value = Integer.parseInt(txt_erv.getText());
try{
Monthly_Depreciation = (Cost_Of_Acquisition - Estimated_Residual_Value)/Estimated_Useful_Life;
Accumulation = Monthly_Depreciation;
Accumulation++;
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
return Accumulation;
}

• 10-29-2013, 06:14 PM
Fou-Lu
And what is your question about this? Its syntactically correct assuming eul, txt_coa, and txt_erv are all class properties, so beyond that I don't know what the issue is.
• 11-05-2013, 02:14 PM
BankTechy
those are text fields not classes. i want to fuse this code into my program where the user enter the cost of acquisiton, residual value and life, it calculates and outputs the results in a jTable
• 11-05-2013, 02:17 PM
BankTechy
this is the updated code i wrote but with this the values are declared but i want the values to be declared in the program.

Code:

private void getAccumulation(){

try{

for(int row = 0; row <= 5; row++)
{

double Cost_Of_Acquisition = 2000;
double Estimated_Residual_Value = 250;
int Estimated_Useful_Life = 5;

int depreciationMethod = 1;

System.out.println (" acquisition cost of asset = " + (int)Cost_Of_Acquisition);
System.out.println (" salvage value = " + (int)Estimated_Residual_Value);
System.out.println (" number of months = " + Estimated_Useful_Life);
System.out.println (" depreciation method  = " + depreciationMethod);

for(int y=1; y<=5 ; y++) {
switch (depreciationMethod)
{

case 1: System.out.println (" straight line depreciation ");
double StraightLineDepreciation = ((Cost_Of_Acquisition-Estimated_Residual_Value)/Estimated_Useful_Life);
double AccumulatedDeprecaitionSL = (StraightLineDepreciation*y);
System.out.println ("Asset number " + "1" + " uses straight line depreciation");
System.out.println (" depreciation charge for asset number " + "1" + " in month " + y + "=" + StraightLineDepreciation);
System.out.println ("accumulated depreciation is " + AccumulatedDeprecaitionSL);
break;
case 2: System.out.println (" sum of months digits depreciation ");
int SumofMonths =(0);
break;

}
}

}

}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}

}

• 11-05-2013, 02:49 PM
Fou-Lu
Actually, those inputs are objects. If they were not, you could not use .getText() on it. That said, that wasn't what I was referring to, what I was referring to is that they must be declared as a class property to not throw a fatal error during compilation since they are not declared and instantiated within this method.

First you have to decide if you are using a gui or a cli interface. The first block indicates you are using a gui, while the second indicates you are using a command line (but with a gui error box, which doesn't really make sense).
If you want to use a JTable, you'll need to use the gui components. The JTable will require you to provide it with a multi-dimensional array of any type (but all the same type), use a Vector of any type (in particular, Vector<Vector<T>>), or a TableModel. If you are mixing datatypes, a custom TableModel would be the best solution.
I personally would write a custom TableModel to do this with. Simply write a collection class and implement the TableModel interface, and then ensure that you trigger a tableChanged event on the listener whenever you change something within the dataset. This allows the table to repaint itself whenever anything within the collection is added or removed. A custom listener could be created to an enclosing class that allows modification to also handle a repaint, but typically that would only make sense if you can modify the dataset from an alternative location while the JTable component is available for viewing.

The alternative is to use a concrete Object[][] and dump data into that. When all data is available, you construct the JTable object by providing it with the Object[][]. Same goes with the Vector approach. Both are easy to write, but not very flexible.