void regret(){ switch(undo.type){ case 0: //print ut no more regrets puts("INGET MER ATT ÅNGRA"); break; case 1: remove_product(amount-1); break; case 2: add_good(undo.copy); break; case 3: *undo.merch = undo.copy; break; default: //error break; } undo.type = 0; }
void add_product_interf() { puts("\n...Lägga till produkt..."); char *name = ask_question_string("Namn: ",100); char *desc = ask_question_string("Beskrivning: ",100); char *place = ask_for_place("Lagerhylla: "); int price = ask_question_int("Pris:"); int count = ask_question_int("Antal:"); puts("\n\nDin produkt:"); printf("Namn: %s \n Beksrivning:%s \n Lagerhylla:%s \n Pris:%d \n Antal:%d \n", name, desc, place, price, count); char q = ask_question_char("Vill du lägga till varan? [J]a,[N]ej,[R]edigera","JNR"); struct goods god= { .name = name, .desc = desc, .place = place, .price = price, .count = count }; switch (q) { case 'J': add_good(god); break; case 'N': free(name); free(desc); free(place); break; case 'R': add_good(god); edit_product(amount-1); break; } // free(name); // free(desc); // free(place); }
int main(void) { char input = 1; struct Goods all_wares[100]; int index = 0; struct action undo; while(input != 'Q' && input != 'q') { puts("\n\nWelcome to Warehouse number 42\n" "[A]dd a good\n" "[R]emove a good\n" "[E]dit a good\n" "[P]rint a good\n" "[U]ndo a bad decision\n" "[L]ist everything\n" "[Q]uit this amazing program\n"); input = ask_for_char("Your options: ", "AaRrEePpUuLlQq"); if (input == 'A' || input == 'a') { //ger oss en pekare till en bit av minne med storleken av struct goods struct Goods *temp = malloc(sizeof(struct Goods)); //checkar så att pekaren inte är null dvs pekaren är giltig assert(temp != NULL); add_good(temp, index, all_wares); bool done = false; char choice; while (!done) { print_good(temp); choice = ask_for_char("\nSave, Discard, Edit :", "SsDdEe"); if (choice == 'S' || choice == 's') { undo.type = 1; undo.mark = &all_wares[index]; all_wares[index] = *temp; index++; done = true; } else if (choice == 'D' || choice == 'd') { printf("Discarded\n"); //skarv done = true; } else if (choice == 'E' || choice == 'e') { edit_good(temp, index, all_wares); done = false; } else { printf("\nFaulty code"); } } assert(temp != NULL); free(temp); } else if (input == 'R' || input == 'r') { int choosenGood = choose_listedgood(all_wares, index); if (choosenGood >= 0) { undo.type = 2; undo.mark = &all_wares[choosenGood]; undo.saved = all_wares[choosenGood]; remove_ware(&all_wares[choosenGood]); } } else if (input == 'E' || input == 'e') { int choosenGood = choose_listedgood(all_wares, index); if (choosenGood >= 0) { undo.type = 3; undo.mark = &all_wares[choosenGood]; undo.saved = all_wares[choosenGood]; edit_good(&all_wares[choosenGood], index, all_wares); } } else if (input == 'P' || input == 'p') { int choosenGood = choose_listedgood(all_wares, index); if (choosenGood >= 0) { print_good(&all_wares[choosenGood]); } } else if (input == 'U' || input == 'u') { if (undo.type == 0) { printf("\nNothing to undo"); } // Add else if (undo.type == 1) { remove_ware(undo.mark); undo.type = 0; index--; } // Remove else if (undo.type == 2) { *undo.mark = undo.saved; undo.type = 0; } // Edit else if (undo.type == 3) { *undo.mark = undo.saved; undo.type = 0; } } else if (input == 'L' || input == 'l') { int page = 0; char choice = 1; while (choice != 'C' && choice != 'c') { list_goods(all_wares, index, page); //för att se om det finns mer varor på nästa page, annars är det "end page" if (index >((1+page)*20)) { choice = ask_for_char("\nNext page or Cancel ", "NnCc"); } else { choice = ask_for_char("\nEnd page, please Cancel", "Cc"); } if (choice == 'N' || choice == 'n') { page++; } } } else { printf("Du are Dolig\n"); } } return 0; }