Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

*sorry how do i attach text files? i wanted to attach all the source code as java files but i dont think i can here*

*sorry how do i attach text files? i wanted to attach all the source code as java files but i dont think i can here*

Junit and TDD implementation in Java

I want to experiment with and understand Test Driven Development and implementing and testing Abstract Data Types, how to design and implement unit tests and how to use a Test Driven Development methodology to produce a robust implementation of a specified ADT interface.

I'm looking to develop an implementation of the ADT interfaces for a simple vending machine, product records and products in the interfaces package in the src directory following a TDD process. Much like many simple vending machines, this vending machine should be able to store a number items belonging to the same product line in a lane. You might imagine the machine having a lane for Ready Salted Crisps, a lane for cans of Orange Juice etc. and each lane is identified by a code such as A1, A2, A3, B1 etc.

That is to write suitable tests in the test package (such as in the supplied Tests class) and implement all the classes in the impl package to satisfy these tests. Parts of impl.Factory have been implemented, for this class you only need to implement the methods containing // TODO com- ments.

Please note that you must NOT change the interfaces. Coding to a given interface is an important aspect of development and is seen as a valuable exercise for this assignment and many others.

Following the TDD process, try to write JUnit5 tests for each element of functionality before writing and testing its implementation. When writing the tests, consider the following:

Normal cases, with expected inputs. Edge cases, such as empty collections, duplicate lane codes for different products, no stock. Exceptional cases, such as dealing with nulls.

Should you find some aspects of the interfaces ambiguous, you will need to make a decision as to how to implement the interface, which your tests should make clear.

SOURCE CODE:

in 'common' package:

AbstractFactoryClient.java, LaneCodeAlreadyInUseException.java, LaneCodeNotRegistered.java, ProductUnavailableException:

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

in 'impl' package:

Factory.java, ProductRecird.java, VendingMachine.java, VendingMachineProduct.java:

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

in 'interfaces' package:

IFactory.java, IProductRecord.java, IVendingMachine.java, IVendingMachineProduct.java

image text in transcribedimage text in transcribed (1)image text in transcribed (2)image text in transcribedimage text in transcribed

in 'tests' pacakage:

Tests.java

image text in transcribed

