import java.io.*;
import java.util.StringTokenizer;
/*
* Your task is to write a program that calculates the first time,
* measured in months, that a car buyer owes less money than a
* car is worth. For this problem, depreciation is specified as
* a percentage of the previous month's value.
*/
public class loan {
//static String inputFilePath = "loan.in";
//static String outputFilePath = "loan.out";
public static void main(String[] args) throws IOException {
//InputStream inputStream = new FileInputStream(new File(inputFilePath));
//OutputStream outputStream = new FileOutputStream(new File(outputFilePath));
//Reader.init(inputStream);
Reader.init(System.in);
int loanDuration;
double downPayment;
double loanAmount;
double carValue;
int noOfDepr;
double monthlyPayment;
while((loanDuration = Reader.nextInt()) > 0) {
//System.out.println("New test case!");
downPayment = Reader.nextDouble();
loanAmount = Reader.nextDouble();
noOfDepr = Reader.nextInt();
monthlyPayment = loanAmount/loanDuration;
carValue = downPayment + loanAmount;
//System.out.println("Initial car value: " + carValue);
int readedDepr = 0;
int lastReadedDeprMonth = -1;
double lastReadedDeprValue = 0;
double monthDeprPercentage = 0;
int currentDepr = 0;
for(int currentMonth = 0; ; currentMonth++) {
if(readedDepr < noOfDepr && lastReadedDeprMonth < currentMonth) {
lastReadedDeprValue = monthDeprPercentage;
currentDepr = Reader.nextInt();
monthDeprPercentage = Reader.nextDouble();
readedDepr++;
lastReadedDeprMonth = currentDepr;
//System.out.println("DEBUG: readedDepr: " + readedDepr + " in month: " + currentMonth);
}
if(currentDepr > currentMonth) {
carValue -= carValue * lastReadedDeprValue;
} else {
carValue -= carValue * monthDeprPercentage;
}
//System.out.println("Doing work. month: " + currentMonth + " carVal: " + carValue + "loan:" + loanAmount);
if(carValue > loanAmount) {
//System.out.println("Month: " + currentMonth);
if(currentMonth == 1) {
//outputStream.write((currentMonth + " month\n").getBytes());
System.out.println(currentMonth + " month");
} else {
//outputStream.write((currentMonth + " months\n").getBytes());
System.out.println(currentMonth + " months");
}
//System.out.println("DEBUG: readedDepr: " + readedDepr + " deprs: " + noOfDepr);
while(readedDepr < noOfDepr) {
currentDepr = Reader.nextInt();
monthDeprPercentage = Reader.nextDouble();
readedDepr++;
//System.out.println("DEBUG: SKIPPED readedDepr: " + readedDepr + " deprs: " + noOfDepr);
}
break;
}
loanAmount -= monthlyPayment;
}
}
}
}
/* Class for buffered reading int and double values */
/* http://www.cpe.ku.ac.th/~jim/java-io.html */
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/** call this method to initialize reader for InputStream */
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input) );
tokenizer = new StringTokenizer("");
}
/** get next word */
static String next() throws IOException {
while ( ! tokenizer.hasMoreTokens() ) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine() );
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt( next() );
}
static double nextDouble() throws IOException {
return Double.parseDouble( next() );
}
}
10/08/2013
10114 - Loansome Car Buyer - Java
Subscribe to:
Post Comments (Atom)
This comment has been removed by a blog administrator.
ReplyDelete