0

I'm working on an exercise in my Data Structures book, and it seems that I am printing the reference address instead of the actual contents? Can someone please take a look at my code and help me out? Thank you for your time.

public class CreditCard {
//  instance variables
private String customer;
private String bank;
private String account;
private int limit;
protected double balance;

// constructors - account for all cases, one with a bal, and one without
public CreditCard(String cust, String bk, String acnt, int lim, double bal){
    customer = cust;
    bank = bk;
    account = acnt;
    limit = lim;
    balance = bal;
}
public CreditCard(String cust, String bk, String acnt, int lim){
    this(cust, bk, acnt, lim, - 0.0);
}
// accessors
public String getCustomer(){return customer;}
public String getBank(){return bank;}
public String getAccount(){return account;}
public double getLimit(){return limit;}
public double getBalance(){return  balance;}

// updaters
public boolean charge(double price){
    if(price + balance > limit){
        return false;
    }
    else balance += price;
    return true;
}
public void makePayment(double amount){
    balance -= amount;
}

// utility (static)
public static void printSummary(CreditCard card){
    System.out.println("Customer = " + card.customer);
    System.out.println("Bank = " + card.bank);
    System.out.println("Account = " + card.account);
    System.out.println("Limit = " + card.limit);
    System.out.println("Balance = " + card.balance);
}

}// end class CreditCard

ggx7
  • 49
  • 8
  • You need to add a `toString()` method for your `CreditCard` class. This method will override the same method in the `Object` superclass. It will also be implicitly called when trying to get a String representation of your object. By default, you get the result you seem to be seeing. – Kon Jul 10 '17 at 16:05

2 Answers2

1

Add toString method (just override one).

public class CreditCard {
    //  instance variables
    private String customer;
    private String bank;
    private String account;
    private int limit;
    protected double balance;

    // constructors - account for all cases, one with a bal, and one without
    public CreditCard(String cust, String bk, String acnt, int lim, double bal) {
        customer = cust;
        bank = bk;
        account = acnt;
        limit = lim;
        balance = bal;
    }

    public CreditCard(String cust, String bk, String acnt, int lim) {
        this(cust, bk, acnt, lim, -0.0);
    }

    // accessors
    public String getCustomer() {
        return customer;
    }

    public String getBank() {
        return bank;
    }

    public String getAccount() {
        return account;
    }

    public double getLimit() {
        return limit;
    }

    public double getBalance() {
        return balance;
    }

    // updaters
    public boolean charge(double price) {
        if (price + balance > limit) {
            return false;
        } else {
            balance += price;
        }
        return true;
    }

    public void makePayment(double amount) {
        balance -= amount;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("CreditCard{");
        sb.append("customer='").append(customer).append('\'');
        sb.append(", bank='").append(bank).append('\'');
        sb.append(", account='").append(account).append('\'');
        sb.append(", limit=").append(limit);
        sb.append(", balance=").append(balance);
        sb.append('}');
        return sb.toString();
    }

    // you do not need this util method because of it converts to
    public static void printSummary(CreditCard card) {
        System.out.println("CreditCard = " + card);
    }
}// end class CreditCard
Sergii
  • 7,044
  • 14
  • 58
  • 116
  • Thank you! I had actually solved it just after I posted the question, but this was very helpful and educational. – ggx7 Jul 12 '17 at 16:22
0

What you got was the toString() implementation of class Object. Quoted from its javadoc:

The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

That means, you need to override it with your own implementation of toString() in class CreditCard. It can be as simple as this:

@Override
public String toString() {
    return getClass().getName()
         + "[ customer = " + customer
         + ", bank = " + bank
         + ", account = " + account
         + ", limit = " + limit
         + ", balance = " + balance
         + "]";
}

If you prefer multi-line output, you may insert some \n into the partial strings above.

By the way: You don't need your method public static void printSummary(CreditCard card) anymore. Instead of using

CreditCard.printSummary(creditCard);

you now can simply use

System.out.println(creditCard);

Under the hood that will call your toString() method.

Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49