This is from Murach Java programming Project. NetBeans Instructions will also be
ID: 3846970 • Letter: T
Question
This is from Murach Java programming Project. NetBeans Instructions will also be very helpfu, Thanks so much.
Console
Welcome to the Customer Maintenance application
COMMAND MENU
list - List all customers
add - Add a customer
del - Delete a customer
help - Show this menu
exit - Exit this application
Enter a command: list
CUSTOMER LIST
frankjones@yahoo.com Frank Jones
johnsmith@hotmail.com John Smith
seagreen@levi.com Cynthia Green
wendyk@warners.com Wendy Kowolski
Enter a command: add
Enter customer email address: jdbc_test@gm'ail.com
Enter first name: JDBC
Enter last name: Test
JDBC Test was added to the database.
Enter a command: list
CUSTOMER LIST
frankjones@yahoo.com Frank Jones
jdbc_test@gm'ail.com JDBC Test
johnsmith@hotmail.com John Smith
seagreen@levi.com Cynthia Green
wendyk@warners.com Wendy Kowolski
Enter a command: del
Enter customer email to delete: jdbc_test@gm'ail.com
JDBC Test was deleted from the database.
Enter a command: list
CUSTOMER LIST
frankjones@yahoo.com Frank Jones
johnsmith@hotmail.com John Smith
seagreen@levi.com Cynthia Green
wendyk@warners.com Wendy Kowolski
Enter a command: exit
Bye.
Operation
This application begins by displaying a menu with five choices: list, add, del, help, and exit.
If the user enters “list”, the application displays the customer data that’s stored in a database table.
If the user enters “add”, the application prompts the user to enter data for a customer and saves that data to the database table.
If the user enters “del”, the application prompts the user for an email address and deletes the corresponding customer from the database table.
If the user enters “help”, the application displays the menu again.
If the user enters “exit”, the application displays a goodbye message and exits.
Specifications
Create a Derby database named MurachDB that contains the necessary data. To do that, you can Use the SQL script stored in the CreateMurachDB.sql file that’s stored in the murachjavaproject_startsproject_21-2 directory. (If you did project 21-1, you have already created this database.)
Create a class named Customer that stores data for the user’s email address, first name, and last name.
Create a class named CustomerDB. This class should include a public method that returns an array list of Customer objects for the customers in the Customer table, a public method that returns a Customer object for the customer with a specified email address, and public methods that add a record to and delete a record from the Customer table.
Create a CustomerMaintApp class that works as shown in the console output. This class should use the Customer and CustomerDB classes to work with the customer data.
Use spaces to align the customer data in columns on the console. To do that, you can create a utility class named StringUtils that has a method that adds the necessary spaces to a string to reach a specified length.
Use the Validator class or a variation of it to validate the user’s entries. Non-empty strings are required for the email address, first name, and last name.
Add an “update” command that lets the user update an existing customer. This command should prompt the user to enter the email address for a customer. Then, it should let the user update the first name and last name for the customer.
Add a method to the Validator class that uses string parsing techniques to validate the email address. At the least, you can check to make sure that this string contains some text, followed by an @ sign, followed by some more text, followed by a period, followed by some more text. For example, “x@x.x” would be valid while “xxx” or “x@x” would not.
Explanation / Answer
CustomerMainAppt.java
import java.util.ArrayList;
import java.util.*;
public class CustomerMaintApp {
public static void showMenu(){
System.out.println("list -List all customers");
System.out.println("add -Add a customers");
System.out.println("del -Delete a customers");
System.out.println("help -Show this menu");
System.out.println("exit -exit this application");
}
public static void main(String args[]){
CustomerDB db = new CustomerDB();
ArrayList<Customer> customers = new ArrayList();
Scanner sc = new Scanner(System.in);
System.out.println("Welcome to the Customer Maintenance application");
showMenu();
System.out.print("Enter a command: ");
String choice= sc.nextLine();
while(!choice.equals("exit")){
if(choice.equals("list")){
customers = db.getAllCustomers();
System.out.println("CUSTOMER LIST");
for(Customer c: customers){
System.out.println(c.getEmail() + " " + c.getFirstname() + " " + c.getLastName());
}
}
else if(choice.equals("add")){
System.out.print("Enter customer email address: ");
String email = sc.nextLine();
System.out.print("Enter customer first name: ");
String firstName = sc.nextLine();
System.out.print("Enter customer last name: ");
String lastName = sc.nextLine();
Customer c = new Customer(email, firstName, lastName);
db.insertCustomer(c);
System.out.println(firstName + " " + lastName + " added to the database");
}
else if(choice.equals("del")){
System.out.print("Enter customer email address to delete: ");
String email = sc.nextLine();
for(Customer c: customers){
if(c.getEmail().equals(email)){
db.deleteCustomer(c);
System.out.println(c.getFirstname() + " " + c.getLastName() + " was deleted from the database.");
}
}
}
else if(choice.equals("help")){
showMenu();
}
System.out.print("Enter a command: ");
choice= sc.nextLine();
}
System.out.println("Program terminated!!");
db.disconnect();
}
}
CustomerDB.java
import java.util.ArrayList;
import java.util.*;
import java.sql.*;
public class CustomerDB {
private ArrayList<Customer> customers;
private static Connection connection ;
private static int count;
public CustomerDB(){
customers = new ArrayList();
connection = connect();
}
public static void disconnect(){
try
{
connection.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
private static Connection connect(){
try
{
String dbDirectory = "/Users/yc/Documents/workspace/JavaCompHw_7";
System.setProperty("derby.system.home", dbDirectory);
String dbUrl = "jdbc:derby:BineetDB";
String username = "";
String password = "";
Connection connection = DriverManager.getConnection(dbUrl, username, password);
return connection;
}
catch(SQLException e)
{
e.printStackTrace();
return null;
}
}
public void readDB(){
try
{
customers.clear();
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("select emailaddress, firstname, lastname "
+ "from customers "
+ "order by emailaddress asc");
Customer c = null;
while(rs.next())
{
String email = rs.getString("EmailAddress");
String firstName = rs.getString("FirstName");
String lastName = rs.getString("LastName");
c = new Customer(email, firstName, lastName);
customers.add(c);
//System.out.println(email + " " + invoiceNumber + " " + date + " " + invoiceTotal);
}
rs.close();
statement.close();
}
catch(SQLException e)
{
e.printStackTrace(); // for debugging
}
}
public ArrayList<Customer> getAllCustomers(){
readDB();
return customers;
}
public Customer getCustomer(String email){
readDB();
Customer c = new Customer("","","");
for(int i=0; i<customers.size(); i++){
if(customers.get(i).getEmail().equals(email)){
c = customers.get(i);
}
}
return c;
}
public void insertCustomer(Customer c){
try
{
PreparedStatement ps = connection.prepareStatement(
"SELECT * FROM Customers WHERE EmailAddress = ?");
ps.setString(1, c.getEmail());
ResultSet rs = ps.executeQuery();
if(rs.next())
{
System.out.println("This customer email already exists: " + c.getEmail());
}
else
{
String insertCustomer =
"INSERT INTO Customers" + //INSERT INTO Customers
"VALUES (?, ?, ?, ?)"; //VALUES (3, 'John', 'Smith', 'johnsmith@hotmail.com');
PreparedStatement ps2 = connection.prepareStatement(insertCustomer);
int id = 11;
ps2.setInt(1, id);
ps2.setString(2, c.getEmail());
ps2.setString(3, c.getFirstname());
ps2.setString(4, c.getLastName());
ps2.executeUpdate();
ps2.executeQuery();
ps2.close();
}
rs.close();
ps.close();
}
catch(SQLException e)
{
e.printStackTrace(); // for debugging
}
}
public void deleteCustomer(Customer c){
try
{
PreparedStatement ps = connection.prepareStatement(
"SELECT * FROM Customers WHERE EmailAddress = ?");
ps.setString(1, c.getEmail());
ResultSet rs = ps.executeQuery();
if(rs.next()){
String deleteCustomer = "Delete from Customers where EmailAddress = ?";
PreparedStatement ps2 = connection.prepareStatement(deleteCustomer);
ps2.setString(1, c.getEmail());
ps2.executeUpdate();
ps2.close();
}
else
{
System.out.println("No record matches this email adress: " + c.getEmail());
}
rs.close();
ps.close();
}
catch(SQLException e)
{
e.printStackTrace(); // for debugging
}
}
}
Customer.java
public class Customer {
private String email;
private String firstName;
private String lastName;
public Customer(String email, String firstName, String lastName){
this.email = email;
this.firstName = firstName;
this.lastName = lastName;
}
public String getEmail(){
return email;
}
public void setEmail(String email){
this.email = email;
}
public String getFirstname(){
return firstName;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public String getLastName(){
return lastName;
}
public void setLastName(){
this.lastName = lastName;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.