Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Could someone look over the code I have written and make sure it is all correct? Thanks! package bfullerProject; import java.awt.FileDialog; import java.awt.Frame; import java.io.File;

Could someone look over the code I have written and make sure it is all correct?

Thanks!

package bfullerProject;

import java.awt.FileDialog; import java.awt.Frame; import java.io.File; 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 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 { // create the ArrayList ArrayList st = new ArrayList(); Frame f = new Frame(); //decide from where to read the file FileDialog foBox = new FileDialog(f,"Reading serialized file", FileDialog.LOAD); foBox.setVisible(true); //get the absolute path to the file String foName = foBox.getFile(); String dirPath = foBox.getDirectory(); File inFile = new File(dirPath + foName); // create a file instance for the absolute path ObjectInputStream OIS=null; try { FileInputStream IS = new FileInputStream(inFile); // create a file input stream for the file that we chose OIS = new ObjectInputStream(IS); // create the object imput stream st = (ArrayList) OIS.readObject(); // note that you can read in the entire object (the array list) at once // now read in that extra piece of data that we wrote out and set the next customer number to use //OIS.readObject(st.size()); //Customer.num=OIS.readInt(); } // catch any IOException that occurs catch (IOException io) { io.printStackTrace(); // great for debugging! System.out.println("An IO Exception occurred"); } // note that you can also have a class not found exception. catch (ClassNotFoundException cnf) { cnf.printStackTrace(); // great for debugging! System.out.println("An IO Exception occurred"); } finally // finally always runs no matter what so close the file here! { // close the file. Java is neurotic - it worried "but what if it is already closed?" so needs another try/catch try{ OIS.close(); } catch (Exception e) {} // note the {} - means "do nothing". I wanted it closed anyway. } return st; } public void writeSerializable(ArrayList st ) // menu item #2 { // create the frame for the FileDialog box Frame f = new Frame(); //decide where to save the file FileDialog foBox = new FileDialog(f,"Saving plants file", FileDialog.SAVE); foBox.setVisible(true); // we need to get the path where the file will be stored. // the user will pick one from the dialog box // the combination of the directory name plus the file name is the absolute path String foName = foBox.getFile(); String dirPath = foBox.getDirectory(); // create a File instance for that absolute path File outFile = new File(dirPath+foName); // create a PrintWriter FileOutputStream OS=null; ObjectOutputStream OOS=null; try { // create the FileOutputStream object OS = new FileOutputStream(outFile); // create the ObjectOutputStream object OOS = new ObjectOutputStream(OS); // create your output string OOS.writeObject(st); // write the entire array list out at once. COOL!!! // we also want to save the next customer number // now print the next customer number that should be used to the file also // need to keep track of this so that when you read the data back in, you do not reuse // customer numbers //OOS.writeInt(Plants.num); OOS.writeInt(st.size()); } // catch any IOExceptions that occur catch (IOException io) { io.printStackTrace(); // shows any errors System.out.println("An IO Exception occurred"); } finally // finally always runs no matter what so close the file here! { // close the file. Java is neurotic - it worried "but what if it is already closed?" so needs another try/catch try{ OOS.close(); } catch (Exception e) {} // note the {} - means "do nothing". I wanted it closed anyway. } } public void print(ArrayList pl) // menu item #3 { System.out.println("Current plants ininventory:"); for(int i=0;i st) // menu item #4 { //total money invested double totalCost = 0; double totalSales = 0; for(int i = 0; i < st.size(); i++){ totalCost += st.get(i).getOurCost(); totalSales += st.get(i).getSalesCost(); } //print the information System.out.println( "The 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:"); String pName = scan.nextLine(); int i = 0; while (!found && i < st.size()){ if(pName==st.get(i).getDescription().toString()){ found = true; break; } else { i++; // look at the next one }

if (!found) // check to see if it ever was found System.out.println("*** 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) { // Ask the user for the id of the plant and how many to add // make certain to indicate if this id does not exist. System.out.println("Now let's find the information for a certain plant"); System.out.println("What id number is associated with that plant?"); String num = scan.nextLine(); idAddPlant(st, num); System.out.println(); } if (ans==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; fullAddPlant(st); } if (ans == 3) { // print all plants toString(); } } } public static void idAddPlant(ArrayList st, String num) { // look for a certain policy Scanner scan = new Scanner(System.in); boolean found = false; System.out.println("Enter the plant ID you want to search:"); String pid = scan.nextLine(); int i = 0; for (i=0; i st){ Scanner scan = new Scanner(System.in); boolean more = true; //int counter = 0; while (more) { int arrSize = scan.nextInt(); for(int j=0;j

public void deletePlants(ArrayList stD){ Scanner scan = new Scanner(System.in);

int choice;

menuDelete();

choice = scan.nextInt();

switch(choice){

case 1:

String id; int quantity;

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 static 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(ArrayList stD, String idP, int qty){

boolean found = false;

for(int i=0; i

// Plants stD = stD.getId(); //String temp;// = stD.getId(); Plants temp1 = stD.get(i); String temp = temp1.getId();

if(temp.equalsIgnoreCase(idP)){

found = true; // to exit the loop when it's found

}

if (found){ int newQty;

if(temp1.getNumInStock() < qty){

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");

} }

}

//Ask for the plant id and totally delete that plant from the collection:

public static void removeAll(ArrayList stD, String idP){

boolean found = false;

for(int i=0; i

Plants temp1 = stD.get(i); String temp = temp1.getId();

if(temp.equalsIgnoreCase(idP)){

found = true; // to exit the loop when it's found

}

if (found){

stD.remove(i); // Remove entry at index i where idP is found

}

else {

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

} }

} }

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 2 Lnai 9285

Authors: Annalisa Appice ,Pedro Pereira Rodrigues ,Vitor Santos Costa ,Joao Gama ,Alipio Jorge ,Carlos Soares

1st Edition

3319235249, 978-3319235240

More Books

Students also viewed these Databases questions