Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdlib>
  4.  
  5.  
  6. using namespace std;
  7.  
  8.  
  9.  
  10.  
  11. class Phone {
  12.  
  13.  
  14.  
  15. private:
  16.  
  17.  
  18. int number;
  19. int pin;
  20. int price;
  21. char *brand;
  22. char *model;
  23. Phone *next;
  24.  
  25.  
  26. public:
  27.  
  28. //Constructor
  29. Phone(int newNumber, int newPin, int newPrice, const char* brand, const char* model);
  30.  
  31. //Destructor
  32. ~Phone();
  33.  
  34.  
  35. void setNext(Phone *nxt) {
  36. next = nxt;
  37. }
  38.  
  39.  
  40. Phone *getNext() const {
  41. return next;
  42. }
  43.  
  44.  
  45. char *getBrand() const {
  46. return brand;
  47. }
  48.  
  49.  
  50. void setBrand(char * b) {
  51. brand = b;
  52. }
  53.  
  54. void setModel(char * m) {
  55. model = m;
  56. }
  57.  
  58. void setPrice(int p) {
  59. price = p;
  60. }
  61.  
  62. void setNumber(int n) {
  63. number = n;
  64. }
  65.  
  66. void setPin(int p) {
  67. pin = p;
  68. }
  69.  
  70.  
  71. char *getModel() const {
  72. return model;
  73. }
  74.  
  75.  
  76. int getPrice() const {
  77. return price;
  78. }
  79.  
  80. int getPin() const {
  81. return pin;
  82. }
  83.  
  84.  
  85.  
  86. int getNumber() const {
  87. return number;
  88. }
  89.  
  90. void displayList();
  91.  
  92. };
  93.  
  94. class PhoneList {
  95.  
  96. //declaration of head and tail
  97. Phone *head;
  98. Phone *tail;
  99.  
  100. public:
  101.  
  102. //Constructor
  103. PhoneList() {
  104. //initialization of head and tail
  105. head = NULL;
  106. tail = NULL;
  107. }
  108.  
  109. //Destructor
  110. ~PhoneList();
  111.  
  112.  
  113.  
  114. void addPhone(int number, int pin, int price, const char* brand, const char* model);
  115. void displayList();
  116. void clearList();
  117. void removeNumber(int number);
  118. bool numberExists(int number);
  119. bool remove(int number);
  120. int modify(int number, const char* brand, const char*model);
  121. char *getBrand(int number);
  122. char *getModel(int number);
  123. Phone *getPhone(int number);
  124.  
  125. };
  126.  
  127.  
  128.  
  129.  
  130.  
  131. Phone::Phone(int newNumber, int newPin, int newPrice, const char* newBrand, const char* newModel)
  132. {
  133.  
  134. //+1 because we are using the index system which starts at 0
  135. brand = new char[strlen(newBrand)+1];
  136. model = new char[strlen(newBrand)+1];
  137.  
  138. if(brand != NULL && model != NULL) {
  139. //Assign values
  140. strcpy(brand, newBrand);
  141. strcpy(model, newModel);
  142. pin = newPin;
  143. number = newNumber;
  144. price = newPrice;
  145. next = NULL;
  146. }
  147. }
  148.  
  149.  
  150.  
  151. Phone::~Phone() //DESTRUCTOR
  152. {
  153. delete[] brand, delete[] model; //Deletes the memory allocated -> new char...
  154. }
  155.  
  156.  
  157.  
  158. void PhoneList::displayList() {
  159. cout << endl << "phones in the list:" << endl;
  160. Phone *current = head;
  161. if(current == NULL) { //If there are no phones in the list
  162. cout << "There are no phones in the list!" << endl;
  163. } else {
  164. while(current != NULL) { //Loop through all of them and print out a information of the phone
  165. cout << "brand: " << current->getBrand() << "," << " model: " << current->getModel() << "," << " number: " <<
  166. current->getNumber() << "," << " Price: " << current->getPrice() << "," << " Pin: " << current->getPin() << endl;
  167. current = current->getNext();
  168. }
  169. }
  170. cout << endl;
  171. }
  172.  
  173.  
  174. void PhoneList::addPhone(int number, int pin, int price, const char* brand, const char* model) {
  175.  
  176.  
  177.  
  178. Phone *previous = NULL; //Previous is null
  179. Phone *current = head; //Current is the head
  180. //Checking if the current number is less than the new number
  181. while(current != NULL) {
  182. if (current->getNumber() < number) {
  183. previous = current;
  184. current = current->getNext();
  185. }
  186. else break;
  187. }
  188.  
  189. //Checking if current phone's number is as same the new number's id. In this case, it exists so we won't be adding it.
  190. if(current != NULL && current->getNumber() == number) {
  191. cout << "Duplicates found for phone: " << "number: " << number << endl;
  192.  
  193. //Adding a new phone
  194. } else {
  195.  
  196. Phone *newPhone = new Phone(number, pin, price, brand, model);
  197. if(newPhone != NULL) {
  198. if(previous != NULL) {
  199. newPhone->setNext(previous->getNext());
  200. previous->setNext(newPhone);
  201. } else {
  202. head = newPhone;
  203. newPhone->setNext(NULL);
  204. }
  205. //Print out the phone added.
  206. cout << "Added a new phone: " << "brand: " << brand << "," << " model: " << model << "," << " number: " << number << endl;
  207. //newPhone is null
  208. } else {
  209. cout << "Error: Memory allocation.";
  210. }
  211. }
  212.  
  213.  
  214.  
  215.  
  216. }
  217.  
  218.  
  219.  
  220. bool PhoneList::numberExists(int number)
  221. {
  222. Phone* current = head;
  223.  
  224. //While not null
  225. while (current != NULL)
  226. {
  227. //Check through the linked list for this id
  228. if (current->getNumber() == number)
  229. return true;
  230. current = current->getNext(); //Go to the next phone
  231. }
  232. return false;
  233. }
  234.  
  235.  
  236.  
  237. bool PhoneList::remove(int number) {
  238.  
  239. Phone *before_temp = NULL;
  240. Phone *temp = head;
  241.  
  242. //While not the end of the list
  243. while(temp != NULL) {
  244.  
  245. //Checks if the current owner id is as same as the passed parameter.
  246. if (temp->getNumber() == number) {
  247. if (temp == head && temp == tail) {
  248. delete head->getBrand();
  249. delete head->getModel();
  250. delete(head); //Deletes the node
  251. head = tail = NULL;
  252. } else if (temp == head) {
  253. temp = head->getNext();
  254. delete head->getBrand();
  255. delete head->getModel();
  256. delete(head); //Deletes the node
  257. head = temp;
  258. } else if (temp == tail) {
  259. delete tail->getBrand();
  260. delete tail->getModel();
  261. delete(tail); //Deletes the node
  262. tail = before_temp;
  263. } else {
  264. before_temp->setNext(temp->getNext());
  265. delete temp->getBrand();
  266. delete temp->getModel();
  267. delete(temp); //Deletes the node
  268. }
  269. return true;
  270. }
  271. //Assign the new values
  272. before_temp = temp;
  273. temp = temp->getNext();
  274. }
  275. return false;
  276. }
  277.  
  278.  
  279.  
  280. void PhoneList::removeNumber(int number) {
  281.  
  282. //If it exists
  283. if(numberExists(number)) {
  284. bool condition;
  285. int phones_deleted = 0; //Calculator.. Not important but just to track and for practice.
  286. while (head != NULL) {
  287. condition = remove(number);
  288. if (condition) {
  289. phones_deleted++; //Increment the deleted phones count
  290. } else {
  291. break;
  292. }
  293. }
  294. cout << "Deleted " << phones_deleted << " phone(s) for number " << number << endl;
  295. } else {
  296. cout << "Number " << number << " doesn't exist to be deleted." << endl;
  297. }
  298. }
  299.  
  300.  
  301.  
  302. void PhoneList::clearList() {
  303.  
  304. Phone *temp;
  305.  
  306. while(head != NULL) {
  307. temp = head;
  308. head = head->getNext();
  309. delete temp;
  310. }
  311. cout << "The list has been cleared" << endl;
  312. }
  313.  
  314.  
  315. //Checks whether the number exists in the linked list of not.
  316. char * PhoneList::getBrand(int number) {
  317.  
  318. bool condition = false;
  319. Phone* current = head;
  320.  
  321. //While not null
  322. while (current != NULL)
  323. {
  324. //Check through the linked list for this number
  325. if (current->getNumber() == number) //If identical
  326. return current->getBrand();
  327.  
  328. current = current->getNext();
  329. }
  330. }
  331.  
  332.  
  333. //Checks whether the number exists in the linked list of not.
  334. char * PhoneList::getModel(int number) {
  335. bool condition = false;
  336. Phone* current = head;
  337.  
  338. //While not null
  339. while (current != NULL)
  340. {
  341. //Check through the linked list for this number
  342. if (current->getNumber() == number) //If identical
  343. return current->getModel();
  344.  
  345. current = current->getNext();
  346. }
  347. }
  348.  
  349.  
  350. //Checks whether the number exists in the linked list of not.
  351. Phone * PhoneList::getPhone(int number) {
  352.  
  353. bool condition = false;
  354. Phone* current = head;
  355.  
  356. //While not null
  357. while (current != NULL)
  358. {
  359. //Check through the linked list for this number
  360. if (current->getNumber() == number) //If identical
  361. return current;
  362.  
  363. current = current->getNext();
  364. }
  365. }
  366.  
  367.  
  368.  
  369.  
  370. int PhoneList::modify(int number, const char *brand, const char *model)
  371. {
  372. //if the list is empty, or the id doesn't exist, Then create a new element.
  373. if(head==NULL || !numberExists(number))
  374. {
  375. addPhone(number, 0, 0, brand, model); //Adding new phone with entered values
  376. return getPhone(number)->getPrice();
  377. //If the id exists in the linked list
  378. } else if(numberExists(number)) {
  379. //if the model or the brand are the same
  380. if(strcmp(getBrand(number), brand) == 0 || strcmp(getModel(number), model) == 0){
  381. Phone * currentPhone = getPhone(number);
  382. strcpy( currentPhone->getModel(), model);
  383. strcpy( currentPhone->getBrand(), brand);
  384. return currentPhone ->getPrice();
  385. }else{
  386. Phone * currentPhone = getPhone(number);
  387. return currentPhone->getPrice();
  388. }
  389. }
  390. cout << endl;
  391. }
  392.  
  393.  
  394.  
  395.  
  396.  
  397. PhoneList::~PhoneList()
  398. {
  399. Phone *tmp = NULL;
  400.  
  401. while(head != NULL)
  402. {
  403. tmp = head;
  404. head = head->getNext();
  405. delete tmp;
  406. }
  407. }
  408.  
  409. /* ---------- The main function here ---------- */
  410.  
  411. int main() {
  412.  
  413. //Declaration and initialization of two lists. Dynamic and static.
  414. PhoneList List1; //Static
  415. PhoneList *List2 = new PhoneList(); //Dynamic
  416.  
  417. cout << "List 1:" << endl;
  418. List1.displayList();
  419.  
  420. List1.addPhone(10, 0,100,"a brand", "a model");
  421. List1.addPhone(14, 0,200,"a brand", "a model");
  422.  
  423. cout << endl;
  424. List1.removeNumber(10);
  425. List1.displayList();
  426.  
  427.  
  428. //Separator between list 1 and 2 to not get lost :P
  429. cout << "-------------------------------------" << endl;
  430. cout << "List 2:" << endl;
  431.  
  432.  
  433. List2->addPhone(2,4, 500,"brand1","model1");
  434. List2->addPhone(2,4, 500,"brand2","model2");
  435. List2->addPhone(10,0,1000, "a brand","a model");
  436. cout << endl;
  437. List2 ->removeNumber(1);
  438. List2 -> displayList();
  439.  
  440.  
  441. delete List2; //deleting the dynamic object.
  442.  
  443. return 0;
  444. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement