Reputation: 1
I am stuck on this. I have user input for some information on some products, and I make an if else if statement to declare which type of product it is after the user has inputted. Only issue is that I want to recall the object that is created. It will either be created as drill, wrench, or shovel. But how do I store this information so I can recall it, e.g I want to output the items price, id, type, etc?
System.out.println("Welcome to our hardware shop. We have three items in stock: \n" + "Wrench CMMT12001\n"
+ "Power Drill 12v\n"
+ "Shovel D-Handle\n"
+ "Which product would you like to buy?");
product = keyboard.nextLine();
System.out.println("How many items of this product would you like?");
items = keyboard.nextInt();
System.out.println("What is the date (00/00/0000) ");
date = keyboard.nextLine();
int dayOfYear = dayOfYear(date);
if (product.equals("Wrench CMMT12001")) {
Products wrench = new Products(productXID, product, productXPrice, "X", items, dayOfYear);
}
else if (product.equals("Power Drill 12v")) {
Products drill = new Products(productYID, product, productYPrice, "Y", items, dayOfYear);
}
else if (product.equals("Shovel D-Handle")) {
Products shovel = new Products(productZID, product, productZPrice, "Z", items, dayOfYear);
}
else
System.out.println("You did not enter a product.");
Upvotes: 0
Views: 92
Reputation: 331
Save the product outside of the IF query:
System.out.println("Welcome to our hardware shop. We have three items in stock: \n" + "Wrench CMMT12001\n"
+ "Power Drill 12v\n"
+ "Shovel D-Handle\n"
+ "Which product would you like to buy?");
product = keyboard.nextLine();
System.out.println("How many items of this product would you like?");
items = keyboard.nextInt();
System.out.println("What is the date (00/00/0000) ");
date = keyboard.nextLine();
int dayOfYear = dayOfYear(date);
Products selected;
if (product.equals("Wrench CMMT12001")) {
selected = new Products(productXID, product, productXPrice, "X", items, dayOfYear);
}
else if (product.equals("Power Drill 12v")) {
selected = new Products(productYID, product, productYPrice, "Y", items, dayOfYear);
}
else if (product.equals("Shovel D-Handle")) {
selected = new Products(productZID, product, productZPrice, "Z", items, dayOfYear);
}
else
System.out.println("You did not enter a product.");
}
Upvotes: 1