I'm pretty new to java and trying to get a grip of using classes between each other. So this is a bank loan program for cars. I've been creating three different objects of the class Bil(Car) and when i run the objects through the method bilLån(), it only runs the last one in line, which is "Opel"-object in this case. It does run, so it must be a logical error?
Class bankLån
import java.util.Scanner;
import java.text.DecimalFormat;
public class bankLån {
DecimalFormat df1 = new DecimalFormat("#.##");
double price;
int years;
double interest;
double interestDiv;;
double oneYr = 14.99;
double threeYr = 10.99;
double fiveYr = 6.99;
double tenYr = 2.99;
double monthlyInterCalc;
double monthlyPayment;
double monthlyInterest;
int months = 12;
Scanner input = new Scanner(System.in);
bankLån() {
}
public void carLoan() {
System.out.println();
System.out.println("You've picked: "+Car.carBrand+"!");
price = (Car.carPrice) - (Car.downPay);
System.out.println("With a down payment of "+Car.downPay);
System.out.println("It will cost you: "+price+"!");
System.out.println("Our interests are: ");
System.out.println("1 year = "+oneYr+"% \t 3 years = "+threeYr+"% \t 5 years = "+fiveYr+"% \t 10 years = "+tenYr+"%");
do {
System.out.println("Please input amount of years: ");
years = input.nextInt();
if (years == 1) {
interest = oneYr;
}
else if (years == 3) {
interest = threeYr;
}
else if (years == 5) {
interest = fiveYr;
}
else if (years == 10) {
interest = tenYr;
}
else {
System.out.println("That amount of years ain't available, please try again.");
}
}while(!(years == 1 || years == 3 || years == 5 || years == 10));
interestDiv = interest / 100;
monthlyInterCalc = (price / years) / (months);
monthlyInterest = monthlyInterCalc * interestDiv;
monthlyPayment = monthlyInterCalc + (monthlyInterCalc * interestDiv);
System.out.println("Your interest will be: "+interest+"% and will cost you around: "+df1.format(monthlyInterest)+ " every month.");
System.out.print("The monthly payment will be around: "+df1.format(monthlyPayment)+".");
System.out.println();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Car ferrari = new Car("Ferrari","Red",1000000,50000);
Car volvo = new Car("Volvo","White",170000,40000);
Car opel = new Car("Opel","Blue",40000,10000);
ferrari.carLoan();
opel.carLoan();
volvo.carLoan();
}
}
Class Car
public class Car extends bankLån{
static String carBrand;
static String carColor;
static double carPrice;
static double downPay;
Car(String brand, String color, double price, double downP) {
carBrand = brand;
carColor = color;
carPrice = price;
downPay = downP;
}
}