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);
System.
out.
println("Welcome to my flower pack interface."); System.
out.
println("Please select a number from the options below");
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!"); }
}
}
private void addFlower
(String flowerPack
[]) {
int index = 0;
Scanner input
= new Scanner
(System.
in); System.
out.
println("What type of flower are you adding?"); str = input.nextLine();
for (int i = 0; i < flowerPack.length; i++)
{
if (flowerPack[i] != null)
{
index++;
if (index == flowerPack.length)
{
System.
out.
println("The pack is full"); }
} else
{
flowerPack[i] = str;
System.
out.
println("Added: " + str
+ " at index " + i
); break;
}
}
}
private void removeFlower
(String flowerPack
[]) {
Scanner input
= new Scanner
(System.
in); System.
out.
println("What flower do you want to remove?"); flr = input.nextLine();
for (int i = 0; i < flowerPack.length - 1; i++)
{
if (flr.equalsIgnoreCase(flowerPack[i]))
{
flowerPack[i] = flowerPack[i + 1];
}
}
}
private void sortFlowers
(String flowerPack
[]) {
for (int i = 0; i < flowerPack.length; i++)
{
String currentMin
= flowerPack
[i
]; int currentMinIndex = i;
for (int j = i; j < flowerPack.length; j++)
{
if (flowerPack[j] != null)
{
if (currentMin.compareToIgnoreCase(flowerPack[j]) > 0)
{
currentMin = flowerPack[j];
currentMinIndex = j;
}
}
}
if (currentMinIndex != i) {
flowerPack[currentMinIndex] = flowerPack[i];
flowerPack[i] = currentMin;
}
}
}
private void searchFlowers
(String flowerPack
[]) {
Scanner input
= new Scanner
(System.
in); System.
out.
println("What flower would you like to search for?"); str = input.nextLine();
boolean found = false;
for (int i = 0; i < flowerPack.length; i++) {
if (flowerPack[i] != null && flowerPack[i].equalsIgnoreCase(str))
{
found = true;
break;
}
}
if (found)
{
System.
out.
println("We found your flower."); }
else
{
System.
out.
println("That flower was not found."); }
}
private void displayFlowers
(String flowerPack
[]) {
sortFlowers(flowerPack);
int count = 1;
for (int i = 0; i < flowerPack.length - 1; i++)
{
if (flowerPack[i] != null)
{
if (flowerPack[i].equalsIgnoreCase(flowerPack[i+1]))
{
count++;
}
}
else
{
if (flowerPack[i] == null)
{
break;
}
}
System.
out.
println(flowerPack
[i
] + "s - " + count
); count = 1;
}
}
}
Input:
1 <enter>, "Lilly" <enter"
1 <enter>, "Lilly" <enter>
1 <enter>, "Rose" <enter>
5 <enter>
Output:
Lillys - 2
Lillys - 1
Roses - 1