Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Below I have some code that simultes a flower pack, I need help with this last part. I needs to be able to sort the

Below I have some code that simultes a flower pack, I need help with this last part.

I needs to be able to sort the items in the pack (plants, flowers, fungi, weeds, and herbs) by their names alphabetically in ascending order (A-Z)

Can you please show me what my code should look like with this feature added and have this feature be usable by a person running the program? Thank you.

Here is my code:

PlantDriver.java import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.LinkedList; import java.util.Scanner; public class PlantDriver { public static void main(String[] args) throws IOException { new PlantDriver(); } public PlantDriver() throws IOException { Scanner input = new Scanner(System.in); LinkedList plantPack = new LinkedList(); System.out.println("Welcome to my Plant 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 a plant item to the pack."); System.out.println("2: Add a flower item to the pack."); System.out.println("3: Add a fungus item to the pack."); System.out.println("4: Add a weed item to the pack."); System.out.println("5: Add a herb item to the pack."); System.out.println("6: Remove a plant item (regardless of its type)from the pack."); System.out.println("7: Search for a plant by name."); System.out.println("8: Display the plants in the pack."); System.out.println("9: Filter plant pack by incomplete name"); System.out.println("10: Print Plants from the pack to a file!"); System.out.println("11: Read Plants from a file that is already created and output them"); System.out.println("12:see how many times a specific charecter is repeating in entire plantpack"); System.out.println("13.see how many times a specific 2charecter substring is repeating in entire plantpack:"); System.out.println("0: Exit the flower pack interface."); // Get the user input int userChoice = input.nextInt(); switch (userChoice) { case 1: addPlant(plantPack); break; case 2: addFlower(plantPack); break; case 3: addFungus(plantPack); break; case 4: addWeed(plantPack); break; case 5: addHerb(plantPack); break; case 6: removePlant(plantPack); break; case 7: searchPlants(plantPack); break; case 8: displayPlants(plantPack); break; case 9: filterPlants(plantPack); break; case 10: savePlantsToFile(plantPack); break; case 11: readPlantsFromFile(); break; case 12: countspecificcharecter(plantPack); break; case 13: countsubcharecters(plantPack); break; case 0: System.out.println("Thank you for using the plant pack interface. See you again soon!"); System.exit(0); } } } private void addPlant(LinkedList plantPack) { // Add a plant that is specified by the user String plantName; String plantColor; String plantID; Scanner plantInput = new Scanner(System.in); System.out.println("Please enter the name of a plant type to add:"); plantName = plantInput.nextLine(); System.out.println("What is the color of the plant you would like to add?: "); plantColor = plantInput.nextLine(); System.out.println("What is the ID of the plant?: "); plantID = plantInput.nextLine(); Plant thePlant = new Plant(plantColor, plantID, plantName); plantPack.add(thePlant); } private void addFlower(LinkedList plantPack) { // Add a plant that is specified by the user String flowerName; String flowerColor; String flowerID; String scentType; String isItThorny; boolean isThorny; Scanner flowerInput = new Scanner(System.in); System.out.println("Please enter the name of a flower type to add:"); flowerName = flowerInput.nextLine(); System.out.println("What is the color of the flower you would like to add?: "); flowerColor = flowerInput.nextLine(); System.out.println("What is the ID of the flower?: "); flowerID = flowerInput.nextLine(); System.out.println("Is the flower a thorny kind of flower? (Please answer yes or no with y or n only"); isItThorny = flowerInput.nextLine(); if (isItThorny.equalsIgnoreCase("y")) { isThorny = true; } else { isThorny = false; } System.out.println("Please describe the scent of the flower: "); scentType = flowerInput.nextLine(); Flower theFlower = new Flower(flowerColor, flowerID, flowerName, scentType, isThorny); plantPack.add(theFlower); } private void addFungus(LinkedList plantPack) { // Add a plant that is specified by the user String fungusName; String fungusColor; String fungusID; String isItPoisnous; boolean isPoisonous; Scanner fungusInput = new Scanner(System.in); System.out.println("Please enter the name of a fungus type to add:"); fungusName = fungusInput.nextLine(); System.out.println("What is the color of the fungus you would like to add?: "); fungusColor = fungusInput.nextLine(); System.out.println("What is the ID of the fungus?: "); fungusID = fungusInput.nextLine(); System.out.println("Is the fungus a poisonous kind of fungus? (Please answer yes or no with y or n only"); isItPoisnous = fungusInput.nextLine(); if (isItPoisnous.equalsIgnoreCase("y")) { isPoisonous = true; } else { isPoisonous = false; } Fungus newFungus = new Fungus(fungusColor, fungusID, fungusName, isPoisonous); plantPack.add(newFungus); } private void addWeed(LinkedList plantPack) { // Add a plant that is specified by the user String weedName; String weedColor; String weedID; String isItEdible; boolean isEdible; String isItMedicinal; boolean isMedicinal; String isItPoisnous; boolean isPoisonous; Scanner weedInput = new Scanner(System.in); System.out.println("Please enter the name of a weed type to add:"); weedName = weedInput.nextLine(); System.out.println("What is the color of the weed you would like to add?: "); weedColor = weedInput.nextLine(); System.out.println("What is the ID of the weed?: "); weedID = weedInput.nextLine(); System.out.println("Is the weed an edible kind of weed? (Please answer yes or no with y or n only"); isItEdible = weedInput.nextLine(); if (isItEdible.equalsIgnoreCase("y")) { isEdible = true; } else { isEdible = false; } System.out.println("Is the weed a medicinal kind of weed? (Please answer yes or no with y or n only"); isItMedicinal = weedInput.nextLine(); if (isItMedicinal.equalsIgnoreCase("y")) { isMedicinal = true; } else { isMedicinal = false; } System.out.println("Is the weed a poisonous kind of weed? (Please answer yes or no with y or n only"); isItPoisnous = weedInput.nextLine(); if (isItPoisnous.equalsIgnoreCase("y")) { isPoisonous = true; } else { isPoisonous = false; } Plant thePlant = new Weed(weedColor, weedID, weedName, isEdible, isMedicinal, isPoisonous); plantPack.add(thePlant); } private void addHerb(LinkedList plantPack) { // Add a plant that is specified by the user String herbName; String herbColor; String herbID; String herbFlavor; String isItMedicinal; boolean isMedicinal; String isItSeasonal; boolean isSeasonal; Scanner herbInput = new Scanner(System.in); System.out.println("Please enter the name of a herb type to add:"); herbName = herbInput.nextLine(); System.out.println("What is the color of the herb you would like to add?: "); herbColor = herbInput.nextLine(); System.out.println("What is the ID of the herb?: "); herbID = herbInput.nextLine(); System.out.println("What is the flavor of the herb?: "); herbFlavor = herbInput.nextLine(); System.out.println("Is the herb a medicinal kind of herb? (Please answer yes or no with y or n only"); isItMedicinal = herbInput.nextLine(); if (isItMedicinal.equalsIgnoreCase("y")) { isMedicinal = true; } else { isMedicinal = false; } System.out.println("Is the herb a seasonal herb? (Please answer yes or no with y or n only"); isItSeasonal = herbInput.nextLine(); if (isItSeasonal.equalsIgnoreCase("y")) { isSeasonal = true; } else { isSeasonal = false; } Plant thePlant = new Herb(herbColor, herbID, herbName, herbFlavor, isMedicinal, isSeasonal); plantPack.add(thePlant); } private void removePlant(LinkedList plantPack) { // Remove a plant that is specified by the user String plantName; Scanner plantInput = new Scanner(System.in); System.out.println( "Please enter the name of the plant (regardless of the type (e.g. Plant, Flower, Fungus, Weed) that you would like to remove: "); plantName = plantInput.nextLine(); for (Plant thePlant : plantPack) { if (thePlant.getName().equalsIgnoreCase(plantName)) { plantPack.remove(thePlant); } } } private void searchPlants(LinkedList plantPack) { String plantName; Scanner plantInput = new Scanner(System.in); System.out.println( "Please enter the name of the plant (regardless of the type (e.g. Plant, Flower, Fungus, Weed, Herb) that you would like to search: "); plantName = plantInput.nextLine(); int index = -1; for (int i = 0; i < plantPack.size(); i++) { // done in O(n) time This // is a linear search if (plantName.equalsIgnoreCase(plantPack.get(i).getName())) { index = i; break; } } if (index != -1) { System.out.println("The search element : " + plantName + " was found"); } else { System.out.println("The search element was not found in the LinkedList."); } } private void displayPlants(LinkedList plantPack) { for (Plant thePlant : plantPack) { System.out.println(thePlant.toString()); } } private void filterPlants(LinkedList plantPack) { // TODO Filter plant String plantName; Scanner plantInput = new Scanner(System.in); System.out.println("Enter the name of a plant to search by first character?"); plantName = plantInput.nextLine(); for (Plant thePlant : plantPack) { if (thePlant.getName().charAt(0) == plantName.charAt(0)) { System.out.println("Flowers based on the first character: " + thePlant.toString()); } } } private void savePlantsToFile(LinkedList plantPack) throws IOException { File plantFile = new File("src/car/plantFile"); FileOutputStream plantStream = new FileOutputStream(plantFile); PrintWriter plantOutStream = new PrintWriter(plantStream); for (Plant thePlant : plantPack) { plantOutStream.println(thePlant.toString()); } plantOutStream.close(); } private void readPlantsFromFile() throws FileNotFoundException { Scanner plantInput = new Scanner(new File("src/car/plantInputData")); try { while (plantInput.hasNext()) { Plant newPlant = new Plant(plantInput.next(), plantInput.next(), plantInput.next()); System.out.println(newPlant.toString()); } plantInput.close(); } catch (Exception e) { e.printStackTrace(); } } private void countspecificcharecter(LinkedList plantPack){ int counter=0; Scanner ch=new Scanner(System.in); System.out.println("ENTER SPECIFIC CHARECTER TO CHECK HOW MANY TIMES IT IS REPEATING IN PLANTPACK NAMES:"); char charname=ch.next().charAt(0); for(Plant p:plantPack){ String name= p.getName(); for(int i=0;i plantPack){ int counter=0; Scanner ch=new Scanner(System.in); System.out.println("ENTER SPECIFIC CHARECTER SEQEUNCE TO CHECK HOW MANY TIMES IT IS REPEATING IN PLANTPACK NAMES:"); String charname=ch.next(); for(Plant p:plantPack){ String name=p.getName(); for(int i=0;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

Information Modeling And Relational Databases

Authors: Terry Halpin, Tony Morgan

2nd Edition

0123735688, 978-0123735683

More Books

Students also viewed these Databases questions

Question

Explain the importance of Human Resource Management

Answered: 1 week ago

Question

Discuss the scope of Human Resource Management

Answered: 1 week ago

Question

Discuss the different types of leadership

Answered: 1 week ago

Question

Write a note on Organisation manuals

Answered: 1 week ago

Question

Define Scientific Management

Answered: 1 week ago

Question

Explain the function and purpose of the Job Level Table.

Answered: 1 week ago