Advertisement
Guest User

anjing2.0

a guest
Dec 11th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.58 KB | None | 0 0
  1. //BANK ACCOUNT CLASS
  2.  
  3. package l8q2;
  4. import java.util.Scanner;
  5. import java.io.PrintWriter;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. /**
  9.  *
  10.  * @author WIF170096
  11.  */
  12. public class BankAccount {
  13.      Scanner k = new Scanner(System.in);
  14.      
  15.      private String name;
  16.      private String ic;
  17.      private double depo;
  18.      double savings = 0;
  19.      double withdraw = 0;
  20.      double savings_initial = 0;
  21.      
  22.      public BankAccount(){
  23. }
  24.  
  25.      public BankAccount(String a, String b, double c){
  26.          name = a;
  27.          ic = b;
  28.          savings = c;
  29.          savings_initial = c;
  30.      }
  31.      
  32.      public String getName(){
  33.         System.out.print("Enter your name: ");
  34.         name = k.nextLine();
  35.         return name;
  36.        
  37.      }
  38.      
  39.      public String getIC(){
  40.          System.out.print("Enter your IC: ");
  41.          ic = k.next();
  42.          return ic;
  43.      }
  44.      
  45.      public double getDepo(){
  46.          depo = k.nextDouble();
  47.          savings_initial = depo;
  48.          return depo;
  49.          
  50.      }
  51.      
  52.      public void deposit(){
  53.          savings = savings + depo;
  54.          
  55.      }
  56.      
  57.      public void withdraw(){
  58.          
  59.          withdraw = k.nextDouble();
  60.          savings = savings - withdraw;
  61.          
  62.      }
  63.      
  64.      public void display(){
  65.          System.out.printf("Updated balance = RM %.2f \n", savings);
  66.          
  67.      }
  68.          
  69.      public void current(){
  70.          System.out.printf("%.2f", savings);
  71.          
  72.      }
  73.      
  74.      public void Log(){
  75.          try{
  76.          PrintWriter os = new PrintWriter(new FileOutputStream("Z:/L8Q2/log.txt"));
  77.          os.println("Name: " + name);
  78.          os.println("IC; " + ic);
  79.          os.println("Initial savings: " + savings_initial);
  80.          os.println("Deposit amount: " + depo);
  81.          os.println("Withdraw amount: " + withdraw);
  82.          os.println("Final amount: " + savings);
  83.          os.close();
  84.          System.out.println("Logged");
  85.          
  86.          }
  87.          catch (IOException e) {
  88.              System.out.println("Problem with output");
  89.          }
  90.          
  91.      }
  92. }
  93.  
  94.  
  95. // MAIN/TESTER CLASS
  96.  
  97. package l8q2;
  98. import java.util.Scanner;
  99. import java.io.PrintWriter;
  100. import java.io.FileOutputStream;
  101. import java.io.IOException;
  102. /**
  103.  *
  104.  * @author WIF170096
  105.  */
  106. public class L8Q2 {
  107.  
  108.     /**
  109.      * @param args the command line arguments
  110.      */
  111.     public static void main(String[] args) {
  112.         // TODO code application logic here
  113.         Scanner p = new Scanner(System.in);
  114.         BankAccount s1 = new BankAccount();
  115.        
  116.         s1.getName();
  117.         s1.getIC();
  118.         System.out.print("Enter deposit amount: ");
  119.         s1.getDepo();
  120.         s1.deposit();
  121.        
  122.         System.out.print("Select your transactions(Current Balance: ");
  123.         s1.current();
  124.         System.out.print("):\n");
  125.         System.out.println("1.Deposit \n2.Withdrawal ");
  126.        
  127.         int check = p.nextInt();
  128.        
  129.         switch(check){
  130.             case 1:System.out.println("Enter your deposit amount: ");
  131.                    s1.getDepo();
  132.                    s1.deposit();
  133.                    s1.display();
  134.                    s1.Log();
  135.                    break;
  136.            
  137.             case 2:System.out.println("Enter your withdrawal amount: ");
  138.                    s1.withdraw();
  139.                    s1.display();
  140.                    s1.Log();
  141.                    break;
  142.            
  143.             default:System.out.println("Please insert the right number.");
  144.        
  145.          
  146.         s1.Log();
  147.         }
  148.        
  149.     }
  150.    
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement