Going through the topic Constructors
Wanted to implement the concept
you should not put your program's logic inside a constructor.
For the following I tried to
Put all the logic in a separate method. printFactoryInfo().
Where I pass a CarFactory object to it as an argument.
putting all the logic there, and simultaneously handle potential errors (like one involving zero years).
Getting Error as as
public class CarFactory{
String name;
int age;
int carsCount;
public CarFactory(String name, int age, int carsCount){
this.name = name;
this.age = age;
this.carsCount = carsCount;
}
void printFactoryInfo(String name, int age, int carsCount){
System.out.println("Our car factory is called " +this.name);
System.out.println("It was founded " +this.age+ " years ago");
System.out.println("Since that time, it has produced " + this.carsCount + " cars");
System.out.println("On average, it produces " + (this.carsCount/this.age) + " cars per year");
}
public static void main(String[] args) {
//CarFactory ford = new CarFactory("Ford", 115 , 50000000);
CarFactory ford = new CarFactory("Amigo Motors", 0 , 1000);
ford.printFactoryInfo();
}
}