Results 1 to 1 of 1
  1. #1
    javanewbie is offline Member
    Join Date
    Jun 2008
    Location
    Australia
    Posts
    43
    Rep Power
    0

    Exclamation Use MoneyConverter Function to determine change of amount

    Good day, everyone.

    Right here, I have created a MoneyConverter program. What I did was I put different functions in one class.:

    Java Code:
    public class MoneyConverter {
    	/* 
    	* Function Name: getPeso
    	* Descriptions: The function, when called, will simply display the equivalent word translation
    	*					of the numbers on the left side of the period.
    	* Algorithm:What the function does first is to know the length of the value of the parameter.
    	*	Then, a loop is created to go through each of the digits and calls a function depending 
    	*	on which place value the digit is on. All of the words that have been concatinated will 
    	*	be returned to the main function.
    	* Arguements: String strNumber
    	* Return Type: String
    	* Known Bugs: The function can only loop itself at a maximum of 6 times(6 digits) only.
    */
    	public String getPeso(String strNumber) {
    		String peso="";
    		int length = strNumber.length();
    		MoneyConverter money = new MoneyConverter();		
    
    		for(int iCtr = 0; iCtr<strNumber.length();iCtr++) {
    			if (length== 6) {
    				if(strNumber.substring(iCtr+1,iCtr+3).equals("00") |strNumber.substring(iCtr+1,iCtr+2).equals("0")) {
    					peso = money.getHundredThousands(strNumber.substring(iCtr,iCtr+1));
    					length =5;
    					iCtr= 1;
    				}
    				
    				else {
    					peso = money.getHundredThousands(strNumber.substring(iCtr,iCtr+1));
    				}					
    			} else if (length== 5) {
    				if (peso == "") {
    					if(Integer.parseInt(strNumber.substring(iCtr,iCtr+1)) != 1) {
    						peso = money.getTenThousands(strNumber.substring(iCtr,iCtr+1));
    					}
    					else {
    						peso = money.getSpecialTenThousands(strNumber.substring(iCtr+1,iCtr+2));
    					}	
    				} else {
    					if(Integer.parseInt(strNumber.substring(iCtr,iCtr+1)) != 1) {
    						peso = peso + " " +money.getTenThousands(strNumber.substring(iCtr,iCtr+1));
    					} else {
    						peso = peso + " " +money.getSpecialTenThousands(strNumber.substring(iCtr+1,iCtr+2));
    					}	
    				}		
    			} else if (length== 4) {
    				if (peso == "") {
    					if (strNumber.substring(iCtr+1,iCtr+4).equals("000")){
    						peso = money.getThousands(strNumber.substring(iCtr,iCtr+1));
    						length = 2;
    						iCtr=4;
    					} else if (strNumber.substring(iCtr+1,iCtr+3).equals("00")){
    						peso = money.getThousands(strNumber.substring(iCtr,iCtr+1)) + " and";
    						length = 2;
    						iCtr=2;
    					} else if (strNumber.substring(iCtr+1,iCtr+2).equals("0")){
    						peso = money.getThousands(strNumber.substring(iCtr,iCtr+1)) + " and";
    						length = 3;
    						iCtr=1;
    					} else {
    						peso = money.getThousands(strNumber.substring(iCtr,iCtr+1)) + " and";
    					}
    				} else {
    					if (strNumber.substring(iCtr+1,iCtr+4).equals("000")){
    						if (strNumber.substring(iCtr-1,iCtr).equals("1")) {
    							peso = peso + money.getThousands("");
    						} else {
    							peso = peso + " " + money.getThousands(strNumber.substring(iCtr,iCtr+1));
    						}
    						length = 2;
    						iCtr=4;
    					} else if (strNumber.substring(iCtr+1,iCtr+3).equals("00")){
    						if (strNumber.substring(iCtr-1,iCtr).equals("1")) {
    							peso = peso + money.getThousands("") + " and";
    						} else {
    							peso = peso + " " + money.getThousands(strNumber.substring(iCtr,iCtr+1)) +" and";
    						}
    						length = 2;
    						iCtr= iCtr+2;
    					} else if (strNumber.substring(iCtr+1,iCtr+2).equals("0")){
    						if (strNumber.substring(iCtr-1,iCtr).equals("1")) {
    							peso = peso + money.getThousands("") + " and";
    						} else {
    							peso = peso + " " + money.getThousands(strNumber.substring(iCtr,iCtr+1)) +" and";
    						}
    						length = 3;
    						iCtr=iCtr +1;
    					} else {
    						if (strNumber.substring(iCtr-1,iCtr).equals("1")) {
    							peso = peso +money.getThousands("") + " and";
    						} else if (strNumber.substring(iCtr-1,iCtr).equals("0")) {
    							peso = peso +money.getThousands(strNumber.substring(iCtr,iCtr+1)) + " and";							
    						} else {
    							peso = peso + " " +money.getThousands(strNumber.substring(iCtr,iCtr+1)) + " and";
    						}
    					}
    				}
    			} else if (length== 3) {
    				if (peso == "") {
    					if(strNumber.substring(iCtr+1,iCtr+2).equals("0")) {
    						peso = money.getHundreds(strNumber.substring(iCtr,iCtr+1));
    						length = 2;
    						iCtr=4;
    					} else {
    						peso = money.getHundreds(strNumber.substring(iCtr,iCtr+1)) + " and";
    					}	
    				}
    				else {
    					if(strNumber.substring(iCtr+1,iCtr+2).equals("0")) {
    						peso = peso + " "+money.getHundreds(strNumber.substring(iCtr,iCtr+1));
    						length =2;
    						iCtr=4;
    					} else {
    						peso = peso + " "+ money.getHundreds(strNumber.substring(iCtr,iCtr+1)) + " and";
    					}
    				}		
    			} else if (length== 2) {
    				if (peso == "") {
    					if(Integer.parseInt(strNumber.substring(iCtr,iCtr+1)) != 1) {
    						peso = money.getTens(strNumber.substring(iCtr,iCtr+1));
    					} else {
    						peso = money.getSpecialTens(strNumber.substring(iCtr+1,iCtr+2));
    					}	
    				} else {
    					if(Integer.parseInt(strNumber.substring(iCtr,iCtr+1)) != 1) {
    						peso = peso + " " +money.getTens(strNumber.substring(iCtr,iCtr+1));
    					} else {
    						peso = peso +" "+money.getSpecialTens(strNumber.substring(iCtr+1,iCtr+2));
    					}	
    				}		
    			} else if (length== 1) {
    				if (peso == "") {
    						peso = money.getOnes(strNumber.substring(iCtr,iCtr+1));	
    				} else if (strNumber.substring(iCtr-1,iCtr).equals("1")) {
    						peso = peso + money.getOnes("0");	
    				} else if (strNumber.substring(iCtr,iCtr+1).equals("0")) {
    						peso = peso + money.getOnes(strNumber.substring(iCtr,iCtr+1));
    				} else {
    						peso = peso + " "+money.getOnes(strNumber.substring(iCtr,iCtr+1));
    				}		
    			} else if (length >6) {
    				peso = "not reachable";
    			} else {
    				continue;
    			}
    			
    			length--;
    		}
    		
    		if(peso.equals("one")) {
    			return peso + " peso";
    		} else if (peso.equals("")){
    			return "Zero";
    		} else if (peso.equals("not reachable")){
    			return "The number inputted cannot be converted. It is over the range";
    		} else {
    			return peso+ " pesos";
    		}
    	}
    	
    /* 
    	* Function Name: getCentavo
    	* Descriptions: The function, when called, will simply display the equivalent word transalation
    	*					of the numbers on the left side of the period.
    	* Algorithm:What the function does first is to know the length of the value of the parameter.
    	*	Then, a loop is created again to go through each of the digits and calls a function depending 
    	*	on which place value the digit is on. All of the words that have been concatinated will 
    	*	be returned to the calling function.
    	* Arguements: String strNumber
    	* Return Type: String
    	* Known Bugs: The function can only loop itself at a maximum of 2 times(2 digits) only.
    */
    	public String getCentavo(String strNumber) {
    		String strCentavo="";
    		MoneyConverter money = new MoneyConverter();		
    
    		for(int iCtr=0;iCtr<strNumber.length(); iCtr++) {
    			if (iCtr == 0) {
    				if(Integer.parseInt(strNumber.substring(iCtr,iCtr+1)) == 0) {
    					continue;
    				}
    				else if (Integer.parseInt(strNumber.substring(iCtr,iCtr+1)) != 1) {
    					if(Integer.parseInt(strNumber.substring(iCtr+1,iCtr+2)) != 0) {
    						strCentavo = money.getTens(strNumber.substring(iCtr,iCtr+1)) + "-";
    					} else {
    						strCentavo = money.getTens(strNumber.substring(iCtr,iCtr+1));
    					}
    				} else {							
    					strCentavo = money.getSpecialTens(strNumber.substring(iCtr+1,iCtr+2));
    				}					
    			} else {
    				if (strNumber.substring(iCtr-1,iCtr).equals("1")) {
    					strCentavo = strCentavo + money.getOnes("");	
    				} else {
    					strCentavo = strCentavo +money.getOnes(strNumber.substring(iCtr,iCtr+1));
    				}		
    			}
    		}
    		
    		if(strCentavo.equals("one")) {
    			return strCentavo + " centavo";
    		} else {
    			return strCentavo+ " centavos";
    		}
    	}
    
    /* 
    	* Function Name: getOnes
    	* Descriptions: The function, when called, will simply display the equivalent word transalation
    	*					of a specific number.
    	* Algorithm:The function checks what the value of the parameter is. Then depending on that value it
    	* 			will return a word equivalent to it.
    	* Arguements: String strNumber
    	* Return Type: String
    	* Known Bugs: none
    	*
    */
    	public String getOnes(String strNumber){
    		String strNum[] = {"","one","two","three","four","five","six","seven","eight","nine"};
    		return strNum[Integer.parseInt(strNumber)];
    
    	}
    /* 
    	* Function Name: getSpecialTens
    	* Descriptions: The function, when called, will simply display the 
    	*			    equivalent word transalation of a specific number.
    	* Algorithm:The function checks what the value of the parameter is. 
    	*           Then depending on that value it will return a word 
    	*           equivalent to it.
    	* Arguements: String strNumber
    	* Return Type: String
    	* Known Bugs: none
    	*
    */
    	public String getSpecialTens(String strNumber) {
    		String strNum[] = {"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
    		return strNum[Integer.parseInt(strNumber)];
    
    	}
    	
    /* 
    	* Function Name: getTens
    	* Descriptions: The function, when called, will simply display the 
    	*               equivalent word transalation of a specific number.
    	* Algorithm:The function checks what the value of the parameter is. 
    	*           Then depending on that value it will return a word equivalent to it.
    	* Arguements: String strNumber
    	* Return Type: String
    	* Known Bugs: none
    	*
    */
    	public String getTens(String strNumber){
    		String strNum[] = {"","","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
    		return strNum[Integer.parseInt(strNumber)];
    	}
    
    /* 
    	* Function Name: getHundreds
    	* Descriptions: The function, when called, will simply display the 
    	*               equivalent word transalation of a specific number.
    	* Algorithm:The function check what the value of the parameter is. 
    	*           Then depending on that value it will return a word equivalent to it.
    	* Arguements: String strNumber
    	* Return Type: String
    	* Known Bugs: --
    	*
    */	
    	public String getHundreds(String strNumber){		
    		MoneyConverter money = new MoneyConverter();
    		if (strNumber.equals("0")) {
    			return money.getOnes(strNumber);
    		}	
    		else {	
    			return money.getOnes(strNumber) + " hundred";
    		}
    	}
    	
    /* 
    	* Function Name: getThousands
    	* Descriptions: The function, when called, will simply display the 
    	*               equivalent word transalation of a specific number.
    	* Algorithm:The function check what the value of the parameter is. 
    	*           Then depending on that value it will return a word equivalent to it.
    	* Arguements: String strNumber
    	* Return Type: String
    	* Known Bugs: --
    	*
    */
    	public String getThousands(String strNumber){
    		MoneyConverter money = new MoneyConverter();		
    		return money.getOnes(strNumber) + " thousand";
    	}
    
    /* 
    	* Function Name: getTenThousands
    	* Descriptions: The function, when called, will simply display the 
    	*               equivalent word transalation of a specific number.
    	* Algorithm:The function check what the value of the parameter is. 
    	*           Then depending on that value it will return a word equivalent to it.
    	* Arguements: String strNumber
    	* Return Type: String
    	* Known Bugs: --
    	*
    */	
    	public String getTenThousands(String strNumber){
    		MoneyConverter money = new MoneyConverter();		
    		return money.getTens(strNumber);
    	}
    
    /* 
    	* Function Name: getSpecialTenThousands
    	* Descriptions: The function, when called, will simply display the 
    	*               equivalent word transalation of a specific number.
    	* Algorithm:The function check what the value of the parameter is. 
    	*           Then depending on that value it will return a word equivalent to it.
    	* Arguements: String strNumber
    	* Return Type: String
    	* Known Bugs: none
    	*
    */	
    	public String getSpecialTenThousands(String strNumber){
    		MoneyConverter money = new MoneyConverter();		
    		return money.getSpecialTens(strNumber);
    	}
    
    /* 
    	* Function Name: getHundredThousands
    	* Descriptions: The function, when called, will simply display the 
    	*               equivalent word transalation of a specific number.
    	* Algorithm:The function check what the value of the parameter is. 
    	*           Then depending on that value it will return a word equivalent to it.
    	* Arguements: String strNumber
    	* Return Type: String
    	* Known Bugs: none
    	*
    */	
    	public String getHundredThousands(String strNumber){
    		MoneyConverter money = new MoneyConverter();		
    		return money.getHundreds(strNumber);
    	}
    
    }
    The following codes will be used in the other seperate file (Validation.java) with a function named 'getPayment' to determine the change amount. Let's say the user will input 55 (pesos). The output would be 55-10 = 40. Instead of the number '40'. The program will display: forty pesos.

    Other example:
    Input: 36.45 -->> 36.45 - 10 = 26.45
    Output: twenty six pesos and forty five cents

    Java Code:
    public void getPayment(){
    		String strPayment;
    		MoneyConverter money = new MoneyConverter();
    		do {
    			strPayment = ReaderData.getStringInput("Enter payment amount: ");
    		} while (new Validation().CheckPayment(strPayment));
    		
    		double dChange = (Double.parseDouble(strPayment)) - 10;
    		String strChange =  String.valueOf(dChange);
    		int iPeriod = strChange.indexOf(".");
    		String strPeso = strChange.substring(0,iPeriod);
    		String strCentavo = strChange.substring(iPeriod+1,strChange.length());
    
    		if (strCentavo.length() ==1){
    			strCentavo = strCentavo + "0";
    		}
    			
    		if(dChange>=0 & Integer.parseInt(strCentavo.substring(0,2)) ==0 ){
    			System.out.println("Your change is " + money.getPeso(strPeso)+".\n");
    		}else if (dChange<1) {
    			System.out.println("Your change is " + money.getCentavo(strCentavo.substring(0,2)) +".\n");
    		} else if (dChange == 0) {
    			System.out.println("Your have no change.\n");			
    		}else {
    			System.out.println("Your change is " + money.getPeso(strPeso) + " and " + money.getCentavo(strCentavo.substring(0,2))+".\n");
    		}	
    	}
    Any comments/suggestions for improvement?


    Thanks in advance.
    Last edited by javanewbie; 08-21-2008 at 02:29 PM. Reason: fix the code part

Similar Threads

  1. How to determine CPU usage using Java?????
    By JavaEmpires in forum New To Java
    Replies: 5
    Last Post: 03-16-2011, 05:49 AM
  2. Replies: 0
    Last Post: 04-07-2008, 08:56 PM
  3. iteration on huge amount of files in a folder
    By tshaked in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 08:08 PM
  4. how to determine the primary key
    By osval in forum JDBC
    Replies: 1
    Last Post: 08-07-2007, 03:31 AM
  5. Replies: 1
    Last Post: 05-20-2007, 12:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •