1. Member
Join Date
Mar 2012
Posts
12
Rep Power
0

## Credit Limit Calculator

I have a Java programming assignment that reads as follows:

Develop an application that will determine whether any of several department-store customers has exceeded the credit limit on a charge account. For each customer, the following facts are available:
a. account number
b. balance at the beginning of the month
c. total of all items charged by the customer this month
d. total of all credits applied to the customer’s account this month
e. allowed credit limit.
The program should input all these facts as integers, calculate the new balance (= beginning balance + charges — credits), display the new balance and determine whether the new balance exceeds the customer’s credit limit. For those customers whose credit limit is exceeded, the program should display the message “Credit limit exceeded”. Your class should have a constructor that initializes the instance variables. Account number is auto generated and user is not supposed to enter it. Provide a set and a get method for each instance variable.

Here is my code:

Java Code:
```package haroldandthepurplecrayon;

public class HaroldandthePurpleCrayon
{

double balance;
double limit;
double charges;
double credit;

public HaroldandthePurpleCrayon(double bal, double lim)
{
this.balance = bal;
this.limit = lim;
this.charges = 0.0;
this.credit = 0.0;
}

public void setBalance()
{
balance = balance + charges - credit;
}
public double getBalance()
{
return balance;
}

public void setLimit(double capacity)
{
limit += capacity;
}
public double getLimit()
{
return limit;
}

public void setCharges(double expenditure)
{
charges += expenditure;
}
public double getCharges()
{
return charges;
}

public void setCredit(double debenture)
{
credit += debenture;
}
public double getCredit()
{
return credit;
}

}```
Java Code:
```package haroldandthepurplecrayon;
import java.util.Random;
import java.util.Scanner;

public class TriptotheSky
{

public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);

Random rand = new Random();
int accountNumber = rand.nextInt(99999) + 10000;

double remainder, capacity, quantity, quota;

System.out.print("Enter the total number of customers: ");

int customers = scanner.nextInt();

for (int patron = 0; patron < customers; patron++)
{
System.out.println("\nAccount Details For Customer " + (patron + 1));
System.out.print("Customer " + (patron + 1) + ": Account Number " + (accountNumber) + "\n\n");
System.out.print("Enter customer's balance: ");
remainder = scanner.nextDouble();

System.out.print("Enter customer's credit limit: ");
capacity = scanner.nextDouble();

HaroldandthePurpleCrayon clientele = new HaroldandthePurpleCrayon(remainder, capacity);
System.out.print("Enter customer's total charges: ");
quantity = scanner.nextDouble();
System.out.print("Enter customer's amount of used credit: ");
quota = scanner.nextDouble();

clientele.setCharges(quantity);
clientele.setCredit(quota);
clientele.setBalance();

if ((remainder + quantity) > (capacity - quota))
{
System.out.print("\nThe credit limit for Customer " + (patron + 1) + " has been exceeded.\n\n");
}

else
{
System.out.print("\nCredit available for Customer " + (patron + 1) + " is sufficient. \n\n");
}
}

}
}```
The program runs perfectly except for one detail. It doesn't update the update the Account Number with a new random number for each customer, so each customer has the same account number. It seems like it should be such a simple fix, and I know that once I get the answer it'll be on of those "Oh, duh!" moments, but right now I'm pulling my hair out trying to tell it to assign a new random number to each customer. I've been writing this program from scratch, start-to-finish for the past seven hours, so maybe that's why I can't figure it out, 'cause my brain is exhausted, but I'd appreciate it if someone could help. Thanks.

Last edited by Nightcrawler; 03-02-2012 at 09:46 PM.

2. ## Re: Credit Limit Calculator

Moving to New to Java.

db

3. Member
Join Date
Mar 2012
Posts
12
Rep Power
0

## Re: Credit Limit Calculator

Oh sorry about that. I didn't realize I clicked on the wrong section. I figured it out anyway, I just had to move "Random rand = new Random(); int accountNumber = rand.nextInt(99999) + 10000;" to inside of the for loop.

4. Member
Join Date
Sep 2010
Posts
84
Rep Power
0

## Re: Credit Limit Calculator

what prevents 2 customers from having the same account number in your program? it looks like 2 customers could potentially still have the same account number. if one random number is 500 and 3 customers later the random number is also 500 then both customers with have same account number.

Instead of doing random account number try declaring a static variable for account number that is increment by 1 every time a new object is created. That way you guarantee that no account numbers will repeat themselves.

5. Member
Join Date
Mar 2012
Posts
12
Rep Power
0

## Re: Credit Limit Calculator

It won't repeat an account number for the first 89999 customers, because although it's called the random function, it's technically not choosing them randomly but rather by an algorithm that won't choose the same number twice until it has chosen every possible number at least once. But I suppose it isn't a foolproof program since a department store could potentially have more than 89999 customers, although I'd hate to see the output window for that many calculations. But thanks for the advice, I'll change it anyway to make sure the program works perpetually and not just for practical purposes, to make sure I don't get graded down for it. Thanks!

6. Member
Join Date
Sep 2010
Posts
84
Rep Power
0

## Re: Credit Limit Calculator

oh really, I didn't know that....lol....I'm a newbie too. That doesn't sound right to me. Can we get someone who is experienced to clarify please

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•