The user must have the option to delete a product that has been saved. The user must first enter the product code to be deleted. Next, the user must confirm whether they want to delete the product.
import java.util.ArrayList;
import java.util.Scanner;
class Product {
private String code;
private String warranty;
private float price;
private int stockLevel;
public Product(String code, String warranty, float price, int stockLevel) {
this.code = code;
this.warranty = warranty;
this.price = price;
this.stockLevel = stockLevel;
}
public String toString() {
return "Code: " + code + "\n" + "Warranty: " + warranty + "\n" + "Price: " + price + "\n" + "Stock level: "
+ stockLevel + "\n";
}
/**
* @return the warranty
*/
public String getWarranty() {
return warranty;
}
/**
* @param warranty the warranty to set
*/
public void setWarranty(String warranty) {
this.warranty = warranty;
}
/**
* @return the price
*/
public float getPrice() {
return price;
}
/**
* @param price the price to set
*/
public void setPrice(float price) {
this.price = price;
}
/**
* @return the stockLevel
*/
public int getStockLevel() {
return stockLevel;
}
/**
* @param stockLevel the stockLevel to set
*/
public void setStockLevel(int stockLevel) {
this.stockLevel = stockLevel;
}
/**
* @return the code
*/
public String getCode() {
return code;
}
/**
* @param code the code to set
*/
public void setCode(String code) {
this.code = code;
}
}
public class Main {
private static int getProductIndex(ArrayList<Product> products, String code) {
for (int i = 0; i < products.size(); i++) {
if (products.get(i).getCode().compareTo(code) == 0) {
return i;
}
}
return -1;
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
ArrayList<Product> products = new ArrayList<Product>();
int choice = -1;
String code;
String warranty;
float price;
int stockLevel;
while (choice != 4) {
System.out.println("1. Add a new product.");
System.out.println("2. Delete the product.");
System.out.println("3. Display all products");
System.out.println("4. Exit");
System.out.print("Your choice: ");
choice = keyboard.nextInt();
keyboard.nextLine();
if (choice == 1) {
System.out.print("Enter code: ");
code = keyboard.nextLine();
System.out.print("Enter warranty: ");
warranty = keyboard.nextLine();
System.out.print("Enter price: ");
price = keyboard.nextFloat();
System.out.print("Enter stock level: ");
stockLevel = keyboard.nextInt();
products.add(new Product(code, warranty, price, stockLevel));
} else if (choice == 2) {
System.out.print("Enter the product code to be deleted: ");
code = keyboard.nextLine();
int index = getProductIndex(products, code);
if (index != -1) {
String answer = "";
while (answer.compareToIgnoreCase("y") != 0 && answer.compareToIgnoreCase("n") != 0) {
System.out.print("Are you sure you want to delete the product? y/n: ");
answer = keyboard.nextLine();
}
if (answer.compareToIgnoreCase("y") == 0) {
products.remove(index);
System.out.println("\nThe product has been deleted.\n");
}
} else {
System.out.println("Wrong code.");
}
} else if (choice == 3) {
for (int i = 0; i < products.size(); i++) {
System.out.println(products.get(i).toString());
}
} else if (choice == 4) {
// exit
} else {
System.out.println("Wrong menu item.");
}
}
keyboard.close();
}
}
Comments
Leave a comment