Facebook
From rozaq, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 240
  1. form1.cs
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11.  
  12. namespace Chapter3
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.  
  17.         Akun akun1 = new Akun(20000, "hiya");
  18.         public Form1()
  19.         {
  20.             InitializeComponent();
  21.             lblSaldo.Text = akun1.Saldo.ToString();
  22.         }
  23.  
  24.         private void TarikSaldo(int total)
  25.         {
  26.             akun1.Tarik(total);
  27.             lblSaldo.Text = akun1.Saldo.ToString();
  28.         }
  29.  
  30.         private void SetorSaldo(int total)
  31.         {
  32.             akun1.Setor(total);
  33.             lblSaldo.Text = akun1.Saldo.ToString();
  34.         }
  35.  
  36.         private void btnLanjut_Click(object sender, EventArgs e)
  37.         {
  38.             if(akun1.PasswordTervalidasi(tbPassword.Text))
  39.             {
  40.                 if (rbSetor.Checked)
  41.                     SetorSaldo(int.Parse(tbUang.Text));
  42.                 else if (rbTarik.Checked)
  43.                     TarikSaldo(int.Parse(tbUang.Text));
  44.             }
  45.             else
  46.             {
  47.                 MessageBox.Show("Password salah");
  48.             }
  49.                
  50.            
  51.            
  52.         }
  53.     }
  54. }
  55.  
  56. Akun.cs
  57. using System;
  58. using System.Collections.Generic;
  59. using System.Linq;
  60. using System.Text;
  61. using System.Threading.Tasks;
  62.  
  63. namespace Chapter3
  64. {
  65.     public class Akun
  66.     {
  67.         private const int maxSaldo = 2000000;
  68.         private int _saldo;
  69.         protected string password;
  70.        
  71.  
  72.        
  73.         public int Saldo
  74.         {
  75.             get
  76.             {
  77.                 return _saldo;
  78.             }
  79.             set
  80.             {
  81.                 if (value < 0)
  82.                     throw new InvalidOperationException("Saldo tidak bisa kurang dari 0 rupiah");
  83.                 else if (value > maxSaldo)
  84.                     throw new InvalidOperationException("Saldo tidak boleh melebihi 2000000 rupiah");
  85.                 else
  86.                     _saldo = value;
  87.             }
  88.         }
  89.  
  90.         public Akun(int saldo, string password)
  91.         {
  92.            
  93.             _saldo = saldo;
  94.             this.password = password;
  95.         }
  96.  
  97.         public  void Setor(int jumlah)
  98.         {
  99.             Saldo += jumlah;
  100.         }
  101.  
  102.         public  void Tarik(int jumlah)
  103.         {
  104.             Saldo -= jumlah;
  105.         }
  106.  
  107.         public bool PasswordTervalidasi(string password)
  108.         {
  109.             if (this.password == password)
  110.                 return true;
  111.             else
  112.                 return false;
  113.         }
  114.     }
  115. }
  116.