Commit 623dbb01 authored by Karel Frajtak's avatar Karel Frajtak

Initial commit

parents
<?xml version="1.0" encoding="UTF-8"?>
<!-- You may freely edit this file. See commented blocks below for -->
<!-- some examples of how to customize the build. -->
<!-- (If you delete it and reopen the project it will be recreated.) -->
<!-- By default, only the Clean and Build commands use this build script. -->
<!-- Commands such as Run, Debug, and Test only use this build script if -->
<!-- the Compile on Save feature is turned off for the project. -->
<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
<!-- in the project's Project Properties dialog box.-->
<project name="eShop" default="default" basedir=".">
<description>Builds, tests, and runs the project eShop.</description>
<import file="nbproject/build-impl.xml"/>
<!--
There exist several targets which are by default empty and which can be
used for execution of your tasks. These targets are usually executed
before and after some main targets. They are:
-pre-init: called before initialization of project properties
-post-init: called after initialization of project properties
-pre-compile: called before javac compilation
-post-compile: called after javac compilation
-pre-compile-single: called before javac compilation of single file
-post-compile-single: called after javac compilation of single file
-pre-compile-test: called before javac compilation of JUnit tests
-post-compile-test: called after javac compilation of JUnit tests
-pre-compile-test-single: called before javac compilation of single JUnit test
-post-compile-test-single: called after javac compilation of single JUunit test
-pre-jar: called before JAR building
-post-jar: called after JAR building
-post-clean: called after cleaning build products
(Targets beginning with '-' are not intended to be called on their own.)
Example of inserting an obfuscator after compilation could look like this:
<target name="-post-compile">
<obfuscate>
<fileset dir="${build.classes.dir}"/>
</obfuscate>
</target>
For list of available properties check the imported
nbproject/build-impl.xml file.
Another way to customize the build is by overriding existing main targets.
The targets of interest are:
-init-macrodef-javac: defines macro for javac compilation
-init-macrodef-junit: defines macro for junit execution
-init-macrodef-debug: defines macro for class debugging
-init-macrodef-java: defines macro for class execution
-do-jar: JAR building
run: execution of project
-javadoc-build: Javadoc generation
test-report: JUnit report generation
An example of overriding the target for project execution could look like this:
<target name="run" depends="eShop-impl.jar">
<exec dir="bin" executable="launcher.exe">
<arg file="${dist.jar}"/>
</exec>
</target>
Notice that the overridden target depends on the jar target and not only on
the compile target as the regular run target does. Again, for a list of available
properties which you can use, check the target you are overriding in the
nbproject/build-impl.xml file.
-->
</project>
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build
This diff is collapsed.
build.xml.data.CRC32=bd44c016
build.xml.script.CRC32=5d2926f4
build.xml.stylesheet.CRC32=8064a381@1.80.1.48
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
nbproject/build-impl.xml.data.CRC32=bd44c016
nbproject/build-impl.xml.script.CRC32=2bf384ee
nbproject/build-impl.xml.stylesheet.CRC32=830a3534@1.80.1.48
compile.on.save=true
user.properties.file=C:\\Users\\Karel\\AppData\\Roaming\\NetBeans\\8.2\\build.properties
annotation.processing.enabled=true
annotation.processing.enabled.in.editor=false
annotation.processing.processor.options=
annotation.processing.processors.list=
annotation.processing.run.all.processors=true
annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
build.classes.dir=${build.dir}/classes
build.classes.excludes=**/*.java,**/*.form
# This directory is removed when the project is cleaned:
build.dir=build
build.generated.dir=${build.dir}/generated
build.generated.sources.dir=${build.dir}/generated-sources
# Only compile against the classpath explicitly listed here:
build.sysclasspath=ignore
build.test.classes.dir=${build.dir}/test/classes
build.test.results.dir=${build.dir}/test/results
# Uncomment to specify the preferred debugger connection transport:
#debug.transport=dt_socket
debug.classpath=\
${run.classpath}
debug.test.classpath=\
${run.test.classpath}
# Files in build.classes.dir which should be excluded from distribution jar
dist.archive.excludes=
# This directory is removed when the project is cleaned:
dist.dir=dist
dist.jar=${dist.dir}/eShop.jar
dist.javadoc.dir=${dist.dir}/javadoc
excludes=
includes=**
jar.compress=false
javac.classpath=
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
javac.external.vm=true
javac.processorpath=\
${javac.classpath}
javac.source=1.8
javac.target=1.8
javac.test.classpath=\
${javac.classpath}:\
${build.classes.dir}:\
${libs.junit_4.classpath}:\
${libs.hamcrest.classpath}
javac.test.processorpath=\
${javac.test.classpath}
javadoc.additionalparam=
javadoc.author=false
javadoc.encoding=${source.encoding}
javadoc.noindex=false
javadoc.nonavbar=false
javadoc.notree=false
javadoc.private=false
javadoc.splitindex=true
javadoc.use=true
javadoc.version=false
javadoc.windowtitle=
main.class=main.Shop
manifest.file=manifest.mf
meta.inf.dir=${src.dir}/META-INF
mkdist.disabled=false
platform.active=default_platform
run.classpath=\
${javac.classpath}:\
${build.classes.dir}
# Space-separated list of JVM arguments used when running the project.
# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
# To set system properties for unit tests define test-sys-prop.name=value:
run.jvmargs=
run.test.classpath=\
${javac.test.classpath}:\
${build.test.classes.dir}
source.encoding=UTF-8
src.dir=src
test.src.dir=test
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.java.j2seproject</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/j2se-project/3">
<name>eShop</name>
<source-roots>
<root id="src.dir"/>
</source-roots>
<test-roots>
<root id="test.src.dir"/>
</test-roots>
</data>
</configuration>
</project>
package archive;
import shop.Item;
class ItemPurchaseArchiveEntry {
private Item refItem;
private int soldCount;
ItemPurchaseArchiveEntry(Item refItem) {
this.refItem = refItem;
soldCount = 1;
}
void increaseCountHowManyTimesHasBeenSold(int x) {
soldCount += x;
}
int getCountHowManyTimesHasBeenSold() {
return soldCount;
}
Item getRefItem() {
return refItem;
}
@Override
public String toString() {
return "ITEM "+refItem.toString()+" HAS BEEN SOLD "+soldCount+" TIMES";
}
}
package archive;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import shop.*;
import storage.*;
/**
* Class for archive of the purchases
*/
public class PurchasesArchive {
private HashMap<Integer, ItemPurchaseArchiveEntry> itemPurchaseArchive;
private ArrayList<Order> orderArchive;
public PurchasesArchive() {
itemPurchaseArchive = new HashMap();
orderArchive = new ArrayList();
}
public PurchasesArchive(HashMap<Integer, ItemPurchaseArchiveEntry> itemArchive, ArrayList<Order> orderArchive) {
this.itemPurchaseArchive = itemArchive;
this.orderArchive = orderArchive;
}
public void printItemPurchaseStatistics() {
Collection<ItemPurchaseArchiveEntry> itemEntries = itemPurchaseArchive.values();
System.out.println("ITEM PURCHASE STATISTICS:");
for(ItemPurchaseArchiveEntry e : itemEntries) System.out.println(e.toString());
}
public int getHowManyTimesHasBeenItemSold(Item item) {
if(itemPurchaseArchive.containsKey(item.getID())){
return itemPurchaseArchive.get(item.getID()).getCountHowManyTimesHasBeenSold();
}
return 0;
}
public void putOrderToPurchasesArchive(Order order) {
orderArchive.add(order);
ArrayList<Item> orderItems = order.getItems();
for(Item i : orderItems){
if(itemPurchaseArchive.containsKey(i.getID())){
ItemPurchaseArchiveEntry e = itemPurchaseArchive.get(i.getID());
e.increaseCountHowManyTimesHasBeenSold(1);
}else{
itemPurchaseArchive.put(i.getID(), new ItemPurchaseArchiveEntry(i));
}
}
}
}
/*
* 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 main;
/**
*
* @author Karel
*/
public class Shop {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
}
}
package shop;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* The class for discounted item in the EShopController, as the handling of discounted items differs form the standard items.
*/
public class DiscountedItem extends Item {
// discount stored in %
private int discount;
private Date discountFrom;
private Date discountTo;
public DiscountedItem(int id, String name, float price, String category, int discount, Date discountFrom, Date discountTo) {
super(id, name, price, category);
this.discount = discount > 100 ? 0 : discount;
this.discountTo = discountFrom;
this.discountTo = discountTo;
}
public DiscountedItem(int id, String name, float price, String category, int discount, String discountFrom, String discountTo) {
super(id, name, price, category);
this.discount = discount;
this.discountFrom = parseDate(discountFrom);
this.discountTo = parseDate(discountTo);
}
private Date parseDate(String date) {
SimpleDateFormat simpleDate = new SimpleDateFormat("dd.MM.yyyy");
try {
return simpleDate.parse(date);
} catch (Exception e) {
System.out.println("Error in DiscountedItem.parseDate() - wrong date format" + e.getMessage());
}
return null;
}
@Override
public String toString() {
return super.toString()+" ORIGINAL PRICE "+getOriginalPrice()+" DISCOUNTED PRICE "+getDiscountedPrice()+" DISCOUNT FROM "+discountFrom.toString()+" DISCOUNT TO "+discountTo.toString();
}
@Override
public boolean equals(Object object) {
if(object instanceof DiscountedItem){
DiscountedItem zbozi = (DiscountedItem) object;
return (super.equals(zbozi)
&& discount == zbozi.getDiscount()
&& discountTo.equals(zbozi.getDiscountTo()));
}
return false;
}
public int getDiscount() {
return discount;
}
public void setDiscount(int discount) {
this.discount = discount;
}
public Date getDiscountFrom() {
return discountFrom;
}
public Date getDiscountTo() {
return discountTo;
}
public void setDiscountTo(Date discountTo) {
this.discountTo = discountTo;
}
public void setDiscountFrom(String discountFrom) {
SimpleDateFormat simpleDate = new SimpleDateFormat("dd.MM.yyyy");
try {
this.discountFrom = simpleDate.parse(discountFrom);
} catch (Exception e) {
System.out.println("Error: Interval.Interval - wrong date format" + e.getMessage());
}
}
public void setDiscountTo(String discountTo) {
SimpleDateFormat simpleDate = new SimpleDateFormat("dd.MM.yyyy");
try {
this.discountTo = simpleDate.parse(discountTo);
} catch (Exception e) {
System.out.println("Error: Interval.Interval - wrong date format" + e.getMessage());
}
}
public float getOriginalPrice() {
return super.getPrice();
}
public float getDiscountedPrice() {
return super.getPrice()*(100 - discount);
}
@Override
public float getPrice() {
return getDiscountedPrice();
}
public DiscountedItem copy() {
return new DiscountedItem(getID(), getName(), getPrice(), getCategory(), discount, discountFrom, discountTo);
}
}
package shop;
import archive.PurchasesArchive;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import storage.*;
import archive.*;
public class EShopController {
private static Storage storage;
private static PurchasesArchive archive;
private static ArrayList<ShoppingCart> carts;
private static ArrayList<Order> orders;
public EShopController() {
}
public static void purchaseShoppingCart(ShoppingCart cart, String customerName, String customerAddress) throws NoItemInStorage {
if (cart.getCartItems().isEmpty()) {
System.out.println("Error: shopping cart is empty");
}
Order order = new Order(cart, customerName, customerAddress);
storage.processOrder(order);
archive.putOrderToPurchasesArchive(order);
}
public static ShoppingCart newCart() {
ShoppingCart newCart = new ShoppingCart();
carts.add(newCart);
return newCart;
}
public static void startEShop() {
if (storage == null) {
storage = new Storage();
archive = new PurchasesArchive();
carts = new ArrayList();
orders = new ArrayList();
}
}
public static void main(String[] args) throws NoItemInStorage {
EShopController.startEShop();
/* make up an artificial data */
int[] itemCount = {10,10,4,5,10,2};
Item[] storageItems = {
new StandardItem(1, "Dancing Panda v.2", 5000, "GADGETS", 5),
new StandardItem(2, "Dancing Panda v.3 with USB port", 6000, "GADGETS", 10),
new StandardItem(3, "Screwdriver", 200, "TOOLS", 5),
new DiscountedItem(4, "Star Wars Jedi buzzer", 500, "GADGETS", 30, "1.8.2013", "1.12.2013"),
new DiscountedItem(5, "Angry bird cup", 300, "GADGETS", 20, "1.9.2013", "1.12.2013"),
new DiscountedItem(6, "Soft toy Angry bird (size 40cm)", 800, "GADGETS", 10, "1.8.2013", "1.12.2013")
};
// insert data to the storage
for (int i = 0; i < storageItems.length; i++) {
storage.insertItems(storageItems[i], itemCount[i]);
}
storage.printListOfStoredItems();
System.out.println();
System.out.println("TEST RUN: Fill and purchase a shopping cart");
ShoppingCart newCart = new ShoppingCart();
newCart.addItem(storageItems[0]);
newCart.addItem(storageItems[1]);
newCart.addItem(storageItems[2]);
newCart.addItem(storageItems[4]);
newCart.addItem(storageItems[5]);
purchaseShoppingCart(newCart, "Libuse Novakova","Kosmonautu 25, Praha 8");
archive.printItemPurchaseStatistics();
storage.printListOfStoredItems();
System.out.println();
System.out.println("TEST RUN: Trying to purchase an empty shopping cart");
ShoppingCart newEmptyCart = new ShoppingCart();
purchaseShoppingCart(newEmptyCart, "Jarmila Novakova", "Spojovaci 23, Praha 3");
}
}
package shop;
/**
* The basic class for item in the EShop.
*/
public abstract class Item {
private int id;
private String name;
private float price;
private String category;
public Item(int id, String name, float price, String category) {
this.id = id;
this.name = name;
this.price = price;
this.category = category;
}
@Override
public String toString() {
return "Item ID "+id+" NAME "+name+" CATEGORY "+category;
}
public int getID() {
return id;
}
public void setID(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getCategory() {
return this.category;
}
public void setCategory(String category) {
this.category = category;
}
@Override
public boolean equals(Object object){
if(object instanceof Item){
Item zbozi = (Item) object;
if( id == zbozi.getID()
&& name.equals(zbozi.getName())
&& price == zbozi.getPrice()
&& category.equals(zbozi.getCategory())
) {
return true;
}
}
return false;
}
}
package shop;
import java.util.ArrayList;
/**
* Order is created, when an user purchases the content of the shopping cart.
*
*/
public class Order {
private ArrayList<Item> items;