JAVAWith NETBEANS Product package roosternest import javatex

JAVA...W\'i\'t\'h N.E.T_B.E.A.N.S_____________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

***********Product*********************

package rooster.nest;

import java.text.NumberFormat;

public class Product {
private long id;
private String code;
private String description;
private double price;

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public String getDescription() {
return description;
}

public void setDescription(String name) {
this.description = name;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
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;
}
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;

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<Product> getAll() throws DBException {
String sql = \"SELECT * FROM Product ORDER BY ProductID\";
List<Product> 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)) {
ps.setString(1, productCode);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
Product p = getProductFromRow(rs);
rs.close();
return p;
} else {
rs.close();
return null;
}
} catch (SQLException e) {
throw new DBException(e);
}
}

public static void add(Product product) throws DBException {
String sql
= \"INSERT INTO Product (Code, Description, ListPrice) \"
+ \"VALUES (?, ?, ?)\";
Connection connection = DBUtil.getConnection();
try (PreparedStatement ps = connection.prepareStatement(sql)) {
ps.setString(1, product.getCode());
ps.setString(2, product.getDescription());
ps.setDouble(3, product.getPrice());
ps.executeUpdate();
} catch (SQLException e) {
throw new DBException(e);
}
}

public static void update(Product product) throws DBException {
String sql = \"UPDATE Product SET \"
+ \"Code = ?, \"
+ \"Description = ?, \"
+ \"ListPrice = ?\"
+ \"WHERE ProductID = ?\";
Connection connection = DBUtil.getConnection();
try (PreparedStatement ps = connection.prepareStatement(sql)) {
ps.setString(1, product.getCode());
ps.setString(2, product.getDescription());
ps.setDouble(3, product.getPrice());
ps.setLong(4, product.getId());
ps.executeUpdate();
} catch (SQLException e) {
throw new DBException(e);
}
}

public static void delete(Product product)
throws DBException {
String sql = \"DELETE FROM Product \"
+ \"WHERE ProductID = ?\";
Connection connection = DBUtil.getConnection();
try (PreparedStatement ps = connection.prepareStatement(sql)) {
ps.setLong(1, product.getId());
ps.executeUpdate();
} catch (SQLException e) {
throw new DBException(e);
}
}
}

***********Console*****************

package dog.breath;

import java.util.Scanner;

public class Console {

private static Scanner sc = new Scanner(System.in);
  
public static void displayNewLine() {
System.out.println();
}

public static void display(String string) {
System.out.println(string);
}

public static String getString(String prompt) {
System.out.print(prompt);
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;
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;
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.\ \");
}
}
}

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<Product> 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() {
String code = Console.getString(\"Enter product code: \");
String description = Console.getString(\"Enter product name: \");
double price = Console.getDouble(\"Enter price: \");

Product product = new Product();
product.setCode(code);
product.setDescription(description);
product.setPrice(price);
  
try {
ProductDB.add(product);
Console.display(product.getDescription()
+ \" was added to the database.\ \");
} catch (DBException e) {
Console.display(\"Error! Unable to add product.\");
Console.display(e + \"\ \");
}
}

public static void updateProduct() {
String code = Console.getString(\"Enter product code to update: \");
Product product;
try {
product = ProductDB.get(code);
if (product == null) {
throw new Exception(\"Product not found.\");
}
} catch (Exception e) {
Console.display(\"Error! Unable to update product.\");
Console.display(e + \"\ \");
return;
}
  
String description = Console.getString(\"Enter product name: \");
double price = Console.getDouble(\"Enter price: \");

product.setDescription(description);
product.setPrice(price);
try {
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();
} 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

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

JAVA...W\'i\'t\'h N.E.T_B.E.A.N.S_______________________________________________________________________________________________________________________________
JAVA...W\'i\'t\'h N.E.T_B.E.A.N.S_______________________________________________________________________________________________________________________________
JAVA...W\'i\'t\'h N.E.T_B.E.A.N.S_______________________________________________________________________________________________________________________________
JAVA...W\'i\'t\'h N.E.T_B.E.A.N.S_______________________________________________________________________________________________________________________________
JAVA...W\'i\'t\'h N.E.T_B.E.A.N.S_______________________________________________________________________________________________________________________________
JAVA...W\'i\'t\'h N.E.T_B.E.A.N.S_______________________________________________________________________________________________________________________________
JAVA...W\'i\'t\'h N.E.T_B.E.A.N.S_______________________________________________________________________________________________________________________________
JAVA...W\'i\'t\'h N.E.T_B.E.A.N.S_______________________________________________________________________________________________________________________________
JAVA...W\'i\'t\'h N.E.T_B.E.A.N.S_______________________________________________________________________________________________________________________________

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site