#include <stdio.h>
#include <string.h>
#include "bank.h"
// TO:DO Create an account automatically for the user upon creating a new user
// TO:DO Handle spaces inside cell-phone input in createAccount();
// TO:DO Add area codes for cell phone
// Create the main bank object
void createBank( struct Bank *bank ) {
strcpy(bank->name, "Bank of Morten");
strcpy(bank->owner, "Morten Hauge");
bank->customer_index = 0;
}
// Add a new customer to the bank
void createCustomer(struct Bank *bank) {
struct Customer customer;
char input[35];
char first_name[35], last_name[35], address[35], city[35];
long int cell;
int zip;
// Fill out necessary customer information
printf("You are creating a new customer.\n\n");
printf("Enter first name:\n\n");
fgets(input, sizeof(input), stdin);
input[strlen(input) - 1] = '\0';
sscanf(input, "%s", first_name);
strcpy(customer.first_name, first_name);
printf("Enter last name:\n\n");
fgets(input, sizeof(input), stdin);
input[strlen(input) - 1] = '\0';
sscanf(input, "%s", last_name);
strcpy(customer.last_name, last_name);
printf("Enter address:\n\n");
fgets(input, sizeof(input), stdin);
input[strlen(input) - 1] = '\0';
sscanf(input, "%30[^\n]", address);
strcpy(customer.address, address);
printf("Enter zip:\n\n");
fgets(input, sizeof(input), stdin);
input[strlen(input) - 1] = '\0';
sscanf(input, "%i", &zip);
customer.zip = zip;
printf("Enter city:\n\n");
fgets(input, sizeof(input), stdin);
input[strlen(input) - 1] = '\0';
sscanf(input, "%s", city);
strcpy(customer.city, city);
printf("Enter cell number:\n\n");
fgets(input, sizeof(input), stdin);
input[strlen(input) - 1] = '\0';
sscanf(input, "%li", &cell);
customer.cell = cell;
customer.num_accounts = 0;
printf("\n"); // For cleaner formatting
bank->customers[bank->customer_index] = customer;
bank->customer_index += 1; // The next customer now gets a unique ID
}
// Add a new bank account to an already existing user, each user can have 10 accounts
void createAccount( struct Bank *bank ) {
printf("You have chosen to create a new account\n\n");
listCustomers( bank );
printf("Please enter a customer id:\n\n");
int id = promptForId();
char input[35], name[35];
if(id > bank->customer_index) {
printf("There is no customer with that ID");
} else {
struct Account account;
struct Customer customer = bank->customers[id];
printf("Enter account name:\n\n");
fgets(input, sizeof(input), stdin);
input[strlen(input) - 1] = '\0';
sscanf(input, "%30[^\n]", name);
strcpy(account.account_type, name);
account.owner_id = customer.id;
account.balance = 1000;
customer.accounts[customer.num_accounts] = account;
bank->customers[id] = customer;
customer.num_accounts += 1;
printf("\n"); // For cleaner formatting
}
}
// Print information about a given user
void printCustomer( struct Bank *bank ) {
printf("You have chosen to print information about a customer\n\n");
listCustomers( bank );
printf("Please enter a customer id:\n\n");
int id = promptForId();
if(id >= bank->customer_index) {
printf("There is no customer with that id\n");
} else {
struct Customer customer = bank->customers[id];
printf("First name: %s\n", customer.first_name);
printf("Last name: %s\n", customer.last_name);
printf("Mobile: %li\n", customer.cell);
printf("Address: %s\n", customer.address);
printf("Zip Code: %i\n", customer.zip);
printf("\n"); // For cleaner formatting
}
}
// Print information about a given account
void printAccount( struct Bank *bank ) {
printf("You have chosen to print information about an account\n\n");
listCustomers( bank );
printf("Please enter a customer id:\n\n");
int id = promptForId();
if(id >= bank->customer_index) {
printf("There is no customer with that id\n");
} else {
struct Customer customer = bank->customers[id];
printf("Please enter an account number\n\n");
int account_id = promptForId();
if(account_id > customer.num_accounts) {
printf("There is no account with that id\n\n");
} else {
struct Account account = customer.accounts[account_id];
printf("Account type: %s\n", account.account_type);
printf("Balance: %g\n", account.balance);
printf("Account owner: %s %s", customer.first_name, customer.last_name);
printf("\n"); // For cleaner formatting
}
}
}
// List all current customers of the bank
void listCustomers( struct Bank *bank ) {
printf("Customers:\n");
for(int i = 0; i < bank->customer_index; i++) {
struct Customer customer = bank->customers[i];
printf("[%i]: %s %s\n", i, customer.first_name, customer.last_name);
}
printf("\n");
}
// Ask the user to enter an id and returns it - can be used for customer id, account id etc.
int promptForId() {
char id_input[20];
int id = 0;
fgets(id_input, sizeof(id_input), stdin);
id_input[strlen(id_input) - 1] = '\0';
sscanf(id_input, "%i", &id);
return id;
}
// Main loop of the program - might consider having no arguments and creating the bank inside it directly
void bankChoiceLoop( struct Bank *bank ) {
createBank ( bank );
int user_answer;
char user_input[15];
do {
printf("1. Create new customer\n");
printf("2. Create new account\n");
printf("3. Show customer information\n");
printf("4. Show account information\n");
printf("5. Exit the program\n");
printf("\n"); // For cleaner formatting
fgets(user_input, sizeof(user_input), stdin);
user_input[strlen(user_input) - 1] = '\0';
sscanf(user_input, "%i", &user_answer);
switch(user_answer) {
case 1:
createCustomer( bank );
break;
case 2:
createAccount( bank );
break;
case 3:
printCustomer( bank );
break;
case 4:
printAccount( bank );
break;
case 5:
printf("You have chosen to exit the program\n");
break;
default: // Handle wrong input from the user
printf("Something went wrong, please review your input\n\n");
continue;
}
} while(user_answer != 5);
}
Comments