Skip to content

Instantly share code, notes, and snippets.

@tamboer
Last active August 29, 2015 14:11
Show Gist options
  • Select an option

  • Save tamboer/fc13de8c24e5f1455c44 to your computer and use it in GitHub Desktop.

Select an option

Save tamboer/fc13de8c24e5f1455c44 to your computer and use it in GitHub Desktop.
javaSnoepAutomaat
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
//package oefs;
import java.util.Scanner;
/**
*
* @author Willem
*/
public class SnoepAutomaat {
public static void main(String args[]){
String str = calc();
System.out.println(str);
}
public static String calc(){
String str = "";
//float snoep = 0.30F;
//float snoep2 = 1.20F;
//float kost = 0.42F;
Scanner input = new Scanner(System.in);
System.out.print("Total: ");
float total = input.nextFloat();
System.out.print("Cash: ");
float cash = input.nextFloat();
float change = cash - total;
float remainingAmount = change * 100;
int qtyOneEuro = (int)remainingAmount / 100;
remainingAmount = remainingAmount % 100;
int qtyFiftyCent = (int)remainingAmount / 50;
remainingAmount = remainingAmount % 50;
int qtyTwentyCent = (int)remainingAmount / 20;
remainingAmount = remainingAmount % 20;
int qtyTenCent = (int)remainingAmount / 10;
remainingAmount = remainingAmount % 10;
int qtyFiveCent = (int)remainingAmount / 5;
remainingAmount = remainingAmount % 5;
int qtyTwoCent = (int)remainingAmount / 2;
remainingAmount = remainingAmount % 2;
int qtyOneCent = (int)remainingAmount / 1;
remainingAmount = remainingAmount % 1;
str += change;
str += "\n " + qtyOneEuro + " x Euros";
str += "\n " + qtyFiftyCent + " x 50 Cents";
str += "\n " + qtyTwentyCent + " x 20 Cents";
str += "\n " + qtyTenCent + " x 10 Cents";
str += "\n " + qtyFiveCent + " x 5 Cents";
str += "\n " + qtyTwoCent + " x 2 Cents";
str += "\n " + qtyOneCent + " x 1 Cents";
str += "\n remaining " + (int)remainingAmount;
return str;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment