Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Add appropriate descriptive comments to each line of code in the project explaining why the code is in the application. package murach.ui; import murach.business.Product; import

Add appropriate descriptive comments to each line of code in the project explaining why the code is in the application.

package murach.ui;

import murach.business.Product; import murach.db.ProductTextFile; import murach.db.DAO;

import java.util.List;

public class ProductManagerApp { private static DAO productFile = new ProductTextFile();

public static void main(String args[]) { System.out.println("Welcome to the Product Manager "); displayMenu();

String action = ""; while (!action.equalsIgnoreCase("exit")) { action = Console.getString("Enter a command: "); System.out.println();

if (action.equalsIgnoreCase("list")) { displayAllProducts(); } else if (action.equalsIgnoreCase("add")) { addProduct(); } else if (action.equalsIgnoreCase("del") || action.equalsIgnoreCase("delete")) { deleteProduct(); } else if (action.equalsIgnoreCase("help") || action.equalsIgnoreCase("menu")) { displayMenu(); } else if (action.equalsIgnoreCase("exit") || action.equalsIgnoreCase("quit")) { System.out.println("Bye. "); } else { System.out.println("Error! Not a valid command. "); } } }

public static void displayMenu() { System.out.println("COMMAND MENU"); System.out.println("list - List all products"); System.out.println("add - Add a product"); System.out.println("del - Delete a product"); System.out.println("help - Show this menu"); System.out.println("exit - Exit this application "); }

public static void displayAllProducts() { System.out.println("PRODUCT LIST");

List products = productFile.getAll(); if (products == null) { System.out.println("No products were found. "); } else { StringBuilder sb = new StringBuilder(); for (Product p : products) { sb.append(StringUtils.padWithSpaces( p.getCode(), 8)); sb.append(StringUtils.padWithSpaces( p.getDescription(), 40)); sb.append( p.getPriceFormatted()); sb.append(" "); } System.out.println(sb.toString()); } }

public static void addProduct() { String code = Console.getString("Enter product code: "); String description = Console.getLine("Enter product description: "); double price = Console.getDouble("Enter price: ");

Product product = new Product(); product.setCode(code); product.setDescription(description); product.setPrice(price); boolean success = productFile.add(product); if (success) { System.out.println(description + " has been added. "); } else { System.out.println(description + " was NOT added. "); } }

public static void deleteProduct() { String code = Console.getString("Enter product code to delete: ");

Product p = productFile.get(code); if (p != null) { boolean success = productFile.delete(p); if (success) { System.out.println(p.getDescription() + " has been deleted. "); } else { System.out.println(p.getDescription() + " was NOT deleted. "); } } else { System.out.println("No product matches that code. "); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

1 2 3 Data Base Techniques

Authors: Dick Andersen

1st Edition

0880223464, 978-0880223461

More Books

Students also viewed these Databases questions