Advertisement
Stelios_Gakis

Seminar_2/Task_1

Sep 25th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.07 KB | None | 0 0
  1. package Seminar_2;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Task_1 {
  6.  
  7.     public static void main(String[] args) {
  8.  
  9.  
  10.         Scanner in = new Scanner(System.in);
  11.         Task_1 myApp = new Task_1();
  12.  
  13.         System.out.printf("Welcome to our bank!\n");
  14.  
  15.         System.out.print("Please, enter your name : ");
  16.         String name = in.nextLine();
  17.         System.out.print("Please, enter your address : ");
  18.         String address = in.nextLine();
  19.         System.out.print("Please, enter your phone number : ");
  20.         String phoneNumber = in.nextLine();
  21.         System.out.print("Please, enter your account number : ");
  22.         int accountNumber = in.nextInt();
  23.  
  24.         double balance = 0;
  25.         boolean exit = false;
  26.  
  27.         do {
  28.             System.out.printf("%n1. Deposit%n2. Withdraw%n3. View account%n4. Exit%n");
  29.             int choice = in.nextInt();
  30.             switch (choice) {
  31.                 case 1:
  32.                     balance = myApp.transaction("deposit", balance);
  33.                     break;
  34.                 case 2:
  35.                     balance = myApp.transaction("withdraw", balance);
  36.                     break;
  37.                 case 3:
  38.                     myApp.info(name, address, phoneNumber, accountNumber, balance);
  39.                     break;
  40.                 case 4:
  41.                     System.out.println("Are you sure you want to exit ? (yes/no)");
  42.                     String exitChoice = in.next();
  43.                     if (exitChoice.equalsIgnoreCase("yes") || exitChoice.equalsIgnoreCase("y")) {
  44.                         System.out.println("Thank you for visiting us.");
  45.                         exit = true;
  46.                     }
  47.                     break;
  48.                 default:
  49.                     System.out.println("Please enter a proper answer.");
  50.                     break;
  51.             }
  52.         } while (!exit);
  53.  
  54.     }
  55.  
  56.     private double deposit(double balance, double depositAmount) {
  57.         balance += depositAmount;
  58.         return balance;
  59.     }
  60.  
  61.     private double withdraw(double balance, double withdrawAmount) {
  62.         balance -= withdrawAmount;
  63.         return balance;
  64.     }
  65.  
  66.     private void info(String name, String address, String phoneNumber, int accountNumber, double balance) {
  67.         System.out.printf("Name : %s%nAddress : %s%nPhone number : %s%nAccount number : %d%n", name, address, phoneNumber, accountNumber);
  68.         System.out.println("Balance : " + balance);
  69.     }
  70.  
  71.     private double transaction(String actionType, double balance) {
  72.         Scanner in = new Scanner(System.in);
  73.         System.out.println("Please enter the amount you would like to " + actionType + " : ");
  74.         if (actionType.equals("deposit")) {
  75.             double depositAmount = in.nextDouble();
  76.             balance = deposit(balance, depositAmount);
  77.         } else if (actionType.equals("withdraw")) {
  78.             double withdrawAmount = in.nextDouble();
  79.             balance = withdraw(balance, withdrawAmount);
  80.         }
  81.         System.out.println("Your new balance is : " + balance);
  82.         return balance;
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement