Advertisement
Crenox

Toy & ToyStore Java Program

Jan 21st, 2015
811
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.54 KB | None | 0 0
  1. //Name: Sammy Samkough
  2. //Prog: Toy & ToyStore
  3. //Spec: Completing methods for a Toy & ToyStore class that consists of an ArrayList of Toy objects.
  4. // The Toy class stores a Toy name and a count of how many exist in the store.
  5.  
  6. public class Toy
  7. {
  8. private String name;
  9. private int count; // how many toys exist
  10.  
  11. /** creates a generic toy named 'toy' with a count of 1 */
  12. public Toy()
  13. {
  14. name = "toy";
  15. count = 1;
  16. }
  17.  
  18. /** assigns the name given with a count of 1*/
  19. public Toy(String nm)
  20. {
  21. name = nm;
  22. count = 1;
  23. }
  24.  
  25. public int getCount()
  26. {
  27. return count;
  28. }
  29.  
  30. public void setCount(int cnt)
  31. {
  32. count = cnt;
  33. }
  34.  
  35. public String getName()
  36. {
  37. return name;
  38. }
  39.  
  40. public void setName(String nm)
  41. {
  42. name = nm;
  43. }
  44.  
  45. public String toString()
  46. {
  47. return name + " " + count;
  48. // return "Name: " + name + "\n" + "Count: " + String.valueOf(count) + "\n";
  49. }
  50. }
  51. -------------------------------------------------------------------------------------------------------------------------------
  52. //Name: Sammy Samkough
  53. //Prog: Toy & ToyStore
  54. //Spec: Completing methods for a Toy & ToyStore class that consists of an ArrayList of Toy objects.
  55. // The Toy class stores a Toy name and a count of how many exist in the store.
  56.  
  57. public class ToyRunner
  58. {
  59. public static void main(String[] args)
  60. {
  61. Toy t = new Toy("sorry");
  62. System.out.println(t);
  63.  
  64. t = new Toy("ji goe");
  65. t.setCount(5);
  66. System.out.println(t);
  67. }
  68. }
  69. /*
  70. sorry 1
  71. ji goe 5
  72. Press any key to continue . . .
  73. */
  74.  
  75. -------------------------------------------------------------------------------------------------------------------------------
  76. //Name: Sammy Samkough
  77. //Prog: Toy & ToyStore
  78. //Spec: Completing methods for a Toy & ToyStore class that consists of an ArrayList of Toy objects.
  79. // The Toy class stores a Toy name and a count of how many exist in the store.
  80.  
  81. import java.util.Scanner;
  82. import java.util.List;
  83. import java.util.ArrayList;
  84. import java.util.Collections;
  85.  
  86. public class ToyStore
  87. {
  88. private ArrayList<Toy> toyList;
  89.  
  90. public ToyStore()
  91. {
  92. toyList = new ArrayList<Toy>();
  93. }
  94.  
  95. /** Load toys takes a String such as "Clue Clue Clue Sorry Sorry"
  96. * It will parse this String into an array of Strings and then either:
  97. * a) Add that toy if it does not yet exist in the toyList or
  98. * b) Increment the count for that toy by one
  99. */
  100. public void loadToys(String toys)
  101. {
  102. for (String name : toys.split(" "))
  103. {
  104. Toy t = getThatToy(name);
  105. if(t == null)
  106. {
  107. toyList.add(new Toy(name));
  108. }
  109. else
  110. {
  111. t.setCount(t.getCount() + 1);
  112. }
  113. }
  114. }
  115.  
  116. /** Searches toyList for a toy with the same name as the parameter nm
  117. * If found - returns that toy, otherwise returns null
  118. */
  119. public Toy getThatToy(String nm)
  120. {
  121. int size = toyList.size();
  122.  
  123. for(int i = 0; i < size; i++)
  124. {
  125. if(toyList.get(i).getName().equals(nm))
  126. {
  127. return toyList.get(i);
  128. }
  129. }
  130.  
  131. return null;
  132. }
  133.  
  134. /** Returns the name of the toy that occurs the most frequently in toyList */
  135. public String getMostFrequentToy()
  136. {
  137. int max = toyList.get(0).getCount();
  138. String name = toyList.get(0).getName();
  139. int size = toyList.size();
  140.  
  141. for(int i = 0; i < size; i++)
  142. {
  143. if(toyList.get(i).getCount() > max)
  144. {
  145. max = toyList.get(i).getCount();
  146. name = toyList.get(i).getName();
  147. }
  148. }
  149.  
  150. return name;
  151. }
  152.  
  153. public void sortToysByCount()
  154. {
  155. ArrayList<Toy> t = new ArrayList<Toy>();
  156. int count = 0;
  157. int size = toyList.size();
  158.  
  159. for(int i = size; i > 0; i--)
  160. {
  161. t.add(new Toy(getMostFrequentToy()));
  162. t.get(count).setCount(getThatToy(getMostFrequentToy()).getCount());
  163. toyList.remove(getThatToy(getMostFrequentToy()));
  164. count++;
  165. }
  166.  
  167. toyList = t;
  168. }
  169.  
  170. public String toString()
  171. {
  172. return "" + toyList;
  173. }
  174. }
  175. -------------------------------------------------------------------------------------------------------------------------------
  176. //Name: Sammy Samkough
  177. //Prog: Toy & ToyStore
  178. //Spec: Completing methods for a Toy & ToyStore class that consists of an ArrayList of Toy objects.
  179. // The Toy class stores a Toy name and a count of how many exist in the store.
  180.  
  181. import java.util.Scanner;
  182. import java.util.ArrayList;
  183. import java.util.Collections;
  184.  
  185. public class ToyStoreRunner
  186. {
  187. public static void main(String args[])
  188. {
  189. ToyStore s = new ToyStore();
  190.  
  191. // first load
  192. s.loadToys("sorry bat sorry sorry sorry train train teddy teddy ball ball"); // loads the toys
  193. System.out.println(s); // prints out load
  194. System.out.println("\nMost Frequent Toy: " + s.getMostFrequentToy()); // prints out most frequent toy
  195. System.out.println("\nSorted: "); // prints out the "Sorted: "
  196. s.sortToysByCount(); // sorts the toys
  197. System.out.println(s); // prints out the sorted toys
  198. System.out.println("\n--------------------------------------------------------------------------------");
  199.  
  200. // second load
  201. s.loadToys("yahtzee monopoly candyland monopoly yahtzee candyland");
  202. System.out.println(s);
  203. System.out.println("\nMost Frequent Toy: " + s.getMostFrequentToy());
  204. System.out.println("\nSorted: ");
  205. s.sortToysByCount();
  206. System.out.println(s);
  207. System.out.println("\n--------------------------------------------------------------------------------");
  208.  
  209. // third load
  210. s.loadToys("operation operation boggle scrabble clue life clue clue");
  211. System.out.println(s);
  212. System.out.println("\nMost Frequent Toy: " + s.getMostFrequentToy());
  213. System.out.println("\nSorted: ");
  214. s.sortToysByCount();
  215. System.out.println(s);
  216. System.out.println("\n--------------------------------------------------------------------------------");
  217. }
  218. }
  219. /*
  220. [sorry 4, bat 1, train 2, teddy 2, ball 2]
  221.  
  222. Most Frequent Toy: sorry
  223.  
  224. Sorted:
  225. [sorry 4, train 2, teddy 2, ball 2, bat 1]
  226.  
  227. --------------------------------------------------------------------------------
  228.  
  229. [sorry 4, train 2, teddy 2, ball 2, bat 1, yahtzee 2, monopoly 2, candyland 2]
  230.  
  231. Most Frequent Toy: sorry
  232.  
  233. Sorted:
  234. [sorry 4, train 2, teddy 2, ball 2, yahtzee 2, monopoly 2, candyland 2, bat 1]
  235.  
  236. --------------------------------------------------------------------------------
  237.  
  238. [sorry 4, train 2, teddy 2, ball 2, yahtzee 2, monopoly 2, candyland 2, bat 1, o
  239. peration 2, boggle 1, scrabble 1, clue 3, life 1]
  240.  
  241. Most Frequent Toy: sorry
  242.  
  243. Sorted:
  244. [sorry 4, clue 3, train 2, teddy 2, ball 2, yahtzee 2, monopoly 2, candyland 2,
  245. operation 2, bat 1, boggle 1, scrabble 1, life 1]
  246.  
  247. --------------------------------------------------------------------------------
  248.  
  249. Press any key to continue . . .
  250. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement