I want to search for a certain plant id number, and if that id number is found a
ID: 3713352 • Letter: I
Question
I want to search for a certain plant id number, and if that id number is found add, however any more plants that id is associated with to the inventory. So I want to add whatever the user inputs into an existing array list. Here is the code that I started:
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;
}
public static void idAddPlant(ArrayList st, String num) {
// look for a certain policy
Scanner scan = new Scanner(System.in);
boolean found = false;
int i = 0;
// note a while loop since we want to stop once we find them
// each one should be present only once!
while (!found && i < st.size()) { // two ways to end - we find it or we get to the end
Plants plants = st.get(i);
String idNum = plants.getId();
if (idNum == num) { // found it!!
found = true; // set found to true to end the loop
System.out.println("How many plants do you want to add to the inventory?");
int addInv = scan.nextInt();
// print it out
} else
i++; // look at the next one
}
if (!found) // check to see if it ever was found
System.out.println("*** That policy does not exist. ***");
Explanation / Answer
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Plants {
private String id;
private String description;
private int numInStock;
private double ourCost;
private double salesCost;
public static List<Plants> items = new ArrayList<Plants>();
public Plants(String id, String description, int numInStock, double ourCost, double salesCost) {
super();
this.id = id;
this.description = description;
this.numInStock = numInStock;
this.ourCost = ourCost;
this.salesCost = salesCost;
}
public static void main(String[] args) {
Plants p = new Plants("1", "abc", 12, 19.00, 20.00);
items.add(p);
boolean found = false;
Scanner reader = new Scanner(System.in);
System.out.println("Enter the plantid you want to search:");
String pid = reader.nextLine();
for (int i = 0; i < items.size(); i++) {
if (pid == items.get(i).id.toString()) {
found = true;
break;
}
}
if (found == false) {
System.out.println("Enter the inventory size you want to add:");
int arrSize = reader.nextInt();
for (int i = 0; i < arrSize; i++) {
System.out.println("Enter inventory of " + i + ":");
String plantId = reader.nextLine();
String plantDesc = reader.nextLine();
int numInStock = reader.nextInt();
double ourCost = reader.nextDouble();
double saleCost = reader.nextDouble();
Plants invPlant = new Plants(plantId, plantDesc, numInStock, ourCost, saleCost);
items.add(invPlant);
}
}
reader.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.