Skip to content

Commit

Permalink
Merge 7f507e0 into 1c7fa7e
Browse files Browse the repository at this point in the history
  • Loading branch information
ClaudiaRaffaelli committed Jan 26, 2022
2 parents 1c7fa7e + 7f507e0 commit 9e504a9
Show file tree
Hide file tree
Showing 9 changed files with 305 additions and 362 deletions.
12 changes: 9 additions & 3 deletions raffaelliscandiffio.shop-totem/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
<mongo.java.server.version>1.39.0</mongo.java.server.version>

<sonar.coverage.exclusions>
**/controller/PurchaseBroker.java,
**/model/Product.java,
**/model/Stock.java,
**/utils/GUITestExtension.java,
</sonar.coverage.exclusions>

Expand Down Expand Up @@ -58,7 +59,7 @@
**/swing/TotemSwingView*
</sonar.issue.ignore.multicriteria.e3.resourceKey>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
Expand Down Expand Up @@ -163,9 +164,13 @@
<configuration>
<targetClasses>
<param>*repository.mongo.ProductMongoRepository*</param>
<param>*controller.PurchaseBroker*</param>
<param>*utils.LogUtilityTest*</param>
</targetClasses>
<targetTests>
<param>*repository.mongo.ProductMongoRepository*</param>
<param>*controller.PurchaseBroker*</param>
<param>*utils.LogUtilityTest*</param>
</targetTests>
<mutators>
<mutator>STRONGER</mutator>
Expand All @@ -180,7 +185,8 @@
<version>0.8.7</version>
<configuration>
<excludes>
<exclude>**/controller/PurchaseBroker.class</exclude>
<exclude>**/model/Product.class</exclude>
<exclude>**/model/Stock.class</exclude>
<exclude>**/utils/GUITestExtension.class</exclude>
</excludes>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,58 +11,109 @@
import com.github.raffaelliscandiffio.model.Stock;
import com.github.raffaelliscandiffio.repository.ProductRepository;
import com.github.raffaelliscandiffio.repository.StockRepository;

import com.github.raffaelliscandiffio.utils.ExcludeGeneratedFromCoverage;
import com.github.raffaelliscandiffio.utils.LogUtility;

