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

Exercise 1: TEST PLAN [5 pts] Write a test plan consisting of a series of tests

ID: 3853696 • Letter: E

Question

Exercise 1: TEST PLAN [5 pts]

Write a test plan consisting of a series of tests and desired outcomes to completely test Project 1 based on the specifications provided in Homework 3b. You will need around 30 to 40 tests to go through all the requirements.

Example:

Exercise 2: ALGORITHMS [5 pts]

Submit the following:

Revised algorithm for the addrecord and deleterecord with C code

Below each line in the algorithm, write the corresponding C code. If needed, insert curly braces (keep the indents unchanged!).

Example:
If you have the following pseudocode,

define an int called i

if( i is not 0 )
    copy 0 to i


You need to insert lines of C code like this.

define an int called i
int i;

if( i is not 0 )
if( i != 0 )
{
    copy 0 to i
    i = 0;
}

Traces that verify the algorithm for the following test cases:

Adding two duplicate records to an empty list

Beginning with a list that consists entirely of two duplicate records, delete all duplicates so you end with an empty list.

-------------------------------------------------------------------------------------

This is my psudocode:

-----------------------------------

addRecord

------------------------------------

addRecord()

define a pointer to record called temp

allocate space on the heap and store its address into temp

copy from uaccountno to accontno in record whose address is in temp

copy from uname to name in record whose address is in temp

copy from uaddress to address in record whose address is in temp

copy null to next in record whose address is in temp

if(start is null)

copy from temp to start

else

define a pointer to record called s

copy from start to s

while(next from record whose address is in s is not null)

copy from next from record whose address is in s to s

copy from temp to next from record whose address is in s

-----------------------------------

deleteRecord

-----------------------------------

deleteRecord(define an int called accNo)

if(start is not null)

define a record called temp

copy from start to temp

while(next from record whose address is in temp is not NULL)

if(accNo is accountno from record whose address is in start and next from record whose address is in start is NULL)

release the space whose address is in temp

copy NULL to start

else if(accNo is accountno from record whose address is in start)

copy from next from record whose address is in start to start

release the space whose address is in temp

else if(accNo is accountno from record whose address is in next)

define a record called temp1

copy from next from record whose address is in temp to temp1

copy from next from record whose address is in next to next

release the space whose address is in temp1

else

copy from next from record whose address is in temp to temp

the language is C

Test 1: Start the program by typing the program name with no arguments. e.g. ./project1 Outcome: Menu is displayed Test 2: Select the Print option Outcome: Displays a message that the list is empty Test 3: Select the Add Option and add the following information: 113344 John Smith 1111 Dole St., Honolulu, HI 96822 Outcome: Record gets added and confirmation is displayed to the user.

Explanation / Answer

int debugMode = 0;

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"record.h"
void openMenu(int*);
void getname(char*);
void getAccountNumber(int*);
void getYearOfBirth(int*);
int addRecord (struct record **, int, char [ ],char [ ],int);
int printRecord (struct record *, int);
int modifyRecord (struct record *, int, char [ ]);
void printAllRecords(struct record *);
int deleteRecord(struct record **, int);
void getfield (char [ ], int);
int readfile(struct record **, char []);
void writefile(struct record *, char []);

/****************************************************************************
*
* Function Name: openMenu
*
* DESCRIPTION: Shows the user the menu of what options they can choose.
*
* Parameters: pick (int*): pointer to the selected variable.
*
* Return Values: None
*
*****************************************************************************/

void openMenu(int* pick)
{
    char word [10];
    printf("You have opened the bank database. ");


    do
    {
        printf("Would you like to: ");
        printf("|%d|: Add a record? ", 1);
        printf("|%d|: Modify a record? ", 2);
        printf("|%d|: Print a record? ", 3);
        printf("|%d|: Delete a record? ", 4);
        printf("|%d|: Print all records in database? ", 5);
        printf("|%d|: Quit the program ", 6);

        fgets(word,10, stdin);
        *pick = atoi(word);
        printf("You have picked |%d| ", *pick);

        if(*pick > 7 || *pick <= 0)
        {
            printf("Invalid, please enter something valid. ");
        }

    }
    while(*pick <= 0 || *pick > 7);


}

/****************************************************************************
*
* Function Name: getfield
*
* DESCRIPTION: Gets address(es) from the user
*
* Parameters: address(char [ ]): stores the addresses in an array
*             length(int): the length of the array
*
* Return Values: none
*
*****************************************************************************/

void getfield(char address[ ], int length)
{
    int i, c;
    i = 0;
    printf("Enter your address. Type '!' when you are finished. ");
  
    do
    {
        c = fgetc(stdin);
        address[i] = c;
        if(c == EOF || c == '!')
        {
            address[i] = '';
            i = length;
        }
        i++;
    }
  
    while(i < length);
  
    printf("You have entered :|%s|. ", address);
    getchar();

}

/***************************************************************************
*
* Function Name: getName
*
* DESCRIPTION: Gets the name that was input by the users.
*
* Parameters: name(char[]) the name's location.
*
* Return Values: None
*
*****************************************************************************/

void getName(char name[])
{
   int length;
   printf("Enter the contact's name. ");
    fgets(name, 25 ,stdin);
    length = strlen(name)-1;
    if( name[length] == ' ')
    {
        name[length] = '';
    }
    printf("You have entered :|%s|. ",name);

}

/**************************************************************************
*
* Function Name: getYearOfBirth
*
* DESCRIPTION: Gets the year of birth that was input by the user.
*
* Parameters: pYearOfBirth(int*): The year of birth variable's location.
*
* Return Values: None
*
****************************************************************************/

void getYearOfBirth(int* pYearOfBirth)
{
   int length;
   char birthYear[100];
   printf("Please enter in year of birth. ");
   fgets(birthYear, 100, stdin);
   *pYearOfBirth = atoi(birthYear);


   printf("You have entered: |%d|. ", *pYearOfBirth);

}

/**************************************************************************
*
* Function Name: getAccountNumber
*
* DESCRIPTION: Gets the account number that was input by the user.
*
* Parameters: int: The user's account number.
*
* Return Values: None
*
****************************************************************************/

void getAccountNumber(int* pAccNumber)
{
        int length;
        char accNumber[100];
        printf("Please enter your account number. ");
        fgets(accNumber, 100, stdin);
        *pAccNumber = atoi(accNumber);


        printf("You have entered: |%d|. ", *pAccNumber);

}

/***************************************************************************
*
* Function Name: readfile
*
* DESCRIPTION: Reads from a file and places bank customers in a list
*
* Parameters: start(struct record**): pointer to the pointer of the
*             start of the list
*             filename(char[]): name of the file to read from
*
* Return Values: 0 if read correctly
*               1 if no file with given filename
*
***************************************************************************/

int readfile(struct record** start, char filename[])
{
    int i;
    FILE *fp;
    int accountno;
    char accNo[100];
    char name[25];
    char address[80];
    char yob[100];
    int yearofbirth;
    fp = fopen(filename, "r");

    if(fp == NULL)
    {
        printf("Could not open |%s|. ", filename);
        fclose(fp);

        return 1;
    }

    while(!feof(fp))
    {
        fgets(accNo, 100, fp);
        accountno = atoi(accNo);

        fgets(name, 25, fp);

        if(feof(fp))
        {
            break;
        }

        i = strlen(name)-1;

        if( name[i] == ' ')
        {
            name[i] = '';
        }

        fgets(address, 80, fp);
        i = strlen(address)-1;

        if(address[i] == ' ')
        {
            address[i] = '';
        }

        fgets(yob, 100, fp);
        yearofbirth = atoi(yob);

        addRecord(start, accountno, name, address, yearofbirth);
    }

    fclose(fp);

    return 0;
}

/**************************************************************************
*
* Function Name: writefile
*
* DESCRIPTION: Writes the bank records to the file
*
* Parameters: first(struct record*): first record in the list
*             filename(char[]): name of the file to write to
*
* Return Values: None
*
**************************************************************************/

void writefile(struct record* first, char filename[])
{
    struct record* temp;
    FILE *fp;
    temp = first;
    fp = fopen(filename, "w");

    if (temp == NULL)
    {
        fclose(fp);
        return;
    }

    if(temp->next == NULL)
    {
        fprintf(fp, "%d ", temp->accountno);
        fprintf(fp, "%s ", temp->name);
        fprintf(fp, "%s ", temp->address);
        fprintf(fp, "%d ", temp->yearofbirth);
    }

    else if(temp->next != NULL)
    {

        while(temp->next != NULL)
        {
            fprintf(fp, "%d ", temp->accountno);
            fprintf(fp, "%s ", temp->name);
            fprintf(fp, "%s ", temp->address);
            fprintf(fp, "%d ", temp->yearofbirth);
            temp = temp->next;
        }

        fprintf(fp, "%d ", temp->accountno);
        fprintf(fp, "%s ", temp->name);
        fprintf(fp, "%s ", temp->address);
        fprintf(fp, "%d ", temp->yearofbirth);
    }

    fclose(fp);
}

/*****************************************************************************
*
* Funciton Name:   main
*
* DESCRIPTION:   Runs the bank program.
*
* Parameters:    argc(int): amount of arguments taken
*       argv(char[]): arguments taken in a list.
*
* Return values: 0 when program is exited
*
******************************************************************************/

int main(int argc, char *argv[])
{  
/* Main method variables */
    int menuSelect;
    char debug[50];
    int runProgram = 1;
    int *pMenuSelect;

    char name[25];
    char address[100];
    int accNumber;
    int* pAccNumber;
    int YearOfBirth;
    int* pYearOfBirth;

    struct record* start;
    struct record** pstart;

    pAccNumber = &accNumber;
    pYearOfBirth = &YearOfBirth;
    pMenuSelect = &menuSelect;
    start = NULL;
    pstart = &start;

    strcpy(debug, "debug");

    if( argc == 2 && strcmp(argv[1] ,debug) == 0)
    {
        debugMode = 1;
        printf("DEBUG MODE ON ");

    }
    else if(argc == 2 && strcmp(argv[1] ,debug) != 0)
    {
        printf("Invalid Argument. ");
        return;
    }

    if(argc > 2)
    {
        printf("Too many arguments ");
        return;
    }

    readfile(pstart, "bankrecord.txt");

    do
    {
        openMenu(pMenuSelect);

        if(menuSelect == 1)
        {
            getAccountNumber(pAccNumber);
            getName(name);
            getfield(address, 100);
            getYearOfBirth(pYearOfBirth);
            addRecord(pstart,accNumber,name,address,YearOfBirth);
            printf("You have added a record to the bank database. ");
        }
        else if(menuSelect == 2)
        {
            getAccountNumber(pAccNumber);
            getfield(address,100);
            modifyRecord(start, accNumber, address);  
        }
        else if(menuSelect == 3)
        {
            getAccountNumber(pAccNumber);
            printRecord(start, accNumber);
        }
        else if(menuSelect == 4)
        {
            getAccountNumber(pAccNumber);
            deleteRecord(pstart, accNumber);
        }
        else if(menuSelect == 5)
        {
            printAllRecords(start);
        }
        else if(menuSelect == 6)
        {
            printf("You have closed the bank database. ");
            writefile(start, "bankrecord.txt");
            runProgram = 0;
        }
                                 
    }
  
    while (runProgram == 1);
  
    return 0;
}