In: Computer Science
As I was on a hike the other day I came across a small child in the woods. He told me his life story, with special mention of his disabled sister who loves flowers, and asked me for a favor. He wanted a way to organize the flowers that he picks for her each day and perform a few basic tasks with them, along with a few restrictions. It is our goal to help him out! • He can only carry 25 flowers as adding any more causes many of them to become crushed. • All flowers should be able to be displayed • He should be able to add and remove flowers by using their name. • He needs to be able to search for a specific type of flower in his pack in case his sister has a special request. • He needs to be able to sort flowers by their names alphabetically in ascending order (A-Z) • He needs to show how many of each flower he has in his pack. Now, I have started a simple program which will serve as guidance for you, please help me finish it. (Please don’t modify the code that I give you, just add your code where required) Submit 1 file: Assignment1.java import java.util.Scanner; public class Assignment01Driver { public static void main(String[] args){ new Assignment01Driver (); } // This will act as our program switchboard public Assignment01Driver (){ Scanner input = new Scanner(System.in); String[] flowerPack = new String[25]; System.out.println("Welcome to my flower pack interface."); System.out.println("Please select a number from the options below"); System.out.println(""); while(true){ // Give the user a list of their options System.out.println("1: Add an item to the pack."); System.out.println("2: Remove an item from the pack."); System.out.println("3: Sort the contents of the pack."); System.out.println("4: Search for a flower."); System.out.println("5: Display the flowers in the pack."); System.out.println("0: Exit the flower pack interfact."); // Get the user input int userChoice = input.nextInt(); switch(userChoice){ case 1: addFlower(flowerPack); break; case 2: removeFlower(flowerPack); break; case 3: sortFlowers(flowerPack); break; case 4: searchFlowers(flowerPack); break; case 5: displayFlowers(flowerPack); break; case 0: System.out.println("Thank you for using the flower pack interface. See you again soon!"); System.exit(0); } } } private void addFlower(String flowerPack[]) { // TODO: Add a flower that is specified by the user } private void removeFlower(String flowerPack[]) { // TODO: Remove a flower that is specified by the user } private void sortFlowers(String flowerPack[]) { // TODO: Sort the flowers in the pack (No need to display them here) - Use Selection or Insertion sorts // NOTE: Special care is needed when dealing with strings! research the compareTo() method with strings } private void searchFlowers(String flowerPack[]) { // TODO: Search for a user specified flower } private void displayFlowers(String flowerPack[]) { // TODO: Display only the unique flowers along with a count of any duplicates /* * For example it should say * Roses - 7 * Daffodils - 3 * Violets - 5 */ } }
Assignment01Driver.java
==================================================
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class Assignment01Driver {
static int flowerscount = 0;
Scanner input = new Scanner(System.in);
public Assignment01Driver() {
String[] flowerPack = new
String[25];
System.out.println("Welcome to my
flower pack interface.");
System.out.println("Please select a
number from the options below");
System.out.println("");
while (true) {
// Give the user
a list of their options
System.out.println("1: Add an item to the pack.");
System.out.println("2: Remove an item from the pack.");
System.out.println("3: Sort the contents of the pack.");
System.out.println("4: Search for a flower.");
System.out.println("5: Display the flowers in the pack.");
System.out.println("0: Exit the flower pack interfact.");
// Get the user
input
int userChoice =
input.nextInt();
switch
(userChoice) {
case 1:
addFlower(flowerPack);
break;
case 2:
removeFlower(flowerPack);
break;
case 3:
sortFlowers(flowerPack);
break;
case 4:
searchFlowers(flowerPack);
break;
case 5:
displayFlowers(flowerPack);
break;
case 0:
System.out.println("Thank you for using the
flower pack interface." + " See you again soon!");
System.exit(0);
}
}
}
private void addFlower(String flowerPack[]) {
// TODO: Add a flower that is
specified by the user
if (flowerscount >= 25)
System.out.println("You have already Added 25 flowers, you cant add
more");
else {
System.out.println("Enter the flower name to add\n");
input.nextLine();
String flower =
input.nextLine();
flowerPack[flowerscount++] = flower;
}
}
private void removeFlower(String flowerPack[]) { //
TODO: Remove a flower
// that is specified by
// the user
System.out.println("Enter the
flower name to remove\n");
input.nextLine();
String flower =
input.nextLine();
List<String> list = new
ArrayList<String>();
Collections.addAll(list,
flowerPack);
if (list.remove(flower)) {
System.out.println("Flower with name " + flower + "
Removed");
--flowerscount;
}
flowerPack =
list.toArray(flowerPack);
}
private void sortFlowers(String flowerPack[])
{
// TODO: Sort the flowers in the
pack (No need to display them here) -
// Use Selection or Insertion sorts
// NOTE: Special care is needed when
// dealing with strings! research
the compareTo() method with strings
String key = "";
int i = 0;
//
System.out.println(Arrays.toString(flowerPack));
for (int j = 1; j <
flowerscount; j++) { // the condition has changed
key =
flowerPack[j];
i = j - 1;
while (i >=
0) {
if (key.compareTo(flowerPack[i]) > 0) {//
here too
break;
}
flowerPack[i + 1] = flowerPack[i];
i--;
}
flowerPack[i +
1] = key;
//
System.out.println(Arrays.toString(flowerPack));
}
System.out.println("Flowers are
Sorted: ");
for (int k = 0; k <
flowerscount; ++k)
System.out.print(flowerPack[k] + " ");
System.out.println();
}
private void searchFlowers(String flowerPack[])
{
// TODO: Search for a user
specified flower
System.out.println("Enter the
flower name to search\n");
input.nextLine();
String flower =
input.nextLine();
boolean flag = false;
for (int i = 0; i <
flowerPack.length; ++i) {
if
(flower.equals(flowerPack[i])) {
System.out.println("Flower with name " + flower
+ " Found");
flag = true;
break;
}
}
if (!flag)
System.out.println("Not found!!");
}
private void displayFlowers(String flowerPack[])
{
// TODO: Display only the unique
flowers along with a count of any
// duplicates
/*
* * For example it should say *
Roses - 7 * Daffodils - 3 * Violets - 5
*/
HashMap<String, Integer>
map = new HashMap<String, Integer>();
System.out.println("Flowers are as
follows: ");
for (int i = 0; i <
flowerPack.length; ++i) {
if
(flowerPack[i] != null) {
if (map.get(flowerPack[i]) == null) {
map.put(flowerPack[i],
1);
} else {
Integer val =
map.get(flowerPack[i]);
map.put(flowerPack[i], val +
1);
}
}
}
Iterator it =
map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair =
(Map.Entry) it.next();
//
System.out.println(pair.getKey() + " = " + pair.getValue());
if ((int)
pair.getValue() == 1)
System.out.print(pair.getKey() + " ");
else
System.out.print(pair.getKey() + "-" +
pair.getValue() + " ");
it.remove();
}
System.out.println();
}
public static void main(String[] args) {
new Assignment01Driver();
}
}
====================================================================================
Adding Output Image , Let me know if there is any concern.
Thanks, Let me know if there
is any concern.