I am beginner in dart and I am currently learning about classes and methods. I just want call the method I have created in the interest class to the main function so that it performs the define operations. I don't want to implement any functions or variables in main. I don't know if it is possible I am trying out something; that probably when you want hide some business logic from the public. please help me. I believe there is but I just can't find it.
import 'dart:io';
class Interest {
double principal;
double rate;
int time;
Interest(double aPrincipal, double aRate, int aTime) {
this.principal = aPrincipal;
this.rate = aRate;
this.time = aTime;
}
double interestAmnt() {
print(
"This is a program to Calculate the over all amount back for your loan");
print("What is the Amount of loan you have taken: ");
this.principal = double.parse(stdin.readLineSync());
print("What is the interest on your loan: ");
this.rate = double.parse(stdin.readLineSync());
print("How long are paying for the long: ");
this.time = int.parse(stdin.readLineSync());
return this.principal * (1 + (this.rate * this.time));
}
}
main(List<String> args) {
}
make your function static, like this:
static double interestAmnt()
And in your main, call your function like this:
main(List<String> args) {
Interest.interestAmnt();
}
There are many ways to do what you like. Dart support top level function but since you prefer creating an object that holds its own business logic, I modified your example to work with some comments:
import 'dart:io';
void main(List<String> args) {
// you should get your variables here, since it's not the responsibility of the Interest class
print('This is a program to Calculate the over all amount back for your loan');
print('What is the Amount of loan you have taken: ');
final principal = double.parse(stdin.readLineSync());
print('What is the interest on your loan: ');
final rate = double.parse(stdin.readLineSync());
print('How long are paying for the long: ');
final time = int.parse(stdin.readLineSync());
// Now you've all the variables needed to create an interest object,
// so create the object which has all the business logic
final interest = Interest(principal, rate, time);
// do operations on the created object
print(interest.interestAmnt());
}
// this is an interest object with one constructor, three class members, and one method.
class Interest {
// mark these as final since they'll not change in the lifetime of the object
final double principal;
final double rate;
final int time;
// unlike some other languages, you can do it this way in dart:
Interest(this.principal, this.rate, this.time);
// constructor body isn't necessary in this case since we are taking the variables as is
// take a look at the dart language tour to understand:
// - how constructors work,
// - initializer list
// - constructor body
// each has its own use case but for your case, this works fine
// this is a class method that can has access to the instance variables
// previously, you had this placed inside the constructor body and so it
// so it was inaccessible
double interestAmnt() {
return principal * (1 + (rate * time));
}
}
The main problem in your example you were defining functions in the constructor body that were not accessible by the class itself. A lot of these terms may sound new to you, but overtime you'll get used to it.
Here's a link for the language tour where you can learn more about Dart: https://dart.dev/guides/language/language-tour
I was able to solve the issue with the guidance above but I still believe there is another way out. thanks a lot guys; Thanks very much!
// interest rate
import 'dart:io';
class Interest {
static double principal;
static double rate;
static double time;
static double interestAmnt() {
print(
"This is a program to Calculate the over all amount back for your loan");
print("What is the Amount of loan you have taken: ");
principal = double.parse(stdin.readLineSync());
print("What is the interest on your loan: ");
rate = double.parse(stdin.readLineSync());
print("How long are paying for the long: ");
time = double.parse(stdin.readLineSync());
double res = principal * (1 + (rate * time));
print(res);
return res;
}
}
main(List<String> args) {
Interest.interestAmnt();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.