What is the best way of manipulating the order things are done based on some conditions (other than writing them again with the different order)?
Let's say there is a Person class and each object of Person represents a different human.
class Person{
int eatingPriority = 3;
int sleepingPriority = 2;
int recreationPriority = 1;
void eat() {/*eats*/}
void sleep() {/*sleeps*/}
void watchTv() {/*watches tv*/}
void satisfyNeeds() {
//HOW TO DO THIS
}
}
How can I make the satisfyNeeds()
methods call the other three methods based on their priority?
Note: I want to make it clear that priorities can change from Person to Person.
Declaration order of methods never matters in C# or Java. Likewise it doesn't matter whether you declare a method before or after a variable that it uses.
In this program, you have to first make a class name 'CallingMethodsInSameClass' inside which you call the main() method. This main() method is further calling the Method1() and Method2(). Now you can call this as a method definition which is performing a call to another lists of method.
To class a method of another class, we need to have the object of that class. Here, we have a class Student that has a method getName() . We access this method from the second class SimpleTesting by using the object of the Student class.
It is not recursion, it is overloading. The two add methods (the one in your snippet, and the one "provided" by ArrayList that you are extending) are not the same method, cause they are declared with different parameters.
You can do this with 1 class and 1 interface.
public class Person {
int eatingPriority = 3;
int sleepingPriority = 2;
int recreationPriority = 1;
PriorityQueue<Action> actions;
void eat() { }
void sleep() { }
void watchTv() { }
public Person() {
actions = new PriorityQueue<Action>(new Comparator<Action>() {
@Override
public int compare(Action o1, Action o2) {
return o2.getPriority() - o1.getPriority();
}
});
actions.add(new Action() {
@Override
public int getPriority() {
return eatingPriority;
}
@Override
public void execute() {
eat();
}
});
actions.add(new Action() {
@Override
public int getPriority() {
return sleepingPriority;
}
@Override
public void execute() {
sleep();
}
});
actions.add(new Action() {
@Override
public int getPriority() {
return recreationPriority;
}
@Override
public void execute() {
watchTv();
}
});
}
public void satisfyNeeds() {
for (Action action : actions) {
action.execute();
}
}
interface Action {
public int getPriority();
public void execute();
}
}
Here is another possible implementation :
abstract class Need {
abstract void satisfy();
}
class Eat extends Need {
@Override
public void satisfy() { /* eat ...*/}
}
class Sleep extends Need {
@Override
public void satisfy() { /* sleep ...*/}
}
class DrinkBeer extends Need {
@Override
public void satisfy() { /* drink beer ...*/}
}
class Person{
// TreeMap will sort the map in the key's natural order (a int here)
private Map<Integer, Need> needs = new TreeMap<>();
Person() {
add(new Eat(), 3);
add(new Sleep(), 2);
add(new DrinkBeer(), 1);
}
void add(Need need, int priority) {
needs.put(Integer.valueOf(priority), need);
}
void satisfyNeeds() {
for(Need need : needs.values())
need.satisfy();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With