Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Details Your program will process letters from Santa’s mailbag, using the MailBa

ID: 3575806 • Letter: D

Question

Details

Your program will process letters from Santa’s mailbag, using the MailBag, Letter, Child, and Gift classes, which I have written for you. The API methods for each of these classes is given below. You will need to include these java files in your Eclipse project, however you do NOT need to read the code for them, or understand how they work. You may, of course, do so if you’re curious. A word of caution however: This project is all about using APIs.

You will track and print out the following pieces of information:

1. NICE List – List of the children who wrote that were “NICE”

2. NAUGHTY List – List of the children who wrote that were “NAUGHTY”

3. NO GIFTS List – List of children who will not be receiving any of the requested gifts.

4. Gift Inventory Requirements – List of all the gifts requested, and how many of each will be required to fulfill the wish lists.

Here is a general description of what the different object classes do: ·

MailBag – Contains an unknown number of Letter objects. You will create one of these objects, and use it to get Letters. ·

Letter – An object that contains one Child object (the child who wrote the letter), and a List (see the Java API for full description) of one or more Gift objects (items that the child wants for Christmas). ·

Child – Information about the child who wrote this letter. This contains name, age, and behavior (naughty/nice) status. ·

Gift – Information about a single Gift this child wants for Christmas. This contains gift name, minimum age that can receive this gift, and a boolean “evenIfNaughty” which indicates whether a child can receive this gift even if they were naughty.

You will write a single class for your program, with a main method. No other methods are required, however you may create some if you wish. You will have a total of 5 java files in the project you turn in – 1 class written by you, and the 4 classes supplied with this assignment. All java files will be in the same src folder.

Your program logic should work as follows:

1. Create 3 Lists to keep track of the children that were NICE, NAUGHTY, and the ones that will receive no gifts (Santa will make sure they get something anyway).

2. Create an array to track the number of each gift type that was requested by all the children, but only if they will actually receive it – see below for more information about that. (Hint: How big should this array be? Check the Gift object API)

3. Create a MailBag object. We will use this to get letters for processing. 4. While the MailBag object contains more letters:

                   a. Get a new letter from the MailBag

                   b. Get information about the Child that wrote the letter (name, age, nice/naughty)

                  c. Add the Child to the appropriate nice or naughty list (add to only one)

                 d. Get the List of Gift objects the child requested

                 e. For each Gift object requested:

                                       i. Find the minimum age for this Gift. If the child is younger than this age, they may not have the gift.

                                       ii. If the child was “naughty”, see if this gift may be given anyway (see The Rules below).

                                       iii. If the child meets both of the above criteria, update the gift count array (Hint: Use the getCode() method on the Gift object to get the array index)

                          f. If this Child will get no gifts after doing section (e) above, add that child to the “NO GIFTS” list.

5. Print out all required information. (Hint: Use methods in the Gift API, and print values from the array)

The Rules

Santa wants every child to be happy, but within limits. Here are the rules he works under: ·

Children may only get gifts that are age-appropriate, regardless of whether they were “nice” or “naughty”. Each Child has an age, and each Gift has a minimum age. If a child is younger than the minimum age for that gift, they may not receive it. ·

Children that were “nice” may receive any gift (if they are old enough) ·

Children that were “naughty” may only receive Gifts which have the isEvenIfNaughty() flag set to TRUE (if they are old enough). Santa is a kind fellow. There are some types of gifts (warm clothing, food, books, etc.) that all children need, no matter how good they were. ·

Santa tracks the “naughty” and “nice” lists because, well, that’s what he does. ·

Santa tracks the “no gifts” list because he needs to know what children can’t get the gifts they requested (He will make sure to do something else special for them to they can have a Merry Christmas too).

Program Requirements

The following requirements must be observed when writing your program. ·

You must use java.util.List objects to keep track of naughty, nice, and no gift lists. ·

Lists must use the “Generics” structure (<> symbols) and give the appropriate object type. Example: ListmyList = new ArrayList(); (Note that Object is NOT the appropriate type to use. You will replace that value with the proper one.) ·

You must use only the public class API methods to get information for output. Hardcoding ANY values in your program (Toy names, number of letters, number of gifts, Child names, etc. will not be accepted) ·

An array must be used to track the number of each gift type needed. (Hint: The Gift.getCode() method can be used to find the array index) · You must use EVERY class method given below in the API description (other methods exist on these classes, but you will not need to use them).

Sample Output

Once all letters are read, your program will output the processing results.   The following is a sample of what that output should look like.

(Note that actual data output will vary each time, depending on the random values selected by the program. To get the same values each time, change line 20 of MailBag.java to read: random = new Random(100); This will allow for easier testing of your program.)

North Pole Postal Processing Center - Santa's Mailbag Report

------------------------------------------------------------

NICE LIST (Count: 11)

Brian Carter

Matthew Taylor

James Miller

Dorothy Gonzalez

Daniel Gonzalez

Richard Perez

Anthony Walker

Matthew Moore

Ashley Wilson

Michael Harris

Barbara Jackson

------------------------------------------------------------

NAUGHTY LIST (Count: 10)

Kimberly Nelson

Sarah Lee

James Wilson

Helen Moore

Brian Moore

Mary Baker

Sarah Moore

Karen Collins

Nancy Evans

Melissa Miller

------------------------------------------------------------

NO GIFTS LIST (Count: 3)

Helen Moore

Daniel Gonzalez

Nancy Evans

------------------------------------------------------------

GIFT TOTALS

BLOCKS (code 0) : 0

DOLL (code 1) : 0

STUFFED_ANIMAL (code 2) : 1

LEGO (code 3) : 0

YOYO (code 4) : 0

WAGON (code 5) : 1

GAME (code 6) : 2

CANDY (code 7) : 1

TOY_CAR (code 8) : 1

MOVIE (code 9) : 0

BIKE (code 10) : 0

PUPPY (code 11) : 1

KITTEN (code 12) : 2

ROLLER_SKATES (code 13) : 0

MONOPOLY (code 14) : 1

SCRABBLE (code 15) : 2

WII (code 16) : 2

NINTENDO (code 17) : 0

PLAY_STATION (code 18) : 0

PHONE (code 19) : 0

FOOD (code 20) : 1

BINKY (code 21) : 0

BOTTLE (code 22) : 3

BLANKET (code 23) : 0

COAT (code 24) : 5

SHOES (code 25) : 0

SOCKS (code 26) : 2

CRAYONS (code 27) : 3

PENCILS (code 28) : 1

BOOK (code 29) : 2

LUNCHBOX (code 30) : 1

BACKPACK (code 31) : 0

NOTEBOOK (code 32) : 1

GLASSES (code 33) : 0

Total Gift Count: 33

Programming Strategy

There are a lot of details to this project, so be careful that you are following the steps outlined. However, the main difficulty will be using the method calls correctly for the classes that I have defined. Read through the API descriptions carefully to make sure that you understand what each method does. As long as the project works, you may use whatever method calls you prefer. You may also edit the classes that I provided if you want to change anything or add your own methods. Also, be careful of instance versus class methods. You may call a class method using the name of an instance (though Eclipse will give you warnings), but you may not call an instance method using the class name.

Using My Classes

In order to use the classes that I am providing, you will need to copy/paste them into your project OR create a new class using Eclipse with the correct name and copy/paste the contents of my files into them.

Classes are listed below:

import java.util.Random;

/**
* Represents a single child who has written to Santa Claus
*
* @author mlookabaugh
*/
public class Child
{
// Maximum age of the children
public static final int MAX_AGE = 12;

// The name of the child (first + last). Ex: "Wendy Jones"
private final String name;
// Age of the child.
private final int age;
// Was this child "NICE"? If true, the child was NICE. If false, the child was NAUGHTY.
private final boolean nice;

/**
* Constructor. Creates a new Child object with the specified name and list of toys.
*
* @param name Name of the child. Ex: "Wendy Jones"
* @param age Age of the child.
* @param nice Was this child "NICE"? If true, the child was NICE. If false, the child was NAUGHTY.
*/
public Child(
   final String name,
   final int age,
   final boolean nice)
{
  super();

  this.name = name;
  this.age = age;
  this.nice = nice;
}

/**
* Name of the child. Ex: "Wendy Jones"
*
* @return
*/
public String getName()
{
  return name;
}

/**
*
* @return
*/
public int getAge()
{
  return age;
}

/**
* Was this child "NICE"? If true, the child was NICE. If false, the child was NAUGHTY.
*
* @return
*/
public boolean wasNice()
{
  return nice;
}

/**
* Whether or not this object is the same as another
*/
@Override
public boolean equals(final Object obj)
{
  if (!(obj instanceof Child)) {
   return false;
  }
  Child other = (Child)obj;
  return name.equals(other.name);
}

/**
* Returns a hash code (numerical representation) for this object
*/
@Override
public int hashCode()
{
  return name.hashCode();
}

/**
* Creates a new child
*
* @param random Random number generator used to produce the new Child
* @return
*/
public static Child newChild(final Random random)
{
  // Create name
  int firstNamePosition = random.nextInt(FirstName.values().length);
  String firstName = FirstName.values()[firstNamePosition].toString();
  int lastNamePosition = random.nextInt(LastName.values().length);
  String lastName = LastName.values()[lastNamePosition].toString();
  String name = firstName + " " + lastName;

  // Age (limited to 1-12 only because the number of teenagers who believe in Santa Claus is vanishingly small. :)
  int age = random.nextInt(MAX_AGE) + 1;

  // Naughty or nice?
  boolean nice = random.nextBoolean();

  return new Child(name, age, nice);
}

/**
* List of first names used to create Child objects.
*
* This list taken from: https://www.ssa.gov/oact/babynames/decades/century.html
*
* @author mlookabaugh
*/
private enum FirstName
{
  James,
  Mary,
  John,
  Patricia,
  Robert,
  Jennifer,
  Michael,
  Elizabeth,
  William,
  Linda,
  David,
  Barbara,
  Richard,
  Susan,
  Joseph,
  Jessica,
  Thomas,
  Margaret,
  Charles,
  Sarah,
  Christopher,
  Karen,
  Daniel,
  Nancy,
  Matthew,
  Betty,
  Anthony,
  Dorothy,
  Donald,
  Lisa,
  Mark,
  Sandra,
  Paul,
  Ashley,
  Steven,
  Kimberly,
  George,
  Donna,
  Kenneth,
  Carol,
  Andrew,
  Michelle,
  Joshua,
  Emily,
  Edward,
  Helen,
  Brian,
  Amanda,
  Kevin,
  Melissa;
}

/**
* List of last names used to create child objects
*
* This list taken from: http://surnames.behindthename.com/top/lists/united-states/1990
*
* @author mlookabaugh
*/
private enum LastName
{
  Smith,
  Johnson,
  Williams,
  Jones,
  Brown,
  Davis,
  Miller,
  Wilson,
  Moore,
  Taylor,
  Anderson,
  Thomas,
  Jackson,
  White,
  Harris,
  Martin,
  Thompson,
  Garcia,
  Martinez,
  Robinson,
  Clark,
  Rodriguez,
  Lewis,
  Lee,
  Walker,
  Hall,
  Allen,
  Young,
  Hernandez,
  King,
  Wright,
  Lopez,
  Hill,
  Scott,
  Green,
  Adams,
  Baker,
  Gonzalez,
  Nelson,
  Carter,
  Mitchell,
  Perez,
  Roberts,
  Turner,
  Phillips,
  Campbell,
  Parker,
  Evans,
  Edwards,
  Collins
}
}

import java.util.Random;

/**
* A gift that a child wants for Christmas.
*
* @author mlookabaugh
*/
public enum Gift
{
// "Nice" only gifts
BLOCKS(1, false),
DOLL(1, false),
STUFFED_ANIMAL(1, false),
LEGO(5, false),
YOYO(5, false),
WAGON(4, false),
GAME(1, false),
CANDY(1, false),
TOY_CAR(1, false),
MOVIE(6, false),
BIKE(7, false),
PUPPY(9, false),
KITTEN(9, false),
ROLLER_SKATES(8, false),
MONOPOLY(8, false),
SCRABBLE(8, false),
WII(8, false),
NINTENDO(8, false),
PLAY_STATION(8, false),
PHONE(10, false),

// Even if naughty gifts
FOOD(1, true),
BINKY(1, true),
BOTTLE(1, true),
BLANKET(1, true),
COAT(1, true),
SHOES(1, true),
SOCKS(1, true),
CRAYONS(2, true),
PENCILS(3, true),
BOOK(3, true),
LUNCHBOX(6, true),
BACKPACK(6, true),
NOTEBOOK(1, true),
GLASSES(8, true);

// Minimum age for this gift
private final int minimumAge;
// Can a child receive this gift even if they were naughty?
private final boolean evenIfNaughty;

/**
* Constructor
*
* Creates a new Gift with the specified information.
*
* @param name Name of this gift
* @param int Minimum age for this gift. If a child is younger than this age, they may not receive this gift.
* @param boolean evenIfNaughty. If TRUE, a child may receive this gift even if they were naughty. If FALSE, they may not.
*/
private Gift(
   final int minimumAge,
   final boolean evenIfNaughty)
{
  this.minimumAge = minimumAge;
  this.evenIfNaughty = evenIfNaughty;
}

/**
* Returns a new gift
*
* @param random Random number generator used to produce the new Gift.
* @return
*/
public static Gift newGift(final Random random)
{
  Gift[] gifts = Gift.values();
  int giftNumber = random.nextInt(gifts.length);
  return gifts[giftNumber];
}

/**
* Minimum age for this gift. If a child is younger than this age, they may not receive this gift.
*
* @return
*/
public int getMinimumAge()
{
  return minimumAge;
}

/**
* If TRUE, a child may receive this gift even if they were naughty. If FALSE, they may not.
*
* @return
*/
public boolean isEvenIfNaughty()
{
  return evenIfNaughty;
}

/**
* Returns the name of this toy. Ex: "BLANKET"
*
* @return
*/
public String getName()
{
  return this.toString();
}

/**
* Returns a number between 0 and maximumGiftCode representing the code number for this gift
*
* @return
*/
public int getCode()
{
  Gift[] gifts = Gift.values();
  for (int x = 0; x < gifts.length; x++) {
   if (this == gifts[x]) {
    return x;
   }
  }

  throw new RuntimeException("No code found for: " + this);
}

/**
* Returns the maximum gift code number that will ever be returned by a Gift object.
*
* @return
*/
public static int maximumGiftCode()
{
  return Gift.values().length - 1;
}

/**
* Returns the gift name for the specified gift code. Ex: 0 = BLOCKS, 1 = DOLL, ... , 33 = GLASSES
*
* @param code Gift code number to return the name for.
* @return
*/
public static String getNameForCode(final int code)
{
  for (Gift gift : Gift.values()) {
   if (code == gift.getCode()) {
    return gift.toString();
   }
  }

  throw new RuntimeException("No gift found for code: " + code);
}
}

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
* A letter sent from a child to Santa Claus
*
* @author mlookabaugh
*/
public class Letter
{
// Maximum number of gifts requested per child
public static int MAX_GIFTS = 5;

// The child who sent this letter
private final Child child;
// The list of gift requests for this child
private final List<Gift> giftRequests = new ArrayList<>();

/**
* Constructor. Creates a new Letter object with the specified information.
*
* @param child The child who sent this letter
* @param giftRequests The list of gift requests for this child
*/
public Letter(
   final Child child,
   final List<Gift> giftRequests)
{
  super();

  this.child = child;
  if (giftRequests != null) {
   this.giftRequests.addAll(giftRequests);
  }
}

/**
* The child who sent this letter
*
* @return
*/
public Child getChild()
{
  return child;
}

/**
* The list of gift requests for this child
*
* @return
*/
public List<Gift> getGiftRequests()
{
  return giftRequests;
}

/**
* Creates a new letter to Santa Claus
*
* @param random Random number generator used to generate this letter.
* @return
*/
public static Letter newLetter(final Random random)
{
  // Create child
  Child child = Child.newChild(random);

  // Create gift list
  int numberOfGiftsRequested = random.nextInt(MAX_GIFTS) + 1;
  List<Gift> gifts = new ArrayList<>();
  for (int x = 0; x < numberOfGiftsRequested; x++) {
   gifts.add(Gift.newGift(random));
  }

  return new Letter(child, gifts);
}
}

import java.util.Iterator;
import java.util.Random;

public class MailBag implements Iterator<Letter>
{
// Maximum number of letters generated by this mailbag
public static int MAXIMUM_LETTERS = 20;

// Number of letters that have been returned so far
private int letterCount = 0;
// Random number generator used to produce letter data
private Random random;

public MailBag()
{
  super();

  random = new Random();
}

@Override
public boolean hasNext()
{
  return letterCount <= MAXIMUM_LETTERS;
}

@Override
public Letter next()
{
  if (letterCount > MAXIMUM_LETTERS) {
   throw new RuntimeException("There were no more letters to read! Be sure to call hasNext() first to see if more letters exist!");
  }
  letterCount++;

  return Letter.newLetter(random);
}
}

Explanation / Answer

Main.java (Other 4 classes remain the same as provided in the question):

package com.santa.gifts;

import java.util.ArrayList;
import java.util.List;

public class Main {

   /**
   * @param args
   */
   public static void main(String[] args) {

       // Create 3 Lists to keep track of the children that were NICE, NAUGHTY,
       // and the ones that will receive no gifts
       List<String> niceList = new ArrayList<String>();
       List<String> naughtyList = new ArrayList<String>();
       List<String> noGiftsList = new ArrayList<String>();

       // Create an array to track the number of each gift type that was
       // requested by all the children
       int maxGiftCode = Gift.maximumGiftCode();
       int[] giftCountArray = new int[maxGiftCode + 1];

       // Create a MailBag object
       MailBag mailBag = new MailBag();

       // While the MailBag object contains more letters
       while (mailBag.hasNext()) {

           // Get a new letter from the MailBag
           Letter letter = mailBag.next();

           // Get information about the Child that wrote the letter (name, age,
           // nice/naughty)
           Child child = letter.getChild();

           String childName = child.getName();
           int age = child.getAge();

           // Add the Child to the appropriate nice or naughty list
           if (child.wasNice()) {
               niceList.add(childName);
           } else {
               naughtyList.add(childName);
           }

           List<Gift> giftsRequested = letter.getGiftRequests();

           boolean giftReceived = false;
           // For each Gift object requested
           for (Gift gift : giftsRequested) {
               int minAge = gift.getMinimumAge();
               if (age >= minAge) {
                   // If the child was “naughty”, see if this gift may be given
                   // anyway
                   if (child.wasNice() || gift.isEvenIfNaughty()) {
                       giftCountArray[gift.getCode()]++;
                       giftReceived = true;
                   }
               }
           }
           if (!giftReceived) {
               noGiftsList.add(childName);
           }

       }
       System.out.println("Santa's Mailbag Report: ");

       // Print Nice List
       System.out.println("NICE LIST (Count: " + niceList.size() + ")");

       for (int i = 0; i < niceList.size(); i++) {
           System.out.println(niceList.get(i));
       }

       // Print Naughty list
       System.out.println("---------------------------");
       System.out.println("NAUGHTY LIST (Count: " + naughtyList.size() + ")");

       for (int i = 0; i < naughtyList.size(); i++) {
           System.out.println(naughtyList.get(i));
       }

       System.out.println("---------------------------");
       System.out.println("NO GIFTS LIST (Count: " + noGiftsList.size() + ")");

       // Print No Gifts list
       for (int i = 0; i < noGiftsList.size(); i++) {
           System.out.println(noGiftsList.get(i));
       }

       System.out.println("---------------------------");
       System.out.println("GIFTS TOTALS");

       // Print count of each gift and the total count
       int totalCount = 0;
       for (int i = 0; i < giftCountArray.length; i++) {
           int count = giftCountArray[i];
           totalCount += count;
           System.out.println(Gift.getNameForCode(i) + " (code " + i + "): "
                   + count);
       }
       System.out.println("TOTAL GIFT COUNT: " + totalCount);
   }
}

--------------------------------------------------------------------------------------------------------------

Sample Output:

Santa's Mailbag Report:

NICE LIST (Count: 14)
Jessica Smith
Kenneth Lee
Linda Wright
Edward Phillips
James Carter
Helen Edwards
Patricia Young
James Johnson
David Perez
Elizabeth Campbell
Michelle Davis
Barbara Evans
Donald Taylor
Kevin King
---------------------------
NAUGHTY LIST (Count: 7)
Sandra Edwards
James Moore
Brian Allen
Anthony Hernandez
Steven Walker
William Jackson
William Thompson
---------------------------
NO GIFTS LIST (Count: 3)
James Moore
Steven Walker
William Thompson
---------------------------
GIFTS TOTALS
BLOCKS (code 0): 0
DOLL (code 1): 1
STUFFED_ANIMAL (code 2): 2
LEGO (code 3): 0
YOYO (code 4): 1
WAGON (code 5): 0
GAME (code 6): 2
CANDY (code 7): 0
TOY_CAR (code 8): 1
MOVIE (code 9): 0
BIKE (code 10): 1
PUPPY (code 11): 1
KITTEN (code 12): 0
ROLLER_SKATES (code 13): 0
MONOPOLY (code 14): 2
SCRABBLE (code 15): 0
WII (code 16): 4
NINTENDO (code 17): 4
PLAY_STATION (code 18): 0
PHONE (code 19): 0
FOOD (code 20): 3
BINKY (code 21): 2
BOTTLE (code 22): 0
BLANKET (code 23): 3
COAT (code 24): 3
SHOES (code 25): 0
SOCKS (code 26): 2
CRAYONS (code 27): 2
PENCILS (code 28): 0
BOOK (code 29): 2
LUNCHBOX (code 30): 0
BACKPACK (code 31): 0
NOTEBOOK (code 32): 4
GLASSES (code 33): 1
TOTAL GIFT COUNT: 41

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote