fork download
  1.  
  2. import java.util.Scanner;
  3.  
  4. public class Assignment01Driver
  5. {
  6. public static void main(String[] args)
  7. {
  8. new Assignment01Driver();
  9. }
  10.  
  11. // This will act as our program switchboard
  12. public Assignment01Driver()
  13. {
  14. Scanner input = new Scanner(System.in);
  15. String[] flowerPack = new String[25];
  16.  
  17. System.out.println("Welcome to my flower pack interface.");
  18. System.out.println("Please select a number from the options below");
  19. System.out.println("");
  20.  
  21. while (true)
  22. {
  23. // Give the user a list of their options
  24. System.out.println("1: Add an item to the pack.");
  25. System.out.println("2: Remove an item from the pack.");
  26. System.out.println("3: Sort the contents of the pack.");
  27. System.out.println("4: Search for a flower.");
  28. System.out.println("5: Display the flowers in the pack.");
  29. System.out.println("0: Exit the flower pack interfact.");
  30.  
  31. // Get the user input
  32. int userChoice = input.nextInt();
  33.  
  34. switch (userChoice)
  35. {
  36. case 1:
  37. addFlower(flowerPack);
  38. break;
  39. case 2:
  40. removeFlower(flowerPack);
  41. break;
  42. case 3:
  43. sortFlowers(flowerPack);
  44. break;
  45. case 4:
  46. searchFlowers(flowerPack);
  47. break;
  48. case 5:
  49. displayFlowers(flowerPack);
  50. break;
  51. case 0:
  52. System.out.println("Thank you for using the flower pack interface. See you again soon!");
  53. System.exit(0);
  54. }
  55. }
  56. }
  57.  
  58. private void addFlower(String flowerPack[])
  59. {
  60. String str;
  61. int index = 0;
  62. Scanner input = new Scanner(System.in);
  63. System.out.println("What type of flower are you adding?");
  64. str = input.nextLine();
  65.  
  66. for (int i = 0; i < flowerPack.length; i++)
  67. {
  68. if (flowerPack[i] != null)
  69. {
  70. index++;
  71. if (index == flowerPack.length)
  72. {
  73. System.out.println("The pack is full");
  74. }
  75. } else
  76. {
  77. flowerPack[i] = str;
  78. System.out.println("Added: " + str + " at index " + i);
  79. break;
  80. }
  81. }
  82. }
  83.  
  84. private void removeFlower(String flowerPack[])
  85. {
  86. String flr;
  87. Scanner input = new Scanner(System.in);
  88. System.out.println("What flower do you want to remove?");
  89. flr = input.nextLine();
  90.  
  91. for (int i = 0; i < flowerPack.length - 1; i++)
  92. {
  93. if (flr.equalsIgnoreCase(flowerPack[i]))
  94. {
  95. flowerPack[i] = flowerPack[i + 1];
  96. }
  97. }
  98. }
  99.  
  100. private void sortFlowers(String flowerPack[])
  101. {
  102. for (int i = 0; i < flowerPack.length; i++)
  103. {
  104. String currentMin = flowerPack[i];
  105. int currentMinIndex = i;
  106.  
  107. for (int j = i; j < flowerPack.length; j++)
  108. {
  109. if (flowerPack[j] != null)
  110. {
  111. if (currentMin.compareToIgnoreCase(flowerPack[j]) > 0)
  112. {
  113. currentMin = flowerPack[j];
  114. currentMinIndex = j;
  115. }
  116. }
  117. }
  118. if (currentMinIndex != i) {
  119. flowerPack[currentMinIndex] = flowerPack[i];
  120. flowerPack[i] = currentMin;
  121. }
  122. }
  123. }
  124.  
  125. private void searchFlowers(String flowerPack[])
  126. {
  127. Scanner input = new Scanner(System.in);
  128. String str;
  129. System.out.println("What flower would you like to search for?");
  130. str = input.nextLine();
  131. boolean found = false;
  132.  
  133. for (int i = 0; i < flowerPack.length; i++) {
  134. if (flowerPack[i] != null && flowerPack[i].equalsIgnoreCase(str))
  135. {
  136. found = true;
  137. break;
  138. }
  139. }
  140. if (found)
  141. {
  142. System.out.println("We found your flower.");
  143. }
  144. else
  145. {
  146. System.out.println("That flower was not found.");
  147. }
  148. }
  149.  
  150. private void displayFlowers(String flowerPack[])
  151. {
  152. sortFlowers(flowerPack);
  153. int count = 1;
  154. for (int i = 0; i < flowerPack.length - 1; i++)
  155. {
  156. if (flowerPack[i] != null)
  157. {
  158. if (flowerPack[i].equalsIgnoreCase(flowerPack[i+1]))
  159. {
  160. count++;
  161. }
  162. }
  163. else
  164. {
  165. if (flowerPack[i] == null)
  166. {
  167. break;
  168. }
  169. }
  170. System.out.println(flowerPack[i] + "s - " + count);
  171. count = 1;
  172. }
  173. }
  174. }
  175.  
  176.  
  177. Input:
  178. 1 <enter>, "Lilly" <enter"
  179. 1 <enter>, "Lilly" <enter>
  180. 1 <enter>, "Rose" <enter>
  181. 5 <enter>
  182.  
  183. Output:
  184. Lillys - 2
  185. Lillys - 1
  186. Roses - 1
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:177: error: class, interface, or enum expected
Input:
^
Main.java:178: error: unclosed string literal
1 <enter>, "Lilly" <enter"
                         ^
2 errors
stdout
Standard output is empty