i need help making a CreditCard object (an instance of class CreditCard) represe
ID: 671078 • Letter: I
Question
i need help making a CreditCard object (an instance of class CreditCard) represents one credit card.
we'll just have one credit card.
CreditCards have various attributes, including:
variables:
account number (int)
owner's name (String)
current balance (float) -- how much we've spent
credit limit (float) -- maximum balance
operations (functions)
charge(amount)
pay bill (amount)
tell balance
.. and some housekeeping
display -- print out all info about card
constructor -- fill in a new card
Here's what your test program might look like, in casual Java:
CreditCard myGoldCard = new CreditCard (/* args here to specify #, name, credit limit */);
// test
myGoldCard.display();
myGoldCard.charge(2.75); -- buy something
myGoldCard.charge(1200); -- are we that rich?
ERROR, charge for $1200 would exceed credit limit -- printout
myGoldCard.payBill(myGoldCard(tellBalance()));
Please have at least two source files: one to hold class CreditCard (it should be named "CreditCard.java"), one to hold main() and the test code.
To make things a bit more interesting, have the CreditCard keep track of:
the largest balance
how many charges have been made since the last time the balance was completely paid off.
(These can be printed by display())
and also
if the current charge would exceed the credit limit, the "charge" function should not only reject the transaction, but also return a value of false. That way, a sales clerk would know whether the charge was accepted.
All of this will be more interesting if we can run it from the keyboard. To help you along (and save time), here's the skeleton of what's needed: fill in the details.
Note that you cannot safely compare Strings with ==. Use the equals() function instead.
{
Scanner scn = new Scanner(System.in); -- create Scanner object (just once)
System.out.print("Your name: "); -- prompt user so s/he knows what we want!
String me = scn.next(); -- next() reads next String value
System.out.print("initial balance: ");
float initial = scn.nextFloat(); -- nextFloat() reads next float value
-- there's also "nextInt()"
while (true)
{
System.out.print("Cmd: ");
String cmd = scn.next();
if ("charge".equals(cmd) || "c".equals(cmd) )
{
// find out amount of charge and process it
}
else if ("pay".equals(cmd) || "p".equals(cmd))
{
// customer is paying bill. Find out how much, and process
}
else if ("quit".equals(cmd))
{
// time to stop
System.out.print("bye");
break;
}
else
{
System.out.println("** did not recognize cmd: .......");
System.out.println(" valid commands are ______ and quit");
}
}
Explanation / Answer
package mani;
import java.util.Scanner;
public class testclass
{
public static void main(String[] args){
Scanner scn = new Scanner(System.in);
System.out.print("Your name: ");
String me = scn.next();
System.out.print("initial balance: ");
float initial = scn.nextFloat();
CreditCard myGoldCard=new CreditCard(me, initial);
while (true)
{
System.out.print("Cmd: ");
String cmd = scn.next();
if ("charge".equals(cmd) || "c".equals(cmd) )
{
System.out.println("enter amount you want to charge: ");
float f=scn.nextFloat();
myGoldCard.charge(f);
myGoldCard.display();
}
else if ("pay".equals(cmd) || "p".equals(cmd))
{
myGoldCard.paybill(myGoldCard.tellbalance());
System.out.println("Thanks you have paid your crdit!!!!");
myGoldCard.display();
}
else if ("quit".equals(cmd)||"q".equals(cmd))
{
// time to stop
System.out.print("bye");
break;
}
else
{
System.out.println("** did not recognize cmd: .......");
System.out.println(" valid commands are ______ and quit");
}
}
}
}
import java.util.Random;
public class CreditCard
{
int accNum;
String name;
float cBal;
float cLimit;
public CreditCard(String name,float cL){
Random r=new Random();
this.name=name;
this.accNum=r.nextInt(100);
this.cLimit=cL;
}
public void charge(float a){
if(cLimit-cBal>=a){
cBal=cBal+a;
}else{
System.out.println(" ERROR, charge for $"+a+" would exceed credit limit");
}
}
public void paybill(float a){
cBal=cBal-a;
}
public float tellbalance(){
return cBal;
}
public void display(){
System.out.println("Name: "+name);
System.out.println("Account Number: "+accNum);
System.out.println("Credit Limit: "+cLimit);
System.out.println("Credit Balance: "+cBal);
System.out.println("Credit Left: "+(cLimit-cBal));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.