Now that we\'ve found a way to replace our lost items and are well on our way to
ID: 3807761 • Letter: N
Question
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 TO BE ALTERED
MAIN CODE
import java.util.*;
public class midterm{
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){
plants=new LinkedList<Plant>();
boolean continueChoice=false;
Scanner scanner=new Scanner(System.in);
int choice=0;
do{
try{
System.out.println("Choose Plant:1. Flower 2.Fungus 3.Weed Enter Option:");
int plantCh=scanner.nextInt();
if ( plantCh < 1 || plantCh > 3 )
{
System.out.println("Wrong Choice Entered! Try Again");
continue;
}
System.out.println("Choose Operation:1.Add 2.Remove 3. Search 4. Show Enter Option:");
int op=scanner.nextInt();
switch(op)
{
case 1:
int id;
System.out.println("Enter ID:");
id=scanner.nextInt();
scanner.nextLine();
System.out.println("Enter Name:");
String name=scanner.nextLine();
System.out.println("Enter Color:");
String color=scanner.nextLine();
Plant p=null;
if (plantCh==1)
{
p=new Flower();
System.out.println("Enter thorns:");
String thorns=scanner.nextLine();
((Flower)p).SetThorns(thorns);
System.out.println("Enter Smell:");
String smell=scanner.nextLine();
((Flower)p).SetSmell(smell);
}
else if(plantCh==2)
{p=new Fungus();
System.out.println("Enter poisnous:");
boolean poison=scanner.nextBoolean();
((Fungus)p).SetPoisonous(poison);
}
else{
p=new Weed();
System.out.println("Enter Medicinal;");
String medicinal=scanner.nextLine();
((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+"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;
default:
System.out.println("Wrong choice");
}
System.out.print("Do you want to continue (true or false): ");
continueChoice=scanner.nextBoolean();
}
catch(Exception e)
{
e.printStackTrace();
}
}
while(continueChoice);
}
}
PLANT
import java.util.*;
class Plant{
String name;
int id;
String color;
Plant(){
}
void SetID(int id)
{
this.id=id;
}
int GetID()
{
return this.id;
}
void SetName(String name)
{
this.name=name;
}
void SetColor(String color)
{
this.color=name;
}
public void Show(){
System.out.println("ID:"+id+" Name:"+name+" Color:"+color);
}
}
FLOWER
class Flower extends Plant{
String thorns;
String smell;
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
class Fungus extends Plant{
boolean poisonous;
void SetPoisonous(boolean poisonous)
{
this.poisonous=poisonous;
}
public void Show(){
super.Show();
System.out.println("poisonous:"+poisonous);
}
}
WEED
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
import java.util.*;
public class midterm{
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){
plants=new LinkedList<Plant>();
boolean continueChoice=false;
Scanner scanner=new Scanner(System.in);
int choice=0;
do{
try{
System.out.println("Choose Plant:1. Flower 2.Fungus 3.Weed Enter Option:");
int plantCh=scanner.nextInt();
if ( plantCh < 1 || plantCh > 3 )
{
System.out.println("Wrong Choice Entered! Try Again");
continue;
}
System.out.println("Choose Operation:1.Add 2.Remove 3. Search 4. Show Enter Option:");
int op=scanner.nextInt();
switch(op)
{
case 1:
int id;
System.out.println("Enter ID:");
id=scanner.nextInt();
scanner.nextLine();
System.out.println("Enter Name:");
String name=scanner.nextLine();
System.out.println("Enter Color:");
String color=scanner.nextLine();
Plant p=null;
if (plantCh==1)
{
p=new Flower();
System.out.println("Enter thorns:");
String thorns=scanner.nextLine();
((Flower)p).SetThorns(thorns);
System.out.println("Enter Smell:");
String smell=scanner.nextLine();
((Flower)p).SetSmell(smell);
}
else if(plantCh==2)
{p=new Fungus();
System.out.println("Enter poisnous:");
boolean poison=scanner.nextBoolean();
((Fungus)p).SetPoisonous(poison);
}
else{
p=new Weed();
System.out.println("Enter Medicinal;");
String medicinal=scanner.nextLine();
((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+"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;
default:
System.out.println("Wrong choice");
}
System.out.print("Do you want to continue (true or false): ");
continueChoice=scanner.nextBoolean();
}
catch(Exception e)
{
e.printStackTrace();
}
}
while(continueChoice);
}
}
PLANT
import java.util.*;
class Plant{
String name;
int id;
String color;
Plant(){
}
void SetID(int id)
{
this.id=id;
}
int GetID()
{
return this.id;
}
void SetName(String name)
{
this.name=name;
}
void SetColor(String color)
{
this.color=name;
}
public void Show(){
System.out.println("ID:"+id+" Name:"+name+" Color:"+color);
}
}
FLOWER
class Flower extends Plant{
String thorns;
String smell;
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);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.