Advertisement
jaedashson

Exercise 18

Mar 22nd, 2019
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.69 KB | None | 0 0
  1. // class Packer /////////////////////////////////////////////////////
  2.  
  3. package moving.logic;
  4.  
  5. import java.util.*;
  6. import moving.domain.Box;
  7. import moving.domain.Thing;
  8.  
  9. public class Packer {
  10.  
  11.     private int boxesVolume;
  12.     private ArrayList<Box> boxes;
  13.  
  14.     public Packer(int boxesVolume) {
  15.         this.boxesVolume = boxesVolume;
  16.         this.boxes = new ArrayList<Box>();
  17.     }
  18.  
  19.     public List<Box> packThings(List<Thing> things) {
  20.         Box box = new Box(this.boxesVolume);
  21.  
  22.         // iterate through things in parameter list of things
  23.         for (Thing thing : things) {
  24.             boolean fit = box.addThing(thing); // does box.addThing(thing) actually add the thing?
  25.             if (!fit) {
  26. //                System.out.println(box); // ***DEBUG***
  27.                 this.boxes.add(box);
  28.                 box = new Box(this.boxesVolume); // this new box isn't "known" to the next thing in the list?
  29.                 box.addThing(thing); // this isn't working
  30. //                System.out.println(box); // ***DEBUG***
  31.             }
  32.         }
  33.  
  34.         return this.boxes;
  35.     }
  36.  
  37. }
  38.  
  39. // class Box /////////////////////////////////////////////////////
  40.  
  41. package moving.domain;
  42.  
  43. import java.util.*;
  44.  
  45. public class Box implements Thing {
  46.  
  47.     private int maximumCapacity;
  48.     private ArrayList<Thing> things;
  49.  
  50.     public Box(int maximumCapacity) {
  51.         this.maximumCapacity = maximumCapacity;
  52.         this.things = new ArrayList<Thing>();
  53.     }
  54.  
  55.     public boolean addThing(Thing thing) {
  56.         if (this.getVolume() + thing.getVolume() <= this.maximumCapacity) {
  57.             this.things.add(thing);
  58.             return true;
  59.         } else {
  60.             return false;
  61.         }
  62.     }
  63.  
  64.     @Override
  65.     public int getVolume() {
  66.         int totalVolume = 0;
  67.         for (Thing thing : this.things) {
  68.             totalVolume += thing.getVolume();
  69.         }
  70.         return totalVolume;
  71.     }
  72.  
  73.     public String toString() {
  74.         return "Contents of box: " + this.things;
  75.     }
  76.  
  77. }
  78.  
  79. // class Item /////////////////////////////////////////////////////
  80.  
  81. package moving.domain;
  82.  
  83. public class Item implements Thing, Comparable<Item> {
  84.  
  85.     private String name;
  86.     private int volume;
  87.  
  88.     public Item(String name, int volume) {
  89.         this.name = name;
  90.         this.volume = volume;
  91.     }
  92.  
  93.     public String getName() {
  94.         return this.name;
  95.     }
  96.  
  97.     @Override
  98.     public int getVolume() {
  99.         return this.volume;
  100.     }
  101.  
  102.     @Override
  103.     public String toString() {
  104.         return this.name + " (" + this.volume + " dm^3)";
  105.     }
  106.    
  107.     @Override
  108.     public int compareTo(Item anotherThing) {
  109.         return this.volume - anotherThing.getVolume();
  110.     }
  111.  
  112. }
  113.  
  114. // class Main /////////////////////////////////////////////////////
  115.  
  116. package moving;
  117.  
  118. import moving.domain.Item;
  119. import moving.domain.Thing;
  120. import java.util.*;
  121. import moving.domain.Box;
  122. import moving.logic.Packer;
  123.  
  124. public class Main {
  125.  
  126.     public static void main(String[] args) {
  127.         // test your program here
  128.         List<Thing> things = new ArrayList<Thing>();
  129.         things.add(new Item("passport", 2));
  130.         things.add(new Item("toothbrash", 1));
  131.         things.add(new Item("book", 4));
  132.         things.add(new Item("circular saw", 8));
  133.  
  134.         // we create a packer which uses boxes whose valume is 10
  135.         Packer packer = new Packer(10);
  136.  
  137.         // we ask our packer to pack things into boxes
  138.         List<Box> boxes = packer.packThings(things);
  139.  
  140.         System.out.println("number of boxes: " + boxes.size());
  141.  
  142.         for (Box box : boxes) {
  143.             System.out.println("  things in the box: " + box.getVolume() + " dm^3");
  144.         }
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement