TEST(test_bank_account, test_withdraw) { srand(time(0)); auto amount1 = rand() / (RAND_MAX / 100.0); auto amount2 = rand() / (RAND_MAX / 100.0); auto a = BankAccount("Roope Rikas", "12345", amount1); auto b = BankAccount("Kaisa Köyhä", "53457", amount2); auto withdraw1 = rand() / (RAND_MAX / 100.0); while(withdraw1 >= amount1) withdraw1 = rand() / (RAND_MAX / 100.0); auto withdraw2 = rand() / (RAND_MAX / 100.0); while(withdraw2 >= amount2) withdraw2 = rand() / (RAND_MAX / 100.0); a.withdraw(withdraw1); b.withdraw(withdraw2); EXPECT_EQ(amount1-withdraw1, a.getBalance()); EXPECT_EQ(amount2-withdraw2, b.getBalance()); a.withdraw(a.getBalance()+100.0); EXPECT_EQ(amount1-withdraw1, a.getBalance()); }
void handleEvents_( DNSServiceRef service, const int32_t timeout = -1 ) { assert( service ); if( !service ) return; const int fd = DNSServiceRefSockFD( service ); const int nfds = fd + 1; while( !handled_ ) { fd_set fdSet; FD_ZERO( &fdSet ); FD_SET( fd, &fdSet ); struct timeval tv; tv.tv_sec = timeout / 1000; tv.tv_usec = (timeout % 1000) * 1000; const int result = ::select( nfds, &fdSet, 0, 0, timeout < 0 ? 0 : &tv ); switch( result ) { case 0: // timeout return; case -1: // error LBWARN << "Select error: " << strerror( errno ) << " (" << errno << ")" << std::endl; if( errno != EINTR ) { withdraw(); handled_ = true; } break; default: if( FD_ISSET( fd, &fdSet )) { const DNSServiceErrorType error = DNSServiceProcessResult( service ); if( error != kDNSServiceErr_NoError ) { LBWARN << "DNSServiceProcessResult error: " << error << std::endl; withdraw(); handled_ = true; } } break; } } handled_ = false; }
int main () { bank b1 = {3876, "checkings account", 1000}; bank b2 = {9845, "savings account", 9000}; b1 = deposit(b1, 100); printf ("After your deposit, this is the new account balance for account %d: %f\n", b1.accountNumber, b1.accountBalance); b1 = withdraw(b1, 400); printf ("After your withdrawl, this is the new account balance for account %d: %f\n", b1.accountNumber, b1.accountBalance); b2 = deposit(b2, 5000); printf ("After your deposit, this is the new account balance for account %d: %f\n", b2.accountNumber, b2.accountBalance); b2 = withdraw(b2, 690); printf ("After your withdrawl, this is the new account balance for account %d: %f\n", b2.accountNumber, b2.accountBalance); }
/** * Forks processes based on the type of process * that the user wants to occur */ void process_fork(int deposit_or_withdraw, int request) { pid_t child_pid; child_pid = fork(); // Fork failed if (child_pid == -1) { perror("Failed to fork process!\n"); exit(EXIT_FAILURE); } // We have a child process else if (child_pid == 0) { // Withdraw if (deposit_or_withdraw == WITHDRAW) { withdraw(request); } // Deposit else if (deposit_or_withdraw == DEPOSIT) { deposit(request); } else { printf("Invalid process type. Choose Withdraw or Deposit.\n"); exit(EXIT_FAILURE); } } // Parent else { return; } }
int main(){ char name; printf("What is your name"); scanf("%s", &name); printf("Would you like to deposit(type 1) or withdraw (type 2)\n"); bank bank1 = {1, name, 600.0}; bank bank2 = {2, name, 500.0}; int response; scanf("%d", &response); if(response == 1){ float depo; printf("How much are you depositing"); scanf("%f", &depo); deposit(depo, bank1); } if(response ==2){ float with; printf("How much are you withdraw"); scanf("%f",&with); withdraw(bank1,with); } printf("%f",bank1.balance); }
void PrefixUpdateProcessor::onCommandValidated(const std::shared_ptr<const ndn::Interest>& request) { const ndn::Name& command = request->getName(); const ndn::Name::Component& verb = command[COMMAND_PREFIX.size()]; const ndn::Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1]; if (verb == ADVERTISE_VERB || verb == WITHDRAW_VERB) { ndn::nfd::ControlParameters parameters; if (!extractParameters(parameterComponent, parameters)) { sendResponse(request, 400, "Malformed command"); return; } if (verb == ADVERTISE_VERB) { advertise(request, parameters); } else if (verb == WITHDRAW_VERB) { withdraw(request, parameters); } sendResponse(request, 200, "Success"); } else { sendResponse(request, 501, "Unsupported command"); } }
void transfer(int fromB, int toB, int account, int value) { if (fromB < toB) { pthread_mutex_lock(&(Bank[fromB].b_lock)); pthread_mutex_lock(&(Bank[toB].b_lock)); } else if (fromB > toB) { pthread_mutex_lock(&(Bank[toB].b_lock)); pthread_mutex_lock(&(Bank[fromB].b_lock)); } else return; int money = withdraw(fromB, account, value); if (money > 0) deposit(toB, account, money); if (fromB < toB) { pthread_mutex_unlock(&(Bank[toB].b_lock)); pthread_mutex_unlock(&(Bank[fromB].b_lock)); } else if (fromB > toB) { pthread_mutex_unlock(&(Bank[fromB].b_lock)); pthread_mutex_unlock(&(Bank[toB].b_lock)); } }
int main () { Account account1; Account account2; account1.balance = 1000; account2.balance = 5000; strcpy (account1.name, "John"); strcpy (account2.name, "Bob"); account1.number = 3000; account2.number = 2000; printf ("%s your balance is %d\n", account1.name, account1.balance); printf ("%s your balance is %d\n", account2.name, account2.balance); account1.balance = deposit (account1, 3); account2.balance = deposit (account2, 5); account1.balance = withdraw (account1, 10); account2.balance = withdraw (account2, 15); return 0; }
/************************************************************************* * Base worker routine * * Child process looks at req_buf to determine requested operation. * Executes assoicated account routine, sends response back to client. *************************************************************************/ void process_request(char *req_buf, int conn) { char resp_buf[COMM_BUF_SIZE]; int trans_id; /* The transaction type of the request */ sscanf(req_buf, "%d", &trans_id); DPRINTF(("SERVER: child process %d, processing request \n", child_pid)); /* "p()" num active semaphore */ num_active_sem_buf.sem_num = 0; num_active_sem_buf.sem_op = -1; /* "p()" */ num_active_sem_buf.sem_flg = SEM_UNDO; if (semop(num_active_sem_id, &num_active_sem_buf, 1) < 0) { perror("SERVER: child process, num_active p semop() failed %d\n"); exit (1); } switch(trans_id) { case OPEN_ACCT_TRANS: open_account(resp_buf); break; case DEPOSIT_TRANS: deposit(req_buf, resp_buf); break; case WITHDRAW_TRANS: withdraw(req_buf, resp_buf); break; case BALANCE_TRANS: balance(req_buf, resp_buf); break; default: handle_bad_trans_id(req_buf, resp_buf); break; } server_comm_send_response(conn, resp_buf); DPRINTF(("SERVER: child process %d, response sent\n", child_pid)); /* "v()" num active semaphore */ num_active_sem_buf.sem_num = 0; num_active_sem_buf.sem_op = 1; /* "v()" */ num_active_sem_buf.sem_flg = SEM_UNDO; if (semop(num_active_sem_id, &num_active_sem_buf, 1) < 0) { perror("SERVER child process, num_active v semop() failed %d\n"); exit (1); } exit(0); }
// simulate id performing 1000 transactions void do1000Transactions(unsigned long id) { for (unsigned i = 0; i < 1000; i++) { if ( odd( id ) ) { deposit(100.00); // odd threads deposit } else { withdraw(100.00); // even threads withdraw } } }
void *petar(void *arg) { int i; for(i = 0; i < 1000000; i++) { if(rand() % 1000 <= 500) { deposit(rand() % 10000, &petar_account); } else { withdraw(rand() % 10000, &petar_account); } } }
void Account::transfer( const int& to, const double& amount ) { //Transfer to checking account //Withdraw from savings, deposit to checkings if ( to == 0 ) { withdraw( 1, amount ); deposit( 0, amount ); } //Transfer to savings account //Withdraw from checking, deposit to saving else if ( to == 1 ) { withdraw( 0, amount ); deposit( 1, amount ); } return; }
void BuyiPhone() { while(1) { wait(t); wait(s); withdraw(); signal(s); buy(); } }
void *test(void *data) { int rand_max; thread_data_t *d = (thread_data_t *)data; unsigned short seed[3]; seeds = seed_rand(); //#ifdef __sparc__ phys_id = the_cores[d->id]; cluster_id = get_cluster(phys_id); //#else // phys_id = d->id; //#endif /* Initialize seed (use rand48 as rand is poor) */ seed[0] = (unsigned short)rand_r(&d->seed); seed[1] = (unsigned short)rand_r(&d->seed); seed[2] = (unsigned short)rand_r(&d->seed); rand_max = d->bank->size - 1; /* local initialization of locks */ local_th_data[d->id] = init_lock_array_local(phys_id, d->bank->size, the_locks); /* Wait on barrier */ barrier_cross(d->barrier); while (stop == 0) { uint32_t nb = (uint32_t) (my_random(&(seeds[0]),&(seeds[1]),&(seeds[2])) % 100); uint32_t acc = (uint32_t) my_random(&(seeds[0]),&(seeds[1]),&(seeds[2])) & rand_max; account_t* accp = &d->bank->accounts[acc]; if (nb < d->deposit_perc) { deposit(accp, 1, d->id); d->nb_deposit++; } else if (nb < d->withdraw_perc) { withdraw(accp, 1, d->id); d->nb_withdraw++; } else /* nb < balance_perc */ { check(accp, d->id); d->nb_balance++; } } /* Free locks */ //free_local(local_th_data[d->id], d->bank->size); return NULL; }
void displayAccounts(int *aPtr, float *irPtr, float *iaPtr, int accountNum) { int choice = 0; printf("Account#: %d\n",accountNum+1); printf("\tBalance:\t%d\n",*(aPtr+accountNum)); printf("\tInterest Rate:\t%.2f\n",*(irPtr+accountNum)); printf("\tInterest:\t%.2f\n",*(iaPtr+accountNum)); printf("Select one of the following options:\n"); printf("1 - Deposit\n"); printf("2 - Withdraw\n"); printf("3 - Done\n"); scanf("%d",&choice); while(choice<1 || choice >3) { printf("You have entered an invalid option. Please choose again.\n"); printf("Please select one of the following options:\n"); printf("1 - Deposit\n"); printf("2 - Withdraw\n"); printf("3 - Done\n"); scanf("%d",&choice); } if(choice==1) { deposit(aPtr, accountNum); calculateInterest(aPtr, irPtr, iaPtr, accountNum); printf("Account#: %d\n",accountNum+1); printf("\tBalance:\t%d\n",*(aPtr+accountNum)); printf("\tInterest Rate:\t%.2f\n",*(irPtr+accountNum)); printf("\tInterest:\t%.2f\n",*(iaPtr+accountNum)); } else if(choice==2) { withdraw(aPtr, accountNum); calculateInterest(aPtr, irPtr, iaPtr, accountNum); printf("Account#: %d\n",accountNum+1); printf("\tBalance:\t%d\n",*(aPtr+accountNum)); printf("\tInterest Rate:\t%.2f\n",*(irPtr+accountNum)); printf("\tInterest:\t%.2f\n",*(iaPtr+accountNum)); } else { printf("Account#: %d\n",accountNum+1); printf("\tBalance:\t%d\n",*(aPtr+accountNum)); printf("\tInterest Rate:\t%.2f\n",*(irPtr+accountNum)); printf("\tInterest:\t%.2f\n",*(iaPtr+accountNum)); } return; }
void *ivan(void *arg) { int i; for(i = 0; i < 1000000; i++) { if(rand() % 2 <= 1) { deposit(rand() % 10000, &ivan_account); } else { withdraw(rand() % 10000, &ivan_account); } } }
void registerCB_( const char* name, const char* type, const char* domain, DNSServiceErrorType error ) { if( error != kDNSServiceErr_NoError) { LBWARN << "Register callback error: " << error << std::endl; withdraw(); } else LBINFO << "Registered " << name << "." << type << "." << domain << std::endl; handled_ = true; }
void *start_thread (void *pass_data) { // casting struct account *customer = (struct account *) pass_data; // get the thread TID const pthread_t me = pthread_self(); // printf() is thread-safe, so it won't output gibberish text because of race condition!! printf("%u\t%s\t%f\n", me, customer->name, customer->balance); int rc = withdraw (customer, WITHDRAW); if(rc){ fprintf(stderr, "can't withdraw $%d because the existent amount is $%f\n", WITHDRAW, customer->balance); } // done return pass_data; }
void send_to_downstream(Node *store, int sock_fd, BankConfig config) { pthread_mutex_lock(&sending_lock); PointerContainer *head = NULL, *temp = NULL; head = withdraw(store, config.bank_max_msg_length); int i = 0; int l = 0; int hash = 0; while (head != NULL ) { temp = head; head = head->next; l = strlen((char*) (temp->contained)); log_debug("Send %d char! %s", l, (char*) (temp->contained)); if (config._downstream_consistent_hashing == 0) { for (i = 0; i < config.destiny_host_count; i++) { if (config.downstream_sockaddr[i] != NULL && (config._destiny_health_check == 0 || config.downstream_error_count[i] <= DOWN_STREAM_ERROR_THRESHOLD)) { sendto(sock_fd, (char*) (temp->contained), l, 0, (struct sockaddr *) (config.downstream_sockaddr[i]), config.sockaddr_len); } } } else { hash = (int) get_hash(temp->label, strlen(temp->label), config.downstream_hash_ring_length); i = get_category(hash, config.downstream_hash_ring, config.downstream_error_count, DOWN_STREAM_ERROR_THRESHOLD, config.destiny_host_count, config.downstream_consistent_hash_replica); log_debug("Consistent hashing shows we send it to %d.", i); if (i < 0) { log_warning("No downstream end-point is available."); continue; } if (config.downstream_sockaddr[i] != NULL ) { sendto(sock_fd, (char*) (temp->contained), l, 0, (struct sockaddr *) (config.downstream_sockaddr[i]), config.sockaddr_len); } } free((char*) (temp->contained)); delete_container(&temp); } pthread_mutex_unlock(&sending_lock); }
void main() { int i; clrscr(); add(); clrscr(); printf("\nThe no. of available books are:\n"); display(); printf("\n1:Withdarw \n2:Deposit\n3:Exit\nEnter your option:"); scanf("%d",&i); if(i==1) withdraw(); if(i==2) deposit(); if(i==3) { getch(); return; } }
void withdrawMenu() { printf("\t enter Id>"); long Id = 0; scanf("%ld", &Id); printf("\tenter passworld>"); long pass = 0; scanf("%ld", &pass); printf("\tenter balance>"); double balance = 0; scanf("%lf", &balance); long ret = withdraw(Id, balance, pass); if (-1 == ret) { printf("===========================\n"); printf("passworld is invalid or no enough money\n"); printf("===========================\n"); } else { printf("===========================\n"); printf("witdraw successful!\n"); printf("===========================\n"); } }
void capd::krak::job::terminate(void) { jobfrm.Clear(desktopColor); jobfrm.Bound(desktopColor); if(immortal) status=sleeping; capd::krak::job *l=(capd::krak::job *)(children.last()),*lprev; if(l!=NULL) while (l!=(capd::krak::link *)&children) { lprev=(capd::krak::job *)(l->prev()); l->terminate(); l=lprev; } if(this != capd::krak::job::rootJob) { if(status!=sleeping) destroy(); else withdraw(); } }
void Session::input(){ printf("Enter a command:\n"); getline(cin, command); command = lower(command); while(command.compare("logout") != 0){ if(command.compare("deposit") == 0){ deposit(); } else if(command.compare("withdraw") == 0){ withdraw(); } else if(command.compare("create") == 0){ create(); } else if(command.compare("delete") == 0){ deleteAccount(); } else if(command.compare("enable") == 0){ enable(); } else if(command.compare("disable") == 0){ disable(); } else if(command.compare("paybill") == 0){ paybill(); } else if(command.compare("transfer") == 0){ transfer(); } else if(command.compare("changeplan") == 0){ changeplan(); } else if(command.compare("login") == 0){ printf("ERROR: You are currently in an active session. Log out first\n"); } else { printf("Error, unrecognized command\n"); } printf("Enter a command:\n"); if(!getline(cin, command)){ printf("End of file, logging out.\n"); break; } command = lower(command); } logout(); return; }
void wait_for_action() { interface_hardware.send(display_withdrawal_options()); incoming.wait() .handle<withdraw_pressed>( [&](withdraw_pressed const & msg) { withdrawal_amount = msg.amount; bank.send(withdraw(account, msg.amount, incoming)); state = &atm::process_withdrawal; } ) .handle<balance_pressed>( [&](balance_pressed const & msg) { bank.send(get_balance(account, incoming)); state = &atm::process_balance; } ) .handle<cancel_pressed>( [&](cancel_pressed const & msg) { state = &atm::done_processing; } ); }
void transaction_type(float iSavings, float iChequings) { int CHOICE; printf("\nWould you like to WITHDRAW, DEPOSIT, or RETURN TO MAIN MENU?\n"); printf(" 1 = WITHDRAW 2 = DEPOSIT 3 = RETURN TO MAIN MENU\n\n -> "); scanf("%d", &CHOICE); if (CHOICE == 1) { withdraw(iSavings, iChequings); } else if (CHOICE == 2) { deposit(iSavings, iChequings); } else { system("clear"); options(); } }
/*other is either "trusted/untrusted" or a number */ int main(int argc, char*argv[]) { char *buffer=(char*)malloc(50*sizeof(char)), *name=(char*)malloc(15*sizeof(char)), *action=(char*)malloc(15*sizeof(char)), *other=(char*)malloc(15*sizeof(char)); FILE *fp = fopen(argv[1], "r"); if (fp==NULL) { printf("error opening file"); return 1; } Table *t= lookup_mkTable(); while ((buffer=nextMessage(fp))!=NULL) { sscanf(buffer, " %s %s %s", name, action, other); if (strcmp(action, "joins")==0) join(t, name, other); if (strcmp(action, "deposits")==0) deposit(t, name, atoi(other)); if (strcmp(action, "withdraws")==0) withdraw(t, name, atoi(other)); if (strcmp(action, "print")==0) print(t, name); } return 0; }
void Ui::start() { std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"; std::string next = mainMenu(); std::string username = ""; Account* account = nullptr; while (next != "quit") { std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"; if (next == "mainMenu") { next = mainMenu(); } else if (next == "accountMenu") { next = accountMenu(); } else if (next == "createAccount") { username = createAccount(); next = "mainMenu"; if (username.length() != 0) { next = "loadAccount"; } } else if (next == "deleteAccount") { deleteAccount(); next = "mainMenu"; } else if (next == "loadAccount") { account = loadAccount(username); username = ""; next = "accountMenu"; if (account == nullptr) { next = "loadAccount"; } } else if (next == "updateAccount") { updateAccount(account); next = "accountMenu"; } else if (next == "getWithdrawals") { getWithdrawals(account); next = "accountMenu"; } else if (next == "getDeposits") { getDeposits(account); next = "accountMenu"; } else if (next == "withdraw") { withdraw(account); next = "accountMenu"; } else if (next == "deposit") { deposit(account); next = "accountMenu"; } else if (next == "quitAccountMenu") { delete account; account = nullptr; next = "mainMenu"; } } }
int Bank::save(const QString &ID, double Amount){ return withdraw(ID, -Amount); }
void* client_service_thread(void *fd) { int connection_fd; char request[MESSAGE_SIZE]; char response[MESSAGE_SIZE]; int status; char *error_info; account_t *current_account; connection_fd = *((int *)fd); free(fd); current_account = NULL; pthread_detach(pthread_self()); printf("server: connected to a client\n"); while (recv(connection_fd, request, MESSAGE_SIZE, 0) > 0) { if (strncmp(request, "create ", 7) == 0) { if ((status = create(&request[7], ¤t_account)) < 0) { error_info = get_error_info(status); send(connection_fd, error_info, strlen(error_info)+1, 0); free(error_info); } else { sprintf(response, "create: success"); send(connection_fd, response, strlen(response)+1, 0); } } else if (strncmp(request, "serve ", 6) == 0) { while ((status = serve(&request[6], ¤t_account)) == BUSY) { sprintf(response, "serve: waiting..."); send(connection_fd, response, strlen(response)+1, 0); sleep(2); } if (status != SUCCESS) { error_info = get_error_info(status); send(connection_fd, error_info, strlen(error_info)+1, 0); free(error_info); } else { sprintf(response, "serve: success"); send(connection_fd, response, strlen(response)+1, 0); } } else if (strncmp(request, "deposit ", 8) == 0) { if ((status = deposit(&request[8],¤t_account)) < 0) { error_info = get_error_info(status); send(connection_fd, error_info, strlen(error_info)+1, 0); free(error_info); } else { sprintf(response, "deposit: success"); send(connection_fd, response, strlen(response)+1, 0); } } else if (strncmp(request, "withdraw ", 9) == 0) { if ((status = withdraw(&request[9],¤t_account)) < 0) { error_info = get_error_info(status); send(connection_fd, error_info, strlen(error_info)+1, 0); free(error_info); } else { sprintf(response, "withdraw: success"); send(connection_fd, response, strlen(response)+1, 0); } } else if (strcmp(request, "query") == 0) { double amount; if ((amount = query(¤t_account)) < 0) { status = amount; error_info = get_error_info(status); send(connection_fd, error_info, strlen(error_info)+1, 0); free(error_info); } else { sprintf(response, "query: success\nbalance: %lf", amount); send(connection_fd, response, strlen(response)+1, 0); } } else if (strcmp(request, "end") == 0) { if ((status = end(¤t_account)) < 0) { error_info = get_error_info(status); send(connection_fd, error_info, strlen(error_info)+1, 0); free(error_info); } else { sprintf(response, "end: success"); send(connection_fd, response, strlen(response)+1, 0); } } else if (strcmp(request, "quit") == 0) { quit(¤t_account); sprintf(response, "quit"); send(connection_fd, response, strlen(response)+1, 0); printf("server: disconnected to a client\n"); close(connection_fd); pthread_exit(NULL); } else if (strcmp(request, "help") == 0) { sprintf(response, "%s", usage); send(connection_fd, response, strlen(response)+1, 0); } else { sprintf(response, "invalid command, enter again('help' for usage)"); send(connection_fd, response, strlen(response)+1, 0); } bzero(request, MESSAGE_SIZE); bzero(response, MESSAGE_SIZE); } close(connection_fd); return NULL; }
~Servus() { #ifdef LUNCHBOX_USE_DNSSD withdraw(); #endif }