/** * GringottsAccount. * * @author John Paxton * @version November 7, 2008 */ public class GringottsAccount { GringottsAccount (String name, int galleons, int sickles, int knuts) { this.owner = name; this.galleons = galleons; this.sickles = sickles; this.knuts = knuts; this.numberOfAccounts++; // demonstrate the use of a class variable } GringottsAccount (String name) { owner = name; galleons = 0; sickles = 0; knuts = 0; this.numberOfAccounts++; // demonstrate the use of a class variable } /** * Convert the instance to a printable representation. * * This method replaces the old "print" method and decouples the * GringottsAccount class from the System.out class * @return a printable representation of a GringottsAccount */ public String toString() { String result; result = "--------------------\n"; result = result + "Account owner: " + owner + "\n"; result = result + "Galleons: " + galleons + "\n"; result = result + "Sickles: " + sickles + "\n"; result = result + "Knuts: " + knuts + "\n"; return result; } /** * Convert the money in the account to knuts. * * Precondition: sickles, knuts and galleons all have initial values * Postcondition: sickles = 0; galleons = 0; knuts is recalculated */ public void convertToKnuts() { sickles = sickles + SICKLES_IN_A_GALLEON * galleons; galleons = 0; knuts = knuts + KNUTS_IN_A_SICKLE * sickles; sickles = 0; } public void convertToGalleons() { sickles = sickles + (knuts / KNUTS_IN_A_SICKLE); knuts = knuts % KNUTS_IN_A_SICKLE; galleons = galleons + (sickles / SICKLES_IN_A_GALLEON); sickles = sickles % SICKLES_IN_A_GALLEON; } public String getOwner() { return owner; } public int getGalleons() { return galleons; } public int getSickles() { return sickles; } public int getKnuts() { return knuts; } public void setGalleons(int galleons) { this.galleons = galleons; } public void setSickles (int sickles) { this.sickles = sickles; } public void setKnuts (int knuts) { this.knuts = knuts; } /** * Make a deposit into a Gringott's Account. * Precondition: the appropriate instance variables and parameters have values * Postcondition: each instance variable is updated by adding the corresponding parameter * * @param galleons the number of galleons to add * @param sickles the number of sickles to add * @param knuts the numbers to add */ public void makeDeposit (int galleons, int sickles, int knuts) { this.galleons = this.galleons + galleons; this.sickles += sickles; this.knuts += knuts; } public static final int SICKLES_IN_A_GALLEON = 17; // class constant or static constant public static final int KNUTS_IN_A_SICKLE = 29; private static int numberOfAccounts = 0; // class variable or static variable private String owner; // instance variable or non-static variable private int galleons; private int sickles; private int knuts; }