Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

//java public class DBConfig { static String db_url = jdbc:mysql://localhost:3306/products ; static String user = System. getenv ( MYSQL_USER ); // TODO set this environment

//java

public class DBConfig { static String db_url = "jdbc:mysql://localhost:3306/products"; static String user = System.getenv("MYSQL_USER"); // TODO set this environment variable  static String password = System.getenv("MYSQL_PASSWORD"); // TODO set this environment variable  } 

=================================================================================================================================

 // Talk to your database from here. // // Use the variables in DBConfig to create your database connection // // This is necessary so the tests can replace your database with a test database. */  public class ProductDB { } 

========================================================================================

import static input.InputUtils.positiveIntInput; class ProductInventory { ProductDB database; private String menu = "" + "1. Show all products " + "2. Add a product " + "3. Edit a product " + "4. Delete a product " + "9. Quit"; public static void main(String[] args) { ProductInventory productInventoryProgram = new ProductInventory(); productInventoryProgram.start(); } public void start() { database = new ProductDB(); showMenuDoAction(); } private void showMenuDoAction() { int choice; do { System.out.println(menu); choice = positiveIntInput("Enter choice"); switch (choice) { case 1: showAll(); break; case 2: addProduct(); break; case 3: editProduct(); break; case 4: deleteProduct(); break; case 9: break; default: System.out.println("Invalid choice, please try again"); } } while (choice != 9); } protected void showAll() { // TODO write and call method in ProductDB to delete this product  // TODO display all products  } protected void addProduct() { // TODO ask user for product name and quantity  // TODO write and call method in ProductDB to add this product  // TODO Deal with product already existing in DB  } protected void editProduct() { // TODO ask user which product to edit  // TODO write and call method in ProductDB to edit this product  // TODO Deal with product not existing in DB  } protected void deleteProduct() { // TODO ask user which product to delete  // TODO write and call method in ProductDB to delete this product  // TODO Deal with product not existing in DB  } } 

===========================================================================================================================

//instruction

Command Line Database Programs: Product Inventory Manager

 Write an GUI application to manage a database of inventory items and the quantity of each item. The user will be able to add a new item, change the quantity of any item, and delete an item. ### Database setup:  You will need to create a database called **products**. You will also need to create a test database called **products_test**  ``` create database products; create database products_test; ```  The products database should have a table called **inventory**. The products_test database should also have a table called **inventory**. The inventory table (in both databases) should have two columns, * **product_name** for the name of a product * **quantity** an integer number, for the quantity of that product in stock For example, the inventory table may look like this, with some example data: ``` product_name quantity  CPU 250 Memory 100 Video Card 93  ```  Create both tables - one for each database - in the SQL shell. Add the following permissions to your user, for both databases's inventory table. Replace YOURUSER with your username. ``` grant update, select, insert, delete on products.inventory to 'YOURUSER'@'localhost'; grant update, select, insert, delete on products_test.inventory to 'YOURUSER'@'localhost'; ```  Set two environment variables called `MYSQL_USER` and `MYSQL_PASSWORD` to hold your MySQL username and password, respectively. In the run configuration for running ProductInventory **and** ProductInventoryTest, set environment variables `MYSQL_USER` and `MYSQL_PASSWORD` to hold your user and password, respectively. If you run the code and run the tests, a run configuration will be created for each, and you can edit both to include the environment variables. ### Database setup, code  Verify the DB connection URL is correct in DBConfig. For all parts of this lab, make sure you add appropriate error handling, and close all resources (ResultSets, PreparedStatements, Statements, Connections) when your program is done with them. Use PreparedStatements for updates, add, delete operations. ### User Interface setup  Create a command-line interface to view, edit, add to, and delete, this data. The ProductDB program already shows a menu and calls a different method for each of the user's choices. All of your database interaction should be in ProductDB. Write methods in this class to read/write/edit/delete data in the database. For example, in ProductInventory showAll(), you'll have code that looks something like this,  ```  protected void showAll() {  Object alldata = database.getAllData(); // write getAllData in ProductDB  // Replace Object alldata with whatever data type getAllData returns, for example a list of items?  System.out.println(alldata); // replace with something more user-friendly  }  ```   Your other methods, `addProduct()` `editProduct()` and `deleteProduct()` will also call methods in ProductDB to do the appropriate tasks. **Showing all Products, showAll()**   Call your new method in ProductDB to get all of the product data. Sort your data by name, in alphabetical order. Display product names followed by quantity. Do not return a ResultSet from ProductDB. Return a list, or something that showAll can easily use to display all products. `showAll` should not use any user input. **Adding a Product, addProduct()**   Ask the user for the name, and the quantity. Use stringInput and intInput. Don't use any other input statements. Call your new method in ProductDB to add a product with this information. If the user enters a name that already exists in the database, display an error message and show the main menu again. Don't ask the user to try again from addProduct. Don't modify the database. **Editing a Product, editProduct()**   Ask the user for the name, and the new quantity. Use stringInput and intInput. Don't use any other input statements. Call your new method in ProductDB to edit the product with this name, and update the quantity. If the user enters a name that does not exist in the database, display an error message and show the main menu again. Don't ask the user to try again from editProduct. Don't modify the database. **Deleting a Product, deleteProduct()**   Ask the user for the name. Use stringInput. Don't use any other input statements. Call your new method in ProductDB to delete the product with this name. If the user enters a name that does not exist in the database, display an error message and show the main menu again. Don't ask the user to try again from deleteProduct. Don't modify the database. 

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

Learning MySQL Get A Handle On Your Data

Authors: Seyed M M Tahaghoghi

1st Edition

0596529465, 9780596529468

More Books

Students also viewed these Databases questions

Question

=+How can I use it in a new way?

Answered: 1 week ago

Question

=+2. Do they use a similar tone of voice and point of view?

Answered: 1 week ago