First picture is the question the 2nd picture is the \"SHOPPING.LIST.TXT\" the 3
ID: 3780308 • Letter: F
Question
First picture is the question the 2nd picture is the "SHOPPING.LIST.TXT" the 3rd picture is the "INVENTORY.TXT". Please remember I need JAVA code ut Document Elements Tables l Charts SmartArt Review 12 A. A Aa. AaBbc subtiss O Teat Boa shape pie Heading 1 Heading 2 1.d) Using the code from part c, write a method to sort the inventory by item code. Do not use a built-in lava sorting process, write the sorting algorithm yourself It is fine to use bubble sort here. Print out the sorted inventory by calling the print method you wrote in part a. Show the sorted inventory BEFORE the shopping list is read and then print the inventory again AFTER the shopping list has been purchased. Save your working program as Lab7Part1d.java. (12 pts)Explanation / Answer
Answer:
Note: user hasn’t provided the code for part c, also the not given the code for print method. So I implemented only the sort method alone.
Assumption: inventory.txt format contains info in the form item_code, aisle-number, category, sub-category, cost, quantity, itemName.
//method to sort inventory
public void sortInventory()
{
//outer-loop for number -of-passes
for(int kk=0;kk<invCnt;kk++)
{
//inner loop for numbe-of-elements
for(int aa=0;aa<invCnt-kk-1;aa++)
{
//sort adjacent elements
if(item_code[aa]>item_code[aa+1])
{
//sort item code
int tc=item_code[aa];
item_code[aa]=item_code[aa+1];
item_code[aa+1]=tc;
//aisle number sorting
int an=aisleNumber[aa];
aisleNumber[aa]=aisleNumber[aa+1];
aisleNumber[aa+1]=an;
//swap category
String cat=category[aa];
category[aa]=category[aa+1];
category[aa+1]=cat;
//swap sub-category
String sc=subCategory[aa];
subCategory[aa]=subCategory[aa+1];
subCategory[aa+1]=sc;
//swap cost
double c=cost[aa];
cost[aa]=cost[aa+1];
cost[aa+1]=c;
//swap quantity
tc=quantity[aa];
quantity[aa]=quantity[aa+1];
quantity[aa+1]=tc;
//swap item name
String itnNm=itemName[aa];
itemName[aa]=itemName[aa+1];
itemName[aa+1]=itnNm;
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.