Java Object Oriented Analysis In Addition To Blueprint Occupation - Vending Auto Purpose 2

This is the minute business office of Java tutorial to present how to create Vending Machine inward Java. In the first part, nosotros receive got discussed work arguing too the solution itself, but unit of measurement testing too pattern document was nonetheless pending, which we'll run across inward this article.  As I said, at that topographic point are multiple ways to implement Vending machine inward Java e.g. you lot could receive got easily used state pattern pattern to implement a vending machine, inward fact, it's 1 of the best examples of State pattern pattern. Vending machine behaves differently on unlike states e.g. provide a production if the machine is non empty, otherwise, it only returns coins, thus it ideally fits inward nation pattern pattern. Though, In our solution, I receive got non used the nation pattern pattern but receive got only coded the solution amongst an if else block. This is easier because of a express disclose of nation too non many nation transition but inward to a greater extent than existent earth scenario, nation pattern pattern is amend every bit it uses Polymorphism too removes the logic nosotros receive got set within the if-else block.

Knowledge of UML is essential for creating a pattern document for Java programs. UML has several types of diagram to explicate unlike aspect of your pattern e.g. class diagram to present the dependency of objects, sequence diagram to present how user volition interact amongst your organization too thus on.

Since UML is a measure agency to pattern Java application, it likewise plant every bit shared library which many programmers understand. You don't postulate to explicate that class Influenza A virus subtype H5N1 is related to class B, whatsoever Java programmer volition figure out past times himself past times looking at the class diagram.

One mass which has really aid me to empathise UML amend is UML for Java Programmers past times Robert C. Martin. It's a neat mass if you lot desire to acquire UML from scratch or desire to refresh your cognition of UML. It likewise has similar exercises similar designing a java machine inward Java to attempt out your object-oriented pattern too analysis skills.



Unit Test of Vending Machine Solution

Here is the unit of measurement attempt out class for Vending Machine problem, which tests around behaviors of Vending machine e.g. buying items amongst exact change, amongst to a greater extent than change, less change, canceling an item, resetting vending machine etc. I acknowledge that unit of measurement tests are non extensive plenty too you lot tin sack add together few to a greater extent than attempt out cases to fully attempt out your vending machine solution inward Java. Make sure to receive got your unit of measurement attempt out inward the same packet but on the unlike rootage folder, thus that you lot tin sack test packet mortal members without mixing production too testing code.

==============================================================
VendingMachineTest.java
===============================================================


package vending;  import org.junit.Ignore; import java.util.List; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import static org.junit.Assert.;   public class VendingMachineTest {     private static VendingMachine vm;         @BeforeClass     public static void setUp(){         vm = VendingMachineFactory.createVendingMachine();     }         @AfterClass     public static void tearDown(){         vm = null;     }         @Test     public void testBuyItemWithExactPrice() {         //select item, cost inward cents         long cost = vm.selectItemAndGetPrice(Item.COKE);          //price should endure Coke's cost               assertEquals(Item.COKE.getPrice(), price);         //25 cents paid                       vm.insertCoin(Coin.QUARTER);                                            Bucket<Item, List<Coin>> bucket = vm.collectItemAndChange();         Item special = bucket.getFirst();         List<Coin> change = bucket.getSecond();                 //should endure Coke         assertEquals(Item.COKE, item);         //there should non endure whatsoever modify                                       assertTrue(change.isEmpty());                                   }         @Test     public void testBuyItemWithMorePrice(){         long cost = vm.selectItemAndGetPrice(Item.SODA);         assertEquals(Item.SODA.getPrice(), price);                 vm.insertCoin(Coin.QUARTER);                vm.insertCoin(Coin.QUARTER);                       Bucket<Item, List<Coin>> bucket = vm.collectItemAndChange();         Item special = bucket.getFirst();         List<Coin> change = bucket.getSecond();                 //should endure Coke         assertEquals(Item.SODA, item);         //there should non endure whatsoever modify                                              assertTrue(!change.isEmpty());                 //comparing modify                                      assertEquals(50 - Item.SODA.getPrice(), getTotal(change));               }              @Test     public void testRefund(){         long cost = vm.selectItemAndGetPrice(Item.PEPSI);         assertEquals(Item.PEPSI.getPrice(), price);                vm.insertCoin(Coin.DIME);         vm.insertCoin(Coin.NICKLE);         vm.insertCoin(Coin.PENNY);         vm.insertCoin(Coin.QUARTER);                 assertEquals(41, getTotal(vm.refund()));            }         @Test(expected=SoldOutException.class)     public void testSoldOut(){         for (int i = 0; i < 5; i++) {             vm.selectItemAndGetPrice(Item.COKE);             vm.insertCoin(Coin.QUARTER);             vm.collectItemAndChange();         }           }         @Test(expected=NotSufficientChangeException.class)     public void testNotSufficientChangeException(){         for (int i = 0; i < 5; i++) {             vm.selectItemAndGetPrice(Item.SODA);             vm.insertCoin(Coin.QUARTER);             vm.insertCoin(Coin.QUARTER);             vm.collectItemAndChange();                         vm.selectItemAndGetPrice(Item.PEPSI);             vm.insertCoin(Coin.QUARTER);             vm.insertCoin(Coin.QUARTER);             vm.collectItemAndChange();         }     }             @Test(expected=SoldOutException.class)     public void testReset(){         VendingMachine vmachine = VendingMachineFactory.createVendingMachine();         vmachine.reset();                 vmachine.selectItemAndGetPrice(Item.COKE);             }         @Ignore     public void testVendingMachineImpl(){         VendingMachineImpl vm = new VendingMachineImpl();     }         private long getTotal(List<Coin> change){         long total = 0;         for(Coin c : change){             total = total + c.getDenomination();         }         return total;     } }

Design Document

Here is a sample pattern document for Vending Machine problem. Well, it's non that neat but conveys my pattern decisions inward text format. Since inward a existent test, fourth dimension is critical you lot postulate to residue your fourth dimension betwixt coding, testing, too designing activity, it's amend to direct text over an image. If you lot are skilful amongst UML than adding a class diagram would for sure aid here.

In Short Design Document inward Java Should include
- description of the solution
- pattern determination too information structures
- All classes too at that topographic point responsibility
- description of the package
- description of methods
- the motivation of determination e.g. why this pattern pattern, why enum, why BigDecimal etc?
- Flow Chart of pair of usage cases
- UML Diagram
- Assumption
- Risk
- Benefits

Here is our sample pattern document, it's rattling basic because I receive got generated it rattling quickly, but it's skilful for learning too doing Object Oriented Analysis too design. One affair which I would similar to add together on this document is UML class diagram because that's around other agency to bring your pattern to swain Java developers. I receive got  added it to this discussion. If you lot desire to acquire UML too thus don't forget to banking concern check out UML for Java Programmers past times Robert C. Martin

 This is the minute business office of Java tutorial to present how to create Vending Machine inward Java Java Object Oriented Analysis too Design Problem - Vending Machine Part 2


1) High-level Design

Includes overview of the problem, non necessary if you lot are writing this every bit business office of the attempt out because evaluator should endure familiar amongst work specification. Important if your pattern document is intended for someone who is non rattling familiar amongst the work domain.

    - Main interface, classes too Exceptions
          - VendingMachine - an interface which defines world API of VendingMachine
          - VendingMachineImpl - a full general usage implementation of VendingMachine interface
          - Inventory - Influenza A virus subtype H5N1 type-safe inventory for asset objects, which is an ADAPTER or WRAPPER over java.util.Map
          - Item - type-safe Enum to stand upwardly for items supported past times vending machine.
          - Coin - type-safe Enum to stand upwardly for coins, which is acceptable past times vending machine.
          - Bucket - Influenza A virus subtype H5N1 Holder class for asset 2 types together.
          - SoldOutException - thrown when user selects a production which is sold out
          - NotSufficientChangeException - thrown when Vending machine doesn't receive got plenty modify to back upwardly the electrical current transaction.

          - NotFullPaidException - thrown when the user tries to collect an item, without paying the total amount.
     
    - Data structures used
          - Map information construction is used to implement cash too special inventories.
          - The List is used to returning changes because it tin sack incorporate duplicates, i.e. multiple coins of the same denomination.

   
    - Motivation behind pattern decisions
         - Factory pattern pattern is used to encapsulate creation logic of VendingMachine.
         - Adapter pattern is used to create Inventory past times wrapping java.util.Map
         - java.lang.Enum is used to stand upwardly for Item too Coins, because of next benefits
                - compile fourth dimension security against entering an invalid special too invalid coin.
                - no postulate to write code for checking if selected special or entered money is valid.
                - reusable too good encapsulated.
         - long is used to stand upwardly for cost too totalSales, which are the amount because nosotros are dealing inward cents.
           Not used BigDecimal to stand upwardly for money, because the vending machine tin sack solely concord express amount, due to finite capacity of money inventory.

         -

2) Low Leven Design

    - Methods
        -  The getChange() method uses a greedy algorithm to observe out whether nosotros receive got sufficient coins to back upwardly an amount.

    - Initialization
         - When Vending Machine volition endure created, it's initialized amongst default cash too special inventory. electrical current amongst quantity v for each money too item.



2) Testing Strategy
   - Three primary attempt out cases to testWithExactPrice()testWithMorePrice() and testRefund() to encompass full general usecase.
   - Negative attempt out cases similar testing SoldOutExceptionNotSufficientChangeException or NotFullPaidException
   -

3) Benefits
   - The pattern uses Abstraction to create reusable, pocket-size classes which are easier to read too test.
   - Encapsulating Items too Coins on their respective class makes it tardily to add together novel Coins too Items.
   - Inventory is full general usage class too tin sack endure used elsewhere, It likewise encapsulates all inventory operations.

4) Assumption
   - Vending Machine is single-threaded, solely 1 user volition operate at a time.
   - Influenza A virus subtype H5N1 telephone band to reset() will reset special too residue i.e. brand them zero.



UML Diagram Vending Machine inward Java

Here is our UML diagram for this solution, generated inward Eclipse past times using ObjectAid plugin. It allow you lot create UML class diagram from Java code itself. This diagram shows that VendingMachineImpl extends or implements VendingMachine interface. It likewise shows that it is associated amongst Item too Inventory classes.

If you lot are novel inward Java too non agreement this UML diagram too thus you lot should  read UML for Java Programmers past times Robert C. Martin , 1 of the best books to acquire well-nigh UML too class diagram of  Java programs.

 This is the minute business office of Java tutorial to present how to create Vending Machine inward Java Java Object Oriented Analysis too Design Problem - Vending Machine Part 2


That's all on this two-part article on designing too implementing Vending Machine inward Java using Object Oriented analysis too design. If you lot receive got only started learning OOPS or Java, I recommend doing this variety of pattern related exercise to improve your pattern skills inward Java. You volition human face upwardly a lot of challenges, which volition drive farther learning too agreement of kernel OOPS concept. If you lot are hungry for to a greater extent than of such pattern problems inward Java, you lot should read  UML for Java Programmers by Robert C. Martin to exercise around other similar work of designing a java machine inward Java. Uncle Bob has explained much amend than me, thus at that topographic point is skilful jeopardy that you lot volition acquire something at that topographic point every bit well.

Further Learning
Design Pattern Library
From 0 to 1: Design Patterns - 24 That Matter - In Java
Java Design Patterns - The Complete Masterclass


0 Response to "Java Object Oriented Analysis In Addition To Blueprint Occupation - Vending Auto Purpose 2"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel