-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPurseClass.java
More file actions
173 lines (150 loc) · 4.86 KB
/
PurseClass.java
File metadata and controls
173 lines (150 loc) · 4.86 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import java.util.ArrayList;
public class Purse
{
private ArrayList<Coin> tokens;
//Default constructor
//Instantiate the ArrayList object tokens
public Purse()
{
tokens = new ArrayList<Coin>();
}
//adds q quarters, d dimes, n nickels, and p pennies
//to the purse.
//make sure it is in the correct order: quarters, then dimes, then nickels, then pennies
public Purse(int q, int d, int n, int p)
{
tokens = new ArrayList<Coin>();
for(int i =0 ;i<q;i++){
int value = 25;
Coin quarters = new Coin(value,"quarters");
tokens.add(quarters);
}
for(int i =0 ;i<d;i++){
int value = 10;
Coin dimes = new Coin(value,"dimes");
tokens.add(dimes);
}
for(int i =0 ;i<n;i++){
int value = 5;
Coin nickels = new Coin(value,"nickels");
tokens.add(nickels);
}
for(int i =0 ;i<p;i++){
int value = 1 ;
Coin pennies = new Coin(value,"pennies");
tokens.add(pennies);
}
}
//adds a coin to the end of the purse
public void addCoin(Coin c)
{
tokens.add(c);
}
//returns the number of quarters in the purse. You cannot assume the purse is sorted in any order.
public int numQuarters()
{
int count=0;
for( int i = 0;i<tokens.size();i++){
if(tokens.get(i).getName().equals("quarters")){
count++;
}
}
return count ;
}
//returns the number of dimes in the purse. You cannot assume the purse is sorted in any order.
public int numDimes()
{
int count=0;
for( int i = 0;i<tokens.size();i++){
if(tokens.get(i).getName().equals("dimes") || tokens.get(i).getValue() == 10){
count++;
}
}
return count ;//your code goes here
}
//returns the number of nickels in the purse. You cannot assume the purse is sorted in any order.
public int numNickels()
{
int count=0;
for( int i = 0;i<tokens.size();i++){
if(tokens.get(i).getName().equals("nickels")){
count++;
}
}
return count ;//your code goes here
}
//returns the number of pennies in the purse. You cannot assume the purse is sorted in any order.
public int numPennies()
{
int count=0;
for( int i = 0;i<tokens.size();i++){
if(tokens.get(i).getName().equals("pennies")|| tokens.get(i).getValue() == 1){
count++;
}
}
return count ;//your code goes here
}
//returns the total value of the purse in cents
public int getValue()
{
int sum = 0;
for(int i =0;i<tokens.size();i++){
sum = sum + tokens.get(i).getValue();
}
return sum;
}
//returns true if any of the coins in the purse are dimes
public boolean hasDimes()
{
for(int i = 0;i<tokens.size();i++){
if(tokens.get(i).getName().equals("dimes")|| tokens.get(i).getValue() == 10){
return true;
}
}
return false;
}
//Converts all dimes to 2 nickels, returns true if a
//conversion occurred, false if not.
public boolean convertDimes()
{
boolean lol = false;
for(int i =tokens.size()-1;i>0;i--){
if(tokens.get(i).getName().equals("dimes")){
tokens.remove(i);
Coin nickels = new Coin(5,"nickels");
tokens.add(nickels);
tokens.add(nickels);
}
}
return lol;
}
//Makes a purchase by removing all the coins necessary
//to reach exactly, or go over, the price. Return true if you’re able to make the purchase
//you should loop backwards for your code in this problem
//if you do not have enough money in the purse, no coins are removed and return false.
public boolean makePurchase(int price)
{
int balance = price;
if(balance>getValue()){
return false;
}
else{
for( int i =0 ;i<tokens.size()-1;i++){
if(tokens.get(i).getValue()<=balance){
balance = balance - tokens.get(i).getValue();
tokens.remove(i);
i--;
}
}
return true;
}
}
//the toString method will print out the number of each type of coins in the purse, and also print out the total value of the purse in cents
//for example, the a call to toString() might have this output:
//"5 quarters, 3 dimes, 2 nickels, and 8 pennies, with a total value of 173 cents."
//HINT: use some of the methods you have already coded!
public String toString()
{
return numQuarters() + " quarters " + numDimes() + " dimes " + numNickels() + " nickels " + numPennies() + " pennies, with a total value of " + getValue() + " cents ";
}
}