Could someone look over the code I have written and make sure it is all correct?
ID: 3718154 • Letter: C
Question
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<Plants> loadPlants()
{
ArrayList<Plants> plants = new ArrayList<Plants>();
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<Plants> readSerializable() // menu item #1
{
// create the ArrayList
ArrayList<Plants> st = new ArrayList<Plants>();
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<Plants> 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<Plants> pl) // menu item #3
{
System.out.println("Current plants ininventory:");
for(int i=0;i<pl.size();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 static void total(ArrayList<Plants> 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<Plants> 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<Plants> 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<Plants> 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.size(); i++){
if(pid==st.get(i).getId().toString()){
found = true;
break;
}
}
if (found ==false){
System.out.println("Enter the inventory size you want to add:");
int arrSize = scan.nextInt();
for(int j=0;j<arrSize;j++){
System.out.println("Enter inventory of" + i + ":");
String plantId = scan.nextLine();
String plantDesc = scan.nextLine();
int numInStock = scan.nextInt();
double ourCost = scan.nextDouble();
double saleCost = scan.nextDouble();
Plants invPlant = new Plants(plantId, plantDesc, numInStock,ourCost, saleCost);
st.add(invPlant);
System.out.println(st.get(i).toString());
}
}
scan.close();
}
public static void fullAddPlant (ArrayList<Plants> st){
Scanner scan = new Scanner(System.in);
boolean more = true;
//int counter = 0;
while (more) {
int arrSize = scan.nextInt();
for(int j=0;j<arrSize;j++){
System.out.println("Enter the plant identification code:");
String plantId = scan.nextLine();
System.out.println("Enter the plant description:");
String plantDesc = scan.nextLine();
System.out.println("Enter the number in stock:");
int numInStock = scan.nextInt();
System.out.println("Enter the cost of the plant:");
double ourCost = scan.nextDouble();
System.out.println("Enter the sales price of the plant:");
double saleCost = scan.nextDouble();
Plants fullPlant = new Plants(plantId, plantDesc, numInStock,ourCost, saleCost);
st.add(fullPlant);
boolean inputIsInvalid = true;
while (inputIsInvalid) {
System.out.println("more plants? (y/n): ");
String choice = scan.next();
if ("y".equalsIgnoreCase(choice))inputIsInvalid = false;
else if ("n".equalsIgnoreCase(choice)){inputIsInvalid = false;
more = false;}
else System.err.print("Error: Only valid answers are Y/N.");
}
scan.nextLine();
}
System.out.println("The new plants are:");
for(int i = 0; i < st.size(); i++){
System.out.println(st.get(i).toString());
}
}
scan.close();
}
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 deletePlants(ArrayList<Plants> 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<Plants> stD, String idP, int qty){
boolean found = false;
for(int i=0; i<stD.size() && !(found) ;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<Plants> stD, String idP){
boolean found = false;
for(int i=0; i<stD.size() && !(found) ;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");
}
}
}
}
Explanation / Answer
Hello there,
As i copied your code and tried to run on eclipse on my system, it was giving error for some methods and constructor from Plants class, so you have to write all these methods and constructor, if you did not; otherwise your code is fine.
And one more thing, if you want to run the code from this MenuInfo.java class then write main() also in this class.
I am mentioning all the methods, which you should write in Plants class:
getOurCost(), getSalesCost(), getId(), getDescription(), getNumInStock().
So, just write all the required methods and run your code.
Hope, you will get the output and got the clarification, feel free to ask any doubt.
Thank you