Skip to content

Instantly share code, notes, and snippets.

@haliknihudas666
Last active January 10, 2021 20:48
Show Gist options
  • Save haliknihudas666/06884a4a2dafb1a26eabfeab43fa91e8 to your computer and use it in GitHub Desktop.
Save haliknihudas666/06884a4a2dafb1a26eabfeab43fa91e8 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
struct client {
int id;
int card_number;
char *name;
int pin;
double money;
};
struct node {
struct client *data;
struct node *next;
};
struct linked_list {
struct node *head;
struct node *tail;
};
//User inputs
int passinput;
int cardinput;
float moneyinput;
int choiceinput;
struct client *new_client(int id, int card_number, char *name, int pin, double money) {
struct client *c = (struct client *) malloc(sizeof(struct client));
//c = realloc(c, sizeof(struct client*));
c->id = id;
c->card_number = card_number;
c->name = strdup(name);
c->pin = pin;
c->money = money;
return c;
}
struct node *new_node(struct client *data) {
struct node *n = malloc(sizeof(struct node *));
//n = realloc(n, sizeof(struct node*));
n->data = data;
n->next = NULL;
return n;
}
struct linked_list *new_list() {
struct linked_list *l = malloc(sizeof(struct linked_list *));
//l = realloc(l, sizeof(struct linked_list*));
l->head = NULL;
l->tail = NULL;
return l;
}
void list_add(struct linked_list *list, struct node *n) {
if (list->head == NULL) {
list->head = n;
} else {
list->tail->next = n;
}
list->tail = n;
}
void list_print(struct linked_list *list) {
printf_s("\nWelcome to Letran Bank\n\n");
printf_s("VIEW RECORDS\n");
printf("%3s %11s %25s %20s\n", "ID", "CARD NUMBER", "NAME", "BALANCE");
for (struct node *n = list->head; n != NULL; n = n->next) {
printf("%3d %11d %25s %20f\n", n->data->id, n->data->card_number, n->data->name, n->data->money);
}
}
void change_pin(struct linked_list *list, int cardnumber) {
for (struct node *n = list->head; n != NULL; n = n->next) {
if (n->data->card_number == cardnumber) {
printf_s("\nWelcome to Letran Bank\n\n");
printf_s("CHANGE PIN\n");
printf_s("Enter current pin: ");
scanf("%d", &passinput);
if (n->data->pin == passinput) {
printf_s("Enter new pin: ");
scanf("%d", &passinput);
n->data->pin = passinput;
} else {
printf("Wrong pin.");
}
}
}
}
void deposit_money(struct linked_list *list, int cardnumber) {
for (struct node *n = list->head; n != NULL; n = n->next) {
if (n->data->card_number == cardnumber) {
printf_s("\nWelcome to Letran Bank\n\n");
printf_s("DEPOSIT MONEY\n");
printf_s("Enter amount to be deposited: ");
scanf("%f", &moneyinput);
n->data->money += moneyinput;
printf_s("Current Balance is: %f", n->data->money);
}
}
}
void withdraw_money(struct linked_list *list, int cardnumber) {
for (struct node *n = list->head; n != NULL; n = n->next) {
if (n->data->card_number == cardnumber) {
printf_s("\nWelcome to Letran Bank\n\n");
printf_s("WITHDRAW MONEY\n");
printf_s("Enter amount to be withdrawn: ");
scanf("%f", &moneyinput);
if (moneyinput <= n->data->money) {
n->data->money -= moneyinput;
printf_s("Withdrawal success.\n");
} else {
printf_s("Insufficient Balance.\n");
}
printf_s("Current Balance is: %f", n->data->money);
}
}
}
void check_balance(struct linked_list *list, int cardnumber) {
for (struct node *n = list->head; n != NULL; n = n->next) {
if (n->data->card_number == cardnumber) {
printf_s("\nWelcome to Letran Bank\n\n");
printf_s("CHECK BALANCE\n");
printf_s("Your Current Savings Balance is Php %f \n\n", n->data->money);
}
}
}
void search_records(struct linked_list *list) {
printf_s("\nWelcome to Letran Bank\n\n");
printf_s("VIEW RECORDS\n");
int input;
printf("Search card number: ");
scanf_s("%d", &input);
printf("%3s %11s %25s %20s\n", "ID", "CARD NUMBER", "NAME", "BALANCE");
for (struct node *n = list->head; n != NULL; n = n->next) {
if (n->data->card_number == input) {
printf("%3d %11d %25s %20f\n", n->data->id, n->data->card_number, n->data->name, n->data->money);
}
}
}
int authentication(struct linked_list *list, int cardnumber, int pin) {
for (struct node *n = list->head; n != NULL; n = n->next) {
if (cardnumber == 123456) {
if (pin == 123456) {
return 1;
} else {
return 3;
}
} else if (n->data->card_number == cardnumber) {
if (n->data->pin == pin) {
return 2;
} else {
return 3;
}
}
}
}
int main() {
int currentcard;
//creates new list
struct linked_list *list = new_list();
//creates new values to be added to nodes;
struct client *c1 = new_client(1, 123456, "Admin", 123456, 0);
struct client *c2 = new_client(2, 111111, "Nicolei Esperida", 111111, 1000);
struct client *c3 = new_client(3, 666666, "Hudas", 666666, 666);
//creates new nodes and add the values
struct node *n1 = new_node(c1);
struct node *n2 = new_node(c2);
struct node *n3 = new_node(c3);
//add each nodes to the list
list_add(list, n1);
list_add(list, n2);
list_add(list, n3);
while (choiceinput != 6) {
printf_s("Welcome to LETRAN BANK\n\n");
printf("Enter cardnumber: ");
scanf_s("%d", &cardinput);
currentcard = cardinput;
printf("Enter pin: ");
scanf_s("%d", &passinput);
if (authentication(list, cardinput, passinput) == 1) {
while (choiceinput != 5) {
printf("\nWelcome to Letran Bank, Admin!\n\n");
printf("[1] View all records\n");
printf("[2] Delete records\n");
printf("[3] Modify records\n");
printf("[4] Search records\n");
printf("[5] Logout\n");
printf("[6] Terminate\n");
printf("Enter choice: ");
scanf_s("%d", &choiceinput);
switch (choiceinput) {
case 1:
list_print(list);
break;
case 2:
//delete_records(list);
break;
case 3:
//modify_records(list);
break;
case 4:
search_records(list);
break;
case 5:
break;
case 6:
break;
default:
printf_s("Invalid input.\n");
break;
}
}
} else if (authentication(list, cardinput, passinput) == 2) {
while (choiceinput != 5) {
printf("\nWelcome to Letran Bank\n\n");
printf("[1] Check Balance\n");
printf("[2] Withdraw\n");
printf("[3] Deposit\n");
printf("[4] Change Pin\n");
printf("[5] Logout\n");
printf("[6] Terminate\n");
printf("Enter choice: ");
scanf_s("%d", &choiceinput);
switch (choiceinput) {
case 1:
check_balance(list, currentcard);
break;
case 2:
withdraw_money(list, currentcard);
break;
case 3:
deposit_money(list, currentcard);
break;
case 4:
change_pin(list, currentcard);
break;
case 5:
break;
case 6:
break;
default:
printf_s("Invalid input.\n");
break;
}
}
} else if (authentication(list, cardinput, passinput)) {
printf("Wrong Pin.\n");
} else {
printf("User doesn't exist.\n");
}
}
return 0;
}
///ADMIN///
void delete_records() {
printf_s("\nWelcome to Letran Bank\n\n");
printf_s("VIEW RECORDS\n");
}
void modify_records() {
printf_s("\nWelcome to Letran Bank\n\n");
printf_s("VIEW RECORDS\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment