Hello, I have a program that is supposed to take in a product\'s (clothes or foo
ID: 3747517 • Letter: H
Question
Hello, I have a program that is supposed to take in a product's (clothes or food) information as a string and output its info broken down into separate categories (color, price, etc). It is also supposed to be able to seach for a product based on its ID and output either "product found" or "product not found" and everything is working except for that part. I know that the issue is in my diver file in case S (shown below in bold). How would I correct it so that when a product id is entered it returns either product found or product not found? Thanks!
import java.io.*;
import java.util.*;
public class Driver
{
public static void main (String[] args)
{
char input1;
String inputInfo = new String();
String line = new String();
boolean found;
// ArrayList object is used to store drink objects
ArrayList<Product> productList = new ArrayList<Product>();
try
{
printMenu(); // print out menu
// create a BufferedReader object to read input from a keyboard
InputStreamReader isr = new InputStreamReader (System.in);
BufferedReader stdin = new BufferedReader (isr);
do
{
System.out.print("What action would you like to perform? ");
line = stdin.readLine().trim();
input1 = line.charAt(0);
input1 = Character.toUpperCase(input1);
if (line.length() == 1)
{
switch (input1)
{
case 'A': //Add Product
System.out.print("Please enter a product information to add: ");
inputInfo = stdin.readLine().trim();
Product product = ProductParser.parseStringToProduct(inputInfo);
productList.add(product);
double totalProductCost = 0;
for(Product prod : productList)
{
prod.computeTotalCost();
totalProductCost += prod.totalCost;
}
System.out.println("total costs computed");
break;
case 'S': //Search for a product
System.out.print("Please enter a productId to search: ");
inputInfo = stdin.readLine().trim();
/***********************************************************************************
*** Search a given productId. If found, set "found" true,
*** and set "found" false otherwise.
***********************************************************************************/
String productId = inputInfo;
found = true;
for(Product prod : productList)
{
if(inputInfo.equals(prod.getProductId()))
{
found = true;
break;
}
else if(!inputInfo.equals(prod.getProductId()))
{
found = false;
break;
}
}
if(found==true)
{
System.out.print("product found ");
}
else
System.out.print("product not found ");
break;
case 'L': //List Products
if(productList.size() == 0)
{
System.out.println("No Product ");
}
else
{
for(Product prod : productList)
{
System.out.println(prod.toString());
}
}
break;
case 'Q': //Quit
break;
case '?': //Display Menu
printMenu();
break;
default:
System.out.print("Unknown action ");
break;
}
}
else
{
System.out.print("Unknown action ");
}
} while (input1 != 'Q'); // stop the loop when Q is read
}
catch (IOException exception)
{
System.out.println("IO Exception");
}
}
/** The method printMenu displays the menu to a use **/
public static void printMenu()
{
System.out.print("Choice Action " +
"------ ------ " +
"A Add Product " +
"C Compute Total Costs " +
"S Search for Product " +
"L List Products " +
"Q Quit " +
"? Display Help ");
}
}
Explanation / Answer
If you have any doubts, please give me comment...
import java.io.*;
import java.util.*;
public class Driver {
public static void main(String[] args) {
char input1;
String inputInfo = new String();
String line = new String();
boolean found;
// ArrayList object is used to store drink objects
ArrayList<Product> productList = new ArrayList<Product>();
try {
printMenu(); // print out menu
// create a BufferedReader object to read input from a keyboard
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader stdin = new BufferedReader(isr);
do {
System.out.print("What action would you like to perform? ");
line = stdin.readLine().trim();
input1 = line.charAt(0);
input1 = Character.toUpperCase(input1);
if (line.length() == 1) {
switch (input1) {
case 'A': // Add Product
System.out.print("Please enter a product information to add: ");
inputInfo = stdin.readLine().trim();
Product product = ProductParser.parseStringToProduct(inputInfo);
productList.add(product);
double totalProductCost = 0;
for (Product prod : productList) {
prod.computeTotalCost();
totalProductCost += prod.totalCost;
}
System.out.println("total costs computed");
break;
case 'S': // Search for a product
System.out.print("Please enter a productId to search: ");
inputInfo = stdin.readLine().trim();
/***********************************************************************************
*** Search a given productId. If found, set "found" true, and set "found" false
* otherwise.
***********************************************************************************/
String productId = inputInfo;
found = false;
for (Product prod : productList) {
if (inputInfo.equals(prod.getProductId())) {
found = true;
break;
}
}
if (found) {
System.out.print("product found ");
} else
System.out.print("product not found ");
break;
case 'L': // List Products
if (productList.size() == 0) {
System.out.println("No Product ");
}
else {
for (Product prod : productList) {
System.out.println(prod.toString());
}
}
break;
case 'Q': // Quit
break;
case '?': // Display Menu
printMenu();
break;
default:
System.out.print("Unknown action ");
break;
}
} else {
System.out.print("Unknown action ");
}
} while (input1 != 'Q'); // stop the loop when Q is read
} catch (IOException exception) {
System.out.println("IO Exception");
}
}
/** The method printMenu displays the menu to a use **/
public static void printMenu() {
System.out.print("Choice Action " + "------ ------ " + "A Add Product "
+ "C Compute Total Costs " + "S Search for Product " + "L List Products " + "Q Quit "
+ "? Display Help ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.