📚 Modul Praktikum: Aplikasi Bank Transfer dengan Runtime Configuration (Node.js)
🎯 Tujuan Pembelajaran
- Mahasiswa mampu membuat aplikasi berbasis console.
- Mahasiswa memahami konsep Runtime Configuration menggunakan file JSON.
- Mahasiswa mampu menghubungkan konfigurasi dengan logika program.
- Mahasiswa terbiasa dengan struktur project yang modular.
🛠 Persiapan Awal
- Pastikan Node.js sudah terinstal di laptop kalian.
- Buka VSCode atau editor favorit.
- Buat folder project baru dengan nama:
➔ Modul 08 – Runtime Configuration dan Internationalization
📦 Struktur Folder Project
1 2 3 4 5 6 7 8 9 10 |
Modul 08 - Runtime Configuration dan Internationalization/ ├── config/ │ └── BankTransferConfig.js ├── data/ │ └── bank_transfer_config.json ├── app/ │ └── BankTransferApp.js ├── index.js ├── .gitignore ├── package.json |
✏️ Langkah-langkah Praktikum
1. Inisialisasi Project Node.js
Buka terminal di folder project:
1 |
npm init -y |
Ini akan membuat file package.json
.
2. Membuat File Konfigurasi
Buat folder data/
, lalu buat file bank_transfer_config.json
isinya:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
{ "lang": "en", "transfer": { "threshold": 25000000, "low_fee": 6500, "high_fee": 15000 }, "methods": ["RTO (real-time)", "SKN", "RTGS", "BI FAST"], "confirmation": { "en": "yes", "id": "ya" } } |
➡ Ini tempat semua pengaturan aplikasi kalian. Bisa diganti-ganti tanpa perlu ubah program!
3. Membuat Class Konfigurasi
Buat folder config/
, lalu buat file BankTransferConfig.js
.
Tujuannya:
✅ Membaca file JSON
✅ Mengisi default kalau file tidak ada
Contoh kodenya:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
const fs = require('fs'); const path = require('path'); class BankTransferConfig { constructor() { this.configPath = path.join(__dirname, '../data/bank_transfer_config.json'); this.defaultConfig = { lang: "en", transfer: { threshold: 25000000, low_fee: 6500, high_fee: 15000 }, methods: ["RTO (real-time)", "SKN", "RTGS", "BI FAST"], confirmation: { en: "yes", id: "ya" } }; this.config = this.loadConfig(); } loadConfig() { if (fs.existsSync(this.configPath)) { const data = fs.readFileSync(this.configPath, 'utf8'); return JSON.parse(data); } else { this.saveConfig(this.defaultConfig); return this.defaultConfig; } } saveConfig(config) { fs.writeFileSync(this.configPath, JSON.stringify(config, null, 2)); } } module.exports = BankTransferConfig; |
4. Membuat Aplikasi Transfer
Buat folder app/
, lalu buat file BankTransferApp.js
.
Isinya:
✅ Menampilkan pertanyaan ke user
✅ Menentukan biaya transfer
✅ Meminta konfirmasi
Contoh kodenya:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
const readline = require('readline'); const BankTransferConfig = require('../config/BankTransferConfig'); class BankTransferApp { constructor() { this.config = new BankTransferConfig().config; this.rl = readline.createInterface({ input: process.stdin, output: process.stdout }); } askQuestion(query) { return new Promise(resolve => this.rl.question(query, resolve)); } async run() { const lang = this.config.lang; const promptAmount = lang === "en" ? "Please insert the amount of money to transfer: " : "Masukkan jumlah uang yang akan di-transfer: "; const amountStr = await this.askQuestion(promptAmount); const amount = parseFloat(amountStr); const fee = amount <= this.config.transfer.threshold ? this.config.transfer.low_fee : this.config.transfer.high_fee; const total = amount + fee; if (lang === "en") { console.log(`Transfer fee = ${fee}`); console.log(`Total amount = ${total}`); } else { console.log(`Biaya transfer = ${fee}`); console.log(`Total biaya = ${total}`); } console.log(lang === "en" ? "Select transfer method:" : "Pilih metode transfer:"); this.config.methods.forEach((method, idx) => { console.log(`${idx + 1}. ${method}`); }); await this.askQuestion(lang === "en" ? "Choose a method (press Enter after): " : "Pilih metode (tekan Enter setelah): "); const confirmationPrompt = lang === "en" ? `Please type "${this.config.confirmation.en}" to confirm the transaction: ` : `Ketik "${this.config.confirmation.id}" untuk mengkonfirmasi transaksi: `; const confirmationInput = await this.askQuestion(confirmationPrompt); if ( (lang === "en" && confirmationInput.trim().toLowerCase() === this.config.confirmation.en) || (lang === "id" && confirmationInput.trim().toLowerCase() === this.config.confirmation.id) ) { console.log(lang === "en" ? "The transfer is completed" : "Proses transfer berhasil"); } else { console.log(lang === "en" ? "Transfer is cancelled" : "Transfer dibatalkan"); } this.rl.close(); } } module.exports = BankTransferApp; |
5. Membuat File Utama
Buat file index.js
:
1 2 3 4 |
const BankTransferApp = require('./app/BankTransferApp'); const app = new BankTransferApp(); app.run(); |
🚀 Cara Menjalankan Program
1 |
node index.js |
📌 Catatan Tambahan
- Konfigurasi mudah diubah cukup lewat file
bank_transfer_config.json
. - Bahasa aplikasi dan fee transfer otomatis berubah sesuai file JSON.
- Kode ini modular ➔ gampang dikembangkan.
🔥 Bonus Challenge untuk Mahasiswa
- Ubah bahasa default menjadi
id
lalu jalankan lagi programnya. - Tambahkan opsi metode transfer baru di file JSON.
- Apa yang terjadi kalau input transfer amount bukan angka?
✨ Penutup
Dengan belajar ini, mahasiswa tidak hanya bisa coding, tapi juga belajar pentingnya desain program yang fleksibel dan berorientasi ke file konfigurasi.