Skip to content

faizan-devtech/prison-management

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

# Prison Management System – Code Restructuring Project This project demonstrates **software re-engineering / code restructuring** on a Prison Management System written in Python. It contains two versions: ``` prison-management/ ├── legacy/ <- BEFORE: messy, single-file, code smells │ └── prison_system.py │ └── refactored/ <- AFTER: layered architecture, design patterns └── prison_app/ ├── __init__.py ├── cli.py (presentation layer) ├── models/ │ ├── __init__.py │ ├── inmate.py │ ├── staff.py │ └── visitor.py ├── services/ (business logic layer) │ ├── __init__.py │ ├── inmate_service.py │ ├── staff_service.py │ └── visitor_service.py ├── repositories/ (data access layer / DAO) │ ├── __init__.py │ ├── database.py │ ├── inmate_repository.py │ ├── staff_repository.py │ └── visitor_repository.py └── utils/ ├── __init__.py ├── constants.py └── exceptions.py ``` --- ## 1. Legacy version (`legacy/prison_system.py`) A single God class `PrisonSystem` that mixes together: - Database setup and raw SQL - Input validation with magic numbers (18, 50, 100, 10000…) - Business logic (risk level calculation) - Display/print logic - A CLI menu, all in `main()` **Code smells present (for your report):** | Smell | Where | |---|---| | God Class | `PrisonSystem` does DB + validation + logic + display | | Magic Numbers | `18`, `50`, `100`, `10000`, `10`, `5` hardcoded | | Duplicated Code | Inmate lookup repeated in `add_visitor()` and `get_inmate_info()` | | Long Method | `main()`, `get_inmate_info()` | | Mixed Responsibilities | Business rule (risk level) embedded in print statements | | No Layering | UI, logic, and DB all directly coupled | --- ## 2. Refactored version (`refactored/prison_app/`) Restructured into a **layered architecture**: - **models/** – plain data classes (`Inmate`, `Staff`, `Visitor`) using `@dataclass`. `Inmate.risk_level` and `Inmate.release_year()` extract business rules out of print statements. - **repositories/** – DAO pattern. All SQL isolated here. `Database` is a **Singleton**, so only one connection exists. - **services/** – business logic + validation (`InmateService`, `StaffService`, `VisitorService`). `VisitorService` reuses `InmateService.get_inmate()` instead of duplicating the lookup query. - **utils/constants.py** – all magic numbers replaced with named constants. - **utils/exceptions.py** – `ValidationError`, `NotFoundError` replace scattered `print()` + `return`. - **cli.py** – presentation layer only; delegates everything to services. **Refactoring techniques applied (map this to your report):** | Technique | Applied to | |---|---| | Extract Class | God class → Inmate/Staff/Visitor models, services, repositories | | Extract Method | Validation logic, risk level calculation | | Replace Magic Number with Constant | `constants.py` | | Introduce DAO Pattern | `repositories/` | | Singleton Pattern | `Database` | | Replace Error Code with Exception | `ValidationError`, `NotFoundError` | | Separation of Concerns / Layered Architecture | models / services / repositories / cli | --- ## 3. How to run in VS Code ### Prerequisites - Install **Python 3.10+** - Install the **Python extension** in VS Code (by Microsoft) ### Step-by-step 1. **Open the folder** - Open VS Code → `File > Open Folder` → select `prison-management/` 2. **Select Python interpreter** - `Ctrl+Shift+P` → "Python: Select Interpreter" → choose your Python 3 install 3. **Run the legacy version** - Open `legacy/prison_system.py` - Open a terminal: `Terminal > New Terminal` - Run: ```bash cd legacy python prison_system.py ``` 4. **Run the refactored version** - Open a terminal at the project root (`prison-management/`) - Run: ```bash cd refactored python -m prison_app.cli ``` - Important: run it as a **module** (`-m prison_app.cli`) from inside `refactored/`, so Python resolves the `prison_app` package imports correctly. Alternatively, on Windows PowerShell / Linux / macOS you can set `PYTHONPATH`: ```bash cd refactored PYTHONPATH=. python prison_app/cli.py ``` 5. **Database files** - Both versions create a local `prison.db` SQLite file in the directory you run them from. - Delete `prison.db` if you want to reset all data. --- ## 4. Suggested report structure 1. Introduction & objectives 2. Description of legacy system + identified code smells (table above) 3. Restructuring plan & techniques chosen 4. New architecture diagram (models → services → repositories → database) 5. Before/after code comparison (pick 2–3 methods, e.g. `add_inmate`, `get_inmate_info`) 6. Metrics comparison (optional: run `radon cc` or `pylint` on both versions) 7. Conclusion & lessons learned ### Optional: generate metrics ```bash pip install radon pylint radon cc legacy/prison_system.py -a radon cc -a refactored/prison_app ``` This gives you cyclomatic complexity numbers to compare in your report. # prison-management

About

**University Project: Software Reengineering (Subject: Software Reengineering)** This Software Reengineering project focuses on code restructuring to improve the quality, readability, maintainability, and efficiency of an existing software system without changing its external functionality. The project involves analyzing poorly organized legacy c

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages