# Modify the compareCoffeePricesList function and rename # the function to \'exp
ID: 3877523 • Letter: #
Question
# Modify the compareCoffeePricesList function and rename
# the function to 'expensesManager'. As the name suggests,
# the 'cafeNameList' should now be used to store a list of expense
# names and it should be renamed to 'expenseNameList' instead. Unlike,
# the cafeNameList, assume that the user will have multiple cost
# entries for the same name. E.g. your expense name lists might
# contain multiple entries called "junk food" and for each there will
# be a corresponding priceList entry for its cost.
# # Allow the user to see the total cost of an expense with
# the same name. E.g. if the user enters "junk food", your program should
# add the prices of all the priceList entries for "junk food" and
# show the toal. If they enter "coffee" it should show the total for
# coffee expenses, if any are in the list. -------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------
def compareCoffeePricesList():
numCafes=requestInteger("How many cafes?")
coffeePrices=[None]*numCafes
total=0
index=0
while(index<numCafes):
coffeePrices[index]=requestNumber("Price of a coffee at cafe "+cafeNameList[index])
total=total+coffeePrices[index]
index=index+1
average=total/numCafes
index=0
while(index<numCafes):
if coffeePrices[index]<average:
print "Cafe:", cafeNameList[index]," coffee is $",(average-coffeePrices[index]),"cheaper than average"
index=index+1
Explanation / Answer
Ans :)
import java.util.Scanner;
public class Test3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Test3 t=new Test3();
t.expensesManager();
}
public void expensesManager(){
System.out.println("Enter How many cafes?");
Scanner sc=new Scanner(System.in);
int numCafes=sc.nextInt();
String expenseNameList[]=new String[numCafes];
float coffeePrices[]=new float[numCafes];
int index=0;
float total=0.0f;
//For entries only (read data)
while(index<numCafes){
sc=new Scanner(System.in);
System.out.println("Enter expenseNameList :"+(index+1));
expenseNameList[index]=sc.next();
System.out.println("Enter Price of a coffee at cafe :"+expenseNameList[index]);
coffeePrices[index]=sc.nextInt();
index=index+1;
}
System.out.println("Enter Entry for getting Total :");
String inputEntry=sc.next();
index=0;
while(index<numCafes){
if(inputEntry.equalsIgnoreCase(expenseNameList[index])){
total=total+(float)coffeePrices[index];
}
index=index+1;
}
if(total!=0.0f)
System.out.println("The Total of "+inputEntry+" is :"+total);
else
System.out.println("The given input "+inputEntry+" is not in the list");
}
}
Explanation : I used here java code , find the simple implementation logic.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.