Money Change Solver in Java

Download
Download is available until [expire_date]
  • Version
  • Download 20
  • File Size 2.68 KB
  • File Count 1
  • Create Date May 10, 2016
  • Last Updated May 10, 2016

Money Change Solver in Java

Money Change Solver in Java

The title is bit of confusing, it is not a money converter program. This program is going to compute your change based on the amount you have purchased and your payment amount.
The formula is change is equal to payment amount – amount purchased.
Note: the payment amount must be greater than or equal to amount purchased.

Source code:

import java.util.*;
public class MoneyChangeSolver
{ 
public static void main(String args[]) {

Scanner input = new Scanner ( System.in );	 
int qty ; 
double result=0;
String cont="n";
String operator;
double paymentAmount=0, change=0;

do
{
	System.out.println ("Please select item:");
	System.out.println ("1 for strawberry($1.99/pc)");
	System.out.println ("2 for apple($2.50/pc)");
	System.out.println ("3 for orange($2.66/pc)");
	System.out.println ("4 for pineapple($3.50/pc)");
	System.out.println ("5 for banana($1.50/pc)");
	System.out.println ("--------------------");
	operator=input.next();	
	
Scanner scanner = new Scanner(System.in);
System.out.print ("Enter quantity: ");
qty = scanner.nextInt();

if (operator.matches("1")){
result = 1.99 * qty ;
//System.out.println("you have purchased " + qty + " strawberry($1.99/pc) total cost: " + result );
} else if (operator.matches("2")){
result = 2.50 * qty ;
//System.out.println("you have purchased " + qty + " apple($2.50/pc) total cost: " + result );
}else if (operator.matches("3")){
result = 2.66 * qty ;
//System.out.println("you have purchased " + qty + " orange($2.66/pc) total cost: " + result );
} else if (operator.matches("4")){
result = 3.50 * qty ;
//System.out.println("you have purchased " + qty + " pineapple($3.50/pc) total cost: " + result );
} else if (operator.matches("5")){
result = 1.50 * qty ;
//System.out.println("you have purchased " + qty + " banana($1.50/pc) total cost: " + result );	
}
else{
System.out.println("Invalid input" 	);
	}

System.out.println ("Enter Payment Amount for : " +  " $" + result);
paymentAmount = scanner.nextDouble();

change=paymentAmount - result;
System.out.println ("Change: " + change);

	System.out.println ("
Try Again? (press y or n) ");
	cont = input.next();
	}while (cont.matches("y"));
	System.out.println ("
Program Closing");

	}
}
, , , , , ,

Post navigation

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.