import java.util.ArrayList; import java.util.List; import java.util.Arrays; import java.util.HashMap; /** * SodaInventory keeps track of inventory of sodas. * A list of transactions is entered with a soda brand * and an amount. The inventory is updated by the amount. * @author JD * @version 11/2009 */ public class SodaInventory { public void strategy0() // THE BONEHEAD APPROACH { int coke = 0; int jolt = 0; int pepsi = 0; int sprite = 0; String sodacodes = " CPJS"; java.util.Scanner console = new java.util.Scanner(System.in); System.out.println("Enter transactions (soda and amount):\n"); // Read all the transactions while (console.hasNext()) { String soda = console.next().toUpperCase(); int amount = console.nextInt(); char response = soda.charAt(0); if (response == 'C') coke += amount; if (response == 'P') pepsi += amount; if (response == 'J') jolt += amount; if (response == 'S') sprite += amount; } System.out.println("Final inventory.\n"); System.out.println("coke " + coke); System.out.println("jolt " + jolt); System.out.println("pepsi " + pepsi); System.out.println("sprite " + sprite); /* Enter transactions (soda and amount): coke 40 jolt 20 sprite 50 coke -10 Final inventory. coke 30 jolt 20 pepsi 0 sprite 50 */ } public void strategy1() { int[] inventory = new int[256]; /* soda inventory */ String sodacodes = " CPJS"; java.util.Scanner console = new java.util.Scanner(System.in); System.out.println("Enter transactions (soda and amount):\n"); // Read all the transactions while (console.hasNext()) { String soda = console.next().toUpperCase(); int amount = console.nextInt(); char response = soda.charAt(0); inventory[(int)response] = inventory[(int)response] + amount; } System.out.println("Final inventory.\n"); for (int index = 1; index <= 4; index++) { char soda = sodacodes.charAt(index); System.out.print(soda +" "); System.out.println(inventory[(int)soda]); } /* Enter transactions (soda and amount): coke 20 sprite 40 coke -5 jolt 30 Final inventory. C 15 P 0 J 30 S 40 */ } /* Strategy 2 String[] sodanames = {"COKE","PEPSI","JOLT","SPRITE"}; but then we have to write the code for a search to find the desired name in the array. Not fun. Not smart, either, since List already has a search functionality (indexOf). */ public void strategy3() { int[] inventory = new int[256]; /* soda inventory */ String[] sodanames = {"COKE","PEPSI","JOLT","SPRITE"}; List sodaList = Arrays.asList(sodanames); java.util.Scanner console = new java.util.Scanner(System.in); System.out.println("Enter transactions (soda and amount):\n"); // Read all the transactions while (console.hasNext()) { String soda = console.next().toUpperCase(); int amount = console.nextInt(); int index = sodaList.indexOf(soda); inventory[index] = inventory[index] + amount; } System.out.println("Final inventory.\n"); for (int index = 0; index < 4; index++) { System.out.print(sodaList.get(index) +" "); System.out.println(inventory[index]); } /* Enter transactions (soda and amount): coke 5 jolt 20 pepsi 10 coke 15 Final inventory. COKE 20 PEPSI 10 JOLT 20 SPRITE 0 */ } public void strategy4() { /* soda inventory */ Mapinventory = new HashMap(); java.util.Scanner console = new java.util.Scanner(System.in); System.out.println("Enter transactions (soda and amount):\n"); // Read all the transactions while (console.hasNext()) { String soda = console.next().toUpperCase(); int amount = console.nextInt(); // inventory already contains this brand, update it if (inventory.containsKey(soda)) { int onHand = inventory.get(soda); onHand +=amount; inventory.put(soda,onHand); } else { inventory.put(soda,amount); } } System.out.println("Final inventory.\n"); for (String name : inventory.keySet()) { System.out.print(name +" "); System.out.println(inventory.get(name)); } } /* Enter transactions (soda and amount): coke 5 pepsi 20 jolt 10 coke 20 Final inventory. JOLT 10 PEPSI 20 COKE 25 */ /* Strategy 5 EnumMap */ }