Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The directions for this assignment requires that I load a sample file into the p

ID: 3827523 • Letter: T

Question

The directions for this assignment requires that I load a sample file into the program without any changes(except the file path). Not sure what went wrong.

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.util.ArrayList;

import java.util.Scanner;

public class flowerpack {

public static void main(String[] args) {

try{

ArrayList<Object> flowerPack= new ArrayList<>();

Scanner input=new Scanner(System.in);

int choice;

String name, color, ID;

boolean poisonous,edible,medicinal,smell,thorns;

while(true)

{

System.out.println("1. Add plants");

System.out.println("2. Remove plants");

System.out.println("3. Search plants");

System.out.println("4. Filter plants");

System.out.println("5. Save to file");

System.out.println("6. Load from file");

System.out.println("7. Quit");

System.out.print("Enter your choice: ");

choice=input.nextInt();

switch(choice)

{

case 1:

input.nextLine();

System.out.print("Would you like to add a flower, weed or fungus? ");

String type=input.nextLine();

if(type.equalsIgnoreCase("Flower"))

{

System.out.print("Enter name : ");

name=input.nextLine();

System.out.print("Enter ID : ");

ID=input.nextLine();

System.out.print("Enter color : ");

color=input.nextLine();

System.out.print("Does it have thorns? ");

thorns=input.hasNextBoolean();

input.nextLine();

System.out.print("What does this flower smell like? ");

smell=input.hasNextBoolean();

input.nextLine();

flowerPack.add(new Flower(name,ID,color,thorns,smell));

}

else if(type.equalsIgnoreCase("Fungus"))

{

System.out.print("Enter name : ");

name=input.nextLine();

System.out.print("Enter ID : ");

ID=input.nextLine();

System.out.print("Enter color : ");

color=input.nextLine();

System.out.print("Is it poisonous? ");

poisonous=input.hasNextBoolean();

input.nextLine();

flowerPack.add(new Fungus(name,ID,color,poisonous));

}

else if(type.equalsIgnoreCase("Weed"))

{

System.out.print("Enter name : ");

name=input.nextLine();

System.out.print("Enter ID : ");

ID=input.nextLine();

System.out.print("Enter color : ");

color=input.nextLine();

System.out.print("Is it Edible? ");

edible=input.hasNextBoolean();

input.nextLine();

System.out.print("Is it Poisonous? ");

poisonous=input.hasNextBoolean();

input.nextLine();

System.out.print("Is it Medicinal? ");

medicinal=input.hasNextBoolean();

input.nextLine();

flowerPack.add(new Weed(name,ID,color,poisonous,edible,medicinal));

}

break;

case 2:input.nextLine();

System.out.print("Enter the name of the plant you want to remove : ");

name=input.nextLine();

int flag=0;

for(Object plant:flowerPack)

{

if(((Plant) plant).getName().equalsIgnoreCase(name))

{

System.out.println("plant removed sucessfully") ;

flag=1;

break;

}

}

if(flag==0)

{

System.out.println("plant not found") ;

}

break;

case 3:

input.nextLine();

System.out.print("Enter the name of the plant you want to search : ");

name=input.nextLine();

int f=0;

for(Object plant:flowerPack)

{

if(((Plant) plant).getName().equalsIgnoreCase(name))

{

System.out.println("plant found sucessfully") ;

f=1;

break;

}

}

if(f==0)

{

System.out.println("plant not found") ;

}

break;

case 4:

input.nextLine();

System.out.print("Enter the name of the plant you want to filter: ");

name=input.nextLine();

f=0;

for(Object plant:flowerPack)

{

if(((Plant) plant).getName().equalsIgnoreCase(name))

{

System.out.println("Name: " + ((Plant) plant).getName() + " ID: " + ((Plant) plant).getID()) ;

f=1;

}

}

if(f==0)

{

System.out.println("NO plant of this name in List") ;

}

break;

case 5:

File file = new File("Plant.txt");

FileOutputStream fStream = new FileOutputStream(file);

ObjectOutputStream stream = new ObjectOutputStream(fStream);

stream.writeObject(flowerPack);

stream.close();

break;

case 6:

File file2 = new File("Plant.txt");

FileInputStream fileStream = new FileInputStream(file2);

ObjectInputStream oStream = new ObjectInputStream(fileStream);

ArrayList readObject = ((ArrayList)oStream.readObject());

flowerPack.addAll(readObject);

oStream.close();

break;

case 7: System.exit(0);

}

}

}catch(Exception e)

{

System.out.println(e);

}

}

}

********plant *******

import java.io.Serializable;

@SuppressWarnings("serial")

public class Plant implements Serializable{

String name;

String ID;

Plant(String name,String ID)

{

this.name=name;

this.ID=ID;

}

public String getName()

{

return name;

}

public String getID()

{

return ID;

}

}

******weed ******

class Weed extends Plant

{

/**

*

*/

private static final long serialVersionUID = 1L;

String Color;

boolean Poisonous;

boolean Edible;

boolean Medicinal;

public Weed(String name, String ID,String Color,boolean Poisonous,boolean Edible, boolean Medicinal) {

super(name, ID);

this.Color=Color;

this.Poisonous=Poisonous;

this.Edible=Edible;

this.Medicinal=Medicinal;

}

}

***** fungus *****

class Fungus extends Plant

{

/**

*

*/

private static final long serialVersionUID = 1L;

String Color;

boolean Poisonous;

public Fungus(String name, String ID,String Color,boolean Poisonous) {

super(name, ID);

this.Color=Color;

this.Poisonous=Poisonous;

}

}

***** flower *****

class Flower extends Plant

{

/**

*

*/

private static final long serialVersionUID = 1L;

String color;

boolean thorns;

boolean smell;

public Flower(String name, String ID,String color,boolean thorns,boolean smell) {

super(name, ID);

this.color=color;

this.thorns=thorns;

this.smell=smell;

}

}

Explanation / Answer

while loading the file at the end you are populating the list flowerPack which is already prepopulated while using add option. So after loading all the plants are coming twice. you have two options to solve this

1. Before loading the file make the flowerPack list reinitialized

flowerPack= new ArrayList<>();

2. Use a different List to populate value from the file

ArrayList<Object> flowerPackLoadedFromFile = new ArrayList<>();