jaredec18

Untitled

Sep 12th, 2019
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. /*
  2. PART - A: Please find the required solution:
  3. --------------------------------------------
  4. */
  5.  
  6. #include <iostream>
  7. using namespace std;
  8. int main()
  9. {
  10. string input;
  11. do
  12. {
  13. cout << "bank>";
  14. cin >> input;
  15. if(input.compare("deposit") == 0)
  16. cout << "DEPOSIT SELECTED";
  17. else if(input.compare("withdraw") == 0)
  18. cout << "WITHDRAW SELECTED";
  19. else if(input.compare("balance") == 0)
  20. cout << "BALANCE SELECTED";
  21. else if(input.compare("quit")!= 0 )
  22. cout << "Please select correct operation";
  23. cout<<"\n";
  24. } while(input.compare("quit") != 0);
  25. return 0;
  26. }
  27.  
  28. /*
  29. Sample output:
  30. --------------
  31. bank>deposit
  32. DEPOSIT SELECTED
  33. bank>withdraw
  34. WITHDRAW SELECTED
  35. bank>balance
  36. BALANCE SELECTED
  37. bank>quit
  38. */
  39.  
  40. /*
  41. PART - B: Please find the required solution:
  42. --------------------------------------------
  43. */
  44.  
  45. #include <iostream>
  46.  
  47. using namespace std;
  48.  
  49. class Bank
  50. {
  51.  
  52. int account_balance[10] ;
  53.  
  54. public:
  55. void initialize() {
  56. for(int i=0 ; i<10;i++)
  57. account_balance[i] = 0;
  58. };
  59. void deposit (int num,int value);
  60. void withdraw (int num,int value);
  61. int balance(int num);
  62. void transfer(int from, int to, int value);
  63. };
  64.  
  65. void Bank::deposit (int num, int value)
  66. {
  67. account_balance[num] = account_balance[num] + value;
  68. }
  69.  
  70. void Bank::withdraw (int num, int value)
  71. {
  72. if(account_balance[num] >= value)
  73. account_balance[num] = account_balance[num] - value;
  74. else
  75. cout << "Insufficient Balance in account" << num << "\n";
  76. }
  77. int Bank::balance (int num)
  78. {
  79. return account_balance[num];
  80. }
  81.  
  82. void Bank::transfer(int from, int to, int value)
  83. {
  84. if(account_balance[from] >= value)
  85. {
  86. account_balance[from] = account_balance[from] - value;
  87. account_balance[to] = account_balance[to] + value;
  88. }
  89. else
  90. {
  91. cout << "Error! funds exceeded" << "\n";
  92. }
  93. }
  94.  
  95. int main()
  96. {
  97. Bank bank;
  98. string input;
  99. int num1,num2,value;
  100.  
  101. bank.initialize();
  102.  
  103. do
  104. {
  105. cout << "bank>";
  106. cin >> input ;
  107. if(input.compare("balance") == 0) {
  108. cin >> num1;
  109. cout << bank.balance(num1);
  110. }
  111. else if(input.compare("deposit") == 0){
  112. cin >> num1 >> num2;
  113. bank.deposit(num1,num2);
  114. }
  115. else if(input.compare("withdraw") == 0){
  116. bank.withdraw(num1,num2);
  117. }
  118. else if(input.compare("transfer") == 0){
  119. cin >> num1 >> num2 >> value;
  120. bank.transfer(num1,num2,value);
  121. }
  122. else if(input.compare("quit")!= 0 )
  123. cout << "Please select correct operation";
  124. cout<<"\n";
  125. } while(input.compare("quit") != 0);
  126. return 0;
  127. }
  128.  
  129. /*
  130. Sample output:
  131. bank>deposit 3 50
  132.  
  133. bank>balance 3
  134. 50
  135. bank>balance 8
  136. 0
  137. bank>transfer 3 8 20
  138.  
  139. bank>balance 3
  140. 30
  141.  
  142. bank>balance 8
  143. 20
  144.  
  145. bank>transfer 3 2 40
  146. Error! funds exceeded
  147.  
  148. bank>quit
  149. --------------*/
Advertisement
Add Comment
Please, Sign In to add comment