Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Now that we've found a way to replace our lost items and are well on our way to getting back on the road we decide

Now that we've found a way to replace our lost items and are 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 who 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 who 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 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. All previously required actions are still required (add, remove, display, etc)

You are allowed to use any file saving and loading methods.

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

Submit 6 files: Assignment4.java, flower.java, fungus.java, plant.java, weed.java and the example sample.txt. Below are the 6 codes that need to be altered.

Main Code:

import java.io.*;

import java.util.*; import javax.swing.*; public class mid { public static void main(String args) throws IOException { Scanner input = new Scanner(System.in); ArrayList plantPack = new ArrayList(); System.out.println("Welcome to the plant pack application."); System.out.println("Please select from one of the following options: "); System.out.println(""); while (true) { System.out.println("1 - Add"); System.out.println("2 - Remove"); System.out.println("3 - Search"); System.out.println("4 - Display"); System.out.println("5 - Filter"); System.out.println("6 - Add or load a plant file."); System.out.println("0 - Exit"); int choice = input.nextInt(); switch (choice) { case 1: add(plantPack); break; case 2: remove(plantPack); break; case 3: search(plantPack); break; case 4: display(plantPack); break; case 5: filter(plantPack); break; case 6: saveLoad(plantPack); break; case 0: System.out.println("Thank you for using the flower pack application, good bye!"); System.exit(0); } } }

public static void add(ArrayListplantPack) { String iD; String name; String color; String smell; String thorns; String poisonous; String edible; String medicinal; Scanner scan = new Scanner(System.in); plant test = new plant(); System.out.println("Please select one of the following."); System.out.println("1 - Add flower"); System.out.println("2 - Add fungus"); System.out.println("3 - Add a weed"); int ans = scan.nextInt(); switch(ans) { case 1: System.out.print("Enter the ID number for your flower"); iD = scan.next(); System.out.print("Enter the name of your flower."); name = scan.next(); System.out.print("Enter the color"); color = scan.next(); System.out.print("Are there any thorns? Yes or no?"); thorns = scan.next(); System.out.print("Does the flower have a smell? Yes or no?"); smell = scan.next(); flower newFlower = new flower(iD, name, color, thorns, smell); plantPack.add(newFlower); System.out.println(); break; case 2: System.out.print("Enter the ID number for your fungus."); iD = scan.next(); System.out.print("Enter the name of your fungus."); name = scan.next(); System.out.print("Enter the color of your fungus."); color = scan.next(); System.out.print("Is the fungus poisonous? Yes or no?"); poisonous = scan.next(); fungus newFungus = new fungus(iD, name, color, poisonous); plantPack.add(newFungus); System.out.println(); break; case 3: System.out.print("Please enter your ID number for your weed."); iD = scan.next(); System.out.print("Enter the name of your weed."); name = scan.next(); System.out.print("Please enter the color of your weed."); color = scan.next(); System.out.print("Is the weed poisonous? Yes or no?"); poisonous = scan.next(); System.out.print("Is it edible? Yes or no?"); edible = scan.next(); System.out.print("Can we use it for medicinal purposes? Yes or no?"); medicinal = scan.next(); weed newWeed = new weed(iD, name, color, poisonous, edible, medicinal); plantPack.add(newWeed); System.out.println(); break; } }

private static void remove(ArrayListplantPack) { Scanner scan = new Scanner(System.in); String s; System.out.println("Enter the name of the plant that you would like to remove."); s = scan.next(); for (int i = 0; i < plantPack.size(); i++) { plant remove = plantPack.get(i); if(remove.getName().equalsIgnoreCase(s)) { plantPack.remove(i); break; } } }

private static void display(ArrayListplantPack) { System.out.println(); for(int i = 0; i < plantPack.size(); i++) { System.out.println(plantPack.get(i)); } System.out.println(); }

public static void search(ArrayListplantPack) { Scanner scan = new Scanner(System.in); System.out.println("What plant would you like to search for?"); String stringAns = scan.next(); boolean FlagAns = false; for(int i = 0; i < plantPack.size(); i++) { plant newPlantPack = plantPack.get(i); if (newPlantPack.getName().equalsIgnoreCase(stringAns)) { FlagAns = true; break; } } if(FlagAns) { System.out.println("Yes, " + stringAns + " is in the pack."); } else { System.out.println("No, " + stringAns + " is not in the pack"); } }

public static void filter(ArrayListplantPack) { Scanner scan = new Scanner(System.in); String keyWord; boolean checkAns = false; System.out.println("Enter the character or word you would like to search for: "); keyWord = scan.nextLine(); for(int i = 0; i < plantPack.size(); i++) { plant newPlantPack = plantPack.get(i); if(newPlantPack.getName().contains(keyWord)) { System.out.println(); System.out.println("" + newPlantPack.getName()); checkAns = true; } } if(checkAns == false) { System.out.println("No results found within the database."); } if (checkAns == true) { System.out.println("This is what was found"); } System.out.println(""); }

public static void saveLoad(ArrayListplantPack) throws IOException { Scanner scan = new Scanner(System.in); System.out.println("Option 1: Would you like to save your file?"); System.out.println("Option 2: Would you like to load a file?"); System.out.print("Answer: "); int i = scan.nextInt(); plant testjr = new plant(); if (i == 1) { File file = new File("plantFile.txt"); FileOutputStream outFileStream = new FileOutputStream(file); PrintWriter outStream = new PrintWriter(outFileStream); for(int x = 0; x

Plant

public class plant { private String ID; private String name; private String color; public plant() { } public plant(String id, String n, String c) { ID = id; name = n; setColor(c); }

public void setId(String id) { ID = id; }

public String getId() { return ID; }

public void setName(String n) { name = n; }

public String getName() { return name; }

public void setColor(String color) { this.color = color; }

public String getColor() { return color; } }

Flower

public class flower extends plant { private String thorns; private String smell; private String id; private String name; public flower() { } public flower(String ID, String name, String color, String thorns, String smell) { super(ID, name, color); this.thorns = thorns; this.smell = smell; id = ID; this.name = name; } public void setThorns(String thorns) { this.thorns = thorns; } public void setSmell(String smell) { this.smell = smell; } public String getThorns() { return thorns; } public String getSmell() { return smell; } public String toString() { return id + " " + name + " " + getColor() + " " + thorns + " " + smell; } }

Fungus

public class fungus extends plant { private String poisonous; private String id; private String name; public fungus() { } public fungus(String ID, String name, String color, String poisonous) { super(ID, name, color); this.poisonous = poisonous; id = ID; this.name = name; } public void setPoisonous(String poisonous) { this.poisonous = poisonous; } public String getPoisonous() { return poisonous; } public String toString() { return id + " " + name + " " + getColor() + " " + poisonous; } }

Weed

public class weed extends plant { String poisonous; String edible; String medicinal; String id; String name; public weed() { } public weed(String ID, String name, String color, String poisonous, String edible, String Medicinal) { super(ID, name, color); this.poisonous = poisonous; this.edible = edible; this.medicinal = medicinal; id = ID; this.name = name; } public void setPoisonous(String poisonous) { this.poisonous = poisonous; } public void setEdible(String edible) { this.edible = edible; } public void setmedicinal (String medicinal) { this.medicinal = medicinal; } public String getPoisonous() { return poisonous; } public String getEdible() { return edible; } public String getMedicinal() { return medicinal; } public String toString() { return id + " " + name + " " + edible + " " + medicinal + getColor() + " " + poisonous; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data, Mining, And Analytics Components Of Strategic Decision Making

Authors: Stephan Kudyba

1st Edition

1466568704, 9781466568709

More Books

Students also viewed these Databases questions

Question

What are Measures in OLAP Cubes?

Answered: 1 week ago

Question

How do OLAP Databases provide for Drilling Down into data?

Answered: 1 week ago

Question

How are OLAP Cubes different from Production Relational Databases?

Answered: 1 week ago