package{ import flash.events.*; import flash.display.*; import flash.ui.*; /** * This class is merely going to act as a tester for listening for specific keyboard input. * It will form strings out of anything typed in between presses of "Return" * * IMPORTANT NOTE ABOUT TESTING THIS CLASS: When testing it from the flash program, in the * player that pops up, you must select "Disable Keyboard Shortcuts" from the control menu * or some of your keys will not register! */ public class CalendarTester extends MovieClip{ var input:String = ""; var year:int = -1; var month:int = -1; var day:int = -1; var haveValidYear:Boolean = false; var haveValidMonth:Boolean = false; var haveValidDay:Boolean = false; public function CalendarTester(){ //this keyboard event listens for ANY key being pressed //NOT just for the down arrow. KEY_UP listens for a key being released! this.stage.addEventListener(KeyboardEvent.KEY_DOWN, readKey); trace("Please enter a year. It must be a positive number"); } /** * Keycode 13 is return, so if the user has entered that, they are done inputing their text. * We can trace out what they entered, and clear the input string. */ public function readKey(e:KeyboardEvent):void{ if(e.keyCode == 13){ //check to see if they already entered a year. if(!haveValidYear){ //parseInt is a function built into flash that attempts to convert a string to an int //for non numeric strings, it returns zero. if(this.checkYear(parseInt(input))){ year = parseInt(input); input =""; haveValidYear = true; trace("Now please enter a month, in the form of a number"); }else{ trace("The year you entered, " + input + " is invalid. Please enter a positive number"); input = ""; } }else if(!haveValidMonth){ //fill this area in for month checking }else if(!haveValidDay){ //fill this area in for day checking //if they enter a valid day, print out to them the year, month, and time entered. //then say "press enter to continue" }else{ //this is where they have pressed enter to continue this.removeEventListener(KeyboardEvent.KEY_DOWN, readKey); this.printDate(); } }else{ input += String.fromCharCode(e.charCode); } } /** * HOMEWORK: Fill in this function so that it returns true only if y is an integer, greater than zero */ public function checkYear(y:int):Boolean{ return true; } /** * HOMEWORK: Fill in this function so that it returns true only if m is an integer * between (and including) 1-12. Sorry, no Smarch on this calendar. */ public function checkMonth(m:int):Boolean{ return true; } /** * HOMEWORK: Fill in this function so that it returns true only if d is a valid day. * This means: * - d must be an integer between 1-28 * - Check the entered month, to determine the upper bound for d, so that they date actually exists * - if the year is a leap year, make sure that you consider that for February! * remember, month and year are global variables, so you have access to them here. * This function can make use of other functions, so you may want to save it until after you do them. */ public function checkDay(d:int):Boolean{ return true; } /** * HOMEWORK: This function, when supplied with a month and year, will tell you how many days * occur in that month, in that year (for leap year consideration). */ public function daysInMonth(m:int, y:int):int{ return -1; } /** * HOMEWORK: This function returns true if the year is a leap year. * Make sure you do some research on leap years, it is not as simple as you might expect */ public function isLeapYear(y:int):Boolean{ return false; } /** * HOMEWORK: This function returns the month as a text string, based on the number * Example: if m is 1, it returns "January" */ public function getMonthName(m:int):String{ return ""; } /** * HOMEWORK: This function returns the appropriate suffix for the day, to help in printing out the date. * EG, if d is 15, it returns "th" */ public function getSuffix(d:int):String{ return ""; } /** * HOMEWORK: This function returns nothing, but prints out the date as a string, using the global variables * year, month, and day. * Example: year = 2004, month = 4, day = 3 * The program should print out "April 3rd, 2004" */ public function printDate():void{ trace("print date"); } } }