();
The class Suitcase has to make sure the thing's weight does not cause the total weight to exceed the maximum weight limit. The method addThing should not add a new thing if the total weight happens to exceed the maximum weight limit.
Below, you find an example of how the class can be used:
publicclass Main {
publicstatic voidmain(String[] args) {
Thing book =newThing("Happiness in three steps", 2);
Thing mobile =newThing("Nokia 3210", 1);
Thing brick =newThing("Brick", 4);
Suitcase suitcase =newSuitcase(5);
System.out.println(suitcase);
suitcase.addThing(book);
System.out.println(suitcase);
suitcase.addThing(mobile);
System.out.println(suitcase);
suitcase.addThing(brick);
System.out.println(suitcase);
}
}
The program output should look like the following:
0 things (0 kg)
1 things (2 kg)
2 things (3 kg)
2 things (3 kg)
This is class Thing
public class Thing {
privateStringname;
privateintweight;
publicThing(Stringname,intweight){
this.name=name;
this.weight=weight;
}
publicStringgetName(){
// returns the thing's name
returnthis.name;
}
publicintgetWeight(){
// returns the thing's weight
returnthis.weight;
}
publicStringtoString(){
returnthis.name+" ("+this.weight+" kg)";
}
}