public class Product {
private String name;
public Product(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class Products {
private ArrayList<Product> products;
public Products() {
products = new ArrayList<>();
}
public Product searchProduct() {
System.out.println("Enter the name of the product:");
String name = new Scanner(System.in).nextLine();
for (Product product : products) {
if (product.getName().equals(name)) {
return product;
}
}
return null;
}
public void saveProduct(Product product) {
products.add(product);
}
public void updateProduct() {
Product product = searchProduct();
if (product == null) {
System.out.println("Not found");
return;
}
System.out.println("Enter new name:");
product.setName(new Scanner(System.in).nextLine());
}
public void deleteProduct() {
Product product = searchProduct();
if (product == null) {
System.out.println("Not found");
return;
}
products.remove(product);
}
public void displayMenu() {
System.out.println("1. Search product\n" +
"2. Save product\n" +
"3. Update product\n" +
"4. Delete product\n"
+ "5. Capture product\n"
+ "6. Exit");
}
public Product captureProduct() {
System.out.println("Enter the name of the product:");
return new Product(new Scanner(System.in).nextLine());
}
public void exitApplication() {
System.exit(0);
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Products products = new Products();
Scanner in = new Scanner(System.in);
String choice;
Product product = null;
while (true) {
products.displayMenu();
choice = in.nextLine();
switch (choice) {
case "1":
product = products.searchProduct();
if (product != null) {
System.out.println(product.getName());
product = null;
}else{
System.out.println("Not found");
}
break;
case "2":
if (product != null) {
products.saveProduct(product);
product = null;
} else {
System.out.println("Capture product.");
}
break;
case "3":
products.updateProduct();
break;
case "4":
products.deleteProduct();
break;
case "5":
product = products.captureProduct();
break;
case "6":
products.exitApplication();
break;
}
}
}
}
Comments
Leave a comment