Exceptions

PPhilipp

Grünschnabel
public class Shopping {

/** Konstruktor von Shopping **//

public Shopping () {

}

public void goShopping (int day, int hour, int minute) {}

}


Diese Klasse sol so erweitert werden, dass die Methode goShopping an Sonntagen (day == 7) und zwischen 20 und 8 Uhr mit einer neu programmierten Exception ShopsClosedException abgebrochen wird.
Zusätzlich soll eine main-Methode eingefügt werden, die folgenden Code ausführt:

Shopping food = new Shopping();
food.goShopping (6, 15, 45);
food.goShopping (4, 13, 11);
food.goShopping (7, 9, 15);
 
Code:
public class Shopping {
    public Shopping() {

    }

    public void goShopping(int day, int hour, int minute) 
    throws ShopsClosedException {
        if((day == 7) || (hour < 8) || (hour > 20)) {
            throw new ShopsClosedException();
        }
        // Laden ist noch offen
    }

    public static void main(String[] args) {
        try {
            Shopping food = new Shopping();
            food.goShopping (6, 15, 45);
            food.goShopping (4, 13, 11);
            food.goShopping (7, 9, 15);
        }
        catch(ShopsClosedException e) {
            // ...
        }
    }
}
 

Neue Beiträge

Zurück