Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Add instruction to what every step does for the following code: package main; im

ID: 3808561 • Letter: A

Question

Add instruction to what every step does for the following code:

package main;

import java.sql.*;

public class Contact {

private int ID;

private String name;
private String address;
private String phoneNumber;
public Contact(String name, String address, String phone) {
this.name = name;
this.address = address;
this.phoneNumber = phone;
}

public String getName() {
return name;
}

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

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getPhoneNumber() {
return phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}

public void setID(int ID) {
this.ID = ID;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Contact contact = (Contact) o;

if (ID != contact.ID) return false;
if (name != null ? !name.equals(contact.name) : contact.name != null) return false;
if (address != null ? !address.equals(contact.address) : contact.address != null) return false;
return phoneNumber != null ? phoneNumber.equals(contact.phoneNumber) : contact.phoneNumber == null;
}

@Override
public int hashCode() {
int result = ID;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (address != null ? address.hashCode() : 0);
result = 31 * result + (phoneNumber != null ? phoneNumber.hashCode() : 0);
return result;
}

public void save() throws SQLException {
Connection connection = DbHelper.getInstance().getConnection();

String insertSQL = "insert into contact(name,address,phone) values(?,?,?)";
PreparedStatement statement = connection.prepareStatement(insertSQL);
statement.setString(1, this.name);
statement.setString(2, this.address);
statement.setString(3, this.phoneNumber);
statement.execute();
this.ID = statement.getGeneratedKeys().getInt(1);
statement.close();
connection.close();

}

@Override
public String toString() {
return String.format("ID: %s Name: %s Address %s Phone %s",ID,name,address,phoneNumber);
}

public int getID() {
return ID;
}

public static Contact load(int id) throws SQLException {
Connection connection = DbHelper.getInstance().getConnection();
Statement statement = connection.createStatement();
String query = String.format("select * from contact where ID = %d",id);
ResultSet rs = statement.executeQuery(query);
Contact contact = null;
if(rs.next()) {
contact = new Contact(rs.getString("name"),
rs.getString("address"),
rs.getString("phone"));
contact.setID(id);

} else {
System.out.println("Query returned empty!");
}
connection.close();
return contact;

}

public void delete() throws SQLException {
Connection connection = DbHelper.getInstance().getConnection();
Statement statement = connection.createStatement();
String query = String.format("delete from contact where ID = %d",this.ID);
statement.execute(query);
statement.close();
connection.close();

}
}

Explanation / Answer

Added comments inline

package main; // Encapsulate classes in a main package

import java.sql.*; // import built-in package java.sql used to connect to SQL

public class Contact {
// Private members (attributes to hold the data)
private int ID;
private String name;
private String address;
private String phoneNumber;
  
// Contructor: to create an object and assign values to private memebers
public Contact(String name, String address, String phone) {
this.name = name;
this.address = address;
this.phoneNumber = phone;
}
// Accessors: methods to access values of private data members
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public String getPhoneNumber() {
return phoneNumber;
}
public int getID() {
return ID;
}
  
// Mutators: methods to update values of private data members
public void setAddress(String address) {
this.address = address;
}
public void setName(String name) {
this.name = name;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public void setID(int ID) {
this.ID = ID;
}
// Override built in function to compare objects of Contact type, this can be used to compare it 2 object of Contact tyep are identical or not
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Contact contact = (Contact) o;
if (ID != contact.ID) return false;
if (name != null ? !name.equals(contact.name) : contact.name != null) return false;
if (address != null ? !address.equals(contact.address) : contact.address != null) return false;
return phoneNumber != null ? phoneNumber.equals(contact.phoneNumber) : contact.phoneNumber == null;
}
  
// Override built in function to create a hascode for each object depending upon its attributes values
@Override
public int hashCode() {
int result = ID;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (address != null ? address.hashCode() : 0);
result = 31 * result + (phoneNumber != null ? phoneNumber.hashCode() : 0);
return result;
}
  
// Save the Contact information to SQL database
public void save() throws SQLException {
Connection connection = DbHelper.getInstance().getConnection(); // Create a SQL connection using DbHelper class
String insertSQL = "insert into contact(name,address,phone) values(?,?,?)"; // SQL query to insert data in table
PreparedStatement statement = connection.prepareStatement(insertSQL);
// Replace ? with attributes in the SQL statement, basically format the SQL statement and add the attributes to be inserted
statement.setString(1, this.name);
statement.setString(2, this.address);
statement.setString(3, this.phoneNumber);
statement.execute();
this.ID = statement.getGeneratedKeys().getInt(1); // Generate primary key for each row and increase it by 1
// Close the SQL connections
statement.close();
connection.close();
}
  
// Override the built in function toString to return data of attributes of an object
@Override
public String toString() {
return String.format("ID: %s Name: %s Address %s Phone %s",ID,name,address,phoneNumber);
}
  
// Method to read data from SQL database (this is static and can be accessed directly using Class name)
public static Contact load(int id) throws SQLException {
Connection connection = DbHelper.getInstance().getConnection(); // Create SQL conection
Statement statement = connection.createStatement();
String query = String.format("select * from contact where ID = %d",id); // Create SQL statement
ResultSet rs = statement.executeQuery(query); // Execute the query and store data as a result set
Contact contact = null;
// Read the row and create a object of contact type with the information obtained
if(rs.next()) {
contact = new Contact(rs.getString("name"),
rs.getString("address"),
rs.getString("phone"));
contact.setID(id);
} else {
System.out.println("Query returned empty!");
}
connection.close(); // Close the connection
return contact; // Return the object created
}
  
// Method to delete any contact from backend (database)
public void delete() throws SQLException {
Connection connection = DbHelper.getInstance().getConnection(); // Create the connection
Statement statement = connection.createStatement(); // Create statement
String query = String.format("delete from contact where ID = %d",this.ID); // Create query to delete the record of the Contact invoking the method
statement.execute(query);// Execute query
statement.close(); // Close all connections
connection.close();
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote