/****************

== Holiday Packages - a Simulation ==

This program SIMULATES the problem of packing heavy objects
into boxes, which will then be shipped (perhaps by DHL).
There are 10 objects of various weights, e.g.

   20 25 13  8  9 14 12  29 35 21

This is a very CHEAP simulation.  The user must type
the POSITION of the object to put it into the box.
For example, you could fill a box EXACTLY full
by taking items #1 , #2 , and #6 making 50 kg exactly.

The goal is to use as few boxes as possible,
as each box must be paid for.  Assume it is being shipped
with a fixed price per box, say 40 EU each.
Then every box you save means you saved 40 EU.

Play the game (oops, RUN the SIMULATION) a few times
to see how it works.  Then make the changes listed below.

(1) Change MAKENUMS so it uses a LOOP instead
    of 10 separate commands.
    
(2) Change MAKENUMS so it uses bigger numbers -
    between 20 and 100.  Then change the limits
    on a box to 100 kg.  Then the game is more challenging.
    
(3) Change the program to use 12 objects instead of 10.
    You will need to change several parts of the program.
    
(4) Write a method called HELP.  If the user types 99,
    the HELP method should run and SUGGEST an item
    that could still fit in the box.  
    
(5) Write an AUTO method.  The COMPUTER should look
    through the array and find several items that
    add up to a number small enough to fit in a box.
    This problem is an example of ARTIFICIAL INTELLIGENCE.
    This is non-trivial, especially if you want a
    "good" answer, or perhaps even "the best" answer.
        
                
*****************/
import java.awt.*;

public class Loading extends EasyApp
{
   public static void main(String[] args)
   {  new Loading(); }
   
   TextField weights = addTextField("",280,50,500,50,null);
   TextArea info = addTextArea("",30,50,250,100,null);
   
   int[] nums = new int[10];   
   
   int boxes = 0;   
   
   public Loading()
   {  setSize(900,400);
      weights.setFont(new Font("Arial",0,24));
      info.setFont(new Font("Arial",0,14));
      
      info.setText("== Packing Boxes ==\n"+
              "Each box can hold 50 kg. \n"+
              "You must put objects (right) \n" +
              "into boxes, but don't exceed 50 kg.\n" +
              "Use as few boxes as possible."
             );
      makeNums();
      showNums();
      System.out.println();
      do
      {
         fillBox();
      } while (checkDone() == false);
      output("You used " + boxes + " boxes");
      System.exit(0);
   }
   
   public void fillBox()
   {  boxes = boxes + 1;
      int total = 50;
      int contents = 0;
      int choice = -1;
      do
      {
         choice = inputInt("Choose a position in the list (-1 to quit):");
         if (choice >= 10)
         {
            output("Illegal position");
         }
         else if (choice < 0)
         {  output("Packing the box now."); }
         else if (nums[choice] <= total)
         {
            contents = contents + nums[choice];
            total = total - nums[choice];
            nums[choice] = 0;
         }
         else
         {  output("That is too heavy for this box."); }
         showNums();
      } while (choice >= 0);
      output("That box contains " + contents + " kg.");
   }
   
   public void makeNums()
   {
      nums[0] = random(5,35);
      nums[1] = random(5,35);
      nums[2] = random(5,35);
      nums[3] = random(5,35);
      nums[4] = random(5,35);
      nums[5] = random(5,35);
      nums[6] = random(5,35);
      nums[7] = random(5,35);
      nums[8] = random(5,35);
      nums[9] = random(5,35);
   }

   public int random(int smallest, int largest)
   {
      return (int)( Math.random()*(largest-smallest+1) + smallest);
   }
   
   public void showNums()
   {
      weights.setText("  " + nums[0] + "  " + nums[1] +
                      "  " + nums[2] + "  " + nums[3] +
                      "  " + nums[4] + "  " + nums[5] +      
                      "  " + nums[6] + "  " + nums[7] +     
                      "  " + nums[8] + "  " + nums[9]      
                     );
   }
   
   public boolean checkDone()
   {
      if (nums[0] > 0 || nums[1] > 0 || nums[2] > 0 ||
          nums[3] > 0 || nums[4] > 0 || nums[5] > 0 ||
          nums[6] > 0 || nums[7] > 0 || nums[8] > 0 || nums[9] > 0
         )
      {  return false; }
      else
      {  return true; }
   }

}