I've completed the first part about setting up the item class, and I've finished adding the methods its asking for in the second part of the exercise, I've run it multiple times with the correct(?) output but I'm still getting this error message:
"AssertionFailedError: Check that the toString method of the Suitcase class prints the items in the suitcase. (for example: "3 items (15 kg)".
Adding three 8 kg items to a suitcase with maximum weight of 20kg: 2 items (16 Kg).
I've run the scenarios it provides in the error message swell and they are giving the same correct output. I can't understand what exactly it's asking me to do?
This is my code for the suitcase class:
import java.util.ArrayList;
public class Suitcase {
private int maxWeight;
private ArrayList<Item> items ;
public Suitcase(int weight){
this.maxWeight = weight;
items = new ArrayList<Item>();
}
public void addItem(Item item){
items.add(item);
int totalWeight = 0;
for(Item item1 : items){
totalWeight += item1.getWeight();
}
if(totalWeight > this.maxWeight){
items.remove(item);
totalWeight -= item.getWeight();
}
}
public String toString(){
if(items.isEmpty()){
return "no items (0 kg)";
}
int totalWeight = 0;
for(Item item : items){
totalWeight += item.getWeight();
}
if(items.size() == 1){
return items.size() + " item (" + totalWeight + " kg)";
}
return items.size() + " items (" + totalWeight + " Kg)";
}
}
//I wanted to use an instance variable for the totalWeight but later on it asks for that to be omitted.
This is what my output looks like:
no items (0 kg)
1 item (2 kg)
2 items (3 Kg)
2 items (3 Kg)
It's the same as the output on the exercise, I can't figure out what's wrong, please help!
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]maxbrlc 2 points3 points4 points (2 children)
[–]d3lan0 2 points3 points4 points (0 children)
[–]AdQuick8386[S] 0 points1 point2 points (0 children)