Created
March 7, 2024 04:34
-
-
Save rizkytegar/effc2f215827cc6d515af63957657019 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Nasabah { | |
private $nama; | |
private $saldo; | |
public function __construct($nama, $saldo) { | |
$this->nama = $nama; | |
$this->saldo = $saldo; | |
} | |
public function setor($jumlah) { | |
$this->saldo += $jumlah; | |
} | |
public function tarik($jumlah) { | |
if ($jumlah > $this->saldo) { | |
echo "Saldo tidak cukup\n"; | |
return; | |
} | |
$this->saldo -= $jumlah; | |
} | |
public function pinjam($jumlah) { | |
if ($jumlah > 0.3 * $this->saldo) { | |
echo "Jumlah pinjaman melebihi batas\n"; | |
return; | |
} | |
$this->saldo -= 0.1 * $jumlah; | |
} | |
public function getSaldo() { | |
return number_format($this->saldo, 0, ',', '.'); | |
} | |
} | |
function showMenu() { | |
echo "Pilih aksi:\n"; | |
echo "1. Setor\n"; | |
echo "2. Tarik\n"; | |
echo "3. Pinjam\n"; | |
echo "Masukkan pilihan (1/2/3): "; | |
return trim(fgets(STDIN)); | |
} | |
$nasabah = new Nasabah("Eka", 1000000); | |
while (true) { | |
echo "Saldo saat ini: Rp " . $nasabah->getSaldo() . "\n"; | |
$pilihan = showMenu(); | |
switch ($pilihan) { | |
case '1': | |
echo "Masukkan jumlah untuk setor: "; | |
$jumlah = (int) trim(fgets(STDIN)); | |
$nasabah->setor($jumlah); | |
break; | |
case '2': | |
echo "Masukkan jumlah untuk tarik: "; | |
$jumlah = (int) trim(fgets(STDIN)); | |
$nasabah->tarik($jumlah); | |
break; | |
case '3': | |
echo "Masukkan jumlah untuk pinjam: "; | |
$jumlah = (int) trim(fgets(STDIN)); | |
$nasabah->pinjam($jumlah); | |
break; | |
default: | |
echo "Pilihan tidak valid\n"; | |
break; | |
} | |
echo "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment