0 Replies - 1117 Views - Last Post: 21 June 2012 - 03:49 PM

#1 c0m4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-June 12

Application architecture difficulties.

Posted 21 June 2012 - 03:49 PM

Hi

I'm writing (the very first program with more than 90% of code written by me) a program which should calculate the payment based on time - a parking meter essentially.

It's working, and all, however I'm not able to properly implement the error handling - the app seem to enter an endless loop and I just can't figure where.
The app must be fool-proof, that's why I've hardcoded cursor positions.

I've got a feeling that although the program doing it's job it could be shorter/more elegant, which would probably help resolve the error handling issue.
The only error handled now is entering time which is in future relatively to machine's local time.
package
{
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.text.*;
	import flash.ui.Keyboard;
	
	[SWF(width="550", height="400", backgroundColor="#FFFFFF", frameRate="60")]

	
	public class ArgamanTimeCalc extends Sprite
	{
		public var format:TextFormat = new TextFormat();
		public var localFormat:TextFormat = new TextFormat()
		public var output:TextField = new TextField ();
		public var colon:TextField = new TextField ();
		public var localClock:TextField = new TextField ();
		public var input:TextField = new TextField (); //obsolete
		public var input1:TextField = new TextField (); //input field for tens of hours
		public var input2:TextField = new TextField (); //input field for hours
		public var input3:TextField = new TextField (); //input field for tens of min
		public var input4:TextField = new TextField (); //input field for tens of hours
		public var theDate:Date = new Date ();
		public var currentDate:String;
		public var currentTime:String;
		public var currentMin:String;
		public var currentHour:String;
		public var currentMinInt:int;
		public var currentHourInt:int;
		public var hours:int;
		public var minutes:int;
		public var calcDone:Boolean = false;
		public var formFull:Boolean=false;
		public var parkStartHour:int;
		public var parkStartMin:int;
		public var parkedHours:int;
		public var parkedMins:int;
		public var totalParkedMins:uint;
		public var parkPayment:Number;
		public var hoursToPay:uint;
		public var quarterHoursToPay:int;
		public var timeTooLateErrorMessage:String="TIME MUST BE EARLIER";
		public var errorCode:uint=0;
		
		public function ArgamanTimeCalc():void
		{
			startCalc();
		}
		public function startCalc():void
		{	trace ("startCalc()");
			errorCode=0;
			trace("output text in startcalc "+output.text);
			calcDone=false;
			formFull=false;
			parkPayment=0;
			setupTextfields();
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressHandler);
			stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
		}
		public function setupTextfields():void
		{	trace ("setupTextFields");
			//set the text format object
			format.font = "Helvetica";
			format.size = 25;
			format.color = 0xFF0000;
			format.align=TextFormatAlign.LEFT;
			
			//set the text format object
			localFormat.font = "Helvetica";
			localFormat.size = 100;
			localFormat.color = 0xFF0000;
			localFormat.align=TextFormatAlign.LEFT;
				
			//configure the output text fiels
			output.defaultTextFormat = format;
			output.width = 200;
			output.height = 100;
			output.border = true;
			output.wordWrap = true;
			
			//configure the output text fiels
			localClock.defaultTextFormat = localFormat;
			localClock.width = 270;
			localClock.height = 100;
			localClock.border = true;
			localClock.wordWrap = false;
			//configure the output text fiels
			colon.width = 20;
			colon.height = 50;
			colon.border = true;
			colon.wordWrap = true;
				
			//display and position the output text field
			stage.addChild(output);
			output.x=125;
			output.y=230;
			
			//display and position the output text field
			stage.addChild(localClock);
			localClock.x=120;
			localClock.y=30;
			
			//display and position the output text field
			stage.addChild(colon);
			colon.text=":";
			colon.x=180;
			colon.y=150;
			
			//configure the input text field
			format.size = 60;
			input.defaultTextFormat = format;
			input.width = 160;
			input.height=60;
			input.border = true;
			input.type = "input";
			input.maxChars = 4;
			input.restrict = "0-9";
			input.background = true;
			input.backgroundColor = 0xCCCCCC;
			input.text = "";
			//configure the input text field
			format.size = 60;
			input1.defaultTextFormat = format;
			input1.width = 40;
			input1.height=60;
			input1.border = true;
			input1.type = "input";
			input1.maxChars = 1;
			input1.restrict = "0-2";
			input1.background = true;
			input1.backgroundColor = 0xCCCCCC;
			input1.text = "";
			//configure the input text field
			format.size = 60;
			input2.defaultTextFormat = format;
			input2.width = 40;
			input2.height=60;
			input2.border = true;
			input2.type = "input";
			input2.maxChars = 1;
			input2.restrict = "0-9";
			input2.background = true;
			input2.backgroundColor = 0xCCCCCC;
			input2.text = "";
			//configure the input text field
			format.size = 60;
			input3.defaultTextFormat = format;
			input3.width = 40;
			input3.height=60;
			input3.border = true;
			input3.type = "input";
			input3.maxChars = 1;
			input3.restrict = "0-5";
			input3.background = true;
			input3.backgroundColor = 0xCCCCCC;
			input3.text = "";
			//configure the input text field
			format.size = 60;
			input4.defaultTextFormat = format;
			input4.width = 40;
			input4.height=60;
			input4.border = true;
			input4.type = "input";
			input4.maxChars = 1;
			input4.restrict = "0-9";
			input4.background = true;
			input4.backgroundColor = 0xCCCCCC;
			input4.text = "";
			
			//display and position input field
			//stage.addChild(input);
			//input.x=70;
			//input.y=150;
			stage.addChild(input1);
			input1.x=100;
			input1.y=150;
			stage.addChild(input2);
			input2.x=140;
			input2.y=150;
			stage.addChild(input3);
			input3.x=200;
			input3.y=150;
			stage.addChild(input4);
			input4.x=240;
			input4.y=150;
			
			stage.focus=input1;
			trace ("output text - end of text field fucn. "+output.text);
		}
		
		public function calcTime():void
		{	trace("calcTime()");
			errorsHandler();
			if (errorCode==0)
			{
				calculator();
				output.text = String(parkPayment);
			}
			calcDone=true;
			trace ("calcTime function end");
		}
		
		public function calculator():void //payment is calculated here
		{	trace ("calculator()");
			CurrentTime();
			parkStartHour=int(input1.text+input2.text);
			parkStartMin=int(input3.text+input4.text);
			hours=(parkStartHour==0) ? 0:hours;
			parkedHours=hours-parkStartHour;
			parkedMins=minutes-parkStartMin;
			trace ("PArked hours mins"+parkedHours+":"+parkedMins);
			totalParkedMins=parkedMins+parkedHours*60;
			trace ("totalParkedMins "+totalParkedMins);
			if (totalParkedMins>=8)
			{
				quarterHoursToPay=(totalParkedMins)/15+1;
			}
			else
			{
				parkPayment=0;
			}
			trace ("QH"+quarterHoursToPay);
			if (quarterHoursToPay<=4 && quarterHoursToPay >1)
			{
				parkPayment=14;
			}
			else if (quarterHoursToPay>4)
			{
				parkPayment=(quarterHoursToPay-4)*3.5+14;
				trace("QH>60 "+quarterHoursToPay);
			}
			
			if (parkPayment>40)
			{
				parkPayment=40;
			}
			
		}

		public function errorsHandler ():void
		{
			if (parkStartHour > hours || (parkStartHour>= hours && parkStartMin >= minutes))
			{
				errorCode=1;
			}
			errorCodesHandler();
		}
		public function errorCodesHandler():void
		{
			if (errorCode==1)
			{	trace (errorCode);
				output.text=timeTooLateErrorMessage;
				calcDone = true;
				errorCode==0;
			}
		}
		public function keyPressHandler(event:KeyboardEvent):void
		{
			if (calcDone == true)
			{
				pauseAndReset();
			}
			else if (event.keyCode==Keyboard.ENTER)
			{
			trace (input.text);
			calcTime();
			}
		}
		
		public function CurrentTime():void
			{
			//trace ("CurrentTime()");
			theDate = new Date;
			currentHour=String(theDate.getHours());
			currentMin=String(theDate.getMinutes());
			currentHour=(currentHour.length == 1) ? "0" + currentHour : currentHour;
			currentMin=(currentMin.length == 1) ? "0" + currentMin : currentMin;
			currentTime=currentHour+":"+currentMin+"\n";
			hours=(uint(currentHour)==0) ? 24 : hours;
			minutes=uint(currentMin);
			}
	
		public function pauseAndReset ():void
			{
				stage.removeEventListener(Event.ENTER_FRAME,enterFrameHandler);
				stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyPressHandler);
				stage.addEventListener(KeyboardEvent.KEY_DOWN, pauseHandler);
				trace ("pause and reset called");
				trace (calcDone);
				startCalc();				
			}
		public function pauseHandler(event:KeyboardEvent):void
		{	trace("pauseHandler");
			if (event.keyCode==Keyboard.ENTER)
			{
				trace (input.text);
			}
		}
		public function enterFrameHandler (event:Event):void
			{
				CurrentTime();
				localClock.text=currentTime;
				trace(calcDone);
				if (calcDone != true )
				{
					fieldFocus();
				}
			}
		public function fieldFocus():void
		{
			if (calcDone == true)
			{
				pauseAndReset();
			}
			if ((calcDone!==true && errorCode==0) && formFull!==true)
			{
				if (input1.length==1)
				{
					stage.focus=input2
				}
				if (input2.length==1)
				{
					stage.focus=input3
				}
				if (input3.length==1)
				{
					stage.focus=input4
				}
				if (input4.length==1)
				{
					formFull=true;				
				}
			}
		}
		
		
		/*public function VariablesReset ():void
		{
			format:TextFormat = new TextFormat();
			localFormat:TextFormat = new TextFormat()
			output:TextField = new TextField ();
			colon:TextField = new TextField ();
			localClock:TextField = new TextField ();
			input:TextField = new TextField (); //obsolete
			input1:TextField = new TextField (); //input field for tens of hours
			input2:TextField = new TextField (); //input field for hours
			input3:TextField = new TextField (); //input field for tens of min
			input4:TextField = new TextField (); //input field for tens of hours
			theDate:Date = new Date ();
			currentDate:String;
			currentTime:String;
			currentMin:String;
			currentHour:String;
			currentMinInt:int;
			currentHourInt:int;
			hours:int;
			minutes:int;
			calcDone:Boolean = false;
			formFull:Boolean=false;
			parkStartHour:int;
			parkStartMin:int;
			parkedHours:int;
			parkedMins:int;
			totalParkedMins:uint;
			parkPayment:Number;
			hoursToPay:uint;
			quarterHoursToPay:int;
			timeTooLateErrorMessage:String="TIME MUST BE EARLIER";
			errorCode:uint=0;
		}*/
		

	}
}


Is This A Good Question/Topic? 0
  • +

Page 1 of 1