1

I'm starting out in Java and was given an exercise to program a simple ATM. Functions include:

  1. Remember Owner - check
  2. open account and be able to directly deposit money though the constructor - check
  3. be able to withdraw money be able to deposit money - check
  4. be able to cancel the account, and have any subsequent actions be met with a throw new IllegalStateException - check
  5. 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;
    }
}
1
  • 1
    take the dollar amount and multiply it by 100, so 53.27 becomes 5327. store that as an int. all math will work fine on it. when necessary, just divide by 100, so 5327 becomes 53.27 again. Commented Aug 21, 2015 at 21:02

2 Answers 2

2

As it says in the hint, it will be easier if you only store a number of cents.

Ex.

public class CheckingAccount
{
    private int _balance;
    private String _name;
    public CheckingAccount(String name, double initialBalance)
    {
        _name = name;
        _balance = (int)(initialBalance * 100);
    }
    ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for taking the time to help. Yes, this is indeed what I have been told as well and I will augment accordingly. Thanks again!
1

You probably shouldn't store "dollar" and "cents" separately in two int values. Just store the "amount" in a double or a BigDecimal, then format the value when displaying.

Example using double:

double amount = 25.5;
System.out.println(amount); // Unformatted

System.out.printf("%.2f%n", amount);
System.out.println(new DecimalFormat("0.00").format(amount));
System.out.println(new DecimalFormat("#,##0.00").format(amount));
System.out.println(NumberFormat.getCurrencyInstance().format(amount));

amount = 100000;
System.out.printf("%.2f%n", amount);
System.out.println(new DecimalFormat("0.00").format(amount));
System.out.println(new DecimalFormat("#,##0.00").format(amount));
System.out.println(NumberFormat.getCurrencyInstance().format(amount));

Output:

25.5

25.50
25.50
25.50
$25.50

100000.00
100000.00
100,000.00
$100,000.00

1 Comment

oh nice! I haven't gotten to formatting printlns yet but now I know how! The point of the exercise was to be able to work with doubles and ints within one class and be able to accurately convert between the two. Will defo use the getCurrencyInstance! Thanks for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.