CS 100: Lecture L23
April 20
public class Retailer { protected int nStores; // Number of stores. protected int nItems; // Number of items. protected int[][] price; // Price array: price[i][j] is the price (in dollars) of item j in store i. protected int[][] inv; // Inventory array: inv[i][j] is the inventory of item j in store i. // The Constructor. public Retailer(int[][] P, int[][] I) // P is the price array and I is the inventory array. They are the same size. The number // of stores equals the number of rows and the number of retail items equals the number of columns. { nStores = P.length; nItems = P[0].length; price = copy(P); inv = copy(I); } // i satisfies 0 <= i < nStores. // PO represents a purchase order. It has length nItems and P0[k] is the // quantity of item k that the customer wishes to buy. // Yields true if the ith store can handle the purchase order, i.e., // PO[j] < = inv[i][j], j=0,...,nItems-1. public boolean iCanDo(int i, int[] PO) { int j=0; while(j< nItems && PO[j]< =inv[i][j]) j++; return (j==nItems); } // i satisfies 0 < = i < nStores. // PO represents a purchase order. It has length nItems and P0[j] is the // quantity of item j that the customer wishes to buy. // Yields the cost of processing the purchase order at store i. public int iCost(int i, int[] PO) { int value = 0; for(int j=0;j < nItems;j++) value = value + price[i][j]*PO[j]; return value; } // PO represents a purchase order. It has length nItems and P0[j] is the // quantity of item j that the customer wishes to buy. // If no store can process the purchase order then an appropriate message is // printed and each store increases its inventory just enough so that it can // process the PO "the next time". // Otherwise, the index of the store that can most cheaply fill the PO is printed // along with the minimum cost. The store that processes the order has its inventory // depleted accordingly. public void processPO(int[] PO) { int i = 0; while (i< nStores && !iCanDo(i,PO)) i++; if (i==nStores) { // No store has sufficient inventory. System.out.println("No single store can process this order at this time."); updateInv(PO); } else { // Find the store that can most cheaply fill the PO. int minCost = iCost(i,PO); int bestStore = i; for (int s=i+1;s < nStores;s++) if (iCanDo(s,PO) && iCost(s,PO) < minCost) { bestStore = s; minCost = iCost(s,PO); } System.out.println("Price at store " + bestStore + " is " + minCost); updateInv(bestStore,PO); } } // PO represents a purchase order. It has length nItems and P0[j] is the // quantity of item j that the customer wishes to buy. The inventory of every // store is increased (if necessary) so that it can just handle the PO. public void updateInv(int[] PO) { for(int i=0;i< nStores;i++) for(int j=0;j< nItems;j++) if (inv[i][j]< PO[j]) inv[i][j] = PO[j]; } // i satisfies 0 < = i < nStores // PO represents a purchase order. It has length nItems and P0[j] is the // quantity of item j that the customer wishes to buy. // The inventory for store i is adjusted to reflect the processing of the PO. public void updateInv(int i, int[] PO) { for(int j=0;j< nItems;j++) inv[i][j] = inv[i][j] - PO[j]; } // Displays the inventory array. public void showInv() { System.out.println(" "); for(int i=0;i< nStores;i++) { for(int j=0;j< nItems;j++) Format.print(System.out," %3d ",inv[i][j]); System.out.println(" "); } System.out.println(" "); } // Yields a reference to an array that is an identical copy of A. public static int[][] copy(int[][] A) { int nRows = A.length; int nCols = A[0].length; int[][] B = new int[nRows][nCols]; for(int i=0;i< nRows;i++) for(int j=0;j< nCols;j++) B[i][j] = A[i][j]; return B; } }
import java.io.*; public class ShowRetailer { public static void main(String args[]) { TokenReader in = new TokenReader(System.in); // A sample price array. int[][] eg_P = { { 32 , 20 , 22 , 12 , 19 , 17 , 31 , 12} , { 34 , 22 , 25 , 10 , 13 , 19 , 33 , 15} , { 34 , 21 , 24 , 13 , 17 , 17 , 34 , 12} }; // A sample inventory array. int[][] eg_I = { { 10 , 20 , 60 , 10 , 40 , 70 , 20 , 10} , { 90 , 40 , 80 , 50 , 30 , 40 , 80 , 90} , { 30 , 60 , 40 , 30 , 90 , 70 , 30 , 80} }; // Build a store and process three customers. Retailer TallMart = new Retailer(eg_P,eg_I); TallMart.showInv(); int[] PO1 = {10,10,10,10,10,10,10,10}; TallMart.processPO(PO1); TallMart.showInv(); int[] PO2 = {20,20,20,20,20,20,20,20}; TallMart.processPO(PO2); TallMart.showInv(); int[] PO3 = {100,0,0,0,0,0,0,0}; TallMart.processPO(PO3); TallMart.showInv(); in.waitUntilEnter(); } } /* 10 20 60 10 40 70 20 10 90 40 80 50 30 40 80 90 30 60 40 30 90 70 30 80 Price at store 0 is 1650 0 10 50 0 30 60 10 0 90 40 80 50 30 40 80 90 30 60 40 30 90 70 30 80 Price at store 1 is 3420 0 10 50 0 30 60 10 0 70 20 60 30 10 20 60 70 30 60 40 30 90 70 30 80 No single store can process this order at this time. 100 10 50 0 30 60 10 0 100 20 60 30 10 20 60 70 100 60 40 30 90 70 30 80 */