In: Computer Science
Can you please explain in detail what each line of code stands for in the Main method
import java.util.Scanner;
public class CashRegister {
private static Scanner scanner = new Scanner(System.in);
private static int dollarBills[] = {1, 2, 5, 10, 20, 50,
100};
private static int cents[] = {25, 10, 5, 1};
public static void main(String[] args) {
double totalAmount = 0;
int count = 0;
for (int i = 0; i < dollarBills.length; i++) {
count = getBillCount("How many $" + dollarBills[i] + " bills do you
have");
totalAmount += count * dollarBills[i];
}
for (int i = 0; i < cents.length; i++) {
count = getBillCount("How many " + cents[i] + " cents do you
have");
totalAmount += count * cents[i]/100.0;
}
System.out.printf("You have total $%.2f in your
register\n",totalAmount);
if(totalAmount>150){
System.out.printf("Deposit Amount
$%.2f\n",(totalAmount-150));
}else if(totalAmount==150){
System.out.println("You have exactly $150. You dont have any
deposit.");
}else {
System.out.println("You have less than $150 in your
register.");
}
}
private static int getBillCount(String message) {
int count = 0;
while (true) {
System.out.print(message + ": ");
try {
count = scanner.nextInt();
} catch (Exception exception) {
System.out.println("Invalid number passed.");
}
if (count < 0) {
System.out.println("Invalid number passed.");
} else {
return count;
}
}
}
}
import java.util.Scanner;
public class CashRegister {
private static Scanner scanner = new
Scanner(System.in);
//here we are creating an array named dollarBills
which contain all type of dollars
private static int dollarBills[] = {1, 2, 5, 10, 20,
50, 100};
//here we are creating an array named cents which
contain all type of cents
private static int cents[] = {25, 10, 5, 1};
public static void main(String[] args)
{
//intially assign the total amount
to 0
double totalAmount = 0;
//the count will also be 0
int count = 0;
//iterating over the array
dollarBills to find the number of dollars of each type
for (int i = 0; i <
dollarBills.length; i++)
{
//this count
variable is to find the total number of dollars of each type
suppose if we have 10 coins of 1$ the count will be 10
count =
getBillCount("How many $" + dollarBills[i] + " bills do you
have");
//we find the
total amount based on the count and the value of the coin suppose
if we have 10 coins of 1 dollar it will be (1*10) and we are adding
it to fint the total amount
totalAmount +=
count * dollarBills[i];
}
//now int the same way we are
iterating through cents array which contains all types of
cents
for (int i = 0; i <
cents.length; i++)
{
//it is same as
of count in dollar bills but it is for cents
count =
getBillCount("How many " + cents[i] + " cents do you have");
//it is also
same as total amount in dollar bills since they are cents we are
dividing with 100
totalAmount +=
count * cents[i]/100.0;
}
//printing the total amount that
you have
System.out.printf("You have total
$%.2f in your register\n",totalAmount);
if(totalAmount>150)
{
//if the amount
is greater than 150 we are depositing amount-150 dollars
System.out.printf("Deposit Amount
$%.2f\n",(totalAmount-150));
}
else if(totalAmount==150)
{
//if the amount
is equal to 150 there is no money to deposit
System.out.println("You have exactly $150. You dont have any
deposit.");
}
else
{
//it doesn't
have the minimum money in the register
System.out.println("You have less than $150 in your
register.");
}
}
private static int getBillCount(String message)
{
int count = 0;
while (true)
{
System.out.print(message + ": ");
try
{
count = scanner.nextInt();
}
catch (Exception
exception)
{
System.out.println("Invalid number
passed.");
}
if (count
< 0)
{
System.out.println("Invalid number
passed.");
}
else
{
return count;
}
}
}
}
If you have any doubts please comment and please don't dislike.