Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

To Do: We want a menu-driven program that keeps track of plant inventory for a nursery. I have a set of start files for you.

To Do: We want a menu-driven program that keeps track of plant inventory for a nursery. I have a set of start files for you. Open up PlantDriver.java. This will be our beginning program. There are eight menu items that you need to get working (I did 0, 3 and 8). Run the program (it runs now but doesnt do anything except print and quit!). Your goal is to get the menu items to work. Note that the stub methods for the menu items are already written (they are in MenuInfo). Do not forget to implement Serializable for the blueprint classes. The code that I have written gives me an error message that says java.lang.NumberFormatException. Any assistance would be greatly appreciated!

package ntorresproj120;

import java.awt.FileDialog;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.util.ArrayList;

import java.util.Scanner;

public class MenuInfo {

public ArrayList loadPlants()

{

ArrayList plants = new ArrayList();

plants.add(new Plants("JAPMA", "Japanese Maple", 30, 60.25, 85.50));

plants.add(new Plants("DOG", "Dogwood", 20, 15.50, 32.50));

plants.add(new Plants("RED", "Redbud", 45, 21.00, 28.00));

plants.add(new Plants("RBUCK", "Red Buckeye", 24, 21.00, 33.00));

plants.add(new Plants("CRMY", "Crape Myrtle", 48, 19.00, 29.50));

plants.add(new Plants("TULIP", "Tulip Tree", 30, 23.00, 38.50));

plants.add(new Plants("FALSE", "Hinoki False Cypress", 40, 18.35, 29.50));

plants.add(new Plants("SERVICE", "Serviceberry", 30, 43.00, 58.50));

plants.add(new Plants("SMOKE", "Smoke Tree", 18, 34.00, 45.50));

return plants;

}

public ArrayList readSerializable() // menu item #1

{

// next line is just a placeholder until you write the method

ArrayList st= new ArrayList();

Frame f = new Frame();

FileDialog foBox = new FileDialog(f,"Reading serialized file", FileDialog.LOAD);

foBox.setVisible(true);

String foName = foBox.getFile();

String dirPath = foBox.getDirectory();

File inFile = new File(dirPath + foName);

ObjectInputStream OIS = null;

try {

FileInputStream IS = new FileInputStream(inFile);

OIS = new ObjectInputStream(IS);

st = (ArrayList) OIS.readObject();

}

catch (IOException io)

{

io.printStackTrace();

System.out.println("An IO Exception occured");

}

catch (ClassNotFoundException cnf)

{

cnf.printStackTrace();

System.out.println("An IO Exception occured");

}

finally

{

try {

OIS.close();

}

catch(Exception e) {}

}

return st;

}

public void writeSerializable(ArrayList st ) // menu item #2

{

Frame f = new Frame();

FileDialog foBox = new FileDialog(f,"Saving plants file", FileDialog.SAVE);

foBox.setVisible(true);

String foName = foBox.getFile();

String dirPath = foBox.getDirectory();

File outFile = new File(dirPath + foName);

FileOutputStream OS = null;

ObjectOutputStream OOS = null;

try {

OS = new FileOutputStream(outFile);

OOS = new ObjectOutputStream(OS);

OOS.writeObject(st);

OOS.writeInt(st.size());

}

catch (IOException io)

{

io.printStackTrace();

System.out.println("An IO Excepetion Occurred");

}

finally

{

OOS.close();

}

catch (Exception e) {}

}

public void print(ArrayList pl) // menu item #5

{

System.out.println("Current plants ininventory:");

for(int i=0;i

System.out.println(pl.get(i).toString());

}

// method should print out two values.

//The first is how much total money we have invested in our plants

//The second is how much we would get if we sold the total inventory

public void total(ArrayList st) // menu item #4

{

double totalCost = 0;

double totalSales = 0;

for(int i = 0; i

totalCost += st.get(i).getOurCost();

totalSales =+ st.get(i).getSalesCost();

}

System.out.println("Thte total Cost to us is: " + totalCost + ". And the total sales is: " + totalSales);

}

public void find(ArrayList st) // menu item #5

{

Scanner scan = new Scanner(System.in);

boolean found = false;

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

int i = 0;

while (!found && i

if(pName==st.get(i).getDescription().toString()) {

found = true;

break;

}

else

{

i++;

}

if(!found)

System.out.println("I am sorry, that plant does not exist.");

}

}

public void addPlants(ArrayList st) // menu item #6

{

// calls the menuAdd method below

menuAdd();

// write the code to do the item depending on their choice

Scanner scan = new Scanner(System.in);

int ans = 0;

while (true)

{

menuAdd();

System.out.println("Choice:");

ans = scan.nextInt();

if (ans == 1)

}

public void deletePlants(ArrayList st) // menu item #7

{

//calls the menuDelete method below

menuDelete();

// write the code to do the item depending on their choice

Scanner scan = new Scanner(System.in);

int choice;

menuDelete();

choice = scan.nextInt();

switch(choice) {

case 1:

String id;

int quality;

System.out.println("Enter plant ID and number of shares to remove");

id = scan.nextLine();

quantity = scan.nextInt();

removeShares(stD, id, quantity);

scan.nextLine();

break;

case 2;

String idAll;

System.out.println("Enter plant ID for which shares are completely removed");

idAll = scan.nextLine();

removeAll(stD, idAll);

break;

case 3: return;

}

}

public void menuAdd() {

//Menu item 1. Ask the user for the id of the plant and how many to add

// make certain to indicate if this id does not exist.

//Menu item 2. ask if they want to add a new plant. If so, ask for the required

// information, create the plant instance, and add it to the collection

//Menu item 3. Go back to the main menu.

System.out.println(" 1. add more plants to existing inventory");

System.out.println("2. add a new plant item (add new id) to inventory");

System.out.println("3. exit to main menu ");

}

public void menuDelete() {

// Menu item 1. Ask the user for the id of the plant and how many shares to remove

// Do not let them remove more than exist in inventory

// If the id does not exist, make certain to indicate this.

//Menu item 2. ask for the plant id and totally delete that plant from the collection

// If the id does not exist, make certain to indicate this.

//Menu item 3. Go back to the main menu.

System.out.println(" 1. delete plants from existing inventory");

System.out.println("2. remove the plant (delete id) from inventory");

System.out.println("3. exit to main menu ");

}

public static void removeShares(ArrayListstD,String idP, int qty) {

boolean found = false;

for(int i = 0; i

Plants temp1 = stD.get(i);

String temp = temp1.getId();

if(temp.equalsIgnoreCase(idP)) {

found = true;

}

}

if(found) {

int newQty;

if(temp1.getNumInStock()

System.out.println("Number of shares present is: "

+ temp1.getNumInStock() + " User provided shares: " + qty + "Cannot remove shares more than what is available");

}else{

newQty = temp1.getNumInStock() - qty;

stD.get(newQty);

}

} else{

System.out.println("Plant with id " + idP + "is not found");

}

}

}

}

}

package ntorresproj120;

import java.util.*;

//***below is PlantDriver****

public class PlantDriver {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

ArrayList plants =new ArrayList();

MenuInfo mi = new MenuInfo();

int ans=0;

while (true)

{

menu();

System.out.println("CHOICE:");

ans = scan.nextInt();

if (ans ==0)

plants = mi.loadPlants();

if (ans==1)

{

// read serializable

plants=mi.readSerializable();

}

if (ans==2)

{

// write serializable

mi.writeSerializable(plants);

}

if (ans == 3)

{

// print all plants

mi.print(plants);

}

if (ans==4)

{

// total value invested and total value of the inventory if sold (print out two values_)

mi.total(plants);

}

if (ans==5)

{

// find a stock by id code

mi.find(plants);

}

if (ans==6)

{

// add a plant or more plants to a current inventory item

mi.addPlants(plants);

}

if (ans==7)

{

// delete some plants or an entire plant entry from the inventory

mi.deletePlants(plants);

}

if (ans==8)

{

System.out.println("See you later!");

System.exit(0);

}

}

}

public static void menu()

{

System.out.println(" 0. prime the data");

System.out.println("1. read a serializable file");

System.out.println("2. write to a serialized file");

System.out.println("3. print all plant information");

System.out.println("4. list the value and the amount invested for the inventory (2 numbers)");

System.out.println("5. find a plant by name or part of name");

System.out.println("6. add plants (existing or new)");

System.out.println("7. delete plants (existing or new)");

System.out.println("8. exit ");

}

}

//***Below is Plants***

package ntorresproj120;

import java.text.NumberFormat;

public class Plants {

//Fields

private String id;

private String description;

private int numInStock;

private double ourCost;

private double salesCost;

public Plants() {

super();

}

public Plants(String id, String desc, int num, double cost,

double value) {

description = desc;

this.id = id;

numInStock = num;

ourCost = cost;

salesCost = value;

}

//toString

public String toString()

{

NumberFormat nf = NumberFormat.getCurrencyInstance();

return "There are " + numInStock + " " + description + " in stock that cost us " +

nf.format(ourCost) + " each that we are selling for "

+ nf.format(salesCost) + " each";

}

//Returns true if ids are equal

public boolean equals(Plants st)

{

if (this.getId() == st.getId())

return true;

else

return false;

}

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public String getDescription() {

return description;

}

public void setDescription(String description) {

this.description = description;

}

public int getNumInStock() {

return numInStock;

}

public void setNumInStock(int numInStock) {

this.numInStock = numInStock;

}

public double getOurCost() {

return ourCost;

}

public void setOurCost(double ourCost) {

this.ourCost = ourCost;

}

public double getSalesCost() {

return salesCost;

}

public void setSalesCost(double salesCost) {

this.salesCost = salesCost;

}

}

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

Logics For Databases And Information Systems

Authors: Jan Chomicki ,Gunter Saake

1st Edition

1461375827, 978-1461375821

More Books

Students also viewed these Databases questions

Question

Evaluate the importance of diversity in the workforce.

Answered: 1 week ago

Question

Identify the legal standards of the recruitment process.

Answered: 1 week ago