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.Scanner;
public class Assignment01Driver
{
Scanner input = new Scanner(System.in);
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];
int totalFlowers = 0;
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,totalFlowers);
totalFlowers++;
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[], int index)
{
System.out.print("Enter the name of the flower " + (index + 1) + "
");
flowerPack[index] = input.nextLine();
}
private void removeFlower(String flowerPack[])
{
System.out.print("What flower would you like to erase? ");
String choice = input.nextLine();
for (int i = 0; i < flowerPack.length; i++)
{
if (flowerPack[i] != null &&
flowerPack[i].equals(choice))
{
flowerPack[i] = "null";
}
}
}
private void sortFlowers(String flowerPack[])
{
String t;
for (int i = 0; i < flowerPack.length; i++)
{
for (int j = i + 1; j < flowerPack.length; j++)
{
if (flowerPack[i] != null && flowerPack[j] != null
&& flowerPack[i].compareTo(flowerPack[j]) > 0)
{
t = flowerPack[i];
flowerPack[i] = flowerPack[j];
flowerPack[j] = t;
}
}
}
System.out.println("Sort complete\nPress any key to
continue");
String choice1 = input.nextLine();
}
private void searchFlowers(String flowerPack[])
{
System.out.print("What flower would you like to search? ");
boolean flag = false;
int counter = 0;
String choice = input.nextLine();
for (int i = 0; i < flowerPack.length; i++)
{
if (flowerPack[i] != null &&
flowerPack[i].equals(choice))
{
counter++;
flag = true;
}
}
if (flag)
{
System.out.print(choice + " - " + counter + "\n");
}
if (!flag)
{
System.out.print("Flower " + choice + " not found\n");
}
System.out.print("Press any key to continue");
String choice1 = input.nextLine();
}
//method to display flowerd
private void displayFlowers(String flowerPack[])
{
//storing names to avoid replication
ArrayList replicatedData = new ArrayList();
//checking for replication
for (int i = 0; i < flowerPack.length; i++)
{
boolean found = false;
for(int k=0;k<replicatedData.size();k++)
{
if(replicatedData.get(k).equals(flowerPack[i]))
{
found = true;
}
}
if(!found)
{
replicatedData.add(flowerPack[i]);
int count = 0;
//counting how many times it repeats
for(int j=0;j<flowerPack.length;j++)
{
if(flowerPack[i].equals(flowerPack[j]))
{
count++;
}
}
//finally printing resuls
System.out.print(flowerPack[i] + "-"+count+"\n");
}
}
}
}
/*
output:
Welcome to my flower pack interface.
Please select a number from the options below
1: Add an item to the pack.
2: Remove an item from the pack.
3: Sort the contents of the pack.
4: Search for a flower.
5: Display the flowers in the pack.
0: Exit the flower pack interfact.
1
Enter the name of the flower 1 Red
1: Add an item to the pack.
2: Remove an item from the pack.
3: Sort the contents of the pack.
4: Search for a flower.
5: Display the flowers in the pack.
0: Exit the flower pack interfact.
1
Enter the name of the flower 2 Rose
1: Add an item to the pack.
2: Remove an item from the pack.
3: Sort the contents of the pack.
4: Search for a flower.
5: Display the flowers in the pack.
0: Exit the flower pack interfact.
1
Enter the name of the flower 3 Green
1: Add an item to the pack.
2: Remove an item from the pack.
3: Sort the contents of the pack.
4: Search for a flower.
5: Display the flowers in the pack.
0: Exit the flower pack interfact.
1
Enter the name of the flower 4 Green
1: Add an item to the pack.
2: Remove an item from the pack.
3: Sort the contents of the pack.
4: Search for a flower.
5: Display the flowers in the pack.
0: Exit the flower pack interfact.
1
Enter the name of the flower 5 Blue
1: Add an item to the pack.
2: Remove an item from the pack.
3: Sort the contents of the pack.
4: Search for a flower.
5: Display the flowers in the pack.
0: Exit the flower pack interfact.
1
Enter the name of the flower 6 Blue
1: Add an item to the pack.
2: Remove an item from the pack.
3: Sort the contents of the pack.
4: Search for a flower.
5: Display the flowers in the pack.
0: Exit the flower pack interfact.
3
Sort complete
Press any key to continue
4
1: Add an item to the pack.
2: Remove an item from the pack.
3: Sort the contents of the pack.
4: Search for a flower.
5: Display the flowers in the pack.
0: Exit the flower pack interfact.
4
What flower would you like to search? Green
Green - 2
Press any key to continue
1: Add an item to the pack.
2: Remove an item from the pack.
3: Sort the contents of the pack.
4: Search for a flower.
5: Display the flowers in the pack.
0: Exit the flower pack interfact.
5
Blue-2
Green-2
Red-1
Rose-1
*/