Skip to content

Instantly share code, notes, and snippets.

@vicsstar
Created February 8, 2018 00:56
Show Gist options
  • Save vicsstar/22c864994ab32c613851c9ce6e00fbb5 to your computer and use it in GitHub Desktop.
Save vicsstar/22c864994ab32c613851c9ce6e00fbb5 to your computer and use it in GitHub Desktop.
Calculates depreciation value based on a cost and depreciation year.
import java.util.Scanner;
public class Depreciation {
private static Scanner scanner = new Scanner(System.in);
private static Column[] columns = {
new Column("Year", " "),
new Column("Start Value", " "),
new Column("Amt. Depreciated", " "),
new Column("Total Depreciation", "")
};
private static void print(Object obj) {
System.out.print(obj);
}
private static void println(Object obj) {
System.out.println(obj);
}
private static String nextInputFromConsole(String instruction) {
print(instruction);
return scanner.next();
}
private static void displayDepreciationTable(Float maxValue, Integer estimatedLife) {
for (Column column : columns) {
print(column.getName() + column.getPaddingSuffix());
}
println("");
Float currentValue = maxValue;
Float totalDepreciation = 0f;
for (int i = 0; i < estimatedLife; i++) {
if (i != estimatedLife - 1) {
// The character "d" is used to denote a double value in Java.
Float depreciationValue = (2f / estimatedLife) * currentValue;
totalDepreciation = totalDepreciation + depreciationValue;
printWithPadding(i + 1, currentValue, depreciationValue, totalDepreciation);
currentValue = currentValue - depreciationValue;
} else {
printWithPadding(i + 1, currentValue, currentValue, maxValue);
}
}
}
/**
* Because the console generally uses font with same with per character,
* we will use space to add some padding to our output.
*/
private static void printWithPadding(Integer year, Float startValue,
Float depreciationAmount, Float totalDepreciation) {
for (int i = 0; i < columns.length; i++) {
// get the associated column for the sake of using it's padding value.
final Column column = columns[i];
switch (i) {
case 0:
print(year);
break;
case 1:
print(startValue);
break;
case 2:
print(depreciationAmount);
break;
case 3:
print(totalDepreciation);
break;
default:
}
print(column.getPaddingSuffix());
}
println("");
}
public static void main(String... args) throws NumberFormatException {
final Float maxValue = 100000f;
Float cost;
do {
try {
cost = Float.parseFloat(nextInputFromConsole("Enter cost: "));
if (cost <= 0) {
println("Please enter a positive value for cost.");
cost = null;
} else if (cost > maxValue) {
println("Please enter a value for cost <= " + maxValue);
}
} catch (NumberFormatException e) {
cost = null;
}
} while (cost == null);
println("");
Integer estimatedLife = Integer.parseInt(nextInputFromConsole("Enter estimated life: "));
displayDepreciationTable(cost, estimatedLife);
}
/**
* Internal class to help with the logic for storing and displaying columns on the console.
*/
static class Column {
private String name;
private String paddingSuffix;
Column(String name, String paddingSuffix) {
this.name = name;
this.paddingSuffix = paddingSuffix;
}
String getName() {
return name;
}
String getPaddingSuffix() {
return paddingSuffix;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment