Skip to content

Instantly share code, notes, and snippets.

@lbattaglioli2000
Created September 24, 2019 16:45
Show Gist options
  • Save lbattaglioli2000/0e265f6830fd41e65ed35f15657a76ba to your computer and use it in GitHub Desktop.
Save lbattaglioli2000/0e265f6830fd41e65ed35f15657a76ba to your computer and use it in GitHub Desktop.
import java.util.Scanner;
/**
* Chargecard Java Applet
*
* @version 1.0
* @author Luigi Battaglioli
*
*/
class Customers {
public static void main(String[] args){
Scanner s = new Scanner(System.in);
/**
* Get the:
*
* - account number (whole number)
* - beginning balance for the month (what customer owed prior to this month)
* - total of all new charges for the month
* - total of all credits (payments) for the month
* - credit limit
*
* It will then calculate and print the ending balance of what they owe you as well if they are
* at, over, or below their credit limit.
*/
int accountNumber;
double beginningBalance, charges, credits, endingBalance, creditLimit;
System.out.println("\nEnter your account number: ");
accountNumber = s.nextInt();
System.out.println("Enter your beginning balance: ");
beginningBalance = s.nextDouble();
System.out.println("Enter the total charges the month: ");
charges = s.nextDouble();
System.out.println("Enter the total credits or payments for the month: ");
credits = s.nextDouble();
System.out.println("What is your credit limit? ");
creditLimit = s.nextDouble();
System.out.printf("\nAccount Number: %d\n", accountNumber);
endingBalance = (beginningBalance + charges) - credits;
System.out.printf("Ending Balance: $%.2f\n", endingBalance);
System.out.printf("Credit Limit: $%.2f\n", creditLimit);
if(endingBalance > creditLimit){
System.out.println("Credit Limit Exceeded!\n\n");
} else if(endingBalance == creditLimit){
System.out.println("Warning: You are at your Credit Limit!\n\n");
} else {
System.out.printf("You are under your credit limit. Your available credit limit is $%.2f\n\n", (creditLimit - endingBalance));
}
}
}
@lbattaglioli2000
Copy link
Author

The if and theelse are lines 51 to 57.

if(endingBalance > creditLimit){
    System.out.println("Credit Limit Exceeded!\n\n");
} else if(endingBalance == creditLimit) {
    System.out.println("Warning: You are at your Credit Limit!\n\n");
} else {
    System.out.printf("You are under your credit limit. Your available credit limit is $%.2f\n\n", (creditLimit - endingBalance));
}

So, what happens is we first check to see if the value stored inside of the variable endingBalance is greater than the creditLimit. If it is, that means the user went over their credit limit, and the code within the first set of braces ({}) will get executed.

If the endingBalance is equal to the creditLimit however, we want to inform the user that they have reached their credit limit. So, everything inside of the {} braces after the else if() condition will get executed.

Lastly, if neither of those first two conditions evaluate to true (in other words if the user is not above or at their credit limit, but below it rather), Java will execute the block of code within the {} after the else keyword.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment