Create a class named UserAccounts that defines an array of 8 accounts as an inst
ID: 3757438 • Letter: C
Question
Create a class named UserAccounts that defines an array of 8 accounts as an instance variable. In the default constructor of this class, write a loop that creates 8 accounts with ids 1 through 7 and initial balance of $50 for each account. Store these accounts in the array.
When the program runs, it asks the use to enter a specific id. When the user enters a correct id, the system displays a menu as shown in the sample run below. The menu options are self explanatory. Option 4 exits the main menu. So if option 4 was chosen, the system will prompt the use again to enter another id. This means that the system will keep running indefinitely.
Sample Run:
Enter an id: 3 Main menu 1: check balance 2: withdraw 3 deposit 4:exit Enter a choice: 1 The balance is 5. Main menu 1: check balance 2: withdraw 3 deposit 4: exit Enter a choice: 2 Enter an amount to withdraw: 25 Main menu 1: check balance 2: withdraw 3 deposit 4: exit Enter a choice: 1 The balance is 25. Main menu 1: check balance 2: withdraw 3 deposit 4: exit Enter a choice: 3 Enter an amount to deposit: 1 Main menu 1: check balance 2: withdraw 3 deposit 4: exit Enter a choice: 1 The balance is 35.e Main menu 1: check balance 2: withdraw 3: deposit 4: exit Enter a choice: 4 Enter an id:Explanation / Answer
I was using GCC so i could not use cin and cout functions, instead i have used printf and scanf. You can change the same if you want to:
Source Code:
#include<stdio.h>
class UserAccounts{
public:
int id;
double balance;
};
int main(){
UserAccounts customer[8];
int i, custId, choice;
double remBal;
char ch;
for(i = 0 ; i < 8 ; i++){
customer[i].id = i+1;
customer[i].balance = 50.0;
}
printf( "Enter id: ");
scanf( "%d",&custId);
if(custId < 0 || custId > 9){
printf( "Invalid id !");
return -1;
}
do{
double withdraw, deposit;
printf( "Main Menu 1. Check balance 2.Withdraw 3.Deposit 4.Exit Enter a choice: ");
scanf( "%d", &choice);
printf(" ");
switch(choice){
case 1:
printf( "The balance is: %.2lf" , customer[custId].balance);
break;
case 2:
printf( "Enter an amount to withdraw: ");
scanf( "%lf", &withdraw);
remBal = customer[custId].balance - withdraw;
if(remBal < 0){
printf("Insufficient balance !!");
break;
}else{
customer[custId].balance -= withdraw;
}
break;
case 3:
printf( "Enter amount you want to deposit:" );
scanf( "%lf",&deposit);
customer[custId].balance += deposit;
break;
case 4:
printf( "Bye !!");
break;
}
while(getchar() != ' '); // flush the stream
printf(" Do you want to continue?(y/n): ");
scanf( "%c", &ch);
}while(ch == 'y' || ch == 'Y');
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.