Answer to Question #237190 in Java | JSP | JSF for Nemo

Question #237190

The user must also have the ability to update specific details of the product. For example, the user must first enter the product code and then confirm whether to update the following product details:

•Update the product warranty.

•Update the product price.

•Update the product stock level


1
Expert's answer
2021-09-14T18:03:10-0400
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 in = new Scanner(System.in);
		ArrayList<Product> products = new ArrayList<Product>();
		int ch = -1;
		String code;
		String warranty;
		float price;
		int stockLevel;
		while (ch != 6) {
			System.out.println("1. Add a new product.");
			System.out.println("2. Update the product warranty.");
			System.out.println("3. Update the product price.");
			System.out.println("4. Update the product stock level");
			System.out.println("5. Display all products");
			System.out.println("6. Exit");
			System.out.print("Your choice: ");
			ch = in.nextInt();
			in.nextLine();
			if (ch == 1) {
				System.out.print("Enter code: ");
				code = in.nextLine();
				System.out.print("Enter warranty: ");
				warranty = in.nextLine();
				System.out.print("Enter price: ");
				price = in.nextFloat();
				System.out.print("Enter stock level: ");
				stockLevel = in.nextInt();
				products.add(new Product(code, warranty, price, stockLevel));
			} else if (ch == 2) {
				System.out.print("Enter code to update: ");
				code = in.nextLine();
				int index = getProductIndex(products, code);
				if (index != -1) {
					System.out.print("Enter a new warranty: ");
					warranty = in.nextLine();
					products.get(index).setWarranty(warranty);
				} else {
					System.out.println("Wrong code.");
				}
			} else if (ch == 3) {
				System.out.print("Enter code to update: ");
				code = in.nextLine();
				int index = getProductIndex(products, code);
				if (index != -1) {
					System.out.print("Enter price: ");
					price = in.nextFloat();
					products.get(index).setPrice(price);
				} else {
					System.out.println("Wrong code.");
				}
			} else if (ch == 4) {
				System.out.print("Enter code to update: ");
				code = in.nextLine();
				int index = getProductIndex(products, code);
				if (index != -1) {
					System.out.print("Enter stock level: ");
					stockLevel = in.nextInt();
					products.get(index).setStockLevel(stockLevel);
				} else {
					System.out.println("Wrong code.");
				}
			} else if (ch == 5) {
				for (int i = 0; i < products.size(); i++) {
					System.out.println(products.get(i).toString());
				}
			} else if (ch == 6) {
				// exit
			} else {
				System.out.println("Wrong menu item.");
			}
		}
		in.close();
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS