Skip to content
Advertisement

I have an enum, but i dont know how can I use correctly?

I searched so long for what enums are useful. In my Opinion there are Variables with many keywords. So have to programming a program what Is used to manage a bank. My Enum has 2 Variables EINZAHLUNG(Deposit) and AUSZAHLUNG(Payout).

public enum Transaktionsart {
    EINZAHLUNG,AUSZAHLUNG;
}

So I have an class Menue which is provide to call the Methods. The menue would be chosen by an UserInput.

public class Menue {
    public void auswahlMenue() {        
        String auswahl;
        do {
            menuePunkte();
            auswahl = MeineEingabe.erfasseAuswahl("Auswahl: ");
        
            switch (auswahl) {
                case "10":
                    geldEinzahlen();
                break;
                case "11":
                    geldAbheben();
                break;
                case "15":
                    System.out.println("Das Programm wurde Beendet!!");
                break;
            }
        } while (!auswahl.equals("15"));
    }
}

The method geldEinzahlen() Pays the desired amount to the account. EINZAHLUNG. This would be change the ArrayList, but first I want to know why I should use enum.

public void geldEinzahlen(){
    
    System.out.println("Ihr aktueller Kontostand betreagt: " +    konto.getKontoStand() +"€");
    double betrag = MeineEingabe.erfasseDouble("Wie viel wollen sie Einzahlen? ");
    System.out.println(konto.getKontoStand() + betrag);
}

The method geldAbheben() is used to pay a fee from the account. The account cant be under 0€. This would be change the ArrayList, but first I want to know why I should use enum.

public void geldAbheben(){
    
    double betrag = MeineEingabe.erfasseDouble("Wie viel wollen Sie abheben? ");

    if(konto.getKontoStand() > betrag){
        
    }
    else if (konto.getKontoStand() < betrag){
        System.out.println("Leider koennen Sie diesen Betrag nicht auszahlen");
    }
}

I have csv.file but I think that is it not so important.

So the teacher wants that I use a enum but in my Opinion it is really useless. I want to ask the community of Stack Overflow. What can I write for example that I have usage of enums.

Advertisement

Answer

It seems that you are missing the great point of enums. Enum can contain implementation, so using enum avoids any need of writing switch/case structure at all.

Let me give you and example. Please excuse me, but I am going to use English in names of identifiers.

public enum Transaction {
    DEPOSIT {
        @Override public void perform(double amount) {
             // code that puts amount to deposit
        }
    },
    PAYOUT {
        @Override public void perform(double amount) {
             // code that pays specified amount 
        }
    },
    ;
    public abstract void perform(double amount);
}

Now it is very easy to use this code:

......
Transaction.valueOf(transactionName).perform(amount);
.......

Where transactionName and amount can be parameters.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement