describe the code in detail add comment lines

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

describe the code in detail add comment lines

package com.templatepattern;

public class TemplatePatternTest1 {

public static void main(String[] args) {
String connectionString = "server=192.168.0.7";
String username = "learner";
String password = "designPatterns";
StudentRegistration sr = new DatabaseBasedStudentRegistration(connectionString, username, password);
System.out.println();

Person p1 = new Person("patrick", "nick");
System.out.println("Registering " + p1);
sr.register(p1);
System.out.println();

Person p2 = new Person("zayn", "nick");
System.out.println("Registering " + p2);
sr.register(p2);
System.out.println();
}

}

abstract class StudentRegistration {

// template method
final Student register(Person person) {
if (checkPerson(person)) {
startOperation();
try {
int studentId = getNewIdentity(person);
Student student = new Student(person, studentId);
completeOperation();

return student;
}
catch (Exception e) {
cancelOperation();
promptError(e.getMessage());
}

}
else {
promptError(person + " is not a valid citizen.");
}

return null;
}

abstract void startOperation();

abstract void completeOperation();

abstract void cancelOperation();

abstract boolean checkPerson(Person person);

abstract int getNewIdentity(Person person);

abstract void promptError(String errorMessage);

}

class DatabaseConnection {

DatabaseConnection(String connectionString, String username, String password) {
System.out.println("Connect to database using " + connectionString + ", Username = " + username + ", Password = " + password);
}

void beginTransaction() {
System.out.println("Begin Transaction");
}

void commitTransaction() {
System.out.println("Commit Transaction");
}

void rollbackTransaction() {
System.out.println("Rollback Transaction");
}
}

class DatabaseBasedStudentRegistration extends StudentRegistration {
private final DatabaseConnection connection;

DatabaseBasedStudentRegistration(String connectionString, String username, String password) {
connection = new DatabaseConnection(connectionString, username, password);
}

@Override
void startOperation() {
connection.beginTransaction();
}

@Override
void completeOperation() {
connection.commitTransaction();
}

@Override
void cancelOperation() {
connection.rollbackTransaction();
}

@Override
boolean checkPerson(Person person) {
return (person.toString().length() > 10);
}

@Override
int getNewIdentity(Person person) {
return Math.abs(person.toString().hashCode()) % 100000000;
}

@Override
void promptError(String errorMessage) {
System.out.println(errorMessage);
}
}

class Person {
final String name;
final String surname;

Person(String name, String surname) {
this.name = name;
this.surname = surname;
}

@Override
public String toString() {
return name + " " + surname;
}
}

class Student extends Person {
final int studentId;

Student(String name, String surname, int studentId) {
super(name, surname);
this.studentId = studentId;
System.out.println(this + " is created");
}

Student(Person person, int studentId) {
super(person.name, person.surname);
this.studentId = studentId;
System.out.println(this + " is created");
}

@Override
public String toString() {
return super.toString() + ", ID = " + studentId;
}
}

Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Hash Table
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education