From bbd3c46a34ad66b0feea738ee410441b6a34c1f2 Mon Sep 17 00:00:00 2001 From: whaffman Date: Fri, 21 Mar 2025 14:46:30 +0100 Subject: [PATCH] finiisish --- .gitignore | 8 ++- common.mk | 1 + ex01/Makefile | 16 +++++ ex01/src/Contact.cpp | 50 ++++++++++++++ ex01/src/Contact.hpp | 28 ++++++++ ex01/src/PhoneBook.cpp | 106 +++++++++++++++++++++++++++++ ex01/src/PhoneBook.hpp | 34 ++++++++++ ex01/src/main.cpp | 25 +++++++ ex02/Account.cpp | 149 +++++++++++++++++++++++++++++++++++++++++ ex02/Account.hpp | 69 +++++++++++++++++++ ex02/Makefile | 19 ++++++ ex02/tests.cpp | 72 ++++++++++++++++++++ 12 files changed, 576 insertions(+), 1 deletion(-) create mode 100644 ex01/Makefile create mode 100644 ex01/src/Contact.cpp create mode 100644 ex01/src/Contact.hpp create mode 100644 ex01/src/PhoneBook.cpp create mode 100644 ex01/src/PhoneBook.hpp create mode 100644 ex01/src/main.cpp create mode 100644 ex02/Account.cpp create mode 100644 ex02/Account.hpp create mode 100644 ex02/Makefile create mode 100644 ex02/tests.cpp diff --git a/.gitignore b/.gitignore index de959af..4d087e0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,9 @@ -.o +*.o Megaphone +ex02/19920104_091532.log +ex01/PhoneBook +ex02/Account +ex02/annotated.log +ex02/test.log +.vscode/settings.json diff --git a/common.mk b/common.mk index 1024c65..19184e4 100644 --- a/common.mk +++ b/common.mk @@ -1,3 +1,4 @@ +VPATH = src OBJ = $(SRC:.cpp=.o) CC = c++ CFLAGS = -Wall -Wextra -Werror diff --git a/ex01/Makefile b/ex01/Makefile new file mode 100644 index 0000000..546f671 --- /dev/null +++ b/ex01/Makefile @@ -0,0 +1,16 @@ +# **************************************************************************** # +# # +# :::::::: # +# Makefile :+: :+: # +# +:+ # +# By: whaffman +#+ # +# +#+ # +# Created: 2025/03/20 10:43:08 by whaffman #+# #+# # +# Updated: 2025/03/20 15:15:56 by whaffman ######## odam.nl # +# # +# **************************************************************************** # + +NAME = PhoneBook +SRC = PhoneBook.cpp Contact.cpp main.cpp + +-include ../common.mk diff --git a/ex01/src/Contact.cpp b/ex01/src/Contact.cpp new file mode 100644 index 0000000..6848c24 --- /dev/null +++ b/ex01/src/Contact.cpp @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* Contact.cpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/03/20 10:44:49 by whaffman #+# #+# */ +/* Updated: 2025/03/21 10:46:44 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "Contact.hpp" +#include +#include + +Contact::Contact() +{ + +} + +Contact::Contact(std::string fn, std::string ln, std::string nn, std::string ds) +{ + firstName = fn; + lastName = ln; + nickname = nn; + darkestSecret = ds; +} + +Contact Contact::newContact() +{ + std::string firstName; + std::string lastName; + std::string nickname; + std::string darkestSecret; + + std::cout << "---------------------------------------------" << std::endl; + std::cout << "Enter the contact information:" << std::endl << std::endl; + std::cout << std::setw(16) << "first name: "; + std::getline(std::cin, firstName); + std::cout << std::setw(16) << "last name: "; + std::getline(std::cin, lastName); + std::cout << std::setw(16) << "nickname: "; + std::getline(std::cin, nickname); + std::cout << std::setw(16) << "darkest secret: "; + std::getline(std::cin, darkestSecret); + std::cout << "---------------------------------------------" << std::endl; + + return (Contact(firstName, lastName, nickname, darkestSecret)); +} \ No newline at end of file diff --git a/ex01/src/Contact.hpp b/ex01/src/Contact.hpp new file mode 100644 index 0000000..3161307 --- /dev/null +++ b/ex01/src/Contact.hpp @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* Contact.hpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/03/20 10:48:38 by whaffman #+# #+# */ +/* Updated: 2025/03/21 10:53:49 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include + +class Contact +{ + public: + std::string firstName; + std::string lastName; + std::string nickname; + std::string darkestSecret; + + Contact(); + Contact(std::string fn, std::string ln, std::string nn, std::string ds); + static Contact newContact(); +}; \ No newline at end of file diff --git a/ex01/src/PhoneBook.cpp b/ex01/src/PhoneBook.cpp new file mode 100644 index 0000000..bfc7d32 --- /dev/null +++ b/ex01/src/PhoneBook.cpp @@ -0,0 +1,106 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* PhoneBook.cpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/03/20 11:02:32 by whaffman #+# #+# */ +/* Updated: 2025/03/21 10:56:49 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "PhoneBook.hpp" +#include "Contact.hpp" +#include +#include + + +PhoneBook::PhoneBook() +{ + _contactCount = 0; +} + +void PhoneBook::addContact() +{ + Contact contact = Contact::newContact(); + + if (_contactCount < MAX_CONTACTS) + { + _contacts[_contactCount] = contact; + _contactCount++; + } + else + { + for (int i = 0; i < MAX_CONTACTS - 1; i++) + { + _contacts[i] = _contacts[i + 1]; + } + _contacts[MAX_CONTACTS - 1] = contact; + } +} + +std::string PhoneBook::truncate(const std::string str) const +{ + if (str.length() > 10) + return (str.substr(0, 9) + "."); + return (str); +} +void PhoneBook::printAllContacts() const +{ + std::cout << std::setw(10) << "Index" << "|"; + std::cout << std::setw(10) << "First name" << "|"; + std::cout << std::setw(10) << "Last name" << "|"; + std::cout << std::setw(10) << "Nickname" << std::endl; + + for (int i = 0; i < _contactCount; i++) + { + std::cout << std::setw(10) << i << "|"; + std::cout << std::setw(10) << truncate(_contacts[i].firstName) << "|"; + std::cout << std::setw(10) << truncate(_contacts[i].lastName) << "|"; + std::cout << std::setw(10) << truncate(_contacts[i].nickname) << std::endl; + } +} + + +void PhoneBook::printContact(const int i) const +{ + if (i < 0 || i >= _contactCount) + { + std::cout << "Invalid index" << std::endl; + return; + } + + std::cout << std::setw(16) << "First name: " << _contacts[i].firstName << std::endl; + std::cout << std::setw(16) << "Last name: " << _contacts[i].lastName << std::endl; + std::cout << std::setw(16) << "Nickname: " << _contacts[i].nickname << std::endl; + std::cout << std::setw(16) << "Darkest secret: " << _contacts[i].darkestSecret << std::endl; + + return; +} + +void PhoneBook::searchContact() const +{ + std::string command; + int index; + + if (_contactCount == 0) + { + std::cout << "The PhoneBook is empty." << std::endl; + return; + } + + std::cout << "---------------------------------------------" << std::endl; + printAllContacts(); + std::cout << "---------------------------------------------" << std::endl; + + std::cout << "Enter the index of the contact you want to see: "; + std::getline(std::cin, command); + std::cout << std::endl; + index = std::stoi(command); + + std::cout << "---------------------------------------------" << std::endl; + printContact(index); + std::cout << "---------------------------------------------" << std::endl; + std::cout << std::endl; +} diff --git a/ex01/src/PhoneBook.hpp b/ex01/src/PhoneBook.hpp new file mode 100644 index 0000000..270e397 --- /dev/null +++ b/ex01/src/PhoneBook.hpp @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* PhoneBook.hpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/03/20 10:53:48 by whaffman #+# #+# */ +/* Updated: 2025/03/21 10:55:03 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "Contact.hpp" +#include + +#define MAX_CONTACTS 2 + +class PhoneBook { + public: + PhoneBook(); + void printContact(const int i) const; + void printAllContacts() const; + void searchContact() const; + void addContact(); + + private: + Contact _contacts[MAX_CONTACTS]; + int _contactCount; + + std::string truncate(std::string str) const; +}; + diff --git a/ex01/src/main.cpp b/ex01/src/main.cpp new file mode 100644 index 0000000..0030d48 --- /dev/null +++ b/ex01/src/main.cpp @@ -0,0 +1,25 @@ +#include +#include + +#include "Contact.hpp" +#include "PhoneBook.hpp" + +int main(void) +{ + std::string command; + PhoneBook phoneBook; + + while (true) + { + std::cout << "Enter a command: "; + getline(std::cin, command); + if (command == "EXIT") + break; + else if (command == "ADD") + phoneBook.addContact(); + else if (command == "SEARCH") + phoneBook.searchContact(); + else + std::cout << "Invalid command" << std::endl; + } +} \ No newline at end of file diff --git a/ex02/Account.cpp b/ex02/Account.cpp new file mode 100644 index 0000000..6f20e20 --- /dev/null +++ b/ex02/Account.cpp @@ -0,0 +1,149 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* Account.cpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/03/21 11:04:19 by whaffman #+# #+# */ +/* Updated: 2025/03/21 14:45:34 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include "Account.hpp" + +int Account::_nbAccounts = 0; +int Account::_totalAmount = 0; +int Account::_totalNbDeposits = 0; +int Account::_totalNbWithdrawals = 0; + +Account::Account() +{ + +} + +Account::Account( int initial_deposit ) +{ + _accountIndex = Account::getNbAccounts(); + _amount = initial_deposit; + _nbDeposits = 0; + _nbWithdrawals = 0; + + _nbAccounts++; + _totalAmount += _amount; + + _displayTimestamp(); + std::cout << "index:" << _accountIndex << ";amount:" << _amount << ";created" << std::endl; +} + +Account::~Account( void ) +{ + _displayTimestamp(); + std::cout << "index:" << _accountIndex << ";amount:" << _amount << ";closed" << std::endl; + +} + +int Account::getNbAccounts( void ) +{ + return (Account::_nbAccounts); +} + +int Account::getTotalAmount( void ) +{ + return (_totalAmount); +} + +int Account::getNbDeposits( void ) +{ + return (_totalNbDeposits); +} + +int Account::getNbWithdrawals( void ) +{ + return (_totalNbWithdrawals); +} + +void Account::displayAccountsInfos( void ) +{ + _displayTimestamp(); + std::cout << "accounts:" << Account::getNbAccounts() << ";" + << "total:" << Account::getTotalAmount() << ";" + << "deposits:" << Account::getNbDeposits() << ";" + << "withdrawals:" << Account::getNbWithdrawals() + << std::endl; +} + +void Account::makeDeposit( int deposit ) +{ + _displayTimestamp(); + std::cout << "index:" << _accountIndex << ";" + << "p_amount:" << _amount << ";"; + + _amount += deposit; + _totalAmount += deposit; + _nbDeposits++; + _totalNbDeposits++; + + std::cout << "deposit:" << deposit << ";" + << "amount:" << _amount << ";" + << "nb_deposits:" << _nbDeposits + << std::endl; + +} +bool Account::makeWithdrawal( int withdrawal ) +{ + + _displayTimestamp(); + std::cout << "index:" << _accountIndex << ";" + << "p_amount:" << _amount << ";"; + + if(withdrawal >= _amount) + { + std::cout << "withdrawal:refused" << std::endl; + return(false); + } + _amount -= withdrawal; + _totalAmount -= withdrawal; + _nbWithdrawals++; + _totalNbWithdrawals++; + std::cout << "withdrawal:" << withdrawal << ";" + << "amount:" << _amount << ";" + << "nb_withdrawals:" << _nbWithdrawals + << std::endl; + return (true); +} +int Account::checkAmount( void ) const +{ + return(_amount); +} + +void Account::displayStatus( void ) const +{ + _displayTimestamp(); + std::cout << "index:" << _accountIndex << ";" + << "amount:" << _amount << ";" + << "deposits:" << _nbDeposits << ";" + << "withdrawals:" << _nbWithdrawals + << std::endl; +} + +void Account::_displayTimestamp( void ) +{ + time_t now = std::time(NULL); + std::tm* localTime = std::localtime(&now); + #include // Add this include at the top of the file for std::setfill and std::setw + + std::cout << "[" + << (localTime->tm_year + 1900) + << std::setfill('0') << std::setw(2) << (localTime->tm_mon + 1) + << std::setfill('0') << std::setw(2) << localTime->tm_mday << "_" + << std::setfill('0') << std::setw(2) << localTime->tm_hour + << std::setfill('0') << std::setw(2) << localTime->tm_min + << std::setfill('0') << std::setw(2) << localTime->tm_sec + << "] "; +} + + diff --git a/ex02/Account.hpp b/ex02/Account.hpp new file mode 100644 index 0000000..cd31e06 --- /dev/null +++ b/ex02/Account.hpp @@ -0,0 +1,69 @@ +// ************************************************************************** // +// // +// Account.hpp for GlobalBanksters United // +// Created on : Thu Nov 20 19:43:15 1989 // +// Last update : Wed Jan 04 14:54:06 1992 // +// Made by : Brad "Buddy" McLane // +// // +// ************************************************************************** // + + +#pragma once +#ifndef __ACCOUNT_H__ +#define __ACCOUNT_H__ + +// ************************************************************************** // +// Account Class // +// ************************************************************************** // + +class Account { + + +public: + + typedef Account t; + + static int getNbAccounts( void ); + static int getTotalAmount( void ); + static int getNbDeposits( void ); + static int getNbWithdrawals( void ); + static void displayAccountsInfos( void ); + + Account( int initial_deposit ); + ~Account( void ); + + void makeDeposit( int deposit ); + bool makeWithdrawal( int withdrawal ); + int checkAmount( void ) const; + void displayStatus( void ) const; + + +private: + + static int _nbAccounts; + static int _totalAmount; + static int _totalNbDeposits; + static int _totalNbWithdrawals; + + static void _displayTimestamp( void ); + + int _accountIndex; + int _amount; + int _nbDeposits; + int _nbWithdrawals; + + Account( void ); + +}; + + + +// ************************************************************************** // +// vim: set ts=4 sw=4 tw=80 noexpandtab: // +// -*- indent-tabs-mode:t; -*- +// -*- mode: c++-mode; -*- +// -*- fill-column: 75; comment-column: 75; -*- +// ************************************************************************** // + + +#endif /* __ACCOUNT_H__ */ diff --git a/ex02/Makefile b/ex02/Makefile new file mode 100644 index 0000000..a8fe258 --- /dev/null +++ b/ex02/Makefile @@ -0,0 +1,19 @@ +# **************************************************************************** # +# # +# :::::::: # +# Makefile :+: :+: # +# +:+ # +# By: whaffman +#+ # +# +#+ # +# Created: 2025/03/21 11:01:22 by whaffman #+# #+# # +# Updated: 2025/03/21 12:20:46 by whaffman ######## odam.nl # +# # +# **************************************************************************** # + +NAME = Account +SRC = Account.cpp tests.cpp + + +-include ../common.mk + +CFLAGS += -std=c++98 -g3 \ No newline at end of file diff --git a/ex02/tests.cpp b/ex02/tests.cpp new file mode 100644 index 0000000..0999525 --- /dev/null +++ b/ex02/tests.cpp @@ -0,0 +1,72 @@ +// ************************************************************************** // +// // +// tests.cpp for GlobalBanksters United // +// Created on : Thu Nov 20 23:45:02 1989 // +// Last update : Wed Jan 04 09:23:52 1992 // +// Made by : Brad "Buddy" McLane // +// // +// ************************************************************************** // + +#include +#include +#include +#include "Account.hpp" + + +int main( void ) { + + typedef std::vector accounts_t; + typedef std::vector ints_t; + typedef std::pair acc_int_t; + + int const amounts[] = { 42, 54, 957, 432, 1234, 0, 754, 16576 }; + size_t const amounts_size( sizeof(amounts) / sizeof(int) ); + accounts_t accounts( amounts, amounts + amounts_size ); + accounts_t::iterator acc_begin = accounts.begin(); + accounts_t::iterator acc_end = accounts.end(); + + int const d[] = { 5, 765, 564, 2, 87, 23, 9, 20 }; + size_t const d_size( sizeof(d) / sizeof(int) ); + ints_t deposits( d, d + d_size ); + ints_t::iterator dep_begin = deposits.begin(); + ints_t::iterator dep_end = deposits.end(); + + int const w[] = { 321, 34, 657, 4, 76, 275, 657, 7654 }; + size_t const w_size( sizeof(w) / sizeof(int) ); + ints_t withdrawals( w, w + w_size ); + ints_t::iterator wit_begin = withdrawals.begin(); + ints_t::iterator wit_end = withdrawals.end(); + + Account::displayAccountsInfos(); + std::for_each( acc_begin, acc_end, std::mem_fun_ref( &Account::displayStatus ) ); + + for ( acc_int_t it( acc_begin, dep_begin ); + it.first != acc_end && it.second != dep_end; + ++(it.first), ++(it.second) ) { + + (*(it.first)).makeDeposit( *(it.second) ); + } + + Account::displayAccountsInfos(); + std::for_each( acc_begin, acc_end, std::mem_fun_ref( &Account::displayStatus ) ); + + for ( acc_int_t it( acc_begin, wit_begin ); + it.first != acc_end && it.second != wit_end; + ++(it.first), ++(it.second) ) { + + (*(it.first)).makeWithdrawal( *(it.second) ); + } + + Account::displayAccountsInfos(); + std::for_each( acc_begin, acc_end, std::mem_fun_ref( &Account::displayStatus ) ); + + return 0; +} + + +// ************************************************************************** // +// vim: set ts=4 sw=4 tw=80 noexpandtab: // +// -*- indent-tabs-mode:t; -*- +// -*- mode: c++-mode; -*- +// -*- fill-column: 75; comment-column: 75; -*- +// ************************************************************************** //