AbstractFactory Client.java X LaneCodeAlreadylnUseException.java LaneCodeNotRegisteredExceptic I Users > nataniasophia > Desktop > CompSci > CS2001 > Practicals > W05Practical > src > common > O AbstractFactory Clie 1 ackage common; 2 3 import impl.Factory; 4 import interfaces. IFactory; 5 6 /** 7 * Abstract base class for classes which need to use the Factory class. 8 9 * 10 public abstract class AbstractFactoryClient { 11 12 private static IFactory factory = Factory.getInstance(); 13 14 /** 15 * Method which returns an instance of IFactory. 16 * @return an instance of Factory 17 */ 18 public static IFactory getFactory() { 19 return factory; 20 } 21 } 22 AbstractFactoryClient.java LaneCodeAlreadylnUseException.java LaneCodeNotRegisteredExce|| Users > nataniasophia > Desktop > CompSci > CS2001 > Practicals > W05Practical > src > common > Lane Code 1 package common; 2 3 /** 4 This exception should be used to indicate the illegal state when multiple products share 5 * the same lane in the vending machine. 6 7 */ 8 public class LaneCodeAlreadyInUseException extends Exception { 9 } 10 * I FactoryClient.java LaneCodeAlreadyInUseException.java LaneCodeNotRegisteredException.java a > Desktop > CompSci > CS2001 > Practicals > W05Practical > src > common > O LaneCodeNotRegisteredException.ja 1 package common; 2 3 /** ! This exception should be used to indicate that a lane code has been presented to 5 * the system before it has been registered for use for an item. 6 7 */ public class LaneCodeNotRegisteredException extends Exception { 9 } 10 4 * 8 InUseException.java LaneCodeNotRegisteredException.java ProductUnavailableException.java X esktop > CompSci > CS2001 > Practicals > W05Practical > src > common > O ProductUnavailableException.java 1 package common; 2 3 /** 4 * This exception should be used to indicate that a product is not available. 5 6 */ 7 public class ProductUnavailableException extends Exception { 8 } * 9 7.java. O ProductUnavailableException.java Factory.java X Factory.java Tests.java IVendin Users > nataniasophia > Desktop > CompSci > CS2001 > Practicals > W05Practical > src > impl > Factory.java > {} impl 1 package impl; 2 . 3 import interfaces. Ifactory; 4 import interfaces. IVendingMachineProduct; 5 import interfaces. IVendingMachine; 6 import interfaces. IProductRecord; 7 8 9 10 * This class implements a singleton factory. 11 * 12 */ 13 public final class Factory implements IFactory { 14 15 private static IFactory factory Instance = null; 16 17 private Factory () { 18 19 } 20 21 22 * Method which returns an instance of the singleton Factory class. 23 * @return the instance of the Factory 24 */ 25 public static IFactory getInstance() { 26 if (factory Instance == null) { 27 factory Instance = new Factory(); 28 } 29 return factory Instance; 30 } 31 32 @Override 33 public IVendingMachineProduct makeVendingMachineProduct(String laneCode, String description) { 34 // TODO Auto-generated method stub 35 return null; 36 } 37 38 @Override 39 public IProductRecord makeProductRecord (IVendingMachineProduct vendingMachineProduct) { 40 // TODO Auto-generated method stub 41 return null; 42 } 43 44 @Override 45 public IVendingMachine makeVendingMachine() { 46 // TODO Auto-generated method stub 47 return null; 48 } 49 50 } y viac * Users > nataniasophia > Desktop > CompSci > CS2001 > Practicals > W05Practical > src > impl > ProductRecord.java > {} impl 1 package impl; 2 3 import common. ProductUnavailableException; 4 import interfaces. IVendingMachine Product; 5 import interfaces. IProductRecord; 6 7 8 * This class represents a ProductRecord, recording information relating to a product sold in a vending machine. 9 10 */ 11 public class ProductRecord implements IProductRecord { 12 13 @Override 14 public IVendingMachine Product getProduct() { 15 // TODO Auto-generated method stub 16 return null; 17 } 18 19 @Override 20 public int getNumberOfSales() { 21 // TODO Auto-generated method stub 22 return 0; 23 } 24 25 @Override 26 public int getNumberAvailable() { 27 // TODO Auto-generated method stub 28 return 0; 29 } 30 31 @Override 32 public void addItem() { 33 // TODO Auto-generated method stub 34 35 } 36 37 @Override 38 public void buyItem() throws ProductUnavailableException { 39 // TODO Auto-generated methodustub 40 41 } 42 43 } 44 va Factory.java Tests.java Vending Machine Product.java ProductRecord.java Vending Machine.java VE Users > nataniasophia > Desktop > CompSci > CS2001 > Practicals > WO5Practical > src>impl > Vending Machine.java > Vending Machine > re 1 package impl; 2 3 4 import common. AbstractFactoryClient; 5 import common. LaneCodeAlready InUseException; 6 import common. LaneCodeNotRegisteredException; 7 import common. ProductUnavailableException; 8 import interfaces. IVendingMachineProduct; 9 import interfaces. IVendingMachine; 10 import interfaces. IProductRecord; 11 12 /** 13 * This class represents a simple vending machine which can stock and sell products. 14 * 15 */ 16 public class VendingMachine extends AbstractFactoryClient implements IVendingMachine { 17 18 @Override 19 public void registerProduct(IVendingMachineProduct vendingMachineProduct) throws LaneCodeAlreadyInUseException { 20 // TODO Auto-generated method stub 21 22 } 23 24 @Override 25 public void unregisterProduct (IVendingMachine Product vendingMachine Product) throws LaneCodeNotRegisteredException { 26 // TODO Auto-generated method stub 27 28 } 29 30 @Override 31 public void addItem(String laneCode) throws LaneCodeNotRegisteredException { 32 // TODO Auto-generated method, stub 33 34 } 35 36 @Override 37 public void buyItem(String laneCode) throws ProductUnavailableException, LaneCodeNotRegisteredException { 38 // TODO Auto-generated method stub 39 40 } 41 42 @Override 43 public int getNumberOfProducts() { 44 // TODO Auto-generated method stub 45 return 0; 46 } 47 48 @Override 49 public int getTotalNumberOfItems() { 50 // TODO Auto-generated method stub 51 return 0; 52 } 53 54 @Override 55 public int getNumber of Items (String laneCode) throws Lane CodeNotRegisteredException { 56 // TODO Auto-generated method stub 57 return; 58 } 59 60 @Override 61 public int getNumberOfSales (String laneCode) throws Lane CodeNotRegisteredException { 62 // TODO Auto-generated method stub 63 return 0; 64 } 65 66 @Override 67 public IVendingMachineProduct getMost Popular() throws LaneCodeNotRegisteredException { 68 // TODO Auto-generated method stub 69 return null; 70 } 71 72 } 73 IVending Machine Product.java O ProductRecord.java IVending Machine.java Vending Machine Product.java x ers > nataniasophia > Desktop > CompSci > CS2001 > Practicals > W05Practical > src > impl > Vending Machine Product.java > {} 1 package impl; 2 import interfaces. IVendingMachineProduct; 3 1 5 /** * This class represents products that can be stocked and sold in a vending machine in a specific lane. 7 * 3 */ public class VendingMachine Product implements IVendingMachineProduct { 1 2 3 1 @Override public String getLaneCode() { // TODO Auto-generated method stub return null; } B @Override public String getDescription() { // TODO Auto-generated method stub return null; } 1 2 3 1. } Factory.java * * O ProductUnavailableException.java Factory.java Factory.java x Tests.java O IVending Machine Product.java Users > nataniasophia > Desktop > CompSci > CS2001 > Practicals > W05Practical > src > interfaces > Factory.java > ... 1 package interfaces; 2 3 4. /** 5 * Interface for a factory allowing the other interfaces to be instantiated without knowing the implementation classes. 6 * 7 */ 8 public interface IFactory { 9 10 11 * Creates an instance of {@link IVendingMachineProduct}. 12 @param laneCode the item's lane code, i.e. which lane the item is in, e.g. A1, A2, A3, B1, ... in the vending machine 13 @param description the description of the item 14 * @return the item 15 */ 16 IVendingMachineProduct makeVendingMachineProduct(String laneCode, String description); 17 18 19 /** 20 * This method creates an instance of {@link IProductRecord} for a new product. 21 * @param vendingMachineProduct the product to use for this record 22 * @return the product record 23 */ 24 IProductRecord makeProductRecord ( IVendingMachineProduct vendingMachineProduct); 25 26 27 /** 28 * Creates an instance of {@link IVendingMachine}. 29 30 * @return the vending machine 31 */ 32 IVendingMachine makeVendingMachine(); 33 34 35 } * eProduct.java ProductRecord.java o IVending Machine.java Vending Machine Product.java Vending Machine s > nataniasophia > Desktop > CompSci > CS2001 > Practicals > W05Practical > src > interfaces > IProductRecord.java > 0 [ProductR package interfaces; import common. ProductUnavailableException; * This is the interface for a ProductRecord, recording information relating to a product sold in a vending machine. public interface IProductRecord { /*** * Returns the product object associated with this record. * @return the product associated with this record */ IVendingMachine Product getProduct(); /** * Returns the number of times this product has been bought. * @return the number of sales for the product */ int getNumberOfSales(); /** * Returns the number available in the machine for the given product. * @return the number available for this product in the machine */ int getNumberAvailable(); * Adds one item to the vending machine for the associated product line - if the product is "Haggis Crisps" then this method adds one bag of Haggis Crisps to the machine. * * void addItem(); * Processes the purchase of one item in this product line. * athrows ProductUnavailableException when the product is not available in the machine */ void buyItem() throws ProductUnavailableException; } package interfaces; . import common. LaneCodeAlreadyInUseException; import common. LaneCodeNot RegisteredException; import common.ProductUnavailableException; * Interface for a simple vending machine ADT. public interface IVendingMachine { /*** * Registers the specified product for sale in the vending machine. * @param vendingMachineProduct the product to register * @throws LaneCodeAlreadyInUseException if a product is already registered in the vending machine with matching lane code void registerProduct (IVendingMachine Product vendingMachineProduct) throws LaneCodeAlreadyInUseException; * Unregisters the specified product from the vending machine. * @param vendingMachineProduct the product to remove * @throws LaneCodeNotRegisteredException if the product's lane code has not been registered for use in the vending machine for the given product void unregister Product (IVendingMachineProduct vendingMachineProduct) throws LaneCodeNotRegisteredException; * Adds one item of stock to a vending machine lane. * @param laneCode the lane code of the product, e.g. A1, A2, A3, B1, ... in the vending machine * @throws LaneCodeNotRegisteredException if the lane code has not been registered for use in the vending machine void addItem(String laneCode) throws LaneCodeNotRegisteredException; * Buys an item in the given vending machine lane. * @param laneCode the lane code of the item, e.g. A1, A2, A3, B1, ... in the vending machine * @throws ProductUnavailableException if the specified lane is empty * @throws LaneCodeNotRegisteredException if the given lane code has not been registered for use in the vending machine void buyItem(String laneCode) throws ProductUnavailableException, LaneCodeNotRegisteredException; 2 * Gets the number of different products available in the machine. If the machine is setup to sell 2 different products, "Haggis Crisps" and "Irn Bru", then the return should be 2 regardless of how many bags of crips and cans of Irn Bru are in the machine the vending machine * @return the number of different products available */ int getNumberOfProducts(); O Help us improve * Gets the total count of all stock items in the machine over all products (i.e. lanes) in the vending machine. * For example, returns 3 if the vending machine has 2 cans of Irn Bru and 1 bag of Haggis Crisps. * @return the total stock count over all products in the machine */ int get TotalNumberOfItems(); * Gets the number of items that are available to buy in the vending machine for a particular product (i.e. lane). * @param laneCode the lane code in the machine * @return the number of items that are available to buy for a particular lane in the machine * @throws LaneCodeNotRegisteredException if the lane code has not been registered for use int getNumberOfItems (String laneCode) throws LaneCodeNotRegisteredException; /** * Gets the total number of times that a given product (associated with a lane) was bought. * @param laneCode the lane code of the product @return the total number of times that the product has been bought * @throws LaneCodeNotRegisteredException if the lane code has not been registered for use in the machine */ int getNumberOfSales (String laneCode) throws LaneCodeNotRegistered Exception; * Gets the product that has been bought the greatest number of times. The behaviour is undefined if there is not a single most popular product. * @return the product that has been bought the greatest number of times * @throws LaneCodeNotRegisteredException if no lane codes have been registered for use in the vending machine */ IVendingMachine Product getMost Popular() throws LaneCodeNotRegisteredException; } Users > nataniasophia > Desktop > CompSci > CS2001 > Practicals > W05Practical > src > interfaces > O Vending Machine Product.java > {} interfaces package interfaces; 1 2 3 4 * Interface for a product that is sold in a vending machine in a specific lane. 5 * 6 */ public interface IVendingMachineProduct { 7 8 9 10 11 /** * This method returns the product's lane code, i.e. which lane the product is in, e.g. A1, A2, A3, B1, ... in the vending machine. * @return the lane code for this product */ String getLaneCode(); 12 13 14 /** 15 16 17 18 19 20 21 22 * This method returns the product description, such as Irn Bru, or Haggis Crisps. * @return the description of the product */ String getDescription(); } 4 ption.java O ProductUnavailableException.java o Factory.java O IFactory.java Tests.java X IVending Machine Product.java Users > nataniasophia > Desktop > CompSci > CS2001 > Practicals > W05Practical > src > test > Tests.java > {} test 1 package test; 2 . 3 import org.junit.jupiter.api. Test; import static org.junit.jupiter.api. Assertions.assertEquals; 5 import static org.junit.jupiter.api.Assertions.assertNotNull; 6 7 import common. AbstractFactoryClient; 8 import interfaces. IVendingMachineProduct; 9 import interfaces. IVendingMachine; 10 import interfaces. IProductRecord; 11 12 /** 13 * This is a JUnit test class for the Vending Machine ADT. 14 */ 15 public class Tests extends AbstractFactoryClient { 16 17 /** 18 * This checks that the factory was able to call a sensible constructor to get a non-null instance of IVendingMachineProduct. 19 */ 20 @Test 21 public void vendingMachineProductNotNull() { 22 IVendingMachineProduct vendingMachineProduct = getFactory().makeVendingMachineProduct("A1", "Haggis Crisps"); 23 assertNotNull(vendingMachineProduct); 24 } 25 } 26 AbstractFactory Client.java X LaneCodeAlreadylnUseException.java LaneCodeNotRegisteredExceptic I Users > nataniasophia > Desktop > CompSci > CS2001 > Practicals > W05Practical > src > common > O AbstractFactory Clie 1 ackage common; 2 3 import impl.Factory; 4 import interfaces. IFactory; 5 6 /** 7 * Abstract base class for classes which need to use the Factory class. 8 9 * 10 public abstract class AbstractFactoryClient { 11 12 private static IFactory factory = Factory.getInstance(); 13 14 /** 15 * Method which returns an instance of IFactory. 16 * @return an instance of Factory 17 */ 18 public static IFactory getFactory() { 19 return factory; 20 } 21 } 22 AbstractFactoryClient.java LaneCodeAlreadylnUseException.java LaneCodeNotRegisteredExce|| Users > nataniasophia > Desktop > CompSci > CS2001 > Practicals > W05Practical > src > common > Lane Code 1 package common; 2 3 /** 4 This exception should be used to indicate the illegal state when multiple products share 5 * the same lane in the vending machine. 6 7 */ 8 public class LaneCodeAlreadyInUseException extends Exception { 9 } 10 * I FactoryClient.java LaneCodeAlreadyInUseException.java LaneCodeNotRegisteredException.java a > Desktop > CompSci > CS2001 > Practicals > W05Practical > src > common > O LaneCodeNotRegisteredException.ja 1 package common; 2 3 /** ! This exception should be used to indicate that a lane code has been presented to 5 * the system before it has been registered for use for an item. 6 7 */ public class LaneCodeNotRegisteredException extends Exception { 9 } 10 4 * 8 InUseException.java LaneCodeNotRegisteredException.java ProductUnavailableException.java X esktop > CompSci > CS2001 > Practicals > W05Practical > src > common > O ProductUnavailableException.java 1 package common; 2 3 /** 4 * This exception should be used to indicate that a product is not available. 5 6 */ 7 public class ProductUnavailableException extends Exception { 8 } * 9 7.java. O ProductUnavailableException.java Factory.java X Factory.java Tests.java IVendin Users > nataniasophia > Desktop > CompSci > CS2001 > Practicals > W05Practical > src > impl > Factory.java > {} impl 1 package impl; 2 . 3 import interfaces. Ifactory; 4 import interfaces. IVendingMachineProduct; 5 import interfaces. IVendingMachine; 6 import interfaces. IProductRecord; 7 8 9 10 * This class implements a singleton factory. 11 * 12 */ 13 public final class Factory implements IFactory { 14 15 private static IFactory factory Instance = null; 16 17 private Factory () { 18 19 } 20 21 22 * Method which returns an instance of the singleton Factory class. 23 * @return the instance of the Factory 24 */ 25 public static IFactory getInstance() { 26 if (factory Instance == null) { 27 factory Instance = new Factory(); 28 } 29 return factory Instance; 30 } 31 32 @Override 33 public IVendingMachineProduct makeVendingMachineProduct(String laneCode, String description) { 34 // TODO Auto-generated method stub 35 return null; 36 } 37 38 @Override 39 public IProductRecord makeProductRecord (IVendingMachineProduct vendingMachineProduct) { 40 // TODO Auto-generated method stub 41 return null; 42 } 43 44 @Override 45 public IVendingMachine makeVendingMachine() { 46 // TODO Auto-generated method stub 47 return null; 48 } 49 50 } y viac * Users > nataniasophia > Desktop > CompSci > CS2001 > Practicals > W05Practical > src > impl > ProductRecord.java > {} impl 1 package impl; 2 3 import common. ProductUnavailableException; 4 import interfaces. IVendingMachine Product; 5 import interfaces. IProductRecord; 6 7 8 * This class represents a ProductRecord, recording information relating to a product sold in a vending machine. 9 10 */ 11 public class ProductRecord implements IProductRecord { 12 13 @Override 14 public IVendingMachine Product getProduct() { 15 // TODO Auto-generated method stub 16 return null; 17 } 18 19 @Override 20 public int getNumberOfSales() { 21 // TODO Auto-generated method stub 22 return 0; 23 } 24 25 @Override 26 public int getNumberAvailable() { 27 // TODO Auto-generated method stub 28 return 0; 29 } 30 31 @Override 32 public void addItem() { 33 // TODO Auto-generated method stub 34 35 } 36 37 @Override 38 public void buyItem() throws ProductUnavailableException { 39 // TODO Auto-generated methodustub 40 41 } 42 43 } 44 va Factory.java Tests.java Vending Machine Product.java ProductRecord.java Vending Machine.java VE Users > nataniasophia > Desktop > CompSci > CS2001 > Practicals > WO5Practical > src>impl > Vending Machine.java > Vending Machine > re 1 package impl; 2 3 4 import common. AbstractFactoryClient; 5 import common. LaneCodeAlready InUseException; 6 import common. LaneCodeNotRegisteredException; 7 import common. ProductUnavailableException; 8 import interfaces. IVendingMachineProduct; 9 import interfaces. IVendingMachine; 10 import interfaces. IProductRecord; 11 12 /** 13 * This class represents a simple vending machine which can stock and sell products. 14 * 15 */ 16 public class VendingMachine extends AbstractFactoryClient implements IVendingMachine { 17 18 @Override 19 public void registerProduct(IVendingMachineProduct vendingMachineProduct) throws LaneCodeAlreadyInUseException { 20 // TODO Auto-generated method stub 21 22 } 23 24 @Override 25 public void unregisterProduct (IVendingMachine Product vendingMachine Product) throws LaneCodeNotRegisteredException { 26 // TODO Auto-generated method stub 27 28 } 29 30 @Override 31 public void addItem(String laneCode) throws LaneCodeNotRegisteredException { 32 // TODO Auto-generated method, stub 33 34 } 35 36 @Override 37 public void buyItem(String laneCode) throws ProductUnavailableException, LaneCodeNotRegisteredException { 38 // TODO Auto-generated method stub 39 40 } 41 42 @Override 43 public int getNumberOfProducts() { 44 // TODO Auto-generated method stub 45 return 0; 46 } 47 48 @Override 49 public int getTotalNumberOfItems() { 50 // TODO Auto-generated method stub 51 return 0; 52 } 53 54 @Override 55 public int getNumber of Items (String laneCode) throws Lane CodeNotRegisteredException { 56 // TODO Auto-generated method stub 57 return; 58 } 59 60 @Override 61 public int getNumberOfSales (String laneCode) throws Lane CodeNotRegisteredException { 62 // TODO Auto-generated method stub 63 return 0; 64 } 65 66 @Override 67 public IVendingMachineProduct getMost Popular() throws LaneCodeNotRegisteredException { 68 // TODO Auto-generated method stub 69 return null; 70 } 71 72 } 73 IVending Machine Product.java O ProductRecord.java IVending Machine.java Vending Machine Product.java x ers > nataniasophia > Desktop > CompSci > CS2001 > Practicals > W05Practical > src > impl > Vending Machine Product.java > {} 1 package impl; 2 import interfaces. IVendingMachineProduct; 3 1 5 /** * This class represents products that can be stocked and sold in a vending machine in a specific lane. 7 * 3 */ public class VendingMachine Product implements IVendingMachineProduct { 1 2 3 1 @Override public String getLaneCode() { // TODO Auto-generated method stub return null; } B @Override public String getDescription() { // TODO Auto-generated method stub return null; } 1 2 3 1. } Factory.java * * O ProductUnavailableException.java Factory.java Factory.java x Tests.java O IVending Machine Product.java Users > nataniasophia > Desktop > CompSci > CS2001 > Practicals > W05Practical > src > interfaces > Factory.java > ... 1 package interfaces; 2 3 4. /** 5 * Interface for a factory allowing the other interfaces to be instantiated without knowing the implementation classes. 6 * 7 */ 8 public interface IFactory { 9 10 11 * Creates an instance of {@link IVendingMachineProduct}. 12 @param laneCode the item's lane code, i.e. which lane the item is in, e.g. A1, A2, A3, B1, ... in the vending machine 13 @param description the description of the item 14 * @return the item 15 */ 16 IVendingMachineProduct makeVendingMachineProduct(String laneCode, String description); 17 18 19 /** 20 * This method creates an instance of {@link IProductRecord} for a new product. 21 * @param vendingMachineProduct the product to use for this record 22 * @return the product record 23 */ 24 IProductRecord makeProductRecord ( IVendingMachineProduct vendingMachineProduct); 25 26 27 /** 28 * Creates an instance of {@link IVendingMachine}. 29 30 * @return the vending machine 31 */ 32 IVendingMachine makeVendingMachine(); 33 34 35 } * eProduct.java ProductRecord.java o IVending Machine.java Vending Machine Product.java Vending Machine s > nataniasophia > Desktop > CompSci > CS2001 > Practicals > W05Practical > src > interfaces > IProductRecord.java > 0 [ProductR package interfaces; import common. ProductUnavailableException; * This is the interface for a ProductRecord, recording information relating to a product sold in a vending machine. public interface IProductRecord { /*** * Returns the product object associated with this record. * @return the product associated with this record */ IVendingMachine Product getProduct(); /** * Returns the number of times this product has been bought. * @return the number of sales for the product */ int getNumberOfSales(); /** * Returns the number available in the machine for the given product. * @return the number available for this product in the machine */ int getNumberAvailable(); * Adds one item to the vending machine for the associated product line - if the product is "Haggis Crisps" then this method adds one bag of Haggis Crisps to the machine. * * void addItem(); * Processes the purchase of one item in this product line. * athrows ProductUnavailableException when the product is not available in the machine */ void buyItem() throws ProductUnavailableException; } package interfaces; . import common. LaneCodeAlreadyInUseException; import common. LaneCodeNot RegisteredException; import common.ProductUnavailableException; * Interface for a simple vending machine ADT. public interface IVendingMachine { /*** * Registers the specified product for sale in the vending machine. * @param vendingMachineProduct the product to register * @throws LaneCodeAlreadyInUseException if a product is already registered in the vending machine with matching lane code void registerProduct (IVendingMachine Product vendingMachineProduct) throws LaneCodeAlreadyInUseException; * Unregisters the specified product from the vending machine. * @param vendingMachineProduct the product to remove * @throws LaneCodeNotRegisteredException if the product's lane code has not been registered for use in the vending machine for the given product void unregister Product (IVendingMachineProduct vendingMachineProduct) throws LaneCodeNotRegisteredException; * Adds one item of stock to a vending machine lane. * @param laneCode the lane code of the product, e.g. A1, A2, A3, B1, ... in the vending machine * @throws LaneCodeNotRegisteredException if the lane code has not been registered for use in the vending machine void addItem(String laneCode) throws LaneCodeNotRegisteredException; * Buys an item in the given vending machine lane. * @param laneCode the lane code of the item, e.g. A1, A2, A3, B1, ... in the vending machine * @throws ProductUnavailableException if the specified lane is empty * @throws LaneCodeNotRegisteredException if the given lane code has not been registered for use in the vending machine void buyItem(String laneCode) throws ProductUnavailableException, LaneCodeNotRegisteredException; 2 * Gets the number of different products available in the machine. If the machine is setup to sell 2 different products, "Haggis Crisps" and "Irn Bru", then the return should be 2 regardless of how many bags of crips and cans of Irn Bru are in the machine the vending machine * @return the number of different products available */ int getNumberOfProducts(); O Help us improve * Gets the total count of all stock items in the machine over all products (i.e. lanes) in the vending machine. * For example, returns 3 if the vending machine has 2 cans of Irn Bru and 1 bag of Haggis Crisps. * @return the total stock count over all products in the machine */ int get TotalNumberOfItems(); * Gets the number of items that are available to buy in the vending machine for a particular product (i.e. lane). * @param laneCode the lane code in the machine * @return the number of items that are available to buy for a particular lane in the machine * @throws LaneCodeNotRegisteredException if the lane code has not been registered for use int getNumberOfItems (String laneCode) throws LaneCodeNotRegisteredException; /** * Gets the total number of times that a given product (associated with a lane) was bought. * @param laneCode the lane code of the product @return the total number of times that the product has been bought * @throws LaneCodeNotRegisteredException if the lane code has not been registered for use in the machine */ int getNumberOfSales (String laneCode) throws LaneCodeNotRegistered Exception; * Gets the product that has been bought the greatest number of times. The behaviour is undefined if there is not a single most popular product. * @return the product that has been bought the greatest number of times * @throws LaneCodeNotRegisteredException if no lane codes have been registered for use in the vending machine */ IVendingMachine Product getMost Popular() throws LaneCodeNotRegisteredException; } Users > nataniasophia > Desktop > CompSci > CS2001 > Practicals > W05Practical > src > interfaces > O Vending Machine Product.java > {} interfaces package interfaces; 1 2 3 4 * Interface for a product that is sold in a vending machine in a specific lane. 5 * 6 */ public interface IVendingMachineProduct { 7 8 9 10 11 /** * This method returns the product's lane code, i.e. which lane the product is in, e.g. A1, A2, A3, B1, ... in the vending machine. * @return the lane code for this product */ String getLaneCode(); 12 13 14 /** 15 16 17 18 19 20 21 22 * This method returns the product description, such as Irn Bru, or Haggis Crisps. * @return the description of the product */ String getDescription(); } 4 ption.java O ProductUnavailableException.java o Factory.java O IFactory.java Tests.java X IVending Machine Product.java Users > nataniasophia > Desktop > CompSci > CS2001 > Practicals > W05Practical > src > test > Tests.java > {} test 1 package test; 2 . 3 import org.junit.jupiter.api. Test; import static org.junit.jupiter.api. Assertions.assertEquals; 5 import static org.junit.jupiter.api.Assertions.assertNotNull; 6 7 import common. AbstractFactoryClient; 8 import interfaces. IVendingMachineProduct; 9 import interfaces. IVendingMachine; 10 import interfaces. IProductRecord; 11 12 /** 13 * This is a JUnit test class for the Vending Machine ADT. 14 */ 15 public class Tests extends AbstractFactoryClient { 16 17 /** 18 * This checks that the factory was able to call a sensible constructor to get a non-null instance of IVendingMachineProduct. 19 */ 20 @Test 21 public void vendingMachineProductNotNull() { 22 IVendingMachineProduct vendingMachineProduct = getFactory().makeVendingMachineProduct("A1", "Haggis Crisps"); 23 assertNotNull(vendingMachineProduct); 24 } 25 } 26

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Principles Of Accounting Volume 1 Financial Accounting

Authors: Mitchell Franklin, Patty Graybeal, Dixon Cooper, OpenStax

1st Edition

1593995946, 978-1593995942

More Books

Students also viewed these Accounting questions

Question

What are the six steps of handling a crisis?

Answered: 1 week ago

Question

Review the outcome research for family therapy.

Answered: 1 week ago