Java-Programming/AufgabenBlatt3/src/Aufgabe8.java

91 lines
3.0 KiB
Java

import javax.swing.*;
/**
* Generic test class for implementing and testing
* custom minimum and maximum methods
* _ _ _ _
* __ ___ __(_) |_| |_ ___ _ __ | |__ _ _
* \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | |
* \ V V /| | | | |_| || __/ | | | | |_) | |_| |
* \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, |
* |___/
* ____ __ __ _
* / ___|_ _____ _ __ \ \ / /__ __ _ ___| |
* \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ |
* ___) \ V / __/ | | | \ V / (_) | (_| | __/ |
* |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_|
* |___/
* Licensed under the GPLv2 License, Version 2.0 (the "License");
* Copyright (c) Sven Vogel
*/
public class Aufgabe8 {
/**
* calculate the linear zins over a span of years
* @param kp kaptial to invest at the beginning
* @param zs zinssatz or the amount of increase in normalized percentage [0; 1]
* @param n number of years to calculate
* @return the total amount of money
*/
private static double linearZins(double kp, double zs, int n) {
double k = kp;
if (n < 0) {
throw new IllegalArgumentException("years must not be negative");
}
for (int i = 0; i < n; i++) {
k += kp * zs;
}
return k;
}
/**
* calculate the exponetial zins over a span of years
* @param kp kaptial to invest at the beginning
* @param zs zinssatz or the amount of increase in normalized percentage [0; 1]
* @param n number of years to calculate
* @return the total amount of money
*/
static double expZins(double kp, double zs, int n) {
double k = kp;
if (n < 0) {
throw new IllegalArgumentException("years must not be negative");
}
for (int i = 0; i < n; i++) {
k += k * zs;
}
return k;
}
/**
* Opens system dialog requesting the user to enter a string.
* Ths string gets converted to a double and tested for its sign.
* If the input is invalid, i.e. not a unsigned rational number, the popup will appear again.
* @param message the message to be displayed
* @return the user input
*/
static double enterUnsigendDouble(String message) {
while (true) {
try {
double value = Double.parseDouble(JOptionPane.showInputDialog(message));
if (value >= 0.0) {
return value;
}
} catch (Exception e) {
System.out.println("not a number");
}
}
}
public static void main(String[] args) {
int n = (int) enterUnsigendDouble("Iterationen: ");
double kp = enterUnsigendDouble("Kapital: ");
double zs = enterUnsigendDouble("Zinssatz: ");
System.out.println(linearZins(kp, zs, n) + " / " + expZins(kp, zs, n));
}
}