Commit d7502288 authored by Karel Frajtak's avatar Karel Frajtak

Loyalty points tests

parent 353dfce7
<?xml version="1.0" encoding="UTF-8"?>
<project-private xmlns="http://www.netbeans.org/ns/project-private/1">
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group>
<file>file:/C:/Users/Karel/Documents/NetBeansProjects/eShop/src/shop/ShoppingCart.java</file>
<file>file:/C:/Users/Karel/Documents/NetBeansProjects/eShop/test/LoyaltyPointsTest.java</file>
<file>file:/C:/Users/Karel/Documents/NetBeansProjects/eShop/src/shop/Customer.java</file>
<file>file:/C:/Users/Karel/Documents/NetBeansProjects/eShop/src/storage/ItemStock.java</file>
<file>file:/C:/Users/Karel/Documents/NetBeansProjects/eShop/src/main/Shop.java</file>
<file>file:/C:/Users/Karel/Documents/NetBeansProjects/eShop/src/shop/Item.java</file>
<file>file:/C:/Users/Karel/Documents/NetBeansProjects/eShop/src/shop/EShopController.java</file>
<file>file:/C:/Users/Karel/Documents/NetBeansProjects/eShop/src/shop/Order.java</file>
<file>file:/C:/Users/Karel/Documents/NetBeansProjects/eShop/src/shop/DiscountedItem.java</file>
<file>file:/C:/Users/Karel/Documents/NetBeansProjects/eShop/src/shop/StandardItem.java</file>
</group>
</open-files>
</project-private>
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package shop;
public class Customer {
private int loyaltyPoints;
String customerAddress;
String customerName;
public Customer(int loyaltyPoints) {
this.loyaltyPoints = loyaltyPoints;
}
public int getLoyaltyPoints() {
return loyaltyPoints;
}
void addLoayltyPoint(int loyaltyPoints) {
this.loyaltyPoints += loyaltyPoints;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerAddress(String customerAddress) {
this.customerAddress = customerAddress;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getCustomerAddress() {
return customerAddress;
}
}
......@@ -25,7 +25,7 @@ public class EShopController {
if (cart.getCartItems().isEmpty()) {
System.out.println("Error: shopping cart is empty");
}
Order order = new Order(cart, customerName, customerAddress);
Order order = new Order(cart);
storage.processOrder(order);
archive.putOrderToPurchasesArchive(order);
}
......
package shop;
import java.util.ArrayList;
import java.util.Date;
/**
* Order is created, when an user purchases the content of the shopping cart.
......@@ -10,22 +10,25 @@ import java.util.ArrayList;
public class Order {
private ArrayList<Item> items;
String customerName;
String customerAddress;
int state;
private Date purchaseOrder;
private Customer customer;
private float totalDiscount;
public Order(ShoppingCart cart, String customerName, String customerAddress, int state) {
public Order(ShoppingCart cart, int state) {
items = cart.getCartItems();
this.customerName = customerName;
this.customerAddress = customerAddress;
this.state = state;
}
public Order(ShoppingCart cart, String customerName, String customerAddress) {
public Order(ShoppingCart cart) {
items = cart.getCartItems();
this.state = 0;
}
public Order(ShoppingCart cart, Date purchaseOrder) {
items = cart.getCartItems();
this.customerName = customerName;
this.customerAddress = customerAddress;
this.state = 0;
this.purchaseOrder = purchaseOrder;
}
......@@ -37,29 +40,59 @@ public class Order {
this.items = goods;
}
public String getCustomerAddress() {
return customerAddress;
public int getState() {
return state;
}
public void setCustomerAddress(String customerAddress) {
this.customerAddress = customerAddress;
public void setState(int state) {
this.state = state;
}
public Date getPurchaseOrder() {
return purchaseOrder;
}
public String getCustomerName() {
return customerName;
public Customer getCustomer() {
return customer;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
public void setCustomer(Customer customer) {
this.customer = customer;
}
public int getState() {
return state;
public void create() {
int loyaltyPoints = 0;
// spocitat vernostni body za polozky v nak. voziku
for (Item item : items) {
if (item instanceof StandardItem)
{
StandardItem standardItem = (StandardItem)item;
loyaltyPoints += standardItem.getLoyaltyPoints();
}
}
if(this.customer == null) {
return;
}
// priradit je zakaznikovi
this.customer.addLoayltyPoint(loyaltyPoints);
}
public void setState(int state) {
this.state = state;
public void applyDiscount() {
int loyaltyPoints = this.customer.getLoyaltyPoints();
// 1 bod = 0.2 sleva
this.totalDiscount = (float) (0.2 * loyaltyPoints);
}
public float getTotalAmount() {
int totalAmount = 0;
// spocitat vernostni body za polozky v nak. voziku
for (Item item : items) {
totalAmount += item.getPrice();
}
return totalAmount - this.totalDiscount;
}
}
import java.util.Date;
import org.junit.Test;
import static org.junit.Assert.*;
import shop.Customer;
import shop.DiscountedItem;
import shop.Order;
import shop.ShoppingCart;
import shop.StandardItem;
public class LoyaltyPointsTest {
@Test
public void willGetLoyaltyPoints() {
// setup
// zakaznik
Customer customer = new Customer(0);
ShoppingCart cart = new ShoppingCart();
cart.addItem(new StandardItem(0, "", 0, "", 7));
cart.addItem(new StandardItem(0, "", 0, "", 3));
cart.addItem(new DiscountedItem(0, "", 0, "", 0, new Date(), new Date()));
// objednavka
Order order = new Order(cart);
// act
order.setCustomer(customer);
order.create();
// assert
assertEquals(10, customer.getLoyaltyPoints());
}
// For certain number of loyalty points the customer gets a discount for one
// purchase (particular amount, which will be decreased from total price)
@Test
public void willGetDiscountForLoyaltyPoints()
{
// setup
int loyaltyPoints = 12;
// zakaznik
Customer customer = new Customer(loyaltyPoints);
// act
ShoppingCart cart = new ShoppingCart();
cart.addItem(new StandardItem(0, "", 10, "", 0));
cart.addItem(new StandardItem(0, "", 20, "", 0));
// objednavka
Order order = new Order(cart);
// act
order.setCustomer(customer);
order.create();
// mezi-assert
assertEquals(30.0, order.getTotalAmount(), 0.0);
order.applyDiscount();
// assert
// 1 bod = 0.2 sleva
assertEquals(30 - loyaltyPoints * .2, order.getTotalAmount(), 0.00001);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment