-
Notifications
You must be signed in to change notification settings - Fork 0
/
MoneyConverter.java
139 lines (109 loc) · 3.9 KB
/
MoneyConverter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
////-------------------------------------------------------------------
// Programmer: Amadou Barry
// Course: COSC 1336 Sections 011/015
// Semester: Spring 2019
// Assignment#: 2
// Due Date: February 13, 2019 @ 11:59 pm
////-------------------------------------------------------------------
package assignment02;
// import the scanner class to enable
// reading in from the keyboard
import java.util.Scanner;
public class MoneyConverter {
public static void main(String[] args) {
// declare constants to be used
final int TEN_DOLLARS = 1000;
final int FIVE_DOLLARS = 500;
final int ONE_DOLLARS = 100;
final int QUARTER = 25;
final int DIME = 10;
final int NICKEL = 5;
final int HUNDRED = 100;
// declare variables to be used
int tens, fives, ones, quarters, dimes, nickels;
int remainingCents;
double total, twiceAmount, halfAmount;
double addTwoDollars, subtractOneDollar;
char dollarSign, cents;
String prefix, firstName, lastName;
// assign variables
dollarSign = '$';
cents = 162; // this is used to assigned the cents symbol and unicode is 162
prefix = "Sir";
firstName = "Amadou";
lastName = "Barry";
// instantiate an object from the
// Scanner class to enable reading
// in from the keyboard
Scanner keyboard = new Scanner(System.in);
// prompt the user to enter a money
// amount then read it in
System.out.println("Enter the monetary amount:");
total = keyboard.nextDouble();
// calculate twice, half, adding two dollars
// and subtracting one dollar from the
// amount entered
twiceAmount = total * 2.0;
halfAmount = total / 2.0;
addTwoDollars = total + 2.0;
subtractOneDollar = total - 1.0;
// convert the amount of money read into cents
remainingCents = (int) (total * HUNDRED);
// print out current calculations
System.out.println("\nThe amount of " + dollarSign + total +
" is equalent to " +
remainingCents + cents +
" " + "(cents)");
System.out.println("Twice the amount is " +
dollarSign +
twiceAmount + "!");
System.out.println("Half the amount is " +
dollarSign +
halfAmount + "!");
System.out.println("Adding two dollars is " +
dollarSign +
addTwoDollars + "!");
System.out.println("Subtracting one dollar is " +
dollarSign +
subtractOneDollar + "!");
// calculate how many ten dollar bills there are
tens = remainingCents / TEN_DOLLARS;
//remainingCents = remainingCents % TEN_DOLLARS;
remainingCents %= TEN_DOLLARS;
// repeat procedure for five dollar bills,
// one dollar bill, quarters, dimes, nickels, and
// pennies using appropriate variables and
// constants; note: at the end, the last value of
// remaingCents will be the value for pennies
fives = remainingCents / FIVE_DOLLARS;
remainingCents %= FIVE_DOLLARS;
ones = remainingCents / ONE_DOLLARS;
remainingCents %= ONE_DOLLARS;
quarters = remainingCents / QUARTER;
remainingCents %= QUARTER;
dimes = remainingCents / DIME;
remainingCents %= DIME;
nickels = remainingCents / NICKEL;
remainingCents %= NICKEL;
// print out information accordingly
System.out.println("\nThat's equivalent to:\n");
System.out.println(tens
+ " ten dollar bills");
System.out.println(fives
+ " five dollar bills");
System.out.println(ones
+ " one dollar bill");
System.out.println(quarters
+ " quarters");
System.out.println(dimes
+ " dimes");
System.out.println(nickels
+ " nickels");
System.out.println(remainingCents
+ " pennies");
System.out.println( "\nThis program was written by " +
prefix + " " + firstName + " " +
lastName);
System.out.println("End of program.");
} //End method main
} // end class MoneyConverter