Answer to Question #238871 in Java | JSP | JSF for Princess

Question #238871
Q.1.12
Finally, provide the ability for the user to exit the application.

Additional Requirements:

1.

In your solution, you must create a class called Products, which will contain all your working

This class will, as a minimum, contain the following methods, but you are encouraged to

add more methods:

18. 20:21

Assessment Sheet (Markim

PARENT MODULE NAM

CHILD MODULE NAME

STUDENT NUMBER:

Question 1 Mark All

3.

STUDENT NAME:

MODULE NAME:

Appendix A

Finally, create a main class to run your application.

2

methods.

SearchProduct();

SaveProduct():

UpdateProduct();

DeleteProduct();

DisplayMenu();

CaptureProduct();

ExitApplication();
1
Expert's answer
2021-09-18T11:26:37-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 Scanner keyboard = new Scanner(System.in);
	private static ArrayList<Product> products = new ArrayList<Product>();


	public static void main(String[] args) {


		int choice = -1;


		while (choice != 5) {
			choice = DisplayMenu();
			keyboard.nextLine();
			if (choice == 1) {
				SaveProduct();
			} else if (choice == 2) {
				UpdateProduct();
			} else if (choice == 3) {
				DeleteProduct();
			} else if (choice == 4) {
				CaptureProduct();
			} else if (choice == 5) {
				ExitApplication();
			} else {
				System.out.println("Wrong menu item.");
			}
		}
		keyboard.close();


	}


	private static int SearchProduct(String code) {
		for (int i = 0; i < products.size(); i++) {
			if (products.get(i).getCode().compareTo(code) == 0) {
				return i;
			}
		}
		return -1;
	}


	private static void SaveProduct() {
		String code;
		String warranty;
		float price;
		int stockLevel;
		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));
	}


	private static void UpdateProduct() {


		String code;
		String warranty;
		float price;
		int stockLevel;
		int ch = -1;
		while (ch != 4) {
			System.out.println("1. Update the product warranty.");
			System.out.println("2. Update the product price.");
			System.out.println("3. Update the product stock level");
			System.out.println("4. Exit to main menu");
			System.out.print("Your choice: ");
			ch = keyboard.nextInt();
			keyboard.nextLine();
			if (ch == 1) {
				System.out.print("Enter code to update: ");
				code = keyboard.nextLine();
				int index = SearchProduct(code);
				if (index != -1) {
					System.out.print("Enter a new warranty: ");
					warranty = keyboard.nextLine();
					products.get(index).setWarranty(warranty);
				} else {
					System.out.println("Wrong code.");
				}
			} else if (ch == 2) {
				System.out.print("Enter code to update: ");
				code = keyboard.nextLine();
				int index = SearchProduct(code);
				if (index != -1) {
					System.out.print("Enter price: ");
					price = keyboard.nextFloat();
					products.get(index).setPrice(price);
				} else {
					System.out.println("Wrong code.");
				}
			} else if (ch == 3) {
				System.out.print("Enter code to update: ");
				code = keyboard.nextLine();
				int index = SearchProduct(code);
				if (index != -1) {
					System.out.print("Enter stock level: ");
					stockLevel = keyboard.nextInt();
					products.get(index).setStockLevel(stockLevel);
				} else {
					System.out.println("Wrong code.");
				}
			}
		}
	}


	private static void DeleteProduct() {
		System.out.print("Enter the product code to be deleted: ");
		String code = keyboard.nextLine();
		int index = SearchProduct(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.");
		}
	}


	private static int DisplayMenu() {
		System.out.println("1. Save a product.");
		System.out.println("2. Update the product.");
		System.out.println("3. Delete the product.");
		System.out.println("4. Display all products");
		System.out.println("5. Exit");
		System.out.print("Your choice: ");
		return keyboard.nextInt();
	}


	private static void CaptureProduct() {
		for (int i = 0; i < products.size(); i++) {
			System.out.println(products.get(i).toString());
		}
	}


	private static void ExitApplication() {
		System.out.println("Good bye");
	}
}

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
New on Blog
APPROVED BY CLIENTS