2. this.price = price;
}
public String getPriceFormatted() {
NumberFormat currencyFormatter =
NumberFormat.getCurrencyInstance();
return currencyFormatter.format(getPrice());
}
}
********DBException************
package crows.foot;
/*
* This is just a wrapper class so we can throw a common exception for
* the UI to catch without tightly coupling the UI to the database layer.
*/
public class DBException extends Exception {
DBException() {}
DBException(Exception e) {
super(e);
}
}
*******DBUtil************
package crows.foot;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBUtil {
private static Connection connection;
private DBUtil() {}
public static synchronized Connection getConnection() throws DBException {
if (connection != null) {
return connection;
}
3. else {
try {
// set the db url, username, and password
String url = "jdbc:mysql://localhost:3306/mma";
String username = "mma_user";
String password = "sesame";
// get and return connection
connection = DriverManager.getConnection(
url, username, password);
return connection;
} catch (SQLException e) {
throw new DBException(e);
}
}
}
public static synchronized void closeConnection() throws DBException {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
throw new DBException(e);
} finally {
connection = null;
}
}
}
}
*************ProductDB**************
package crows.foot;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
4. import rooster.nest.Product;
public class ProductDB {
private static Product getProductFromRow(ResultSet rs) throws SQLException {
int productID = rs.getInt(1);
String code = rs.getString(2);
String description = rs.getString(3);
double price = rs.getDouble(4);
String note = rs.getString(5);
Product p = new Product();
p.setId(productID);
p.setCode(code);
p.setDescription(description);
p.setPrice(price);
return p;
}
public static List getAll() throws DBException {
String sql = "SELECT * FROM Product ORDER BY ProductID";
List products = new ArrayList<>();
Connection connection = DBUtil.getConnection();
try (PreparedStatement ps = connection.prepareStatement(sql);
ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
Product p = getProductFromRow(rs);
products.add(p);
}
rs.close();
return products;
} catch (SQLException e) {
throw new DBException(e);
}
}
public static Product get(String productCode) throws DBException {
String sql = "SELECT * FROM Product WHERE Code = ?";
Connection connection = DBUtil.getConnection();
try (PreparedStatement ps = connection.prepareStatement(sql)) {
7. String s = sc.nextLine(); // read the whole line
return s;
}
public static int getInt(String prompt) {
boolean isValid = false;
int i = 0;
while (isValid == false) {
System.out.print(prompt);
try {
i = Integer.parseInt(sc.nextLine());
isValid = true;
} catch (NumberFormatException e) {
System.out.println("Error! Invalid integer value. Try again.");
}
}
return i;
}
public static int getInt(String prompt, int min, int max) {
int i = 0;
boolean isValid = false;
while (isValid == false) {
i = getInt(prompt);
if (i <= min) {
System.out.println(
"Error! Number must be greater than " + min);
} else if (i >= max) {
System.out.println(
"Error! Number must be less than " + max);
} else {
isValid = true;
}
}
return i;
}
public static double getDouble(String prompt) {
boolean isValid = false;
8. double d = 0;
while (isValid == false) {
System.out.print(prompt);
try {
d = Double.parseDouble(sc.nextLine());
isValid = true;
} catch (NumberFormatException e) {
System.out.println("Error! Invalid decimal value. Try again.");
}
}
return d;
}
public static double getDouble(String prompt,
double min, double max) {
double d = 0;
boolean isValid = false;
while (isValid == false) {
d = getDouble(prompt);
if (d <= min) {
System.out.println(
"Error! Number must be greater than " + min);
} else if (d >= max) {
System.out.println(
"Error! Number must be less than " + max);
} else {
isValid = true;
}
}
return d;
}
}
*************Main***************
package dog.breath;
import java.util.List;
import rooster.nest.Product;
import crows.foot.DBException;
9. import crows.foot.DBUtil;
import crows.foot.ProductDB;
public class Main {
// declare a class variable
public static void main(String args[]) {
// display a welcome message
Console.displayNewLine();
Console.display("Welcome to the Product Manager ");
// display the command menu
displayMenu();
// perform 1 or more actions
String action = "";
while (!action.equalsIgnoreCase("exit")) {
// get the input from the user
action = Console.getString("Enter a command: ");
Console.displayNewLine();
if (action.equalsIgnoreCase("list")) {
displayAllProducts();
} else if (action.equalsIgnoreCase("add")) {
addProduct();
} else if (action.equalsIgnoreCase("update")) {
updateProduct();
} else if (action.equalsIgnoreCase("del") ||
action.equalsIgnoreCase("delete")) {
deleteProduct();
} else if (action.equalsIgnoreCase("help") ||
action.equalsIgnoreCase("menu")) {
displayMenu();
} else if (action.equalsIgnoreCase("exit")) {
Console.display("Bye. ");
} else {
Console.display("Error! Not a valid command. ");
}
}
}
10. public static void displayMenu() {
Console.display("COMMAND MENU");
Console.display("list - List all products");
Console.display("add - Add a product");
Console.display("update - Update a product");
Console.display("del - Delete a product");
Console.display("help - Show this menu");
Console.display("exit - Exit this application ");
}
public static void displayAllProducts() {
Console.display("PRODUCT LIST");
List products = null;
try {
products = ProductDB.getAll();
} catch (DBException e) {
Console.display(e + " ");
}
if (products == null) {
Console.display("Error! Unable to get products. ");
} else {
Product p;
StringBuilder sb = new StringBuilder();
for (Product product : products) {
p = product;
sb.append(StringUtil.padWithSpaces(
p.getCode(), 12));
sb.append(StringUtil.padWithSpaces(
p.getDescription(), 34));
sb.append(p.getPriceFormatted());
sb.append(" ");
}
Console.display(sb.toString());
}
}
public static void addProduct() {
12. ProductDB.update(product);
} catch (DBException e) {
Console.display("Error! Unable to update product.");
Console.display(e + " ");
}
Console.display(product.getDescription() + " was updated in the database. ");
}
public static void deleteProduct() {
String code = Console.getString("Enter product code to delete: ");
Product product;
try {
product = ProductDB.get(code);
if (product == null) {
throw new Exception("Product not found.");
}
} catch (Exception e) {
Console.display("Error! Unable to delete product.");
Console.display(e + " ");
return;
}
try {
ProductDB.delete(product);
} catch (DBException e) {
Console.display("Error! Unable to delete product.");
Console.display(e + " ");
}
Console.display(product.getDescription() + " was deleted from the database. ");
}
public static void exit() {
try {
DBUtil.closeConnection();
13. } catch (DBException e) {
Console.display("Error! Unable to close connection.");
Console.display(e + " ");
}
System.out.println("Bye. ");
System.exit(0);
}
}
***********StringUtil********************
package dog.breath;
public class StringUtil {
public static String padWithSpaces(String s, int length) {
if (s.length() < length) {
StringBuilder sb = new StringBuilder(s);
while (sb.length() < length) {
sb.append(" ");
}
return sb.toString();
} else {
return s.substring(0, length);
}
}
}
Solution
Java defines several bitwise operators, which can be applied to the integer sorts, long, int, short,
char, and byte.
Bitwise operator works on bits and performs bit-via-bit operation. count on if a = 60 and b = 13;
now in binary format they'll be as follows
a = 0011 1100
b = 0000 1101
-----------------
a&b = 0000 1100
athe subsequent desk lists the bitwise operators
14. anticipate integer variable A holds 60 and variable B holds thirteen then
show Examples
Operator Description example
& (bitwise and) Binary AND Operator copies a bit to the end result if it exists in each
operands. (A & B) will supply 12 that is 0000 1100
a chunk if it exists in either B) will supply 61 that is 0011 1101
^ (bitwise XOR) Binary XOR Operator copies the bit if it's far set in one operand but no
longer both. (A ^ B) will deliver forty nine that's 0011 0001
~ (bitwise compliment) Binary Ones supplement Operator is unary and has the effect of
'flipping' bits. (~A ) will supply -61 which is 1100 0011 in 2's supplement shape because of a
signed binary range.
<< (left shift) Binary Left Shift Operator. The left operands fee is moved left by using the
variety of bits distinct by means of the proper operand. A << 2 will give 240 which is 1111
0000
>> (proper shift) Binary right Shift Operator. The left operands fee is moved proper by using
the number of bits detailed through the right operand. A >> 2 will give 15 which is 1111
>>> (zero fill right shift) Shift proper 0 fill operator. The left operands price is moved proper
by the wide variety of bits detailed by means of the right operand and shifted values are filled up
with zeros. A >>>2 will give 15