예제 #1
0
  public static void main(String[] args) {
    String directory = "C:\\Users\\.file\\Desktop\\";
    String cartId = null;
    ShoppingCart cart = null;
    System.out.println("Enter the ID of the cart file to create and serialise it or q to exit.");

    try (BufferedReader in = new BufferedReader(new InputStreamReader(System.in))) {
      cartId = in.readLine();
      if (cartId.equals("q")) {
        System.exit(0);
      }
      cart = new ShoppingCart(new Integer(cartId).intValue());
    } catch (IOException | NumberFormatException e) {
      System.out.println("Exception: " + e);
      System.exit(-1);
    }

    cart.addItem(new Item(101, "Duke Plastic Circular Flying Disc", 10.95));
    cart.addItem(new Item(123, "Duke Soccer Pro Soccer Ball", 29.95));
    cart.addItem(new Item(45, "Duke \"The Edge\" Tennis Balls - 12-Ball Bag", 17.49));

    System.out.println("Shopping cart " + cartId + " contains " + cart.getCartSize() + " items.");
    System.out.println(
        "Shopping cart total: " + NumberFormat.getCurrencyInstance().format(cart.getCartTotal()));

    String cartFile = directory + "cart" + cart.getCartId() + ".ser";
    try (FileOutputStream fos = new FileOutputStream(cartFile);
        ObjectOutputStream oos = new ObjectOutputStream(fos)) {
      oos.writeObject(cart);
    } catch (IOException e) {
      System.out.println("Exception serializing " + cartFile + ": " + e);
      System.exit(-1);
    }
    System.out.println("Successfully serialized shopping cart with ID: " + cart.getCartId());
  }
예제 #2
0
  public static void main(String args[]) throws Exception {

    shoppingCart.addItem("Bread");
    shoppingCart.addItem("Milk");
    Thread.sleep(15000);
    shoppingCart.addItem("Tea");

    System.out.println("Contents of your cart are:");
    Collection<String> items = shoppingCart.getItems();
    for (String item : items) {
      System.out.println(item);
    }

    shoppingCart.finished();
  }
예제 #3
0
  public static void main(String ar[]) {

    ShoppingCart cart = new ShoppingCart();

    Item item1 = new Item("C12", 2256);
    Item item2 = new Item("B22", 1999);
    Item item3 = new Item("DX2", 999);

    cart.addItem(item1);
    cart.addItem(item2);
    cart.addItem(item3);

    PaymentStrategy creditCardPayment =
        new CreditCardPayment("*****@*****.**", "1111111111111111", 111, "11/11/1111");
    cart.pay(creditCardPayment);

    PaymentStrategy paypalPayment = new PaypalPayment("*****@*****.**", "12423");
    cart.pay(paypalPayment);
  }
예제 #4
0
 @Test
 public void testAddItem() throws Exception {
   cart.addItem("9781430224693");
   assertEquals(3, cart.getItems().size());
   assertEquals(1, (Object) cart.getItems().get("9781430224693"));
 }
예제 #5
0
 @Before
 public void setUp() throws Exception {
   cart = new ShoppingCart();
   cart.addItem("9781430216407");
   cart.addItem("9781430219088");
 }