Your parents have given you a credit card!!!! Both of your parents and yourself
ID: 3857785 • Letter: Y
Question
Your parents have given you a credit card!!!! Both of your parents and yourself (+ any siblings)
all have your own individual cards tied to the same account. This means that all charges collectively go
to the same account.
Define a class named CreditCardAcct that has the following instance variables:
-Card hold name
-Card #
-Purchases (made by this card holder)
Create a constructor that allows a user of the class to initialize all values.
Create the following static variables:
-Overall account limit, setting limit to $10,000.00 (can't change)
-Overall account balance
Create a Purchase method
2) Create a driver program with atleast 10 different purchases to demo the class methods.
I want the code in JAVA program. and use class methods only.
My class didn't study Array yet so please don't use array too.
Explanation / Answer
Given below is the code adn output. Please rate the answer if it helped. Thank you.
CreditCardAcct
public class CreditCardAcct {
//instance variables
private String holderName;
private String cardNum;
private double purchases;
//static variables
private static final double LIMIT = 10000; //can't change this since its final
private static double account_balance; //can change and is shared by all objects
public CreditCardAcct(String name, String cardNo, double purchaseAmt)
{
this.holderName = name;
this.cardNum = cardNo;
this.purchases = purchaseAmt;
}
public boolean purchase(double amount)
{
if(account_balance + amount <= LIMIT)
{
purchases += amount;
account_balance += amount;
return true;
}
else
{
return false;
}
}
public static double getAccountBalance()
{
return account_balance;
}
public double getPurchases()
{
return purchases;
}
public String getCardNumber()
{
return cardNum;
}
public String getHolderName()
{
return holderName;
}
public void display()
{
System.out.println("Name: " + holderName + " Card No: " + cardNum + " Purchases: " + purchases + " Account Balance: " + account_balance);
}
}
CreditCardAccDriver.java
public class CreditCardAccDriver {
public static void main(String[] args) {
CreditCardAcct dad = new CreditCardAcct("John", "1111111111", 0);
CreditCardAcct mom = new CreditCardAcct("Elizbeth", "2222222222", 0);
CreditCardAcct bro = new CreditCardAcct("Peter", "3333333333", 0);
CreditCardAcct me = new CreditCardAcct("Henry", "4444444444", 0);
//make some purchase for each of the card holders
System.out.println("Purchases by for all card holders dad(1500), mom(800), bro(500), me(1000)");
dad.purchase(1500);
mom.purchase(800);
bro.purchase(500);
me.purchase(1000);
//display the card information for all the card holders
dad.display();
mom.display();
bro.display();
me.display();
//make more purchases
dad.purchase(3000);
mom.purchase(2500);
System.out.println("After Purchase from mom(2500) and dad(3000) , balance still within limit");
dad.display();
mom.display();
bro.display();
me.display();
//now only allowed purchase of 700 for others
if(bro.purchase(1800))
System.out.println("Bro could make a purchase for 1800");
else
System.out.println("Bro could not make a purchase for 1800");
dad.display();
mom.display();
bro.display();
me.display();
}
}
output
Purchases by for all card holders dad(1500), mom(800), bro(500), me(1000)
Name: John Card No: 1111111111 Purchases: 1500.0 Account Balance: 3800.0
Name: Elizbeth Card No: 2222222222 Purchases: 800.0 Account Balance: 3800.0
Name: Peter Card No: 3333333333 Purchases: 500.0 Account Balance: 3800.0
Name: Henry Card No: 4444444444 Purchases: 1000.0 Account Balance: 3800.0
After Purchase from mom(2500) and dad(3000) , balance still within limit
Name: John Card No: 1111111111 Purchases: 4500.0 Account Balance: 9300.0
Name: Elizbeth Card No: 2222222222 Purchases: 3300.0 Account Balance: 9300.0
Name: Peter Card No: 3333333333 Purchases: 500.0 Account Balance: 9300.0
Name: Henry Card No: 4444444444 Purchases: 1000.0 Account Balance: 9300.0
Bro could not make a purchase for 1800
Name: John Card No: 1111111111 Purchases: 4500.0 Account Balance: 9300.0
Name: Elizbeth Card No: 2222222222 Purchases: 3300.0 Account Balance: 9300.0
Name: Peter Card No: 3333333333 Purchases: 500.0 Account Balance: 9300.0
Name: Henry Card No: 4444444444 Purchases: 1000.0 Account Balance: 9300.0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.