0% found this document useful (0 votes)
9 views6 pages

Exercise 9 - Improve Reusability and Maintainability Using Design Patterns-9034-Ganesan.P Design Patterns To Implement

The document outlines the implementation of design patterns to improve the reusability and maintainability of a car rental system. It details the Layered Architecture, Client-Server Pattern, and Repository Pattern, showcasing how each pattern contributes to the system's structure and functionality. The application of these design patterns results in cleaner code, easier maintenance, and decoupled components.

Uploaded by

linena3121
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

Exercise 9 - Improve Reusability and Maintainability Using Design Patterns-9034-Ganesan.P Design Patterns To Implement

The document outlines the implementation of design patterns to improve the reusability and maintainability of a car rental system. It details the Layered Architecture, Client-Server Pattern, and Repository Pattern, showcasing how each pattern contributes to the system's structure and functionality. The application of these design patterns results in cleaner code, easier maintenance, and decoupled components.

Uploaded by

linena3121
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

EXERCISE 9 – IMPROVE REUSABILITY AND MAINTAINABILITY USING DESIGN

PATTERNS-9034-GANESAN.P
DESIGN PATTERNS TO IMPLEMENT:
1. Layered Architecture
2. Client-Server Pattern
3. Repository Pattern
1. LAYERED ARCHITECTURE
controller → contains business logic
dao → handles database interactions
model → entity/data classes
view → Swing UI screens

package com.domain.carrental.controller;
import com.domain.carrental.dao.CarDAO;
import com.domain.carrental.model.Car;
import java.util.List;

public class CarController {


private CarDAO carDAO;
public CarController() {
this.carDAO = new CarDAO();
}
public List<Car> getAvailableCars() {
return carDAO.getAvailableCars();
}

public boolean addCar(String brand, String model, int year, double pricePerDay) {
Car car = new Car(0, brand, model, year, pricePerDay, true); // ID is auto-generated by DB
return carDAO.addCar(car);

}
2. CLIENT-SERVER PATTERN

package com.domain.carrental.view;
import com.domain.carrental.controller.CarController;
import com.domain.carrental.model.Car;
import javax.swing.*;
import java.awt.*;
import java.util.List;
public class AdminDashboard extends JFrame {
private CarController carController;
public AdminDashboard() {
carController = new CarController();
setTitle("Admin Dashboard - Car Rental System");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btnAddCar = new JButton("Add New Car");
btnAddCar.addActionListener(e -> showAddCarDialog());
JButton btnViewCars = new JButton("View Available Cars");
btnViewCars.addActionListener(e -> showAvailableCars());
JPanel panel = new JPanel(new GridLayout(2, 1));
panel.add(btnAddCar);
panel.add(btnViewCars);
add(panel);
setVisible(true);
}
private void showAddCarDialog() {
JTextField tfBrand = new JTextField();
JTextField tfModel = new JTextField();
JTextField tfYear = new JTextField();
JTextField tfPricePerDay = new JTextField();
Object[] fields = {
"Brand:", tfBrand,
"Model:", tfModel,
"Year:", tfYear,
"Price Per Day:", tfPricePerDay,
};
int result = JOptionPane.showConfirmDialog(this, fields, "Add New Car",
JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
String brand = tfBrand.getText();
String model = tfModel.getText();
int year = Integer.parseInt(tfYear.getText());
double pricePerDay = Double.parseDouble(tfPricePerDay.getText());
boolean success = carController.addCar(brand, model, year, pricePerDay);
if (success) {
JOptionPane.showMessageDialog(this, "Car added successfully!");
} else {
JOptionPane.showMessageDialog(this, "Failed to add car!", "Error",
JOptionPane.ERROR_MESSAGE);} } }
private void showAvailableCars() {
List<Car> cars = carController.getAvailableCars();
StringBuilder sb = new StringBuilder("Available Cars:\n");
for (Car car : cars) {
sb.append(car.toString()).append("\n");
JOptionPane.showMessageDialog(this, sb.toString()); }}

4. REPOSITORY PATTERN
package com.domain.carrental.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DatabaseConnector {
private static final String DB_URL = "jdbc:mysql://localhost:3306/car_rental";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "Sql@13677";
public DatabaseConnector() { }
public static Connection connect() throws SQLException {
return DriverManager.getConnection("jdbc:mysql://localhost:3306/car_rental", "root",
"Sql@13677"); }
public static void main(String[] var0) {
try {
Connection var1 = connect();
System.out.println("Connection successful!");
var1.close();
} catch (SQLException var2) {
var2.printStackTrace(); } }}
Summary Table
Design Pattern Used In ATM System Benefit
Layered Separated UI, Logic Clean code, easier
Architecture (ATMService), DB maintenance
Client-Server Java Swing UI ↔ MySQL via Decouples interface
Pattern JDBC and storage
Repository SQLManage.java handles DB Reusable DB logic,
Pattern operations easy to test

Result:
Thus, the application of appropriate design patterns was successfully implemented to enhance
the reusability and maintainability of the software system

You might also like