-3

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;


    }


}

  • 6
    All your fields are `static`, which means they exist per-class and not per-instance. Remove the `static` qualifier. – Joachim Sauer May 04 '20 at 10:07
  • 1
    The fact that `Bil` extends `bankLån` is weird. Is Car a Bank Loan? Do we have "is a" relation here? – Amongalen May 04 '20 at 10:09
  • Can I suggest that you should stick to one language: `Bil`, `Lån` are Danish or Norwegian (sorry, don't know which)? `carBrand`, `carColor` etc are English. Use one or the other consistently. – Andy Turner May 04 '20 at 10:20
  • Hey Andy! Sorry for that, now everything is in English except the name of the "main" class. – JimNotCarry May 04 '20 at 10:40

2 Answers2

0
public class Bil extends bankLån {

static String carBrand;
static String carColor;
static double carPrice;
static double downPay;

In the class Bil, all your variables are static. Meaning the values they hold would be the same for all the different objects of this class. This causes your object ferrari, and volvo to have the same attribute values as opel, as opel is set in the last.

Remove the static keyword before these variables

The method carLoan is accessing Car as a static class, instead of its objects.

System.out.println("You've picked: "+Car.carBrand+"!"); 
    price = (Car.carPrice) -  (Car.downPay);

Car is not an instance object, it is referring to the Static Class, hence the error you get "Cannot make a static reference to the non-static field Bil.carBrand"

To fix this, take a Car object inside your method and access it instead of the class.

public void carLoan(Car car) {

    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();

}
Farhan Qasim
  • 990
  • 5
  • 18
  • I hear you, and i've been trying to get rid of them, but then an error occurs on the values of the objects, "bil.carBrand" for example in the method, "Cannot make a static reference to the non-static field Bil.carBrand". Thats the only reason i changed it to static in the first place. – JimNotCarry May 04 '20 at 10:15
  • @JimNotCarry I am not sure how you are even accessing the method carLoan() as it is a method inside the Main class and not the Car class. Move that method to the car class, and instead of using the variable Car, use this. – Farhan Qasim May 04 '20 at 11:06
0

@Farhan Qasim said you need to remove static .

but if you want to do process in every object you need to pass object reference in method bilLån() like

public void bilLån(Bil  mBill) {

        System.out.println();

        System.out.println("You've picked: "+Bil.carBrand+"!"); 
        price = (mBill.carPrice) -  (mBill.downPay);

        System.out.println("With a down payment of "+mBill.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();

    }

in main method :

  public static void main(String[] args) {
        // TODO Auto-generated method stub

        Bil ferrari = new Bil("Ferrari","Red",1000000,50000);
        Bil volvo = new Bil("Volvo","White",170000,40000);
        Bil opel = new Bil("Opel","Blue",40000,10000);


        ferrari.bilLån(ferrari);
        opel.bilLån(volvo);
        volvo.bilLån(opel);

    }
Vishal Nagvadiya
  • 1,178
  • 12
  • 15