Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

We recently discussed data structures, and coded data structures. Simulate \"sho

ID: 3764459 • Letter: W

Question

We recently discussed data structures, and coded data structures.

Simulate "shopping list" software (can assume String items), by using an ArrayList. Your program must repeatedly output these menu options, until the user chooses "7" to Exit and stop the program:

1. Show shopping list. [If empty, say "No items".]

2. Add new item to list. [Ask user to input new item, then add it to back of list.]

3. Remove an item from the list. [Ask user to input the list number of the item.]

4. Insert an item into the list. [Ask user to input the new data, and the list number of the insertion point.]

5. Change an item in the list. [Ask user to input the list number, and the new data.]

6. Clear the list. [Empties all items, results in an empty list.]

7. Exit. [Exits the program.]

Recall that ArrayList items start at position index 0.

But, this electronic "shopping list" software must assume that the shopping list data, when processing the user's menu choices, start at position 1, as is typically the case with human-numbered lists. When you output the list, for menu option 1, each item in the list must show its position number, starting at 1.

For example, if the user chooses menu option 1, the program's output is something like: 1. Chips 2. Sodas 3. More chips where the shopping list software numbers from index 1, although the data-tracking ArrayList is actually numbered from index 0.

Explanation / Answer

#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <fstream.h>
#include <process.h>
#include <stdlib.h>

class list
    {
    int count;
    char serial[30][30];
    char name[30][30];
    char price[30][30];
    char quality[30][30];
    char quantity[30][30];
    public:
    void goods();
    void order_goods();
    void show_goods();
    };

void list::order_goods()
    {
    char pid[30];
    long qid;
    int found=0;
    cout<<"Enter Product id: ";
    cin>>pid;
    int v;
    for(v=0;v<count;v++)
    {
    if(strcmp(serial[v],pid)==0)
        {
        found=1;
        break;
        }
    }
    if(found==1)
    {
    char read;
    cout<<"Enter quantity(kg): ";
    cin>>qid;
    AA:
    cout<<"Are you sure of ordering "<<qid<<" kg of "<<name[v];
    cout<<" Enter (y/n)";
    cin>>read;
    if(read==121)
    {
    fstream fout;
    fout.open("list.txt",ios::out);
    long sum=0;
    int d=1;
    for(int s=strlen(quantity[v])-1;s>=0;s--)
        {
        sum+=(quantity[v][s]-48)*d;
        d*=10;
        }
    sum+=qid;

    //sum=(long)quantity[v];
    //cout<<"sum"<<sum;

    itoa(sum,quantity[v],10);

    for(int m=0;m<count;m++)
        {
        fout<<serial[m]<<";";
        fout<<name[m]<<";";
        fout<<price[m]<<";";
        fout<<quality[m]<<";";
        fout<<quantity[m]<<";"<<" ";
        }
        long ati=atoi(price[v])*qid;
    cout<<" "<<ati <<" taka has been truncated from shop account ";
    }
    else if(read==110);
    else
    {
    clrscr();
    goto AA;
    }
    //char y='n';
    //printf("%d",y);
    }
    }
void list::goods()
    {
    char pricestr[20];
    char str[2000];
    ifstream fin;
    fin.open("list.txt",ofstream::in);

    //char s[2000];
    count=0;
    while(fin>>str)
    {

    int savei=0;
    //cout<<str;
    //   cout<<strlen(str)<<" ";
    int i=0;
    int d=0;
    while(i<strlen(str))
    {

    if(str[i]==';')
        {

    //cout<<"Savei="<<savei<<" "<<"i="<<i<<" ";
    char s[30];
    int l=0;
    for(int j=savei;j<i;j++)
          {
          s[l]=str[j];
          l++;
          }
          s[l]='';
    if(d==0)
    strcpy(serial[count],s);
    else if(d==1)
    strcpy(name[count],s);
    else if(d==2)
    strcpy(price[count],s);
    else if(d==3)
    strcpy(quality[count],s);
    else if(d==4)
    strcpy(quantity[count],s);
    d++;
       //cout<<"strlen="<<l<<",s="<<s<<" ";
    //char tab=' ';
    //printf("%5c",tab);

       i++;
       savei=i;
        }
    //printf("%c",str[i]);

    i++;

    }
    count++;
    if(fin.eof())
    break;
    //cout<<" ";
    }
    fin.close();


      }
      void list::show_goods()
      {
      cout<<"p-id name price    quality quantity(kg) ";
    cout<<"----------------------------------------------------------- ";
      for(int v=0;v<count;v++)
    {
    printf("%s",serial[v]);
    printf("%8s",name[v]);
    printf("%8s",price[v]);
    printf("%15s",quality[v]);
    printf("%10s",quantity[v]);
    printf(" ");
    }
      }
void main()
{
A:
clrscr();
cout<<"Make your choice: [1] For see products [2] For order products [3] For quit ";
list ob;
char a;
cin>>a;
clrscr();
ob.goods();
if(a==49)
ob.show_goods();
else if(a==50)
ob.order_goods();
else if(a==51)
exit(0);
else
goto A;
cout<<" Press any key to return to Main Menu.";
getch();
goto A;

}

////////////////////////////////////////////////////////////////////////////
You must include a file "list.txt",You can include data in this file "list.txt" like below,

12001;salt;20;good;1022;
12002;sugar;35;excellent;1104;
12003;Milk;42;excellent;500;
12004;Rice;23;good;2523;
12005;Atta;35;excellent;100023;

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote