Advertisement
Guest User

Untitled

a guest
Dec 15th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. import static java.lang.System.*;
  2. import java.util.ArrayList;
  3. import java.io.*;
  4. import java.util.*;
  5.  
  6. public class Java3002{
  7. public static void main (String[] args){
  8. new Purse();
  9. }}
  10.  
  11.  
  12. class Purse
  13. {
  14. private List<Coin> money = new ArrayList<Coin> ();
  15. // private double total = 0;
  16.  
  17. Purse()
  18. {
  19. populate();
  20.  
  21. outputAll();
  22. countCoins();
  23. outputTotal();
  24. removePennies();
  25. countCoins();
  26. outputTotal();
  27.  
  28. }
  29.  
  30. void populate()
  31. {
  32. money = new ArrayList<Coin>();
  33.  
  34. try{
  35. Scanner fileScan = new Scanner(new File("Java3002.dat"));
  36. while(fileScan.hasNext())
  37. {
  38. Double cost = Double.valueOf( fileScan.next() );
  39. String cn = fileScan.next();
  40. money.add( new Coin( cost, cn) );
  41. }
  42.  
  43. }catch(Exception e){}
  44. }
  45.  
  46. void outputAll()
  47. {
  48. out.println();
  49. out.println( money );
  50. out.println();
  51. }
  52.  
  53. void removePennies()
  54. {
  55. for(int x = money.size()-1; x > -1; x--)
  56. {
  57. if(money.get(x).getName().equals("penny"))
  58. {
  59. money.remove(x);
  60. }
  61.  
  62. }
  63. out.println("NOW REMOVING PENNIES!\n");
  64. }
  65.  
  66. void outputTotal()
  67. {
  68. Double total = 0.0;
  69.  
  70. for(Coin t: money)
  71. {
  72. if(t.getName().equals("penny"))
  73. {
  74. total = total + 0.01;
  75. }
  76. else
  77. if(t.getName().equals("dime"))
  78. {
  79. total = total + 0.10;
  80. }
  81. else
  82. if(t.getName().equals("nickel"))
  83. {
  84. total = total + 0.05;
  85. }
  86. else
  87. total = total + 0.25;
  88. } // use a for each loop to total the coins
  89.  
  90.  
  91. out.println("The total of your coins: " + String.format("$%.2f", total) );
  92. out.println();
  93. }
  94.  
  95. public void countCoins()
  96. {
  97. int pennies = 0;
  98. int dimes = 0;
  99. int nickels = 0;
  100. int quarters = 0;
  101.  
  102. for(Coin c: money)
  103. {
  104. if( c.getName().equals("penny") )
  105. {
  106. pennies++;
  107. }
  108. else
  109. if( c.getName().equals("dime") )
  110. {
  111. dimes++;
  112. }
  113. else
  114. if( c.getName().equals("nickel") )
  115. {
  116. nickels++;
  117. }
  118. else
  119. {
  120. quarters++;
  121. }
  122. } // use a for each loop to count the coins
  123.  
  124. out.println(pennies + " pennies");
  125. out.println(nickels + " nickels");
  126. out.println(dimes + " dimes");
  127. out.println(quarters + " quarters");
  128. }
  129.  
  130. }
  131.  
  132.  
  133. class Coin
  134. {
  135. private double value;
  136. private String name;
  137.  
  138. public Coin( double v, String n)
  139. {
  140. value = v;
  141. name = n;
  142. }
  143.  
  144. public Double getValue()
  145. {
  146. return value;
  147. }
  148.  
  149. public String getName()
  150. {
  151. return name;
  152. }
  153.  
  154. public String toString()
  155. {
  156. return "<" + value + "," + name + ">";
  157. }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement