Results 1 to 6 of 6
Thread: Credit Limit Calculator
- 03-02-2012, 10:36 PM #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"); } } } }
Last edited by Nightcrawler; 03-02-2012 at 10:46 PM.
- 03-02-2012, 10:40 PM #2
Re: Credit Limit Calculator
Homework isn't 'Advanced Java'
Moving to New to Java.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 03-02-2012, 10:55 PM #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.
- 03-05-2012, 03:11 AM #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.
- 03-05-2012, 03:22 AM #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!
- 03-05-2012, 03:49 AM #6
Member
- Join Date
- Sep 2010
- Posts
- 84
- Rep Power
- 0
Similar Threads
-
Seaching a text file for credit card numbers
By javamann in forum New To JavaReplies: 9Last Post: 11-21-2011, 01:44 AM -
Masking a credit card value
By Samurai Coder in forum New To JavaReplies: 2Last Post: 12-02-2009, 10:05 PM -
Java Project for extra Credit
By Ankiel24 in forum New To JavaReplies: 5Last Post: 04-24-2009, 10:56 AM -
Credit Card Validator
By bluegti02 in forum New To JavaReplies: 2Last Post: 06-17-2008, 07:09 AM -
[SOLVED] File I/O Extra Credit Assignment FTW!
By Bascotie in forum New To JavaReplies: 9Last Post: 06-10-2008, 09:20 AM
Bookmarks