After dinner with Alexander and Elizabeth we notice that Alexander is mixing som
ID: 3857762 • Letter: A
Question
After dinner with Alexander and Elizabeth we notice that Alexander is mixing some herbs as a medication for Elizabeth which eases her pain and helps her sleep. Once the medicine is administered we begin small talk with Alexander. During the conversation he tells us the story about how his mother and father were hopelessly in love and were lucky enough to die side by side protecting the things they loved most, their children.
In the middle of the story we become distracted by Elizabeth trying to request something, but we can't make out what she wants because her medicine has kicked in. After a few moments we determine she is requesting a flower, but we can't understand the entire name, just the start. Instantly we remember that we know how to search through our flower pack while only using partial names. We instantly turn around and start working on yet another improvement to the flower pack.
Create a flower object that has specific traits (name, color, presence of thorns and smell)
These flower objects must be able to stay in his pack (Use an ArrayList)
Be able to add and remove flowers
Implement a partial search (Searching for 'r' should return all flowers with an 'r' in their name, and the same goes for any partial search). This is commonly known as a filter.
Explanation / Answer
Hi, Let me know if you need more information/implementation:-
======================================================
Added few more facilitites to add the flowser too ,with menu options.
=======================================================
Flower.java
========================================================
public class Flower { // Declare
// attributes here
private String name;
private String color;
private String presenceOfThorns;
private String smell;
public Flower() {
super();
name = "";
color = "";
presenceOfThorns = "";
smell = "";
} // Create an
// overridden constructor here //Create accessors and
// mutators for your triats.
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getPresenceOfThorns() {
return presenceOfThorns;
}
public void setPresenceOfThorns(String presenceOfThorns) {
this.presenceOfThorns = presenceOfThorns;
}
public String getSmell() {
return smell;
}
public void setSmell(String smell) {
this.smell = smell;
}
@Override
public String toString() {
return "Flower [name=" + name + ", color=" + color
+ ", presenceOfThorns=" + presenceOfThorns + ", smell=" + smell
+ "]";
}
}
============================================================
FlowerTester.java
===========================================================
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class FlowerTester {
public static void main(String[] args) {
new FlowerTester();
} // This will act as our program switchboard
public FlowerTester() {
Scanner input = new Scanner(System.in);
ArrayList flowerPack = new ArrayList();
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: Search for a flower.");
System.out.println("4: Display the flowers in the pack.");
System.out.println("5: Filter flower pack by incomplete name");
System.out.println("0: Exit the flower pack interface.");
// Get the user input
int userChoice = input.nextInt();
switch (userChoice) {
case 1:
addFlower(flowerPack);
break;
case 2:
removeFlower(flowerPack);
break;
case 3:
searchFlowers(flowerPack);
break;
case 4:
displayFlowers(flowerPack);
break;
case 5:
filterFlowers(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(ArrayList flowerPack) {
// TODO: Add a flower that is specified by the user
Scanner input = new Scanner(System.in);
Flower flower = new Flower();
System.out.println("Enter color");
flower.setColor(input.next());
System.out.println("Enter name");
flower.setName(input.next());
System.out.println("Enter Presence Of Thorns");
flower.setPresenceOfThorns(input.next());
System.out.println("Enter smell");
flower.setSmell(input.next());
flowerPack.add(flower);
}
private void removeFlower(ArrayList flowerPack) { // TODO: Remove
// a flower that is specified by the user
Scanner input = new Scanner(System.in);
System.out.println("Enter Flower Values to be removed");
Flower flower = new Flower();
System.out.println("Enter color");
flower.setColor(input.next());
System.out.println("Enter name");
flower.setName(input.next());
System.out.println("Enter Presence Of Thorns");
flower.setPresenceOfThorns(input.next());
System.out.println("Enter smell");
flower.setSmell(input.next());
Iterator<Flower> itr = flowerPack.iterator();
int i = 0;
while (itr.hasNext()) {
Flower tmp = itr.next();
if (tmp.getSmell().equals(flower.getSmell())
&& tmp.getColor().equals(flower.getColor())
&& tmp.getName().equals(flower.getName())
&& tmp.getPresenceOfThorns().equals(
flower.getPresenceOfThorns())) {
flowerPack.remove(i);
}
i++;
}
}
private void searchFlowers(ArrayList flowerPack) { // TODO: Search
// for a user specified flower
Scanner input = new Scanner(System.in);
System.out.println("Enter Flower Values to be serached..");
Flower flower = new Flower();
System.out.println("Enter color");
flower.setColor(input.next());
System.out.println("Enter name");
flower.setName(input.next());
System.out.println("Enter Presence Of Thorns");
flower.setPresenceOfThorns(input.next());
System.out.println("Enter smell");
flower.setSmell(input.next());
Iterator<Flower> itr = flowerPack.iterator();
int i = 0;
while (itr.hasNext()) {
Flower tmp = itr.next();
if (tmp.getSmell().equals(flower.getSmell())
&& tmp.getColor().equals(flower.getColor())
&& tmp.getName().equals(flower.getName())
&& tmp.getPresenceOfThorns().equals(
flower.getPresenceOfThorns())) {
System.out.println("Flower which you mentioned is found");
}
i++;
}
}
private void displayFlowers(ArrayList flowerPack) { // TODO:
// Display flowers using any technique you like
Iterator<Flower> itr = flowerPack.iterator();
while (itr.hasNext()) {
System.out.println(((Flower) itr.next()).toString());
}
}
private void filterFlowers(ArrayList flowerPack) {
// // TODO Filter flower results
// for a user specified flower
Scanner input = new Scanner(System.in);
System.out.println("Please Enter Keyword...");
String val = input.next();
Iterator<Flower> itr = flowerPack.iterator();
int i = 0;
while (itr.hasNext()) {
Flower tmp = itr.next();
if (tmp.getName().indexOf(val) >= 0) {
System.out.println("Found Flower" + tmp.toString());
}
}
}
}
=============================================================
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: Search for a flower.
4: Display the flowers in the pack.
5: Filter flower pack by incomplete name
0: Exit the flower pack interface.
1
Enter color
RED
Enter name
ROSE
Enter Presence Of Thorns
NOTHING
Enter smell
NOTHING
1: Add an item to the pack.
2: Remove an item from the pack.
3: Search for a flower.
4: Display the flowers in the pack.
5: Filter flower pack by incomplete name
0: Exit the flower pack interface.
5
Please Enter Keyword...
R
Found FlowerFlower [name=ROSE, color=RED, presenceOfThorns=NOTHING, smell=NOTHING]
1: Add an item to the pack.
2: Remove an item from the pack.
3: Search for a flower.
4: Display the flowers in the pack.
5: Filter flower pack by incomplete name
0: Exit the flower pack interface.
================================
Please comment if required.
==============================================================
Thanks
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.