============================================================ IT6003 - Advanced Java Programming Homework 3: Multithreading, File I/O & JDBC State University of Zanzibar (SUZA) ============================================================ Total Marks: 40 Due: As announced by instructor ============================================================ INSTRUCTIONS: - Submit .java files in a ZIP named RegNo_HW3.zip - For JDBC questions, include the .db file and any CSV files - Include a README.txt explaining how to run each program ============================================================ Problem 1: Thread-Safe Bank System [12 marks] ------------------------------------------------- Implement a multi-threaded banking system: a) Create Account class with: accountNo (String), holderName (String), balance (double). All methods accessing balance must be synchronized. [2 marks] b) Implement synchronized methods: deposit(double), withdraw(double), getBalance(). withdraw() should wait() if insufficient funds and be notified when a deposit is made. [3 marks] c) Create TransferService with method: void transfer(Account from, Account to, double amount) Must be thread-safe and avoid deadlock (acquire locks in consistent order based on account number). [3 marks] d) Create a simulation: - 5 accounts with initial balance 1000 each - 10 threads performing random transfers (random amount 10-200, random source and destination accounts) - Each thread performs 20 transfers - After all threads finish, verify total money is preserved (should equal 5 * 1000 = 5000). [2 marks] e) Print a transaction log showing each transfer with timestamp, thread name, from, to, amount, resulting balances. [2 marks] Problem 2: File Processing Pipeline [12 marks] -------------------------------------------------- Build a log file analysis system: a) Generate a log file "server.log" with 500+ lines in format: 2025-03-15 10:23:45 [INFO] User login successful: user123 2025-03-15 10:24:12 [ERROR] Database connection timeout 2025-03-15 10:25:00 [WARNING] Memory usage at 85% 2025-03-15 10:25:30 [DEBUG] Cache miss for key: session_456 Include timestamps, 4 log levels, varied messages. [2 marks] b) Read and parse using BufferedReader with try-with-resources. Create a LogEntry class with: timestamp, level, message. Parse each line into LogEntry objects. [2 marks] c) Analysis (use streams where possible): [4 marks] - Count of each log level - All ERROR messages sorted by timestamp - Errors per hour (group by hour) - Most common error message - Lines between two timestamps (date range filter) d) Export results: - Write errors to "errors.log" using BufferedWriter - Write summary report to "report.txt" - Serialize List to "entries.dat" [2 marks] e) Use NIO to: - Watch a directory for new .log files (WatchService - bonus) - Read file attributes (size, creation time, last modified) [2 marks] Problem 3: Student Database Application [16 marks] ------------------------------------------------------ Build a complete JDBC application for managing student records: a) Database schema (SQLite): [2 marks] Table: departments (dept_id INTEGER PK, dept_name TEXT, hod TEXT) Table: students (reg_no TEXT PK, name TEXT, dept_id INT FK, gpa REAL, email TEXT, enrollment_date TEXT) Table: courses (course_id TEXT PK, course_name TEXT, credits INT, dept_id INT FK) Table: enrollments (reg_no TEXT FK, course_id TEXT FK, grade TEXT, PRIMARY KEY (reg_no, course_id)) b) Create StudentDAO class with PreparedStatement for: [3 marks] - insertStudent(), findByRegNo(), findAll(), updateGPA(), deleteStudent() - findByDepartment(int deptId), findByGPARange(double min, double max) c) Create DepartmentDAO and CourseDAO with similar CRUD. [2 marks] d) Implement complex queries: [4 marks] - Student transcript: all courses with grades for a student (JOIN) - Department report: student count, avg GPA per department (GROUP BY) - Top students: top 5 by GPA with department name (JOIN + ORDER BY + LIMIT) - Course enrollment: how many students per course (GROUP BY) - Students not enrolled in any course (LEFT JOIN with NULL check) e) Transaction management: [3 marks] - enrollStudent(String regNo, String courseId, String grade): must check course exists AND student exists before enrolling - bulkImport(String csvFile): read CSV and batch insert students with conn.setAutoCommit(false), addBatch(), executeBatch(), commit() - If any insert fails, rollback entire batch f) Menu-driven interface (console): [2 marks] 1. Add Student 2. View Students 3. Search 4. Update GPA 5. Delete 6. Enroll in Course 7. View Transcript 8. Department Report 9. Import CSV 10. Export CSV 11. Exit ============================================================ Viva Questions (be prepared): 1. What is a race condition? How do you prevent it? 2. Explain the difference between wait() and sleep(). 3. What is the purpose of serialVersionUID? 4. Why should you use PreparedStatement instead of Statement? 5. Explain ACID properties in the context of JDBC transactions. 6. What is the difference between Callable and Runnable? ============================================================