Why can't I use add? [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
I have a problem with and exercise in java.
I'm managin a logistics company I want to add a Booking into a BookingRegister.
Booking booking = Booking.makebooking (trip,originCity,arrivalDate,merchandise,departureDate,kg);
BookingRegister bookingRegister = getBookingRegister();
bookingRegister.add(Prenotazione);
But i can't use add() even if the BookingRegister is an ArrayList
public class BookingRegister {
private List<Booking> BookingRegister = new ArrayList<>();
}

First, you should not make the List public. Instead, provide a method that takes a Booking and does something with it. That could be adding it to a List. Like,
public class BookingRegister {
private List<Booking> register = new ArrayList<>();
public void addBooking(Booking b) {
register.add(b);
}
}
Next, call that method. Like,
// This looks like a builder pattern. Why not new Booking?
Booking booking = Booking.makebooking (trip,
originCity, arrivalDate, merchandise, departureDate, kg);
BookingRegister bookingRegister = getBookingRegister();
bookingRegister.addBooking(booking);

Because the List BookingRegister is private.
Make it public.
Another thing is, in line 3 you have used the class name instead of the variable name.
Changing it to bookingRegister would solve it...

Related

Why am i unable to add an array as an instance field? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 11 months ago.
Improve this question
I wanted to add an array as an instance field, also a part of an object, however it gives me interesting errors when i try to do so:
class Surgeon {
private String[] surgeons;
public Surgeon() {
this.surgeons = {"Romero", "Jackson", "Hannah", "Camilla"};
}
public static void main(String[] args) {
Surgeon surg = new Surgeon();
}
}
And it gives me three errors.
This does not make any sense bc is it not how you initialize an array?
You have to add the operator "new" to instantiate the array in the class constructor
public Surgeon() {
this.surgeons = new String[]{"Romero", "Jackson", "Hannah", "Camilla"};
}

How to check if at least one method return true? [closed]

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 an Interface with method:
boolean isForbidden();
and few implementations, which are Spring Components.
In used class, I autowirding all of them and I want to check if at least one of them returning true. How can I do that? Is there a simply way to check it?
Interface:
public interface ForbiddenChecker {
boolean isForbidden();
}
Class:
#Autowired
private List<ForbiddenChecker> validators;
And I want to create a method something like:
public boolean shouldInvokeProcess() {
// at least one component return true
}
What might the body of this method look like?
Feels like I'm missing something here...
public boolean shouldInvokeProcess() {
for (var checker : validators) if (checker.isForbidden()) return false;
return true;
}
If you're one of those 'ooh! shiny new hammer!' folks:
public boolean shouldInvokeProcess() {
return !validators.stream().anyMatch(ForbiddenChecker::isForbidden);
}
Both seem about equally fine, here, and trivial.
If I understand correctly, surely you can just iterate over the list of validators, calling each ones "isForbidden" method. If one returns true then return true from "shouldInvokeProcess"

How can I change a method to pass a proper object property? [closed]

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;
}

easier way to format lines of code in Java to make your job easier? [closed]

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.

Looping through Object of Objects in java [closed]

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

Categories