public class PurchaseBroker {

private static final Logger LOGGER = LogManager.getLogger(PurchaseBroker.class);
private static final LogUtility logUtil = new LogUtility();
private ProductRepository productRepository;
private StockRepository stockRepository;


public PurchaseBroker(ProductRepository productRepository, StockRepository stockRepository) {
this.productRepository = productRepository;
this.stockRepository = stockRepository;
}

/**
* Constructs a new Product with the specified id, name and price and a Stock
* with the same id and quantity.
*
* @param id of the product and stock
* @param name of the product
* @param price of the product
* @param quantity of the product in stock
* @throws IllegalArgumentException if the specified name is empty or null,
* price is negative, quantity is negative
*/
public void saveNewProductInStock(long id, String name, double price, int quantity) {
if (!(name != null && !name.trim().isEmpty())) {
throw new IllegalArgumentException("Null or empty name is not allowed");
}
if (price < 0) {
throw new IllegalArgumentException("Negative price: " + price);
}
if (quantity < 0) {
throw new IllegalArgumentException("Negative quantity: " + quantity);
}

productRepository.save(new Product(id, name, price));
stockRepository.save(new Stock(id, quantity));
LOGGER.log(Level.INFO,
"New product with ID: {}, name: {}, price: {}", id, name, price);
LOGGER.log(Level.INFO,
"New stock with ID: {}, quantity: {}", id, quantity);
}

public List<Product> retrieveProducts() {
return productRepository.findAll();
}

public int takeAvailable(long productId, int quantity) {
/**
* Fetches the stock with given id and saves it back after subtracting the given
* quantity. If current quantity in stock is not sufficient only the available
* quantity is taken.
*
* @param id of the product/stock
* @param quantity positive of the product that needs to be subtracted from
* current stock quantity
* @return the quantity available given the requested, in the following
* conditions:
* <ul>
* <li>the whole quantity requested if available in stock</li>
* <li>the whole quantity available if the requested was not entirely in
* stock</li>
* <li>zero if the product is out of stock or not found</li>
* </ul>
*/
public int takeAvailable(long id, int quantity) {
Stock stock;
try {
stock = stockRepository.findById(productId);
}catch(NoSuchElementException e){
LOGGER.log(Level.ERROR, String.format("Stock with id %d not found", productId), e);
stock = stockRepository.findById(id);
} catch (NoSuchElementException e) {
LOGGER.log(Level.ERROR,
"Stock with id {} not found \n{}", id, logUtil.getReducedStackTrace(e));
return 0;
}
int stockAvailableQuantity = stock.getAvailableQuantity();
if(stockAvailableQuantity == 0) {
int stockAvailableQuantity = stock.getQuantity();

if (stockAvailableQuantity == 0) {
return 0;
}else {
int returnedQuantity = Math.max(0, stockAvailableQuantity-quantity);
stock.setAvailableQuantity(returnedQuantity);
} else {
int returnedQuantity = Math.max(0, stockAvailableQuantity - quantity);
stock.setQuantity(returnedQuantity);
stockRepository.save(stock);
if(returnedQuantity == 0) {
if (returnedQuantity == 0) {
return stockAvailableQuantity;
}else {
} else {
return quantity;
}
}
}

public void returnProduct(long productId, int quantity) {

@ExcludeGeneratedFromCoverage
public void returnProduct(long id, int quantity) {
// TODO Auto-generated method stub
}

public boolean doesProductExist(long productId) {
public boolean doesProductExist(long id) {
try {
productRepository.findById(productId);
productRepository.findById(id);
return true;
}catch(NoSuchElementException e){
LOGGER.log(Level.WARN, String.format("Product with id %d not found", productId), e);
} catch (NoSuchElementException e) {
LOGGER.log(Level.ERROR,
"Product with id {} not found \n{}", id, logUtil.getReducedStackTrace(e));
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.Objects;

import com.github.raffaelliscandiffio.utils.ExcludeGeneratedFromCoverage;

public class Product {

Expand All @@ -15,24 +14,14 @@ public class Product {
* @param id of the product
* @param name of the product
* @param price of the product
* @throws IllegalArgumentException if the specified name is empty or null, price is negative
*/
public Product(long id, String name, double price) {

if (!(name != null && !name.trim().isEmpty())) {
throw new IllegalArgumentException("Null or empty name is not allowed");
}
this.id = id;
this.name = name;

if (price < 0) {
throw new IllegalArgumentException("Negative price: " + price);
}
this.price = price;

this.id = id;
}


/**
* Returns the price
* @return the price
Expand All @@ -57,13 +46,11 @@ public long getId() {
return id;
}

@ExcludeGeneratedFromCoverage
@Override
public int hashCode() {
return Objects.hash(id, name, price);
}

@ExcludeGeneratedFromCoverage
@Override
public boolean equals(Object obj) {
if (this == obj)
Expand All @@ -77,5 +64,4 @@ public boolean equals(Object obj) {
&& Double.doubleToLongBits(price) == Double.doubleToLongBits(other.price);
}


}
Original file line number Diff line number Diff line change
@@ -1,51 +1,37 @@
package com.github.raffaelliscandiffio.model;

import java.util.Objects;

public class Stock {

private final long id;
private final Product product;
private int availableQuantity;
private int quantity;

/**
* Constructs a new Stock with a product and relative available quantity. Sets ID as product ID.
* @param product object
* @param availableQuantity of the product
* @throws NullPointerException if the specified product is null
* @throws IllegalArgumentException if the quantity is negative
* Constructs a new Stock with an id and quantity available.
* @param id of the stock
* @param quantity of the product currently in stock
*/
public Stock(Product product, int availableQuantity) {
public Stock(long id, int quantity) {

if (product==null) {
throw new NullPointerException("Null product");
}
this.product=product;

setAvailableQuantityValidation(availableQuantity);

this.id = product.getId();
this.id = id;
this.quantity = quantity;
}

private void setAvailableQuantityValidation(int availableQuantity) {
if (availableQuantity < 0) {
throw new IllegalArgumentException("Negative available quantity: " + availableQuantity);
}
this.availableQuantity = availableQuantity;
}


/**
* Returns the available quantity
* @return the available quantity
* Sets quantity
* @param quantity available in stock
*/
public int getAvailableQuantity() {
return availableQuantity;
public void setQuantity(int quantity) {
this.quantity = quantity;
}

/**
* Returns the product
* @return the product
* Returns the quantity currently in stock
* @return quantity
*/
public Product getProduct() {
return product;
public int getQuantity() {
return quantity;
}

/**
Expand All @@ -55,25 +41,21 @@ public Product getProduct() {
public long getId() {
return id;
}

/**
* Sets available quantity to the specified quantity.
* @param availableQuantity to be set
* @throws IllegalArgumentException if the specified availableQuantity is negative
*/
public void setAvailableQuantity(int availableQuantity) {
setAvailableQuantityValidation(availableQuantity);
}


// used for test only
void initAvailableQuantity(int availableQuantity) {
this.availableQuantity = availableQuantity;
}

// used for test only
Stock() {
this.id = 0;
this.product = null;
@Override
public int hashCode() {
return Objects.hash(quantity, id);
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Stock other = (Stock) obj;
return quantity == other.quantity && id == other.id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.raffaelliscandiffio.utils;

public class LogUtility {

public String getReducedStackTrace(Exception e) {
StackTraceElement el = e.getStackTrace()[0];
return String.format("-> at %s.%s() - line %s", el.getClassName(), el.getMethodName(), el.getLineNumber());
}

}
Loading

0 comments on commit 9e504a9

Please sign in to comment.