Пример #1
0
void BankRBTree::mergeAccount(const string& id1, const string& passwd1, const string& id2, const string& passwd2){
  //find account1's position, if not exist return |NULL|
  string now_id1 = id1;
  DataNode la1 = DataNode(&now_id1, NULL);
  DataNode* res1 = (DataNode*)rb_find(rb_tree, &la1);

  if(res1 == NULL){
    cout << "ID " << id1 << " not found\n";
    return;
  }
  //find account2's position, if not exist return |NULL|
  string now_id2 = id2;
  DataNode la2 = DataNode(&now_id2, NULL);
  DataNode* res2 = (DataNode*)rb_find(rb_tree, &la2);
  
  if(res2 == NULL){
    cout << "ID " << id2 << " not found\n";
    return;
  }
  
  //verify passwd1
  if(res1->second->verifyPassword(passwd1) == false){
    cout << "wrong password1\n";
    return;
  }
  //verify passwd2
  if(res2->second->verifyPassword(passwd2) == false){
    cout << "wrong password2\n";
    return;
  }

  res1->second->mergeAccount(*(res2->second));
  rb_delete(rb_tree, res2);
}
Пример #2
0
	DataNode* DataNode::AddNode(const WString& name)
	{
		int delPos = name.Find("/");
		if (delPos >= 0)
		{
			WString namePart = name.SubStr(0, delPos);

			DataNode* node = GetNode(namePart);
			if (!node)
			{
				node = mnew DataNode();
				node->SetName(namePart);
				node->mParent = this;
				mChildNodes.Add(node);
			}

			return node->AddNode(name.SubStr(delPos + 1));
		}

		DataNode* newNode = mnew DataNode();
		newNode->SetName(name);
		newNode->mParent = this;
		mChildNodes.Add(newNode);

		return newNode;
	}
Пример #3
0
	DataNode::DataNode(const DataNode& other) :
		mName(other.mName), mData(other.mData), mParent(nullptr)
	{
		for (auto child : other.mChildNodes)
		{
			DataNode* newNode = mnew DataNode(*child);
			newNode->mParent = this;
			mChildNodes.Add(newNode);
		}
	}
Пример #4
0
void BankRBTree::transfer(const string& id, const int& money){
  //find account2's position, if not exist return |NULL|
  string now_id = id;
  DataNode la2 = DataNode(&now_id, NULL);
  DataNode* res2 = (DataNode*)rb_find(rb_tree, &la2);

  if(res2 == NULL){
    cout << "ID " << id << " not found, ";
    RecommendId rid;
    existRecommend(id, rid);
    if(rid.size() > 0)
      cout << rid[0];
    for (int i = 1; i < rid.size(); ++i)
      cout << "," << rid[i];
    cout << "\n";
  }

  else{
    DataNode la = DataNode(&current_login_user, NULL);
    DataNode* res = (DataNode*)rb_find(rb_tree, &la);
    res->second->transferOut((*(res2->second)), money, lg);
  }
}
Пример #5
0
	DataNode& DataNode::SetValue(const DataNode& other)
	{
		for (auto child : mChildNodes)
			delete child;

		mChildNodes.Clear();

		for (auto child : other.mChildNodes)
			mChildNodes.Add(mnew DataNode(*child));

		//mName = other.mName;
		mData = other.mData;

		return *this;
	}
Пример #6
0
void BankRBTree::deleteAccount(const string& id, const string& passwd){
  //find account's position, if not exist return |NULL|
  string now_id = id;
  DataNode la = DataNode(&now_id, NULL);
  DataNode* res = (DataNode*)rb_find(rb_tree, &la);

  if(res == NULL)
    cout << "ID " << id << " not found\n";

  else
    if(res->second->verifyPassword(passwd) == true){
      rb_delete(rb_tree, res);
      cout << "success\n";
    }
    else
      cout << "wrong password\n";
}
Пример #7
0
void BankRBTree::runRecommend(string id, string oid, int len, RecommendId& rid, int degree_c, int degree_a) {
  if (degree_c == 0 && degree_a == 0) {
    string now_id = id;
  DataNode la = DataNode(&now_id, NULL);
  DataNode* res = (DataNode*)rb_find(rb_tree, &la);
    if (res == NULL) {
      // it means that the id isn't exist in the bank
      rid.push_back(id);
      return;
    }
  }

  string tmpid;
  int delta = abs_num((int)oid.length() - (int)id.length());
  if (degree_a > delta) {
    if (id.length() >= oid.length() && id.length() < MAX_STRING_SIZE) {
      for (char c = '0'; ; c = next_char(c)) {
        runRecommend(id + c, oid, id.length() + 1, rid, degree_c, degree_a - delta - 1);
        if (c == 'z') break;
      }
    }
    if (id.length() <= oid.length() && id.length() > len && id.length() > 1) {
      tmpid = id;
      tmpid.pop_back();
      runRecommend(tmpid, oid, len, rid, degree_c, degree_a - delta - 1);
    }
  }

  if (degree_c > 0) {
    for (int i = max_num(min_num(id.length(), oid.length()) - degree_c, len); i < min_num(oid.length(), id.length()); i++) {
      if (degree_c >= min_num(oid.length(), id.length()) - i) {
        for (char c = '0'; c <= 'z'; c = next_char(c)) {
          if (c != oid[i]) {
          tmpid = id;
          tmpid[i] = c;
          runRecommend(tmpid, oid, i + 1, rid, degree_c - (min_num(oid.length(), id.length()) - i), degree_a);
          }
          if (c == 'z') break;
        }
      }
    }
  } 
}
Пример #8
0
void BankRBTree::createAccount(const string& id, const string& passwd){
  //find account's position, if not exist return |NULL|
  string now_id = id;
  DataNode la = DataNode(&now_id, NULL);
  if(rb_find(rb_tree, &la) == NULL){
    string* new_id = new string(id);
    Account* new_ac = new Account(id, passwd);
    DataNode* data = new DataNode(new_id, new_ac);
    rb_probe(rb_tree, data);
    cout << "success\n";
  }

  else{
    cout << "ID " << id << " exists, ";
    // recommend 10 best account
    RecommendId best10;
    getRecommend(id, best10);
    cout << best10[0];
    for (int i = 1; i < 10; ++i)
      cout << "," <<  best10[i];
    cout << "\n";
  }
}
Пример #9
0
			static void WriteObject(void* object, void* source, const ObjectType& type, DataNode& node)
			{
				for (auto baseType : type.GetBaseTypes())
				{
					const ObjectType* baseObjectType = dynamic_cast<const ObjectType*>(baseType.type);
					if (!baseObjectType)
						continue;

					void* baseObject = (*baseType.dynamicCastUpFunc)(object);
					void* baseSourceObject = (*baseType.dynamicCastUpFunc)(source);
					WriteObject(baseObject, baseSourceObject, *baseObjectType, node);
				}

				for (auto field : type.GetFields())
				{
					if (!field->GetAttribute<SerializableAttribute>())
						continue;

					if (field->GetType()->IsBasedOn(TypeOf(IObject)))
					{
						bool usedConverter = false;
						for (auto conv : mDataConverters)
						{
							if (conv->IsConvertsType(&type))
							{
								if (!field->IsValueEquals(object, source))
									conv->ToData(&object, *node.AddNode(field->GetName()));

								usedConverter = true;

								break;
							}
						}

						if (usedConverter)
							continue;

						DataNode* newFieldNode = mnew DataNode();
						newFieldNode->SetName(field->GetName());

						newFieldNode->SetValueDelta(*(IObject*)field->GetValuePtr(object),
													*(IObject*)field->GetValuePtr(source));

						if (!newFieldNode->IsEmpty())
							node.AddNode(newFieldNode);
						else
							delete newFieldNode;

						continue;
					}

					if (!field->IsValueEquals(object, source))
					{
						DataNode* newFieldNode = mnew DataNode();
						newFieldNode->SetName(field->GetName());

						field->SerializeFromObject(object, *newFieldNode);

						if (!newFieldNode->IsEmpty())
							node.AddNode(newFieldNode);
						else
							delete newFieldNode;
					}
				}
			}
Пример #10
0
void BankRBTree::searchHistory(const string& id){
  DataNode la = DataNode(&current_login_user, NULL);
  DataNode* res = (DataNode*)rb_find(rb_tree, &la);
  //Account* res1 = (Account*)rb_find(&rb_tree, &current_login_user);
  res->second->searchHistory(id);   
}
Пример #11
0
void BankRBTree::accountWithdraw(const int& money){
  DataNode la = DataNode(&current_login_user, NULL);
  DataNode* res = (DataNode*)rb_find(rb_tree, &la);
  res->second->withdrawMoney(money);
}