has to be done using pseudo code Assume that a file named inventory, which conta
ID: 3779252 • Letter: H
Question
has to be done using pseudo code
Assume that a file named inventory, which contains the inventory of parts for the Legendary Lawn Mower Company, already exists with records of the following form: partnumber (integer), partname (string), quantity (integer).
Also assume that the records in this file are ordered by increasing part number. For each part of this problem, write a program that performs the indicated task.
a) Input a part number from the user and delete the corresponding record from the inventory file.
b) Input a part number and a quantity from the user and modify the record corresponding to that part number by changing the value of its last field to the quantity input.
c) Input a new part number, part name, and quantity from the user and insert the corresponding record in the proper place in the inventory file.
Explanation / Answer
a)
Delete(partnumber) //write a function to delete a record
open file inventory using inventory =fopen("inventory","r") in read mode
open temporary file in write mode temp= fopen("temp","w")
string a //declaring string
getline(inventory,a) //getting first line of inventory
int part = a.partnumber //can obtain by splitting line by comma and convert first value into integer
while(a!=eof) { //checking whether file ended or not
if (part != partnumber) //check if partnumber is same as the one to be deleted
since partnumber is not same as the one to be deleted write the line a into temp
else
since this record is to be deleted don't write this to temp.
getline(inventory,a)
part = a,partnumber
}
After while loop is done
close both file descriptors and rename temp to inventory
So at the end of function we'll have inventory without the record of input partnumber.
b)
UpdateRecord(partnumber , quantity)
open file inventory using inventory =fopen("inventory","r") in read mode
open temporary file in write mode temp= fopen("temp","w")
string a //declaring string
getline(inventory,a) //getting first line of inventory
while(a!=eof) //checking whether file ended or not
if (a.partnumber != partnumber) //check if partnumber is same as the one to be deleted
since partnumber is not same as the one to be updated write the same line a into temp
else
write record into temp file with updated quantity since quantity of part has changed.
getline(inventory,a)
}
After while loop is done
close both file descriptors and rename temp to inventory
So at the end of function we'll have inventory with the record of input partnumber updated with new quantity number.
c)
InsertPart(partnumber,partname,quantity)
open file inventory using inventory =fopen("inventory","r") in read mode
open temporary file in write mode temp= fopen("temp","w")
string current //declaring string
getline(inventory,current) //getting first line of inventory
int current_part = current.partnumber //can obtain by splitting line by comma and convert first value into integer
int inserted =0 //check whether record is inserted or not
while(current!=eof) { //checking whether file ended or not
if(current_part > partnumber && inserted ! =1 ) //checking where to insert so that increasing order is preserved or not.
{ write new record ("partnumber,partname,quantity") to temp because inventory should be in increasing order
write current to temp //because this follows the inserted record and initial record is already sorted
inserted = 1 // so that it's not inserted again
}
else
write current to temp file // since this is not the position to insert
getline(inventory,current)
current_part = current.partnumber
}
After while loop is done check whether new part is inserted or not by the value of inserted
if (inserted == 0) // implies new record should be inserted at end
{
write new record to temp file with string "partnumber,partname,quantity"
}
close both file descriptors and rename temp to inventory
So at end of function we'll have new inventory with part inserted at position according to increasing order.
Main()
output to user asking which task to perform : " 1 for deleting , 2 for updating quantity of a part , 3 for inserting a new part and 4 to exit "
if task is 1 //for deleting
cin>>partnumber //take input partnumber
delete(partnumber)
if task is 2 // implies updating quantity of a part
cin>>partnumber>>quantity //take inputs
UpdateRecord(partnumber,quantity) //function call to update record
if task is 3 // task is to insert new part into inventory
cin>>partnumber>>partname>>quantity
InsertPart(partnumber,partname,quantity)
if task is 4
exit
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.