Advertisement
Crenox

Another Toy & ToyStore Java Program

Feb 3rd, 2015
811
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.57 KB | None | 0 0
  1. //Name: Peter Aydin
  2. //Prog: Toy.java
  3. //Spec: Simulates a toy and potential accessor and mutator methods for a "toy"
  4.  
  5.  
  6. public class Toy
  7. {
  8. private String name;
  9. private int count;
  10.  
  11. /** creates a generic toy named 'toy' with a count of 1 */
  12. public Toy()
  13. {
  14. name = "";
  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. }
  49. }
  50. -------------------------------------------------------------------------------------------------------------------------------
  51. //Name: Peter Aydin
  52. //Prog: ToyStore.java
  53. //Spec: Simulates an ArrayList which represents a Ty store
  54.  
  55. import java.util.Scanner;
  56. import java.util.List;
  57. import java.util.ArrayList;
  58. import java.util.Collections;
  59.  
  60. public class ToyStore
  61. {
  62. private ArrayList<Toy> toyList;
  63.  
  64. public ToyStore()
  65. {
  66. toyList = new ArrayList<Toy>();
  67. }
  68.  
  69. /** Load toys takes a String such as "Clue Clue Clue Sorry Sorry"
  70. * It will parse this String into an array of Strings and then either:
  71. * a) Add that toy if it does not yet exist in the toyList or
  72. * b) Increment the count for that toy by one
  73. */
  74. public void loadToys( String toys )
  75. {
  76. for(String names: toys.split(" "))
  77. {
  78. Toy t = getThatToy(names);
  79. if(t==null) //uses the getThatToy method to see if the toy exists, if not then a new toy is made for it
  80. {
  81. toyList.add(new Toy(names));
  82. }
  83. else //if we find that the toy already existed in the ArrayList, we add 1 to the count and avoid duplication
  84. {
  85. t.setCount(t.getCount() + 1);
  86. }
  87. }
  88. }
  89.  
  90. /** Searches toyList for a toy with the same name as the parameter nm
  91. * If found - returns that toy, otherwise returns null
  92. */
  93. public Toy getThatToy( String nm )
  94. {
  95. for(int i = 0; i<toyList.size(); i++) //I run through a for loop to see if the arrayList has a toy that has the same name as "nm"
  96. {
  97. if(toyList.get(i).getName().equals(nm))
  98. return toyList.get(i);
  99. }
  100. return null; //if there are no matches, null is returned instead of an actual Toy
  101. }
  102.  
  103. /** Returns the name of the toy that occurs the most frequently in toyList */
  104. public String getMostFrequentToy()
  105. {
  106. int max = toyList.get(0).getCount(); //by default makes the first index the maximum
  107. String name = toyList.get(0).getName(); //gets the name of the first index
  108. for(int i = 0; i<toyList.size();i++)
  109. {
  110. if(toyList.get(i).getCount()>max) //checks to see if the current index ("i") is larger than the current max
  111. {
  112. max = toyList.get(i).getCount(); //gives a new value to the max variable
  113. name = toyList.get(i).getName(); //changes the name of the maximum value
  114. }
  115. }
  116. return name;
  117. }
  118. /* My approach***
  119. **I've decided to create a temporary ArrayList called temp
  120. **I did this because I wanted to use the getMostFrequent method
  121. **to constantly get the most common toy and add it to the temp arraylist.
  122. **Then I got rid of the Toy I just recently added to the new list
  123. **After the loop ends the toyList arrayList should be empty I set toyList
  124. **I don't want to so I set toyList equal to "temp" to make sure that toyList doesn't
  125. **stay empty.
  126. */
  127. public void sortToysByCount()
  128. {
  129. int count = 0; //keeps the current index of the new arrayList for temp
  130. ArrayList<Toy> temp = new ArrayList<Toy>();
  131. for(int i = toyList.size(); i>0; i--)
  132. {
  133. temp.add(new Toy(getMostFrequentToy()));
  134. temp.get(count).setCount(getThatToy(getMostFrequentToy()).getCount());
  135. toyList.remove(getThatToy(getMostFrequentToy()));
  136. count++; //increments the variable to make sure that we don't get an out of bounds error with the temp arrayList
  137. }
  138. toyList = temp; //makes sure that toyList(which is used everywhere else in the class) doesn't actually stay cleared
  139. }
  140.  
  141. public String toString()
  142. {
  143. return "" + toyList;
  144. }
  145. }
  146. -------------------------------------------------------------------------------------------------------------------------------
  147. //Name: Peter Aydin
  148. //Prog: ToyStoreRunner.java
  149. //Spec: Tests the ToyStore class
  150.  
  151. import java.util.Scanner;
  152. import java.util.ArrayList;
  153. import java.util.Collections;
  154.  
  155. public class ToyStoreRunner
  156. {
  157. public static void main( String args[] )
  158. {
  159. ToyStore store = new ToyStore();
  160. store.loadToys("sorry bat sorry sorry sorry train train teddy teddy ball ball");
  161. System.out.println(store);
  162. System.out.println("\nMost Frequent: " + store.getMostFrequentToy());
  163. System.out.println("\nAfter Sorting....\n");
  164. store.sortToysByCount();
  165. System.out.println(store);
  166. System.out.println("\n------------------------After adding more toys----------------------------------");
  167. store.loadToys("yahtzee monopoly candyland monopoly yahtzee candyland");
  168. System.out.println(store);
  169. System.out.println("\nMost Frequent: " + store.getMostFrequentToy());
  170. System.out.println("\nAfter Sorting....\n");
  171. store.sortToysByCount();
  172. System.out.println(store);
  173. System.out.println("\n--------------------------After adding more toys--------------------------------");
  174. store.loadToys("operation operation boggle scrabble clue life clue clue");
  175. System.out.println(store);
  176. System.out.println("\nMost Frequent: " + store.getMostFrequentToy());
  177. System.out.println("\nAfter Sorting....\n");
  178. store.sortToysByCount();
  179. System.out.println(store);
  180. System.out.println("--------------------------------------------------------------------------------");
  181. }
  182. }
  183.  
  184. /*
  185. [sorry 4, bat 1, train 2, teddy 2, ball 2]
  186.  
  187. Most Frequent: sorry
  188.  
  189. After Sorting....
  190.  
  191. [sorry 4, train 2, teddy 2, ball 2, bat 1]
  192.  
  193. ------------------------After adding more toys----------------------------------
  194.  
  195. [sorry 4, train 2, teddy 2, ball 2, bat 1, yahtzee 2, monopoly 2, candyland 2]
  196.  
  197. Most Frequent: sorry
  198.  
  199. After Sorting....
  200.  
  201. [sorry 4, train 2, teddy 2, ball 2, yahtzee 2, monopoly 2, candyland 2, bat 1]
  202.  
  203. --------------------------After adding more toys--------------------------------
  204.  
  205. [sorry 4, train 2, teddy 2, ball 2, yahtzee 2, monopoly 2, candyland 2, bat 1, o
  206. peration 2, boggle 1, scrabble 1, clue 3, life 1]
  207.  
  208. Most Frequent: sorry
  209.  
  210. After Sorting....
  211.  
  212. [sorry 4, clue 3, train 2, teddy 2, ball 2, yahtzee 2, monopoly 2, candyland 2,
  213. operation 2, bat 1, boggle 1, scrabble 1, life 1]
  214. --------------------------------------------------------------------------------
  215.  
  216. Press any key to continue . . .
  217. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement