120 likes | 237 Views
Prutt05 Tentamen. Model Answers Note that many variations are possible to obtain full marks. Q1. 0,1. 1. 1. Shopping_Cart. List_of_Item. totalPrice:float. Item purchase. 1. totalDiscount:float. clubMember:[]bool. Add(Item purchase). itemCount:int. Delete(Item purchase). Delete( ).
E N D
Prutt05 Tentamen • Model Answers • Note that many variations are possible to obtain full marks.
Q1. 0,1 1 1 Shopping_Cart List_of_Item totalPrice:float Item purchase 1 totalDiscount:float clubMember:[]bool Add(Item purchase) itemCount:int Delete(Item purchase) Delete( ) Add(Item purchase) * Delete(Item purchase) * Item Delete( ) Price:float checkout( ) DiscountOnPrice:float ShoppingCart(Item purchase ) Quantity:Int Book Clothing DVD author:String size:Int artist:String title:String description:String title:String MensClothing WomensClothing ChildrensClothing
Q1.(iii) Add(Item purchase) { if (purchase.Quantity >0) { this.theItemList.add(purchase); purchase.Quantity--; this.itemCount++; if (purchase instanceOf Clothing && clubMember[1]) || (purchase instanceOf Book && clubMember[2]) || (purchase instanceOf DVD && clubMember[3]) { this.totalPrice += ( purchase.Price - purchase.DiscountOnPrice) ; } else {this.totalPrice += purchase.Price; } } else { // throw an exception: OutOfStock error? } } // of Add()
Q2.(i) Precondition: user has entered the web site. No shopping cart exists yet. Actors: end_user Scenario: • The user clicks on an item and selects “purchase” option. A new ShoppingCart object is created, and its List_Of_Item component is initialised to contain the purchase. • Repeated any finite number of times 2.a The user clicks on an item and selects the purchase option. The item is added to the ShoppingCart’s List_Of_Item attribute. OR 2.b The user clicks on an item in the ShoppingCart and selects the delete option. The item is deleted from the ShoppingCart’s List_Of_Item attribute. 3.a. The user clicks on the “checkout” button, OR 3.b The user clicks on the “empty shopping cart” button. Postcondition: User has placed an order and shopping cart is deleted, or else no order and shopping cart is empty but still exists.
:Shopping_Cart :List_Of_Item ShoppingCart(purchase) * Alt Add(purchase) Add(purchase) Delete(purchase) Delete(purchase) Alt Checkout() Delete() Delete()
Q4 import junit.framework.TestCase public class Shopping_Cart_Test extends TestCase { private Shopping_Cart testCart; private Book testBook = new Book(“Title1”, “Author1”, 10.99, 1.00, 10); // 10 copies in store at 10.99 each, discount 1.00 private Book testBook2 = new Book(“Title2”, “Author2”, 11.99, 2.00, 20); // 20 copies in store at 11.99 each, discount 1.00 protected void setUp() { testCart = new Shopping_Cart(); // no items clubMember[2] = True; testCart.addItem(testBook); // setup with a first item } protected void tearDown() { // not needed }
Q3. For such a small project XP or COTS would seem to be the best models. There seems to be no room for large documentation. However both these approaches will satisfy any bank needs for project visibility. Study the course notes further to appreciate the consequences of these two choices. Consider also the many disadvantages of the spiral model and waterfall model in this case. Lastly, rapid prototyping doesn’t seem necessary here, as the product is quite well understood. (We may even be able to buy most of it off the shelf!).
Public void testAdd() { // add an item and then check its there testCart.addItem(testBook2); assertEquals(testCart.totalPrice, 9.99 + 9.99); assertEquals(testCart.itemCount, 2); assertEquals(testCart.totalDiscount, 2.00 + 1.00); assertEquals(testBook2.getQuantity(), 19); } // testBook2 is still in the cart Public void testDelete(Item purchase) throws ItemNotFoundException { // delete an item and check its gone i.e. back in store testCart.delete(testBook2); assertEquals(testCart.totalPrice, 9.99); assertEquals(testCart.itemCount, 1); assertEquals(testCart.totalDiscount, 1.00); assertEquals(testBook2.getQuantity(), 20); }
Public void testDeletItemNotInCart() {// try to delete a non-existent item, // is this possible via the user interface? Maybe? try { private Book testBook3 = new Book(“Title3”, “Author3”, 10.99, 1.00, 10); testCart.delete(testBook3); fail(“Should raise an ItemNotFoundException”); } catch (ItemNotFoundException e) { //passes the test } } public void testDelete() { // empty the cart and it should be empty! testCart.delete(); assertEquals(testCart.itemCount, 0); assertEquals(testCart.totalPrice, 0.0); }
Q5. <DOCTYPE Shopping_Cart [ <!ELEMENT Shopping_Cart (Shopper, Book*, DVD*, Clothes*)> <!ATTLIST Shopping_Cart totalPrice #REQUIRED> <!ATTLIST Shopping_Cart totalDiscount #REQUIRED> <!ATTLIST Shopping_Cart itemCount #REQUIRED> <!ATTLIST Shopping_Cart dvdMember (TRUE|FALSE) “FALSE”> <!ATTLIST Shopping_Cart bookMember (TRUE|FALSE) “FALSE”> <!ATTLIST Shopping_Cart clothingMember (TRUE|FALSE) “FALSE”> <!ELEMENT Book (Title, Author+)> <!ELEMENT Title (#PCDATA)> <!ELEMENT Author (#PCDATA)> <!ATTLIST Book Price #REQUIRED> <!ATTLIST Book DiscountOnPrice #REQUIRED> <!ATTLIST Book Quantity #REQUIRED>
<!ELEMENT DVD (Title, Artist+)> <!ELEMENT Title (#PCDATA)> <!ELEMENT Artist (#PCDATA)> <!ATTLIST DVD Price #REQUIRED> <!ATTLIST DVD DiscountOnPrice #REQUIRED> <!ATTLIST DVD Quantity #REQUIRED> <!ELEMENT Clothing (Style, Size, Description)> <!ELEMENT Style (#PCDATA|MensClothing|WomensClothing|ChildrensClothing)*> <!ELEMENT Size (#PCDATA|XS|S|M|L|XL)*> <!ELEMENT Description (#PCDATA)> <!ATTLIST Clothing Price #REQUIRED> <!ATTLIST Clothing DiscountOnPrice #REQUIRED> <!ATTLIST Clothing Quantity #REQUIRED>
<?xml version=“1.0” encoding=“UTF-8”?> <Shopping_Cart totalPrice=“11.99”, itemCount=“1”, totalDiscount=“1.00”, dvdMember=“TRUE”, bookMember=“TRUE”, clothesMember=“TRUE”> <Book price=“11.99”, discount_On_Price=“1.00”,> <Title>”MyTitle”</Title> <Author>”MyAuthor”</Author> </Book> </Shopping_Cart> Q6. The problem here is that item prices need to be synchronised. If this is done then it is not possible for cusomers to access them while they are being changed. If they are not synchronised then “race” can occur, which leads to the phenomenon described in the question. Study the course notes in order to appreciate how to make changes to the code.