Advertisement
erikblomqvist

class: Person (updated)

Nov 12th, 2011
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.12 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4. /**
  5.  * Requires a name and then generates a week with random events for each day of the week.
  6.  * Every event affects the mood in a specific way.
  7.  * The mood starts at the value of 200.
  8.  *
  9.  * @author Erik Blomqvist
  10.  * @version 2011-11-07
  11.  */
  12. public class Person
  13. {
  14.     private String mName;
  15.     private int mMood;
  16.     private HashMap<String, Event> mWeekEvent;
  17.     private EventGenerator mGenEvent;
  18.     private ArrayList<String> mWeekday;
  19.  
  20.  
  21.     /**
  22.      * Constructor for objects of class. Automatically fills the list with random events.
  23.      * Requires the name of a person.
  24.      *
  25.      * @param name Name of the person
  26.      */
  27.     public Person(String name)
  28.     {
  29.         mName = name;
  30.         mMood = 200;
  31.         mWeekEvent = new HashMap<String, Event>();
  32.         mGenEvent = new EventGenerator();
  33.         ArrayList<String> mWeekday = new ArrayList<String>();
  34.        
  35.         mWeekday.add(new String("Måndag"));
  36.         mWeekday.add(new String("Tisdag"));
  37.         mWeekday.add(new String("Onsdag"));
  38.         mWeekday.add(new String("Torsdag"));
  39.         mWeekday.add(new String("Fredag"));
  40.         mWeekday.add(new String("Lördag"));
  41.         mWeekday.add(new String("Söndag"));
  42.     }
  43.  
  44.     /**
  45.      * The only public method which prints out the randomized week events and how the person's
  46.      * mood is affected.
  47.      */
  48.     public void listWeek()
  49.     {
  50.         System.out.println("Hej, " + mName);
  51.         System.out.println(" Du startar med gott humör på nivå: " + mMood);
  52.         System.out.println(" Din vecka slumpar sig såhär...");
  53.  
  54.         for(String thisDay : mWeekday) {
  55.             // Vänta en sekund...
  56.             wait(1000);
  57.             System.out.println(thisDay);
  58.             generateDayEvent();
  59.             Event thisEvent = (Event)mWeekEvent.get(thisDay);
  60.             System.out.println(thisEvent);
  61.             affectMood(thisDay);
  62.             System.out.println(" Ditt humör är nu på: " + mMood);
  63.         }
  64.     }
  65.  
  66.     /**
  67.      * The method to affect the total mood value based on the day of the week parameter.
  68.      *
  69.      * @param day Day of the week
  70.      */
  71.     private void affectMood(String day)
  72.     {
  73.         Event thisEvent = (Event)mWeekEvent.get(day);
  74.         int newMood = thisEvent.getAffectMood();
  75.         mMood += newMood;
  76.     }
  77.    
  78.     /**
  79.      * Generates seven different events and puts them in the mWeekEvent HashMap.
  80.      */
  81.     private void generateDayEvent()
  82.     {
  83.         for(String thisDay : mWeekday) {
  84.             mWeekEvent.put(thisDay, mGenEvent.generateEvent());
  85.         }
  86.     }
  87.    
  88.     /**
  89.      * Wait for a specified number of milliseconds before finishing.
  90.      * This provides an easy way to specify a small delay which can be
  91.      * used when producing animations.
  92.      * @param  milliseconds  the number
  93.      */
  94.     private void wait(int milliseconds)
  95.     {
  96.         try
  97.         {
  98.             Thread.sleep(milliseconds);
  99.         }
  100.         catch (Exception e)
  101.         {
  102.             // ignoring exception at the moment
  103.         }
  104.     }
  105. }
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement