I'm starting out in Java and was given an exercise to program a simple ATM. Functions include:
- Remember Owner - check
- open account and be able to directly deposit money though the constructor - check
- be able to withdraw money be able to deposit money - check
- be able to cancel the account, and have any subsequent actions be met with a throw new IllegalStateException - check
- have a maximum of 100.000 dollars acceptable in the account and tell operator if they are trying to go over the limit and how much they can deposit until their at their limit - check(sort of)
Hints included: People want to see their money in 100.00 form, but that's bad for calculating in Java, so keep the internal machine operating on cents (Rappen in Switzerland) and any external messages in Dollars and Cents (ie. double). Everything is working fine apart from the translation from cents to dollars & cents. I decided to use a parallel double next to the cents and call a method that would turn cents into dollars and cents without overwriting or changing the "internal" count. Unfortunately, it doesn't seem to want to pass the parameters successfully to the mentioned method and is thus not doing the conversion correctly.
In the constructor I wanted to have kontostandInFranken (account balance in Dollars and Cents) carry the same weight as the kontostand (account balance in cents), but already there I'm hitting walls. For example Dave opens an account and immediately deposits 25.50 dollars in there. When I run that calculation in the constructor the 50 cents will go missing because I have to convert the kontostandInFranken (account balance in Dollars and Cents) into an int before I can multiply it by 100 and have it equal kontostand (account balance in cents)
/**
* @author ([email protected])
* @version (1.0)
*
* things that don't work:
* PASSING PARAMETERS BETWEEN METHODS. WHAT AM I DOING WRONG?
* CONVERTING BETWEEN RAPPEN AND FRANKEN & RAPPEN. ALWAYS ISSUES WITH DOUBLE VS. INTS
*/
public class Aufgabe2a
{
private int kontostand;
private double kontostandInFranken;
private String kontoInhaber;
private boolean kontoSaldiert;
/**
* Constructor
*/
public Aufgabe2a(String kontoInhaber, double kontostandInFranken)
{
this.kontoInhaber = kontoInhaber;
boolean kontoSaldiert = false;
this.kontostand = kontostand;
/**
*First issue. I can't seem to get the conversion from cents to dollars and *cents right
*/ kontostand = (int)kontostandInFranken * (int)100;
}
/**
* Ermöglicht Kontoeinzahlungen
*/
public int geldEinzahlen(int geldEingezahlt){
if(geldEingezahlt <=0){
System.out.println("Sie können nur positive Beträge einzahlen. Bitte versuchen Sie es erneut.");
return 0;
}
else if (kontoSaldiert == true){
kontoCheck();
}
else frankenInRappen(geldEingezahlt); //calling converting method here
if (kontostand + geldEingezahlt > 10000000){
System.out.println("Sie überschreiten mit dieser Einzahlung die Maximalhöhe von CHF 100.000. Sie können nur CHF " + kontostandInFranken(kontostand) +
(100000 - kontostandInFranken) + "einzahlen.");
}
else kontostand = (kontostand + geldEingezahlt);
return kontostand;
}
/**
* Ermöglicht Geld abzuheben
*/
public int geldAbheben(int geldAbgehoben){
if(geldAbgehoben <=0){
System.out.println("Sie können nur positive Beträge abheben. Bitte versuchen Sie es erneut.");
return 0;
}
else if (kontoSaldiert == true){
kontoCheck();
}
else frankenInRappen(geldAbgehoben);
if (kontostand - geldAbgehoben < 0){
System.out.println("Sie möchten zuviel Geld abheben. Sie können nur" + kontostand + "abheben.");
}
else kontostand = (kontostand - geldAbgehoben);
return kontostand;
}
/**
* Rechnet Rappen in Franken um - calculats cents into dollars and cents *ISSUE HERE
*/
private double rappenInFranken(double betrag){
double frankenBetrag = (betrag/100.0);
return frankenBetrag;
}
/**
* Rechnet Franken in Rappen um
*/
private int frankenInRappen(int betrag){
int rappenBetrag = (betrag*100);
return rappenBetrag;
}
/**
* Kontostand abfragen
*/
public void kontostandAbfragen(){
kontoCheck();
rappenInFranken(kontostand);
System.out.println("Kontoinhaber: " + kontoInhaber);
System.out.println("Kontostand: CHF " + kontostand +".-" );
frankenInRappen(kontostand);
}
/**
* Konto Saldieren/Auflösen
*/
public void kontoSaldieren(boolean kontoSaldiert){
kontoCheck();
this.kontoSaldiert = kontoSaldiert;
System.out.println("Sie haben Ihr Konto aufgelöst. Bitte nehmen Sie den restlichen Kontostand an sich");
kontostand = kontostand - kontostand;
}
/**
* Überprüft ob das Konto saldiert ist
*/
private void kontoCheck(){
if (kontoSaldiert == true){
throw new IllegalStateException("Konto ist saldiert, keine weiteren Aktionen möglich.");
} else return;
}
/**
* Führt einen paralelen Kontostand in Franken
*/
private double kontostandInFranken(int kontostand){
kontostandInFranken = (kontostand / 100.0);
return kontostandInFranken;
}
}