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

Now that we\'ve found a way to replace our lost items and our well on our way to

ID: 3814990 • Letter: N

Question

Now that we've found a way to replace our lost items and our well on our way to getting back on the road we decide that we should think about implementing additional features (at a much higher cost!) for those businesses that feel they can afford a bit more. By doing this, we will not only be able to replace our stolen items, but also leave Alexander and Elizabeth with a little nest egg of their own, maybe large enough to hire a doctor to treat Elizabeth. Every shop owner that has our pack absolutely loves it, but wishes they didn't have to empty it out each night and put everything back in the next morning. We've heard of a way to store the pack so that it can be easily saved and when reopened everything is as fresh as the previous day.

Using your midterm solution 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.

You are allowed to use any file saving and loading methods introduced in the lecture or the book, but only those.

You MUST submit a sample file that can be loaded into your program without any changes (except the file path)

import java.util.LinkedList;
import java.util.*;
public class Mid{
  
static LinkedList <Plant> plants;
  
public static Plant SearchPlant(int id)
{
for(Plant p:plants){  
if ( p.GetID() == id){
return p;  
}
}
return null;
}
  
public static void main(String []args){
boolean continueChoice = false;
plants=new LinkedList <Plant>();
Scanner scanner = new Scanner(System.in);

int choice = 0;
do {
try{
System.out.println("Please enter a number from the selection below: 1. Flower 2. Fungus 3. Weed ");
int plantType = scanner.nextInt();
if ( plantType < 1 || plantType > 3 ){
continue;
}
System.out.println("What would you like to do? 1. Add 2. Remove 3. Search 4. Show ");
int a = scanner.nextInt();  
switch(a){

case 1:
int id;
System.out.println("Enter ID:");
id = scanner.nextInt();

scanner.nextLine();
  
System.out.println("Name: ");
String name = scanner.next();

System.out.println("What is the color?: ");
String color = scanner.next();
  
Plant p = null;
if (plantType == 1){

p = new Flower();
System.out.println("Does it have thorns?");
String thorns = scanner.next();
((Flower)p).SetThorns(thorns);

System.out.println("What does it smell like?:");
String smell = scanner.next();
((Flower)p).SetSmell(smell);
  
}
else if(plantType == 2){
p = new Fungus();

System.out.println("Is it poisonous? (true/false)");
boolean poison = scanner.nextBoolean();
((Fungus)p).SetPoisonous(poison);
  
  
}
else {
p = new Weed();

System.out.println("Is this weed poisonous? (true or false)");
boolean poison =scanner.nextBoolean();
((Weed)p).SetPoisonous(poison);
  
System.out.println("Enter Edible:");
String edible = scanner.next();  
((Weed)p).SetEdible(edible);
  
System.out.println("Enter Medicinal:");
String medicinal = scanner.next();  
((Weed)p).SetMedicinal(medicinal);
}

p.SetID(id);
p.SetName(name);
p.SetColor(color);  
plants.add(p);
  
break;

case 2:{

System.out.println("Enter Plant ID");
id = scanner.nextInt();
Plant plantOBJ = SearchPlant(id);
if ( plantOBJ != null ) {
plants.remove(plantOBJ);
System.out.println("Flower ID:" + id + "removed");
}
else {
System.out.println("Flower ID:"+ id + "not found");
}
}
break;
case 3:
System.out.println("Enter Plant ID:");
id = scanner.nextInt();
if ( SearchPlant(id) != null){
System.out.println("Flower ID:" + id + "found");
}
else
{
System.out.println("Flower ID"+ id + "not found");
}  
  
break;
  
case 4:
{
System.out.println("Enter Plant ID:");
id = scanner.nextInt();
Plant plantObj = SearchPlant(id);
if ( SearchPlant(id) != null){
plantObj.Show();
}
else
{
System.out.println("Flower ID"+ id + " not found");
}  
}
break;
  
}
System.out.print("Do you want to continue (true or false): ");
continueChoice = scanner.nextBoolean();
}
catch(Exception e)
{
e.printStackTrace();
}
}
while(continueChoice);
}
}

******************************plant.java*****************************

package flowerpack;

class Plant{

int id;
String name;
String color;
Plant(){
}
void SetID(int id)
{
   this.id = id;
}
int GetID()
{
return this.id;
}
void SetName(String name)
{
   this.name = name;
}
String GetName()
{
return this.name;
}
void SetColor(String color)
{
this.color = name;
}
public void Show(){
System.out.println("ID:" + id + " Name:" + name + " Color:" + color);
}
}

*******************************Flower.java*****************************

package flowerpack;

class Flower extends Plant{
String smell;
String thorns;

void SetSmell(String smell)
{
this.smell = smell;
}
void SetThorns(String thorn)
{
this.thorns = thorn;
}
public void Show(){
super.Show();  
System.out.println("thorns:" + thorns + "Smell:" + smell);
}
}

******************************Fungus.java****************************

package flowerpack;

class Fungus extends Plant{
  
boolean poisonous;
void SetPoisonous(boolean poisonous)
{
this.poisonous = poisonous;
}
public void Show(){
super.Show();  
System.out.println("poisonous:"+poisonous);
}
}

*****************************Weed.java*******************************

package flowerpack;

class Weed extends Plant{
  
boolean poisonous;
String edible;
String medicinal;
void SetPoisonous(boolean poisonous)
{
this.poisonous = poisonous;
}
void SetEdible(String edible)
{
this.edible = edible;
}
void SetMedicinal(String medicinal)
{
this.medicinal = medicinal;
}
public void Show(){
super.Show();  
System.out.println("poisnous:" + poisonous + " edible:" + edible + " medicinal:" + medicinal);
}
}

Explanation / Answer

Here is the way to save as well as load the file:

//Driver.java
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.ArrayList;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.*;
public class Driver {
  
public static void main(String[] args) throws Exception {
new Driver();
}
  
public Driver() throws Exception {
Scanner input = new Scanner(System.in);
Plant newPlant = new Plant();
ArrayList<Plant> plantPack = new ArrayList<Plant>();
  
System.out.println("Welcome to my PlantPack Interface");
System.out.println("Please select a number from the options below:");
System.out.println("");
  
while(true) {
System.out.println("1: Add a Plant to the pack.");
System.out.println("2: Remove a Plant from the pack.");
System.out.println("3: Search for a Plant by Name.");
System.out.println("4: Filter PlantPack by Plant Partial Name.");
System.out.println("5: Display Plants in the PlantPack.");
System.out.println("6: Save PlantPack to a File.");
System.out.println("7: Load PlantPack from a File.");
System.out.println("0: Exit the Plant Pack interface.");
  
int userChoice = input.nextInt();
  
switch (userChoice) {
case 1:
System.out.println("Enter Plant Type: ");
System.out.println("1: Flower");
System.out.println("2: Fungus");
System.out.println("3: Weed");
  
int plantType = input.nextInt();
switch(plantType) {
case 1:
newPlant = new Flower();
addPlant(plantPack, newPlant);
break;
case 2:
newPlant = new Fungus();
addPlant(plantPack, newPlant);
break;
case 3:
newPlant = new Weed();
addPlant(plantPack, newPlant);
break;
}
  
break;
  
case 2:
removePlant(plantPack);
break;
case 3:
searchPlant(plantPack);
break;
case 4:
filterPlant(plantPack);
break;
case 5:
displayPlant(plantPack);
break;
case 6:
savePlant(plantPack);
break;
case 7:
loadPlant(plantPack);
break;
case 0:
System.out.println("Thank you for using the Plant Pack Interface. See you again soon!");
System.exit(0);
}
}
  
}
  
  
private void savePlant(ArrayList<Plant>plantPack) throws Exception {
  
try (
java.io.PrintWriter output = new java.io.PrintWriter("PlantPack.txt"); ) {
if(plantPack.isEmpty()) {
System.out.println("Pack is empty!");
System.out.println("");
} else {
for(Plant plant : plantPack) {
if(plant instanceof Flower) {
output.println(" Flower: ");
} else if(plant instanceof Fungus) {
output.println(" Fungus: ");
} else if(plant instanceof Weed) {
output.println(" Weed: ");
}
output.println(plant);
}
}
  
}
System.out.println("PlantPack Saved!");
System.out.println("");
}
  
  
private void loadPlant(ArrayList<Plant>plantPack) {
Scanner input = new Scanner(System.in);
String loadFile;
System.out.println("Enter file name including file format (ex. file.txt): ");
loadFile = input.nextLine();
  
try {
java.io.File file = new java.io.File(loadFile);
Scanner reader = new Scanner(file);
reader.useDelimiter(" ");
while(reader.hasNext()) {
System.out.println(reader.nextLine());
  
}
reader.close();
} catch(FileNotFoundException ex) {
System.out.println("File not found.");
}
  
}
  
  
private void addPlant(ArrayList<Plant>plantPack, Plant newPlant) {
Scanner input = new Scanner(System.in);
String Name, ID, Color, Smell, Thorns, Poisonous, Edible, Medicinal;
if(newPlant instanceof Flower) {
  
System.out.print("Enter Flower ID: ");
ID = input.nextLine();
System.out.print("Enter Flower Name: ");
Name = input.nextLine();
System.out.print("Enter Flower Color: ");
Color = input.nextLine();
System.out.print("How does this Flower smell? ");
Smell = input.nextLine();
System.out.print("Does this Flower have Thorns?(Yes or No): ");
Thorns = input.nextLine();
  
while(!Thorns.equalsIgnoreCase("Yes") && !Thorns.equalsIgnoreCase("No")) {
System.out.println("Please select 'Yes' or 'No', Try Again.");
Thorns = input.nextLine();
}
  
newPlant.setID(ID);
newPlant.setName(Name);
((Flower)newPlant).setColor(Color);
((Flower)newPlant).setSmell(Smell);
((Flower)newPlant).setThorns(Thorns);
  
plantPack.add(newPlant);
System.out.println("Flower Added!");
System.out.println("");
  
} else if(newPlant instanceof Fungus) {
  
System.out.print("Enter Fungus ID: ");
ID = input.nextLine();
//input.nextLine();
System.out.print("Enter Fungus Name: ");
Name = input.nextLine();
System.out.print("Enter Fungus Color: ");
Color = input.nextLine();
System.out.print("Is this Fungus Poisonous?(Yes or No): ");
Poisonous = input.nextLine();
  
while(!Poisonous.equalsIgnoreCase("Yes") && !Poisonous.equalsIgnoreCase("No")) {
System.out.println("Please select 'Yes' or 'No', Try Again.");
Poisonous = input.nextLine();
}
  
newPlant.setID(ID);
newPlant.setName(Name);
((Fungus)newPlant).setColor(Color);
((Fungus)newPlant).setPoisonous(Poisonous);
  
plantPack.add(newPlant);
System.out.println("Fungus Added!");
System.out.println("");
  
} else {
  
System.out.print("Enter Weed ID: ");
ID = input.nextLine();
System.out.print("Enter Weed Name: ");
Name = input.nextLine();
System.out.print("Enter Weed Color: ");
Color = input.nextLine();
System.out.print("Is this Weed Poisonous?(Yes or No): ");
Poisonous = input.nextLine();
  
while(!Poisonous.equalsIgnoreCase("Yes") && !Poisonous.equalsIgnoreCase("No")) {
System.out.println("Please select 'Yes' or 'No', Try Again.");
Poisonous = input.nextLine();
}
  
System.out.print("Is this Weed Edible?(Yes or No): ");
Edible = input.nextLine();
  
while(!Edible.equalsIgnoreCase("Yes") && !Edible.equalsIgnoreCase("No")) {
System.out.println("Please select 'Yes' or 'No', Try Again.");
Edible = input.nextLine();
}
  
System.out.print("Is this Weed Medicinal?(Yes or No): ");
Medicinal = input.nextLine();
  
while(!Medicinal.equalsIgnoreCase("Yes") && !Medicinal.equalsIgnoreCase("No")) {
System.out.println("Please select 'Yes' or 'No', Try Again.");
Medicinal = input.nextLine();
}
  
newPlant.setID(ID);
newPlant.setName(Name);
((Weed)newPlant).setColor(Color);
((Weed)newPlant).setPoisonous(Poisonous);
((Weed)newPlant).setEdible(Edible);
((Weed)newPlant).setMedicinal(Medicinal);
  
plantPack.add(newPlant);
System.out.println("Weed Added!");
System.out.println("");
}
}
  
private void removePlant(ArrayList<Plant>plantPack) {
Scanner input = new Scanner(System.in);
String removePlant;
boolean found = false;
if(plantPack.isEmpty()) {
System.out.println("Pack is empty!");
System.out.println("");
} else {
System.out.print("Enter name of the plant to remove: ");
removePlant = input.nextLine();
for(Plant plant : plantPack) {
if(plant.getName().equalsIgnoreCase(removePlant)) {
plantPack.remove(plant);
System.out.println("Removed " + removePlant + "!");
System.out.println("");
found = true;
break;
}
}
if(found) {
} else {
System.out.println("No Match!");
System.out.println("");
}
}
}
  
private void searchPlant(ArrayList<Plant>plantPack) {
Scanner input = new Scanner(System.in);
String searchPlant;
boolean found = false;
if(plantPack.isEmpty()) {
System.out.println("Pack is empty!");
System.out.println("");
} else {
System.out.print("Enter name of the plant to search: ");
searchPlant = input.nextLine();
for(int i = 0; i < plantPack.size(); i++) {
if(plantPack.get(i).getName().equalsIgnoreCase(searchPlant)) {
found = true;
System.out.println("Your plant " + searchPlant + " is in position "
+ (i + 1));
System.out.println("");
break;
}
}
if(!found) {
System.out.println("No Match!");
System.out.println("");
}
}
}
  
private void filterPlant(ArrayList<Plant>plantPack) {
Scanner input = new Scanner(System.in);
System.out.print("Enter partial name of plant to search: ");
String filterPlant = input.nextLine();
ArrayList<Plant> filterPack = new ArrayList<>();
boolean found = false;
if(plantPack.isEmpty()) {
System.out.println("Pack is empty!");
System.out.println("");
} else {
  
for(int i = 0; i < plantPack.size(); i++) {
Pattern pattern = Pattern.compile(filterPlant, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(plantPack.get(i).getName());
  
while(matcher.find()) {
filterPack.add(plantPack.get(i));
found = true;   //This is where your code goes wrong. You forgot to write this step.
break;
}
}
if(found) {
  
} else {
System.out.println("No Match!");
System.out.println("");
}
}
System.out.println("Plants containing " + filterPlant + " are:");
for(Plant plant: filterPack) {
if(plant instanceof Flower) {
System.out.println(" Flower: ");
} else if (plant instanceof Fungus) {
System.out.println(" Fungus: ");
} else if (plant instanceof Weed) {
System.out.println(" Weed: ");
}
System.out.println(plant);
System.out.println("");
}
}
  
private void displayPlant(ArrayList<Plant>plantPack) {
if(plantPack.isEmpty()) {
System.out.println("Pack is empty!");
System.out.println("");
} else {
for(Plant plant : plantPack) {
if(plant instanceof Flower) {
System.out.println(" Flower: ");
} else if(plant instanceof Fungus) {
System.out.println(" Fungus: ");
} else if(plant instanceof Weed) {
System.out.println(" Weed: ");
}
System.out.println(plant);
System.out.println("");
}
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote