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

Need help with Java coding Instructor provided files: ca case2.txt case3 txt cas

ID: 3848764 • Letter: N

Question

Need help with Java coding

Instructor provided files: ca case2.txt case3 txt case4.txt case5.txt Deals on Cookies It is lunch time again and I am starving. I would like to buy some cookies from a small Cafe, named Cafe CS201, owned and operated by CSC201 Cafe CS201 offen offers discounts for buying multiple cookies I wonder which of the discounts provides the best values. For this problem, please write a program to help me find the best deal Input The input contains multiple scenarios. Each scenario starts with a line containing two numbers N 1 N K-10) and M 1

Explanation / Answer

Solution: See the code below:

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

package cokiedeals;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
* CokieDealsSelection class
*
*/
public class CokieDealsSelection {

   /**
   * @param args
   * @throws Exception
   */
   public static void main(String[] args) throws Exception {
       // TODO Auto-generated method stub
       int N=-1; //deals available
       int M=-1; //max. cookies required by customer
       BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
       while(N != 0 && M !=0)
       {
           //read deals available and cookies required
           String line=reader.readLine();
           String[] tokens=line.split(" ");
           N=Integer.parseInt(tokens[0]);
           M=Integer.parseInt(tokens[1]);
           //check if all parameters ok
           if((N >= 1 && N <= 10) && (M >= 1 && M <= 20))
           {
               int[][] deals = new int[N][2]; //two-dimensional array to store deals
               //capture deals
               for(int i=0;i<N;i++)
               {
                   line=reader.readLine();
                   tokens=line.split(" ");
                   deals[i][0]=Integer.parseInt(tokens[0]);
                   deals[i][1]=Integer.parseInt(tokens[1]);
               }
               //process deals
               //first filter possible best deals
               int[] possibleBestDeals=new int[N];
               int numPossibleBestDeals=0;
               for(int i=0;i<N;i++)
               {
                   int numCookies=deals[i][0];
                   if(numCookies <= M)
                   {
                       possibleBestDeals[numPossibleBestDeals]=i;
                       numPossibleBestDeals++;
                   }
               }
               //now identify best deal from possible best deals
               int bestDeal=possibleBestDeals[0];
               int bestDealCookies=deals[bestDeal][0];
               int bestDealCost=deals[bestDeal][1];
               double bestDealCostPerCookie=((double)bestDealCost)/bestDealCookies;              
               for(int i=1;i<numPossibleBestDeals;i++)
               {
                   int deal=possibleBestDeals[i];
                   int numCookies=deals[deal][0];
                   int dealCost=deals[deal][1];
                   double dealCostPerCookie=((double)dealCost)/numCookies;
                   if(dealCostPerCookie < bestDealCostPerCookie)
                   {
                       bestDeal=deal;
                       bestDealCookies=numCookies;
                       bestDealCost=dealCost;
                       bestDealCostPerCookie=dealCostPerCookie;
                   }
                   else if(dealCostPerCookie == bestDealCostPerCookie)
                   {
                       if(numCookies > bestDealCookies)
                       {
                           bestDeal=deal;
                           bestDealCookies=numCookies;
                           bestDealCost=dealCost;
                       }
                   }                  
               }
               //print output
               if(numPossibleBestDeals > 0)
                   System.out.println("Purchase "+deals[bestDeal][0]+" for $"+deals[bestDeal][1]);
               else
                   System.out.println("No good deals");
           }
           else
               break;
       }      
       reader.close();
   }
}

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

Output:

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

5 6
12 10
9 8
6 6
3 3
1 2
Purchase 6 for $6
3 5
1 3
3 5
4 7
Purchase 3 for $5
0 0

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