JAVA PROGRAM - File saving and loading 1. Using the code below implement an opti
ID: 3815223 • Letter: J
Question
JAVA PROGRAM - File saving and loading
1. Using the code below implement an option to save the plants to a file. You must also be able to load the saved information from a previously saved file.
2. Use the file class and/or scanner class for file saving/loading methods
3. You MUST submit a sample file that can be loaded into your program without any changes (except the file path)
package flowerpack;
import java.util.ArrayList;
import java.util.Scanner;
public class FlowerPack {
public static void main(String[] args) {
try{
ArrayList <Plant> 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. Quit");
System.out.print("Enter your choice: ");
choice=input.nextInt();
switch(choice)
{
case 1:
input.nextLine();
System.out.print("You want to add Flower, Fungus or Weed? <Flower / Fungus / Weed> ");
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("Has thorns? ");
thorns=input.hasNextBoolean();
input.nextLine();
System.out.print("Has smell? ");
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 Poisonous? ");
poisonous=input.hasNextBoolean();
input.nextLine();
System.out.print("Is it Edible? ");
edible=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(Plant plant:flowerPack)
{
if(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(Plant plant:flowerPack)
{
if(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(Plant plant:flowerPack)
{
if(plant.getName().equalsIgnoreCase(name))
{
System.out.println("Name: " + plant.getName() + " ID: " + plant.getID()) ;
f=1;
}
}
if(f==0)
{
System.out.println("NO plant of this name in List") ;
}
break;
case 5: System.exit(0);
}
}
}catch(Exception e)
{
System.out.println(e);
}
}
}
*************
package flowerpack;
public class Plant {
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;
}
}
class Flower extends Plant
{
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;
}
}
class Fungus extends Plant
{
String Color;
boolean Poisonous;
public Fungus(String name, String ID,String Color,boolean Poisonous) {
super(name, ID);
this.Color=Color;
this.Poisonous=Poisonous;
}
}
class Weed extends Plant
{
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;
}
}
Explanation / Answer
//Plant.java
package flowerpack;
import java.io.Serializable;
//Implementing Serializible for Persisting Object
public class Plant implements Serializable{
String name;
String ID;
Plant(String name,String ID)
{
this.name=name;
this.ID=ID;
}
/Setters and Getters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getID() {
return ID;
}
public void setID(String iD) {
ID = iD;
}
}
//Flower.java
package flowerpack;
class Flower extends Plant
{
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;
}
}
//Fungus.java
package flowerpack;
class Fungus extends Plant
{
String Color;
boolean Poisonous;
public Fungus(String name, String ID,String Color,boolean Poisonous) {
super(name, ID);
this.Color=Color;
this.Poisonous=Poisonous;
}
}
//Weed.java
package flowerpack;
class Weed extends Plant
{
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;
}
}
FlowerPack.java
package flowerpack;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
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) throws IOException{
//Create Object Output Stream for Storing Object
FileOutputStream fos = new FileOutputStream("E:\Plants1.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
//Object InpuStream for reading objects
FileInputStream fis = new FileInputStream("E:\Plants1.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
try{
//ArrayList of Plant Type
ArrayList <Plant> flowerPack= new ArrayList <Plant>();
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. Quit");
System.out.print("Enter your choice: ");
choice=input.nextInt();
switch(choice)
{
case 1:
input.nextLine();
System.out.print("You want to add Flower, Fungus or Weed? <Flower / Fungus / Weed> ");
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("Has thorns? ");
thorns=input.hasNextBoolean();
input.nextLine();
System.out.print("Has smell? ");
smell=input.hasNextBoolean();
input.nextLine();
flowerPack.add(new Flower(name,ID,color,thorns,smell));
//oos.writeObject(flowerPack);
}
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));
//oos.writeObject(flowerPack);
}
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 Poisonous? ");
poisonous=input.hasNextBoolean();
input.nextLine();
System.out.print("Is it Edible? ");
edible=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));
}
//oos.close();fos.close();
break;
case 2:
//Adding Objects List to File
oos.writeObject(flowerPack);
input.nextLine();
System.out.print("Enter the name of the plant you want to remove : ");
name=input.nextLine();
int count=0;
Plant pp=null;
//Retrieving List from file
ArrayList<Plant> alp = (ArrayList<Plant>) ois.readObject();
for(Plant p : alp){
if(p.getName().equalsIgnoreCase(name)){
pp = p;
count++;
}
}
//Remove Object from file
alp.remove(pp);
//Again Adding List to file
oos.writeObject(alp);
if(count==0)
System.out.println("No plant is found to remove....");
break;
case 3:
oos.writeObject(flowerPack);
input.nextLine();
System.out.print("Enter the name of the plant you want to search : ");
name=input.nextLine();
ArrayList<Plant> alp1 = (ArrayList<Plant>) ois.readObject();
int count1=0;
//Searching Object in the list from file
for(Plant p1 : alp1){
if(p1.getName().equalsIgnoreCase(name)){
System.out.println("Plant Information:");
System.out.println(p1.getID()+" "+p1.getName());
count1++;
}
}
if(count1==0)
System.out.println(name+" "+"not found..");
break;
case 4:
oos.writeObject(flowerPack);
input.nextLine();
System.out.print("Enter the name of the plant you want to filter: ");
name=input.nextLine();
ArrayList<Plant> alp2 = (ArrayList<Plant>) ois.readObject();
int count2=0;
//Filtering Object from file
for(Plant p2 : alp2){
if(p2.getName().equalsIgnoreCase(name)){
System.out.println("Plant Information:");
System.out.println(p2.getID()+" "+p2.getName());
count2++;
}
}
if(count2==0)
System.out.println(name+" "+"not found..");
break;
//Exit from Switch
case 5: System.exit(0);
}
}
}catch(Exception e)
{
System.out.println(e);
}
}
}
Output:
1. Add plants
2. Remove plants
3. Search plants
4. Filter plants
5. Quit
Enter your choice: 1
You want to add Flower, Fungus or Weed? <Flower / Fungus / Weed> Flower
Enter name : Rose
Enter ID : F1
Enter color : Red
Has thorns? true
Has smell? true
1. Add plants
2. Remove plants
3. Search plants
4. Filter plants
5. Quit
Enter your choice: 1
You want to add Flower, Fungus or Weed? <Flower / Fungus / Weed> Fungus
Enter name : Flagella
Enter ID : F2
Enter color : Green
Is it poisonous? true
1. Add plants
2. Remove plants
3. Search plants
4. Filter plants
5. Quit
Enter your choice: 1
You want to add Flower, Fungus or Weed? <Flower / Fungus / Weed> Weed
Enter name : Tulasi
Enter ID : F3
Enter color : Green
Is it Poisonous? false
Is it Edible? true
Is it Medicinal? true
1. Add plants
2. Remove plants
3. Search plants
4. Filter plants
5. Quit
Enter your choice: 3
Enter the name of the plant you want to search : Tulasi
Plant Information:
F3 Tulasi
1. Add plants
2. Remove plants
3. Search plants
4. Filter plants
5. Quit
Enter your choice: 3
Enter the name of the plant you want to search : Rose
Plant Information:
F1 Rose
1. Add plants
2. Remove plants
3. Search plants
4. Filter plants
5. Quit
Enter your choice: 2
Enter the name of the plant you want to remove : Rose
1. Add plants
2. Remove plants
3. Search plants
4. Filter plants
5. Quit
Enter your choice: 3
Enter the name of the plant you want to search : Rose
Rose not found..
1. Add plants
2. Remove plants
3. Search plants
4. Filter plants
5. Quit
Enter your choice: 4
Enter the name of the plant you want to filter: Flagella
Plant Information:
F2 Flagella
1. Add plants
2. Remove plants
3. Search plants
4. Filter plants
5. Quit
Enter your choice: 5
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.