This commit is contained in:
Imbus 2024-12-05 12:02:17 +01:00
parent 6130bdee66
commit 1dae994364
2 changed files with 7 additions and 0 deletions

View file

@ -27,6 +27,7 @@ UserTable::UserTable(const std::string &fname) : UserTable{} {
void UserTable::addUser(const User &u) { void UserTable::addUser(const User &u) {
// gör tabellen större vid behov // gör tabellen större vid behov
ensureCapacity(n + 1); ensureCapacity(n + 1);
// 1. Hitta rätt plats // 1. Hitta rätt plats
int pos{0}; int pos{0};
while ((pos < n) && (users[pos].getCardNbr() < u.getCardNbr())) { while ((pos < n) && (users[pos].getCardNbr() < u.getCardNbr())) {
@ -40,6 +41,8 @@ void UserTable::addUser(const User &u) {
// 3. stoppa in den nya användaren i luckan // 3. stoppa in den nya användaren i luckan
users[pos] = u; users[pos] = u;
++n;
} }
User UserTable::find(int c) const { User UserTable::find(int c) const {

View file

@ -18,4 +18,8 @@ int main() {
assert(u3.getCardNbr() != u4.getCardNbr()); assert(u3.getCardNbr() != u4.getCardNbr());
UserTable ut1 = UserTable("users.txt"); UserTable ut1 = UserTable("users.txt");
ut1.addUser(u3);
User us = ut1.find(1200);
assert(us != UserTable::user_not_found);
} }