Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA PROGRAM !! I need this to search correctly, remove a flower correctly, and display the results with the count of flower types next to

JAVA PROGRAM

!! I need this to search correctly, remove a flower correctly, and display the results with the count of flower types next to each !!

package assignement1;

import java.util.Arrays; import java.util.Comparator; import java.util.Scanner;

public class Assignment01Driver { //added a new Scanner object Scanner sc = 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]; 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 boolean found = false; int index=0; for(int i =0;i if(flowerPack[i]==null){ found=true; index = i; break; } } if(found){ System.out.println("Enter the flower name"); String newFlower = sc.nextLine(); flowerPack[index]=newFlower; } else{ System.out.println("no space"); } } private void removeFlower(String flowerPack[]) { // TODO: Remove a flower that is specified by the user System.out.println("Enter the flower name for remove"); String removeFlower = sc.nextLine(); for(int i=0;i if(flowerPack[i]==removeFlower){ flowerPack[i]=null; System.out.println(removeFlower+" removed from index no "+i); break; } } } 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 Arrays.sort(flowerPack, new Comparator() { // @Override public int compare(String o1, String o2) { if (o1 == null && o2 == null) { return 0; } if (o1 == null) { return 1; } if (o2 == null) { return -1; } return o1.compareTo(o2); }}); } private void searchFlowers(String flowerPack[]) { // TODO: Search for a user specified flower System.out.println("Enter the flower name "); String flower = sc.nextLine(); int count = 1; for(int i=0;i if(flowerPack[i]==flower){ count++; } } System.out.println(); if(count<=0){ System.out.println(flower +" found"); } else{ System.out.println("no match found for : " + 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 */ for(int i = 0;i if(flowerPack[i]==null){} else System.out.println(flowerPack[i]); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Learn To Program Databases With Visual Basic 6

Authors: John Smiley

1st Edition

1902745035, 978-1902745039

Students also viewed these Databases questions