SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser
Chapter:
Java Primer
Exercise:
Exercises
Question:25 | ISBN:9781118771334 | Edition: 6

Question

Modify the CreditCard class to add a toString( ) method that returns a String representation of the card (rather than printing it to the console, as done by printSummary). Modify the main method from Code Fragment 1.6 accordingly
to use the standard println command.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

package java_problems_datastructures;

public class CreditCard1 {
	// Instance variables:
	private String customer; // name of the customer (e.g., ”John Bowman”)
	private String bank; // name of the bank (e.g., ”California Savings”)
	private String account; // account identifier (e.g., ”5391 0375 9387 5309”)
	private int limit; // credit limit (measured in dollars)
	protected double balance; // current balance (measured in dollars)
	// Constructors:

	public CreditCard1(String cust, String bk, String acnt, int lim, double initialBal) {
		customer = cust;
		bank = bk;
		account = acnt;
		limit = lim;
		balance = initialBal;
	}

	public CreditCard1(String cust, String bk, String acnt, int lim) {
		this(cust, bk, acnt, lim, 0.0); // use a balance of zero as default
	}

	// Accessor methods:
	public String getCustomer() {
		return customer;
	}

	public String getBank() {
		return bank;
	}

	public String getAccount() {
		return account;
	}

	public int getLimit() {
		return limit;
	}

	public double getBalance() {
		return balance;
	}

	// Update methods:
	public boolean charge(double price) { // make a charge
		if (price + balance > limit) // if charge would surpass limit
			return false; // refuse the charge
		// at this point, the charge is successful
		balance += price; // update the balance
		return true; // announce the good news
	}

	public void makePayment(double amount) { // make a payment
		balance -= amount;
	}
	
	// commenting out the printSummery method to make sure it override our toString method.

	// Utility method to print a card's information
//	public static void printSummary(CreditCard1 card) {
//		System.out.println("Customer = " + card.customer);
//		System.out.println("Bank = " + card.bank);
//		System.out.println("Account = " + card.account);
//		System.out.println("Balance = " + card.balance); // implicit cast
//		System.out.println("Limit = " + card.limit); // implicit cast
//	}

	// We will override this toString method to print CreditCard objects
	@Override
	public String toString() {
	 
	 return "name: "+customer+"\n"
	 		+ "bank: " +bank+ "\n"
	 		+ "Account no: " +account+ "\n" 
	 		+ "Balance: " +balance+ "\n" 
	 		+ "limit: " +limit+"."; 
	 
 }
	// main method shown on next page...

	public static void main(String[] args) {
		CreditCard1[] wallet = new CreditCard1[3];
		wallet[0] = new CreditCard1("John Bowman", "California Savings", "5391 0375 9387 5309", 5000);
		wallet[1] = new CreditCard1("John Bowman", "California Federal", "3485 0399 3395 1954", 3500);
		wallet[2] = new CreditCard1("John Bowman", "California Finance", "5391 0375 9387 5309", 2500, 300);

		
		
		for (int val = 1; val <= 16; val++) {
			 wallet[0].charge(3*val);
			 wallet[1].charge(3*val);
			 wallet[2].charge(val);
			 }
			
			 for (CreditCard1 card : wallet) {
			 System.out.print((card)); // Here we are printing card details directly by overriding toString() method
			 while (card.getBalance() > 200.0) {
			 card.makePayment(200);
			 System.out.println("New balance = " + card.getBalance());
			  }
			 System.out.println();
			 }
		
		
	}
}


Output:

name: John Bowman
bank: California Savings
Account no: 5391 0375 9387 5309
Balance: 408.0
limit: 5000.New balance = 208.0
New balance = 8.0

name: John Bowman
bank: California Federal
Account no: 3485 0399 3395 1954
Balance: 408.0
limit: 3500.New balance = 208.0
New balance = 8.0

name: John Bowman
bank: California Finance
Account no: 5391 0375 9387 5309
Balance: 436.0
limit: 2500.New balance = 236.0
New balance = 36.0

 

0 0

Discussions

Post the discussion to improve the above solution.