JAVA PROGRAM !! I need this to search correctly, remove a flower correctly, and
ID: 3791236 • Letter: J
Question
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]);
}
}
}
Explanation / Answer
Hi buddy, please find the below java program. It's working fine.
import java.util.*;
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 Main() {
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<flowerPack.length;i++){
if (flowerPack[i] == null) {
found = true;
index = i;
break;
}
}
if (found) {
System.out.println("Enter the flower name");
String newFlower = sc.next();
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.next();
for (int i = 0; i < flowerPack.length; i++)
if (flowerPack[i].equals(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<String>() {
// @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.next();
int count = 1;
for (int i = 0; i < flowerPack.length ;i++){
System.out.println("Searching "+flowerPack[i]+" "+flower);
if (flowerPack[i]!=null&&flowerPack[i].equals(flower)) {
count=-1;
break;
}
}
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
*/
HashMap<String,Integer> hm = new HashMap();
for (int i = 0; i < flowerPack.length; i++){
if (flowerPack[i] == null) {} else{
if(!hm.containsKey(flowerPack[i])){
hm.put(flowerPack[i],0);
}
hm.put(flowerPack[i],hm.get(flowerPack[i])+1);
}
}
Set<String> key = hm.keySet();
for(String k : key){
System.out.println(k+" "+hm.get(k));
}
}
}
OUTPUT :
D:>java Main
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 flower name
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 flower name
Lotus
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 flower name
Hibiscus
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
Rose 1
Lotus 1
Hibiscus 1
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
Enter the flower name
Rose
Searching Rose Rose
Rose found
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
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 flower name
Lotus
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
Rose 1
Lotus 2
Hibiscus 1
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.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.