Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // class Packer /////////////////////////////////////////////////////
- package moving.logic;
- import java.util.*;
- import moving.domain.Box;
- import moving.domain.Thing;
- public class Packer {
- private int boxesVolume;
- private ArrayList<Box> boxes;
- public Packer(int boxesVolume) {
- this.boxesVolume = boxesVolume;
- this.boxes = new ArrayList<Box>();
- }
- public List<Box> packThings(List<Thing> things) {
- Box box = new Box(this.boxesVolume);
- // iterate through things in parameter list of things
- for (Thing thing : things) {
- boolean fit = box.addThing(thing); // does box.addThing(thing) actually add the thing?
- if (!fit) {
- // System.out.println(box); // ***DEBUG***
- this.boxes.add(box);
- box = new Box(this.boxesVolume); // this new box isn't "known" to the next thing in the list?
- box.addThing(thing); // this isn't working
- // System.out.println(box); // ***DEBUG***
- }
- }
- return this.boxes;
- }
- }
- // class Box /////////////////////////////////////////////////////
- package moving.domain;
- import java.util.*;
- public class Box implements Thing {
- private int maximumCapacity;
- private ArrayList<Thing> things;
- public Box(int maximumCapacity) {
- this.maximumCapacity = maximumCapacity;
- this.things = new ArrayList<Thing>();
- }
- public boolean addThing(Thing thing) {
- if (this.getVolume() + thing.getVolume() <= this.maximumCapacity) {
- this.things.add(thing);
- return true;
- } else {
- return false;
- }
- }
- @Override
- public int getVolume() {
- int totalVolume = 0;
- for (Thing thing : this.things) {
- totalVolume += thing.getVolume();
- }
- return totalVolume;
- }
- public String toString() {
- return "Contents of box: " + this.things;
- }
- }
- // class Item /////////////////////////////////////////////////////
- package moving.domain;
- public class Item implements Thing, Comparable<Item> {
- private String name;
- private int volume;
- public Item(String name, int volume) {
- this.name = name;
- this.volume = volume;
- }
- public String getName() {
- return this.name;
- }
- @Override
- public int getVolume() {
- return this.volume;
- }
- @Override
- public String toString() {
- return this.name + " (" + this.volume + " dm^3)";
- }
- @Override
- public int compareTo(Item anotherThing) {
- return this.volume - anotherThing.getVolume();
- }
- }
- // class Main /////////////////////////////////////////////////////
- package moving;
- import moving.domain.Item;
- import moving.domain.Thing;
- import java.util.*;
- import moving.domain.Box;
- import moving.logic.Packer;
- public class Main {
- public static void main(String[] args) {
- // test your program here
- List<Thing> things = new ArrayList<Thing>();
- things.add(new Item("passport", 2));
- things.add(new Item("toothbrash", 1));
- things.add(new Item("book", 4));
- things.add(new Item("circular saw", 8));
- // we create a packer which uses boxes whose valume is 10
- Packer packer = new Packer(10);
- // we ask our packer to pack things into boxes
- List<Box> boxes = packer.packThings(things);
- System.out.println("number of boxes: " + boxes.size());
- for (Box box : boxes) {
- System.out.println(" things in the box: " + box.getVolume() + " dm^3");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement