Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

By using Java, Complete the following 7 tasks; 1. Create a class to hold credit

ID: 3641664 • Letter: B

Question

By using Java,  Complete the following 7 tasks;

1. Create a class to hold credit card information.
2. The information to store is the card type (Visa, Mastercard, etc.) card number, the limit, and the current balance.
3. Write methods to set the card type, card number, limit, and balance.
4. Write a method to make a payment (amount only).
5. Write a method to add a charge (amount only).
6. Write methods to return the card type, card number, limit, and balance.
7. Do not take any input in your class. The class that uses the Credit Card class will prompt for all input.
8. Do not include a main method.

Explanation / Answer

public class CreditCard
{
private String cardType;
private long cardNumber;
private float limit;//Max amount you can spend.
private float currentBalance;//The amount you have spent. It should be called amountUsed ???

//Constructors
CreditCard(){}
CreditCard(String cardType, long cardNumber, float limit, float currentBalance)
{
this.cardType = cardType;
this.cardNumber = cardNumber;
this.limit = limit;
this.currentBalance = currentBalance;
}

//Setters
public void setCardType(String cardType){ this.cardType = cardType;}
public void setCardNumber(long cardNumber){this.cardNumber = cardNumber;}
public void setLimit(float limit){this.limit = limit;}
public void setCurrentBalance(float currentBalance){this.currentBalance = currentBalance;}

//Getters
public String setCardType(){ return this.cardType;}
public long setCardNumber(){return this.cardNumber;}
public float setLimit(){return this.limit;}
public float setCurrentBalance(){return this.currentBalance;}

//Other methods
public boolean createCharge(float price)
{
if(price <= limit - currentBalance)
{
makePayment(price);
System.out.println("Transaction Successful !");
return true;//
}else
{
System.out.println("Transaction Faailure: Insufficient funds !");
return false;
}

}

public boolean makePayment(float amount)
{
if(amount <= limit-currentBalance && amount > 0)
{
currentBalance = currentBalance + amount;
return true;//That is, transaction succeeded and a payment was made.
}else{return false;}

}

}