Modify the CreditCard class from Code Fragment 1.5 so that printSummary becomes a nonstaticmethod, and modify the mainmethod fromCode Fragment 1.6 accordingly.
package java_problems_datastructures;
// problem: chapter 1 - 24;
// Change the printSummery method to non static and calling using it's object
public class CreditCard2 {
// 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 CreditCard2(String cust, String bk, String acnt, int lim, double initialBal) {
customer = cust;
bank = bk;
account = acnt;
limit = lim;
balance = initialBal;
}
public CreditCard2(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;
}
// Utility method to print a card's information
// Change the method from static to non static
public void printSummary(CreditCard2 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
}
// Driver method
public static void main(String[] args) {
CreditCard2[] wallet = new CreditCard2[3];
wallet[0] = new CreditCard2("John Bowman", "California Savings", "5391 0375 9387 5309", 5000);
wallet[1] = new CreditCard2("John Bowman", "California Federal", "3485 0399 3395 1954", 3500);
wallet[2] = new CreditCard2("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 (CreditCard2 card : wallet) {
// To call a non static method we have to call the method by creating object for that class.
card.printSummary(card);
while (card.getBalance() > 200.0) {
card.makePayment(200);
System.out.println("New balance = " + card.getBalance());
}
System.out.println();
}
}
}
Output:
Customer = John Bowman
Bank = California Savings
Account = 5391 0375 9387 5309
Balance = 408.0
Limit = 5000
New balance = 208.0
New balance = 8.0
Customer = John Bowman
Bank = California Federal
Account = 3485 0399 3395 1954
Balance = 408.0
Limit = 3500
New balance = 208.0
New balance = 8.0
Customer = John Bowman
Bank = California Finance
Account = 5391 0375 9387 5309
Balance = 436.0
Limit = 2500
New balance = 236.0
New balance = 36.0