Driver.java (6238B)
1 package collections; 2 3 import java.util.Scanner; 4 5 public class Driver { 6 Scanner in = new Scanner(System.in); 7 8 public static void main(String[] args) { 9 int userSelection; 10 double totalBagWeight = 0; 11 double totalSampleWeight = 0; 12 Driver ui = new Driver(); 13 14 /* 15 try { 16 StackInterface<String> packageBag = (StackInterface) Class.forName(args[0]).newInstance(); 17 } 18 catch (ClassNotFoundException e1) { 19 e1.printStackTrace(); 20 } 21 catch (InstantiationException e1) { 22 e1.printStackTrace(); 23 } 24 catch (IllegalAccessException e1) { 25 e1.printStackTrace(); 26 } 27 */ 28 29 StackRA<Package> packageBag = new StackRA<>(); 30 StackRA<Sample> sampleBag = new StackRA<>(); 31 32 ui.listOptions(); 33 34 do { 35 userSelection = ui.menuSelection(); 36 System.out.println(userSelection); 37 switch (userSelection) { 38 case 0: 39 ui.exitProgram(); 40 break; 41 case 1: 42 totalBagWeight += ui.pickUp(packageBag); 43 break; 44 case 2: 45 if (ui.askForSample(packageBag)) { 46 ui.addSample(packageBag, sampleBag); // Take a sample if yes. 47 } 48 totalBagWeight -= ui.updateBagWeight(packageBag); 49 totalSampleWeight += ui.dropOff(packageBag); 50 break; 51 case 3: 52 ui.getBagContents(packageBag, totalBagWeight); 53 break; 54 case 4: 55 ui.getSampleContents(sampleBag, totalSampleWeight); 56 break; 57 case 5: 58 totalSampleWeight -= ui.updateSampleWeight(sampleBag); 59 ui.enjoySample(sampleBag); 60 break; 61 case 6: 62 totalSampleWeight = 0; 63 sampleBag.popAll(); 64 System.out.println("Sample bag has been emptied"); 65 } 66 } while (userSelection != 0); 67 } 68 69 public double pickUp(StackRA<Package> myStack) { 70 String name; 71 double weight; 72 int number; 73 String sender; 74 String recipient; 75 76 System.out.println("Please specify package info:"); 77 System.out.print("Item name: "); 78 name = in.nextLine().trim(); 79 System.out.print(name); 80 81 System.out.print("Item weight: "); 82 weight = Double.parseDouble(in.nextLine().trim()); 83 System.out.print(weight); 84 85 System.out.print("Number of items: "); 86 number = Integer.parseInt(in.nextLine().trim()); 87 System.out.print(number); 88 89 System.out.print("Sender: "); 90 sender = in.nextLine().trim(); 91 System.out.print(sender); 92 93 System.out.print("Recipient"); 94 recipient = in.nextLine().trim(); 95 System.out.print(recipient); 96 97 Package bagPackage = new Package(name, weight, number, sender, recipient); 98 99 100 myStack.push(bagPackage); 101 System.out.println("A package of " + name + " each weighing " + weight + " are now in the bag"); 102 103 return weight * number; 104 } 105 106 public boolean askForSample(StackRA<Package> bag) { 107 if (bag.isEmpty()) { 108 System.out.println("No deliveries to process!"); 109 return false; 110 } 111 else { 112 char answer; 113 System.out.println("Here is your package " + bag.peek().getRecipient() + ", may I please, please keep a sample (Y/N)?"); 114 answer = in.nextLine().charAt(0); 115 System.out.println(answer); 116 117 if (answer == 'Y') { 118 return true; 119 } 120 return false; 121 } 122 } 123 124 public double dropOff(StackRA<Package> myStack) { 125 if(myStack.isEmpty()) { 126 return 0; 127 } 128 129 Package pack = myStack.peek(); 130 myStack.pop(); 131 return pack.getWeight(); 132 } 133 134 public void addSample(StackRA<Package> packageBag, StackRA<Sample> sampleBag) { 135 Package pack = packageBag.peek(); 136 Sample newSample = new Sample(pack.getName(), pack.getWeight()); 137 sampleBag.push(newSample); 138 } 139 140 public double updateBagWeight(StackRA<Package> bag) { 141 if (!bag.isEmpty()) { 142 double weight = bag.peek().getWeight() * bag.peek().getNumber(); 143 return weight; 144 } 145 return 0; 146 } 147 148 public double updateSampleWeight(StackRA<Sample> bag) { 149 if (!bag.isEmpty()) { 150 return bag.peek().getWeight(); 151 } 152 return 0; 153 } 154 155 public void getBagContents(StackRA<Package> bag, double totalWeight) { 156 System.out.println("Bag has " + bag.size() + " items and weighs " + totalWeight + " lbs"); 157 } 158 159 public void getSampleContents(StackRA<Sample> samples, double totalWeight) { 160 161 System.out.println("Bag has " + samples.size() + " items and weighs " + totalWeight + " lbs"); 162 } 163 164 public void enjoySample(StackRA<Sample> sampleBag) { 165 if (sampleBag.isEmpty()) { 166 System.out.println("No samples to enjoy!"); 167 } 168 else { 169 System.out.println("This " + sampleBag.peek() + " is amazing! I love free stuff"); 170 sampleBag.pop(); 171 } 172 } 173 174 public int menuSelection() { 175 System.out.print("Make your menu selection now: "); 176 return Integer.parseInt(in.nextLine().trim()); 177 } 178 179 public void listOptions(){ 180 System.out.println("Select from the following menu:"); 181 System.out.println(" 0. Exit"); 182 System.out.println(" 1. Pick up an order"); 183 System.out.println(" 2. Drop off an order"); 184 System.out.println(" 3. Display number of packages and weight of bag."); 185 System.out.println(" 4. Display number of items and weight of bag of samples"); 186 System.out.println(" 5. Enjoy an item from bag of samples"); 187 System.out.println(" 6. Empty bag of samples"); 188 } 189 190 void exitProgram(){ 191 System.out.println("Exiting program...Good Bye"); 192 System.exit(0); 193 } 194 } 195