Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
There is java bean with list of elements and discount. I have to apply some calculate logic in those elements using recursion until that discount value gets zero.
Current implementation
for(CustomClass custom : customList) {
Pair<CustomClass, Integer> returnVal = myMethod(custom, discount);
}
private Pair<CustomClass, Integer> myMethod(CustomClass custom, Integer discount) {
pair.getKey().add(custom.setAmount(custom.getAmount - discount));
pair.getValue().add(discount - custom.getAmount);
return pair;
}
I have to do something like above and have to reuse the discount in multiple elements in the customList until it is zero. I want to make it recursive so that I get the discounted value in the CustomClass and discount is reduced.
returnVal has updated discount but it is not being used in next iteration.
Could someone give me good approach to solve this issue.?
To make recursive call, you need to call same method inside
private Pair<CustomClass, Integer> myMethod(CustomClass custom, Integer discount) {
int dis = discount - custom.getAmount;
pair.getKey().add(custom.setAmount(custom.getAmount - discount))
pair.getValue().add(dis);
if (dis <= 0)
{
return pair;
}
return myMethod (custom, dis);
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a method:
public void dealDmg(int hp) {
this.hp = hp;
this.hp -= this.dmg;
}
But when i use it as dmg_dealer.dealDmg(dmg_receiver.hp);, it doesn't work properly as in the dmg_receiver's hp is not decreased, but the dmg_dealer's hp becomes of the same amount as of the dmg_receiver, and then dmg_dealer's hp gets reduced by the dmg amount. How can I change a body of the method so that it works in such a way?
You need to pass in the dmg_receiver object to the method instead of dmg_receiver.hp, that way you can update hp in dmg_receiver as well. The updated method should look like below:
public void dealDmg(DmgReceiver dmg_receiver) {
this.hp = dmg_receiver.hp;
dmg_receiver.hp -= this.dmg;
}
If the object represented by this is dealing damage to another object, the dealDmg needs to the object being dealt damage to (assuming that dmg is a member):
public void dealDmg(Unit other) {
other.hp -= this.dmg;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am currently making an application that uses an api and it prints out information about that thing. So basically it gets the api and If i do System.out.println(result.getPlayer().get("displayname")); it will return the display name of the player that I am searching for. I was wondering if there was a way to make result.getPlayer().get("displayname") a variable because I have hundreds of statistics that I need to gather. so is it possible to have that line of code called displayname? Sorry if you don't understand.
I suggest that you make a special statistics/logging class that has static methods specifically for this. For example with your case, the following class can be used both to get the name and to print it. Of course you can combine them into a single method if you want just one functionality.
public class StatsLog {
public static String getPlayerDisplayName(final Result result) {
return (result == null)
? null
: result.getPlayer().get("displayname");
}
public static void printPlayerDisplayName(final Result result) {
final String displayName = getPlayerDisplayName(result);
if (displayName != null) {
System.out.println(displayName);
}
}
}
And when you call it:
StatsLog.printPlayerDisplayName(result);
You can use a getter like #Andrew Tobilko said. like this:
public String getDisplayName(){
return (result != null)? result.getPlayer().get("displayname") : "";
}
However, it depends on what is the "result" and the design of your class. And make sure to check for Null.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have an object which in turn contains other objects. Now I have to iterate through this main object and then pick each object and then iterate through them to find out whether any empty fields are present in them. If the object itself is empty, I have to cut it out of main object. Any thoughts on this please.
public class Transactions {
private Integer totalTransactionCount = null;
private List<Transaction> transactionsList = new ArrayList<Transaction>();
}
public class Transaction {
private String amount = null;
private Foreign foreign = null;
}
public class Foreign {
private String amount = null;
private String commissionAmount = null;
private String exchangeRate = null;
}
Now I have a Transaction object with me and I have to loop throught each of its fields and in turn loop through their fields to find out any null/empty fields.
pseudo code for looping through a list of lists:
for each (innerList in outerList) do
if(innerlist.size == 0) then
//Code for removing empty inner lists.
else
for each ( object in innerList) do
//Check if objects are empty as well and remove it
end for
end if
end for
EDIT: Pointing out lack of research.
I would like to point out that you haven't really done your research properly, simply by googling iterate list of object as well as iterate list of list of object I got plenty of solutions.
Not to mention a question already asked here on Stack Overflow, please read the first answer of this post
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
To be more specific, I want to write a code that throws IllegalArgumentException if the given value is negative. Should I include this code inside of setter/constructor or should I check the value when the appropriate method is called? (Eg: start(), init(), print() or run(). whatever.)
My code (simplified):
public class LLUAlgorithm {
private int temperature;
public int getTemperature() {
return temperature;
}
public void setTemperature(int temperature) {
if (temperature < 0)
throw new IllegalArgumentException("can't be smaller than 0.")
this.temperature = temperature;
}
public void run() {
...
}
I don't recall a single case that a setter throws an exception as above. But I'm curious if it is good / bad.
The best approach is to make your object immutable, get rid of your setters and throws your exception in the constructor otherwise whatever the way you choose, in case of an error, you have a high risk to have your object in an inconsistent state which will cause hard bug to find. For a better understanding, please read this especially the section related to Failure Atomicity.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to get my head round the bucketsorting algorithm, but failed to do so.
Looked at numerous examples... but can't get it working...
Let's say I have this:
public class Employee {
int id; /// example: 52015
String firstname,lastname;
String department;
}
I have a huge list of employees, I then strip the list of all employees to sublists for each department. And the goal is to bucketsort these lists, on employee id. So I have my arraylists of employees, ready to pass on. I just can't seem to understand it.
THANK YOU!
Instead of bucket-sorting use Comparable<Employee> interface.
public class Employee implement Comparable<Employee> {
int id; /// example: 52015
String firstname,lastname;
String department;
public int compareTo(Employee compareEmployee) {
return this.id - compareEmployee.getID();
}
Anyway U can read this article to understand this mechanism better.