finiisish
This commit is contained in:
parent
91728d978a
commit
bbd3c46a34
8
.gitignore
vendored
8
.gitignore
vendored
@ -1,3 +1,9 @@
|
||||
.o
|
||||
*.o
|
||||
Megaphone
|
||||
|
||||
ex02/19920104_091532.log
|
||||
ex01/PhoneBook
|
||||
ex02/Account
|
||||
ex02/annotated.log
|
||||
ex02/test.log
|
||||
.vscode/settings.json
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
VPATH = src
|
||||
OBJ = $(SRC:.cpp=.o)
|
||||
CC = c++
|
||||
CFLAGS = -Wall -Wextra -Werror
|
||||
|
||||
16
ex01/Makefile
Normal file
16
ex01/Makefile
Normal file
@ -0,0 +1,16 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# :::::::: #
|
||||
# Makefile :+: :+: #
|
||||
# +:+ #
|
||||
# By: whaffman <whaffman@student.codam.nl> +#+ #
|
||||
# +#+ #
|
||||
# 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
|
||||
50
ex01/src/Contact.cpp
Normal file
50
ex01/src/Contact.cpp
Normal file
@ -0,0 +1,50 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* Contact.cpp :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/03/20 10:44:49 by whaffman #+# #+# */
|
||||
/* Updated: 2025/03/21 10:46:44 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Contact.hpp"
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
|
||||
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));
|
||||
}
|
||||
28
ex01/src/Contact.hpp
Normal file
28
ex01/src/Contact.hpp
Normal file
@ -0,0 +1,28 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* Contact.hpp :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/03/20 10:48:38 by whaffman #+# #+# */
|
||||
/* Updated: 2025/03/21 10:53:49 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
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();
|
||||
};
|
||||
106
ex01/src/PhoneBook.cpp
Normal file
106
ex01/src/PhoneBook.cpp
Normal file
@ -0,0 +1,106 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* PhoneBook.cpp :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* 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 <iostream>
|
||||
#include <iomanip>
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
34
ex01/src/PhoneBook.hpp
Normal file
34
ex01/src/PhoneBook.hpp
Normal file
@ -0,0 +1,34 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* PhoneBook.hpp :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* 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 <string>
|
||||
|
||||
#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;
|
||||
};
|
||||
|
||||
25
ex01/src/main.cpp
Normal file
25
ex01/src/main.cpp
Normal file
@ -0,0 +1,25 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#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;
|
||||
}
|
||||
}
|
||||
149
ex02/Account.cpp
Normal file
149
ex02/Account.cpp
Normal file
@ -0,0 +1,149 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* Account.cpp :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/03/21 11:04:19 by whaffman #+# #+# */
|
||||
/* Updated: 2025/03/21 14:45:34 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <iostream>
|
||||
#include <ctime>
|
||||
#include <iomanip>
|
||||
#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 <iomanip> // 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
|
||||
<< "] ";
|
||||
}
|
||||
|
||||
|
||||
69
ex02/Account.hpp
Normal file
69
ex02/Account.hpp
Normal file
@ -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 <bm@gbu.com> //
|
||||
// //
|
||||
// ************************************************************************** //
|
||||
|
||||
|
||||
#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__ */
|
||||
19
ex02/Makefile
Normal file
19
ex02/Makefile
Normal file
@ -0,0 +1,19 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# :::::::: #
|
||||
# Makefile :+: :+: #
|
||||
# +:+ #
|
||||
# By: whaffman <whaffman@student.codam.nl> +#+ #
|
||||
# +#+ #
|
||||
# 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
|
||||
72
ex02/tests.cpp
Normal file
72
ex02/tests.cpp
Normal file
@ -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 <bm@gbu.com> //
|
||||
// //
|
||||
// ************************************************************************** //
|
||||
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include "Account.hpp"
|
||||
|
||||
|
||||
int main( void ) {
|
||||
|
||||
typedef std::vector<Account::t> accounts_t;
|
||||
typedef std::vector<int> ints_t;
|
||||
typedef std::pair<accounts_t::iterator, ints_t::iterator> 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; -*-
|
||||
// ************************************************************************** //
|
||||
Loading…
Reference in New Issue
Block a user