int choose_listedgood(struct Goods *listOfGoods, int index) { int page = 0; char choice = 1; while (choice != 'C' && choice != 'c') { list_goods(listOfGoods, index, page); if (index >((1+page)*20)) { choice = ask_for_char("\nChoose ware, Next page or Cancel", "WwNnCc"); } else { choice = ask_for_char("\nEnd page, Choose ware or Cancel", "WwCc"); } if (choice == 'N' || choice == 'n') { page++; } else if (choice == 'W' || choice == 'w') { //Returna valet-1 så det blir det correcta indexet return (ask_for_int("Choose ware: ", 20) -1); } } //för att acceptera att man cancelar ifrån den här functionen utan att ange en vara. return -1; }
void db_autofill(db_t *db, int items){ const char *names[] = {"Bullar", "Citron", "Deg", "Fisk", "Gurka", "Ingefära", "Jordnötter", "Kaffe", "Lingon", "Mjölk", "Nudlar", "Päron", "Smör", "Tomat", NULL}; int names_s = -1; while(names[++names_s]); const char desc[] = "Blah blah "; int names_usage[sizeof(names)/sizeof(char)]; const int amounts[] = {100, 200, 300, 400, 1337}; const int price_divider = 20; int c; for(c=0; c<names_s; c++){ names_usage[c] = 1; } for(c=0; c<items; c++){ ware_t ware; strcpy(ware.name, names[c%names_s]); strcpy(ware.description, desc); strcat(ware.description, names[c%names_s]); ware.amount=amounts[c%(sizeof(amounts)/sizeof(int))]; ware.price=(amounts[c%(sizeof(amounts)/sizeof(int))]/price_divider); ware.storage_location=(storage_location_t){.character=*names[c%names_s], .number=names_usage[c%names_s]++}; db_insert_mute(db, ware); } } int main(int argc, char *argv[]){ bool running = true; char input; ware_t wares[1024]; db_t db = {.current_index=0, .max_index=sizeof(wares)/sizeof(ware_t), .wares=wares, .copy_index=-1}; db_autofill(&db, 10); while(running){ input = ask_question_char("\nVälkommen till lagerhantering 1.0\n=================================\n[L]ägga till en vara\n[T]a bort en vara\n[R]edigera en vara\nÅn[g]ra senaste ändringen\nLista [h]ela varukatalogen\n[A]vsluta\n\nVad vill du göra idag? _"); switch(input){ case 'L': add_goods(&db); break; case 'T': remove_goods(&db); break; case 'R': edit_goods(&db); break; case 'G': undo_goods(&db); break; case 'H': list_goods(&db); break; case 'A': running = exit_program(); break; default: puts("Okänt kommando."); } } return 0; }
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; }