Java in der Schule. Immer wieder ein lustiges Thema, manchmal jedoch leicht deprimierend. Letzte Aufgabe: Erstelle einen Kindergeld-Rechner. Ich poste hier mal meinen. Wer nicht weis was die Klasse IO ist – ich weis es auch nicht wirklich. Trotzdem wird diese mysteriöse Klasse im Unterricht verwendet und steht auf der must-use Liste. IO.readInt(); liest von der semantik her einfach ein Integer-Wert aus der Konsole ein. Gestern habe ich zudem erfahren, dass optional als String noch der Prefix für die Eingabe übergeben werden kann. more für SourceCode anklicken.
/**
* Calculate the money which you
* should receive by your
* government for your children
*
* @author Christian `TheReaper` Jantz
* @copyright 2010
* @since 05/17/2010 10:54 pm
*
*/
public class Kindergeld {
/**
* Money for the first child upwards
*/
protected static int iMoneyFirst = 184;
/**
* Money for the third child upwards
*/
protected static int iMoneyThird = 190;
/**
* Money for the fourth child upwards
*/
protected static int iMoneyFourth = 215;
/**
* Initialize the script
*
* @param String[] args
*/
public static void main(String[] args) {
int iChildren = 0;
int iMoney = 0;
// get count of children
iChildren = getChildren();
// calculate the money
iMoney = calcMoney(iChildren);
// output the money
outputMoney(iMoney, iChildren);
}
/**
* Prompt for how many children
* the family has
*
* @return integer
*/
public static int getChildren() {
int iChildren = 0;
// use do while to ensure that there is at least one child
do {
System.out.print("Children Count (must be >= 1): ");
iChildren = IO.readInt();
} while(iChildren <= 0);
return iChildren;
}
/**
* Calculate how many money the
* family should receive
*
* @param integer iChildren
* @return integer Money the family receives
*/
public static int calcMoney(int iChildren) {
int iMoney = 0;
// calculate the money, use a loop for doing this
for(int i = 0; i < iChildren; i++) {
// add the money to the variable, use short if-structure
iMoney += ((i < 2) ? iMoneyFirst : ((i < 3) ? iMoneyThird : iMoneyFourth));
}
return iMoney;
}
/**
* Output how much money the family
* should receive
*
* @param integer iMoney
* @param integer iChildren
*/
public static void outputMoney(int iMoney, int iChildren) {
System.out.println("The family should receive " + iMoney + " € (" + iChildren + " children) a month.");
}
}