Ever wondered how change is disbursed to customers in certain stores? In Namibia the following notes exists 200,100,50,20,10 and following coins: 5, 1, 50c, 10c, 5c. For the sake of this exercises let us only consider change below 50 see below example: Sample run 1: Enter item name: Apples Enter QTY of Apples: 2 Enter price per item N$: 3.25 Amount tendered N$: 50 Your change is: N$ 43.50 Disbursed as follows: 2 x N$20; 0 x N$10; 0 x N$5; 3 x N$1; 1 x 50c; 0 x 10c; 0 x 5c [Hint: make use of % and / operators to determine the values needed, see Pg. 113 for more info
package namibia;
import java.util.Scanner;
public class Namibia {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter Item name\n");
String item_name = scan.next();
System.out.printf("Enter QTY of %s\n", item_name);
int QTY = scan.nextInt();
System.out.println("Enter price per item of N$: \n");
float price_per_item = scan.nextFloat();
System.out.println("Amount tendered N$: \n");
float amount_tendered = scan.nextFloat();
float change = amount_tendered - (QTY * price_per_item);
System.out.println(" Your change is N$: "+change);
int n_20, n_10, n_5, n_1, n_50c, n_10c, n_5c;
float remainder = change % 20;
n_20 = (int)change / 20;
change = remainder;
remainder = remainder % 10;
n_10 = (int) change / 10;
change = remainder;
remainder = remainder % 5;
n_5= (int) change / 5;
change = remainder;
remainder = remainder % 1;
n_1= (int) change / 1;
change = remainder;
remainder = remainder % (float) 0.1;
n_50c= (int) (change / 0.1);
change = remainder;
remainder = remainder % (float) 0.5;
n_10c= (int) (change / 0.5);
change = remainder;
remainder = remainder % (float) 0.05;
n_5c= (int) (change / 0.05);
System.out.printf("Disbursed as follows: %d x N$20; "
+ "%d x N$10; %d x N$5; %d x N$1; %d x "
+ "50c; %d x 10c; %d x 5c",n_20,n_10,n_5,n_1,n_50c,n_10c,n_5c);
}
}
Comments
Leave a comment