package week_9; import java.util.ArrayList; import static…

Question package week_9; import java.util.ArrayList; import static… package week_9; import java.util.ArrayList;import static input.InputUtils.*;public class DBConfig {    /*     * Don’t modify this class.     *     * Your code will use the db_url variable to create a connection to the database.     * You can access this variable from elsewhere in the project with,     *     * DBConfig.db_url     *     * When the tests run, they’ll find this variable and change it to the     * location of the test database. When the tests end, they’ll change it back     * to the original value.     *     * You don’t need to do anything in your code to make this happen,     * except making sure that the products.db and the products_test.db     * databases exist before the code, or the tests, run.     *     * See instructions in the Lab 9 Questions.md file for creating the databases.     *     * */    static String db_url = “jdbc:sqlite:products.db”;    }package week_9;public class Product {    private String name;    private int quantity;    Product(String name, int quantity){        this.name = name;        this.quantity = quantity;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getQuantity() {        return quantity;    }    public void setQuantity(int quantity) {        this.quantity = quantity;    }} package week_9;/** Talk to your database from here. Use the db_url variable in DBConfig to create your database connection This is necessary so the tests can replace your database with a test database. You can access this variable with DBConfig.db_url*/public class ProductDB {    // TODO add methods here to work with the database.        private String name;        private String description;        private double price;        private int quantity;        private String type;        public ProductDB() {            name = “no name yet”;            description = “nothing yet”;            price = 0;            quantity = 0;            type = “no type yet”;        }        public ProductDB(String aName, String descrip, double aPrice,                         int aQuantity, String aType) {            name = aName;            description = descrip;            price = aPrice;            quantity = aQuantity;            type = aType;        }        public String getName() {            return name;        }        public void setName(String aName) {            name = aName;        }        public String getDescription() {            return description;        }        public void setDescription(String aDescription) {            description = aDescription;        }        public double getPrice() {            return price;        }        public void setPrice(double aPrice) {            price = aPrice;        }        public void incrementQuantity(int increment) {            quantity += increment;        }        public void decrementQuantity(int decrement) {            quantity -= decrement;        }        public String getType() {            return type;        }        public void setType(String aType) {            if (aType == “Pet” || aType == “Supply”) {                type = aType;            } else {                System.out.println(“Error”);            }        }        public String toString() {            String result = “”;            result = “Product’s name: ” + name + “nDescription: ” + description                    + “nPrice: ” + price + “nQuantity: ” + quantity + “nType: “+ type;            return result;        }} package week_9;import java.util.ArrayList;import static input.InputUtils.*;class ProductManager {    ProductDB database;    // TODO use this object to access instance methods in the ProductDB class    private final String menu = “” +            “1. Show all productsn” +            “2. Add a productn” +            “3. Edit a product’s quantityn” +            “4. Delete a productn” +            “9. Quit”;    public static void main(String[] args) {        // You do not need to modify this method.        ProductManager productManager = new ProductManager();        productManager.start();    }    public void start() {        // You do not need to modify this method.        database = new ProductDB();        showMenuDoAction();    }    private void showMenuDoAction() {        // You do not need to modify this method.        int choice;        while (true) {            System.out.println(menu);            choice = intInput(“Enter choice”);            if (choice == 1) {                showAll();            } else if (choice == 2) {                addProduct();            } else if (choice == 3) {                editProductQuantity();            } else if (choice == 4) {                deleteProduct();            } else if (choice == 9) {                break;   // end the loop            } else {                System.out.println(“Invalid choice, please try again”);            }        }    }    protected void showAll() {        // TODO write and call method in ProductDB to fetch all products, sorted by product name.        // TODO display all product names and quantities neatly,        //  for example, display the name and quantity of one product per row.        // Call method in ProductDB to fetch all products, sorted by product name.        ArrayList allProducts = database.getAllProducts();        // Display all products        for (DBConfig.Product product : allProducts) {            System.out.printf(“%s: %dn”, product.getName(), product.getQuantity());        }        protected void addProduct() {            // TODO ask user for product name and quantity. Use intInput, not positiveIntInput.            // TODO write and call method in ProductDB to add this product            // TODO Deal with product already existing in DB. Do not add another product with the same name.            //  If a product with this name is already in the database, display this message: “Product already in database”            // Ask user for product name and quantity            String productName = stringInput(“What is the products name? “);            int productQuantity = intInput(“What is the quantity of the product? “);            // Write and call method in ProductDB to add this product            // Deal with product already existing in DB. Do not add another product with the same name.            System.out.println(database.addNewProduct(productName, productQuantity) ?                    “Product added” :                    “Error: Check if product is in inventory already.”);        }        protected void editProductQuantity() {            // TODO ask user for the name of the product to edit, and the new quantity. Use intInput, not positiveIntInput.            // TODO write and call method in ProductDB to edit this product’s quantity.            //  Display confirmation message if product found and quantity edited.            // TODO Deal with product not existing in DB. If a product with the name given does not exist,            //  do not make any changes to the database, and display this message: “Product to edit not found”            String productToEdit = stringInput(“Change quantity of what product? “);            int changeToQuantity = intInput(String.format(“Set quantity of %s to what?”, productToEdit));            System.out.println(database.editProduct(productToEdit, changeToQuantity) ?                    String.format(“Changed quantity of %s to %d”, productToEdit, changeToQuantity) :                    “An error occurred, check if product is in database.”);        }        protected void deleteProduct() {            // TODO ask user for the name of a product to delete            // TODO write and call method in ProductDB to delete this product’s record from the database.            //  Display a confirmation message.            // TODO Deal with product not existing in DB. If a product with the name given does not exist,            //  do not make any changes to the database, and display this message: “Product to delete not found”            String productToDelete = stringInput(“Which product would you like to delete?”);            System.out.println(database.deleteProduct(productToDelete) ?                    “Product deleted” :                    “There was an error: check if the product is in database.”);        }    }} Computer Science Engineering & Technology Java Programming CSCI 1541 Share QuestionEmailCopy link Comments (0)