Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
152 commits
Select commit Hold shift + click to select a range
1ae6eda
Starting GUI
Dec 14, 2017
2cc6b02
Adding functionality
Dec 15, 2017
99ba640
Updated Testcontainers version + some features introduced
Dec 15, 2017
8182b5d
Added more functionality + some extras, since they came as default ones
Dec 18, 2017
3f2cc92
small cleaning
Dec 18, 2017
b540b4f
Deleted redundant file - Updated README
Dec 19, 2017
f193b4c
Added functionality - see orders and their total
Dec 21, 2017
4131bd8
Added notifications for inventory scene
Dec 21, 2017
11c2a67
User can add/remove products to/from basket
Dec 22, 2017
419d21c
Added functionality for admin to add/delete products from inventory &…
Dec 22, 2017
a595535
Fixing error in tests
Dec 22, 2017
2bbeabb
Fixing error in tests
Dec 22, 2017
1d81ce6
Fixing outlined bugs
Dec 23, 2017
d9aafc4
Fixed bug with adding new items to inventory
Dec 23, 2017
21b010d
Updated README
Dec 23, 2017
3edf544
Trying GUI tests in Travis CI
Dec 30, 2017
8eca3d0
Enabling testing in Travis CI
Dec 30, 2017
c284bc0
Enabling testing in Travis CI
Dec 30, 2017
8c13b02
Enabling testing in Travis CI
Dec 31, 2017
bff4c3a
Enabling testing in Travis CI
Dec 31, 2017
099bd3e
Enabling testing in Travis CI
Dec 31, 2017
fb9493e
Enabling testing in Travis CI
Dec 31, 2017
ed7cda2
Enabling testing in Travis CI
Dec 31, 2017
b24f91d
Enabling testing in Travis CI
Dec 31, 2017
a6815b2
Enabling testing in Travis CI
Dec 31, 2017
25301f9
Enabling testing in Travis CI
Dec 31, 2017
18bcafc
Enabling testing in Travis CI
Dec 31, 2017
c7bf948
Enabling testing in Travis CI
Dec 31, 2017
b846899
Enabling testing in Travis CI
Dec 31, 2017
e9bcf09
Enabling testing in Travis CI
Dec 31, 2017
75331ed
Enabling testing in Travis CI
Dec 31, 2017
6e46213
Enabling testing in Travis CI
Dec 31, 2017
e68505e
Enabling testing in Travis CI
Dec 31, 2017
d8b87e0
Enabling testing in Travis CI
Dec 31, 2017
c87e246
GUI testing enabled in Travis CI
Dec 31, 2017
47c8697
Added GUI test + made an extension for TableViewMatchers
Jan 1, 2018
e81303a
Fixed typo in test
Jan 1, 2018
b11874a
More tests for GUI
Jan 1, 2018
f154fc2
Tests
Jan 1, 2018
ac507c1
Tests done for InventoryUserScene
Jan 2, 2018
27e2958
Added tests for InventoryAdminScene
Jan 2, 2018
5fb0526
Fixed TestFX issue; can use their methods for testing now
Jan 2, 2018
e8b0ceb
Added tests for HistoryScene
Jan 2, 2018
45fc7f5
Issue with HistoryScene tests is fixed - they work
Jan 2, 2018
73e2507
Code cleaning
Jan 2, 2018
d986455
HistoryScene tests should work
Jan 3, 2018
16497e4
Trying to sync horizontal scrollbars
Jan 3, 2018
a6d71e1
Trying to sync horizontal scrollbars
Jan 3, 2018
dc38e54
Trying to sync horizontal scrollbars
Jan 3, 2018
6dfaadd
Added GUI to add/remove users from system
Jan 3, 2018
d8d1f87
All GUI is done. Tests are needed for LoginScene
Jan 4, 2018
9326027
InventoryScene refactoring
Jan 5, 2018
1dbf117
Client GUI is done. Moving to main stage
Jan 5, 2018
1c1eb61
Started to bring all Scenes together
Jan 5, 2018
f1b04be
GUI is working; Need to enable connection to DB
Jan 8, 2018
fbfc31b
Connected OrderGUI bit to DB
Jan 9, 2018
fd65a28
Fixing tests
Jan 9, 2018
4d2d965
Fixing tests
Jan 9, 2018
a047c78
Fixing tests
Jan 10, 2018
a49d9a0
Fixing tests
Jan 10, 2018
d1711ec
Fixing tests
Jan 10, 2018
ee2966f
Fixing tests
Jan 10, 2018
1983213
Fixing tests
Jan 10, 2018
982d70b
Fixing tests
Jan 10, 2018
aaa7c96
All tests are fixed
Jan 10, 2018
7f457c7
Updated README
Jan 10, 2018
d576c4c
Basket is now saved on app closure and restored after user logged in
Jan 11, 2018
69f3240
Finishing CLient GUI/DB connection
Jan 11, 2018
7d2fc0f
Fixing test
Jan 11, 2018
40a2312
Updating code and related test
Jan 11, 2018
2111831
Deleting redundant Client methods/ Updating test
Jan 11, 2018
394de29
Deleting redundant Client methods/ Updating test
Jan 11, 2018
2ec5a99
Deleting redundant Client methods/ Updating test
Jan 11, 2018
cc7e468
Cleaned Client class code and test
Jan 11, 2018
7ed84db
Fixed test cases + added more to cover new functionality
Jan 11, 2018
7b57427
fixes
Jan 11, 2018
2444537
fixes
Jan 11, 2018
15b5f15
Small UI improvements for Client
Jan 11, 2018
64a9c6c
fix
Jan 11, 2018
964627a
Add/Delete user connected to DB
Jan 12, 2018
aaabcd5
test fix
Jan 12, 2018
451eb39
typo fix + test fix
Jan 12, 2018
800742b
typo fix
Jan 12, 2018
8d5a8fe
Add/Delete user added to MainGUI stage
Jan 12, 2018
d3c641f
updated README
Jan 12, 2018
cde027a
typos
Jan 12, 2018
54b5ece
Code cleaning
Jan 12, 2018
ebb46e8
Can add new product to inventory/DB
Jan 12, 2018
d680d58
fixing test
Jan 12, 2018
451de8e
Increase/Decrease product amount in inventory
Jan 12, 2018
df3b996
updated README
Jan 12, 2018
854c800
Added more tests for InventoryScene
Jan 14, 2018
7e47834
fixing
Jan 14, 2018
bab85cc
fixing
Jan 14, 2018
61acfbe
fixing
Jan 14, 2018
56388ed
fixing
Jan 14, 2018
4b111f9
fixing
Jan 14, 2018
f70e08d
fixing
Jan 14, 2018
e43d54f
Cleaning/ improving code quality
Jan 14, 2018
3718186
Cleaning/ improving code quality
Jan 14, 2018
e7e3d3c
Cleaning/improving code quality
Jan 14, 2018
c044c8f
Cleaning/improving code quality
Jan 14, 2018
40070a0
Cleaning/improving code quality
Jan 14, 2018
4565dbc
Cleaning is done
Jan 14, 2018
f35b10b
first MainScene test
Jan 15, 2018
0aebf83
typo
Jan 15, 2018
c543e14
fix
Jan 15, 2018
ba847a4
fix
Jan 15, 2018
1137aae
fix
Jan 15, 2018
8165fb4
fix
Jan 15, 2018
22f8710
fix
Jan 15, 2018
8176ba2
Moved code around to enable easier testing
Jan 15, 2018
58c9909
fixing test
Jan 15, 2018
19d403e
fixing test
Jan 15, 2018
431c1e7
MainScene-Admin-InventoryTab tests
Jan 15, 2018
15c450e
MainScene-Admin-InventoryTab tests
Jan 16, 2018
d97b2ae
test fix
Jan 16, 2018
d70e7ce
test fix
Jan 16, 2018
159f6c4
test fix
Jan 16, 2018
460b6aa
test fixes
Jan 16, 2018
a5d937e
test refactoring
Jan 16, 2018
10e16b3
Finished MainScene-Admin tests
Jan 16, 2018
9aae21f
fixes
Jan 16, 2018
b2ea5a1
fixes
Jan 16, 2018
b355f6c
fixes
Jan 16, 2018
023b9a2
fixes
Jan 16, 2018
e93d7b5
test fixes
Jan 16, 2018
2801deb
test fixes
Jan 16, 2018
263a624
pom fix
Jan 16, 2018
2a31200
pom fix
Jan 17, 2018
531df87
tests fix
Jan 17, 2018
f64631d
tests fix
Jan 17, 2018
af778d4
tests fix
Jan 17, 2018
37fbb6d
tests fix
Jan 17, 2018
afd75f9
test fixes
Jan 17, 2018
e67726f
test fixes
Jan 17, 2018
3ea74e2
test fixes
Jan 17, 2018
fe94d30
test fixes
Jan 17, 2018
f72d73a
fixed?
Jan 17, 2018
7fcf26a
MainScene-Client tests
Jan 17, 2018
4d75f18
Fixed Client tests
Jan 18, 2018
7ec45df
Ready to merge into master
Jan 18, 2018
9639b04
code cleaning + import optimisation
Jan 18, 2018
2e819e1
Adding Codacy test coverage
Jan 18, 2018
3d5fc4b
Adding Codacy test coverage
Jan 18, 2018
69df5c9
Adding Codacy test coverage
Jan 18, 2018
7a8140d
Adding Codacy test coverage
Jan 18, 2018
a0d6f6f
Adding Codacy test coverage
Jan 18, 2018
e8a5704
Adding Codacy test coverage
Jan 18, 2018
9a6b555
Adding Codacy test coverage
Jan 18, 2018
234a309
Adding Codacy test coverage
Jan 18, 2018
6e2f30f
fixed travis.ci
Jan 18, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
language: java
jdk:
- oraclejdk8

env:
- CODACY_PROJECT_TOKEN=30ded88adc3944479b2de1780b26f01b

sudo: required

services:
- docker

before_install:
- sudo apt-get install jq
- wget -O ~/codacy-coverage-reporter-assembly-latest.jar $(curl https://api.github.com/repos/codacy/codacy-coverage-reporter/releases/latest | jq -r .assets[0].browser_download_url)
- sudo apt-get install jq
- wget -O ~/codacy-coverage-reporter-assembly-latest.jar $(curl https://api.github.com/repos/codacy/codacy-coverage-reporter/releases/latest | jq -r .assets[0].browser_download_url)

matrix:
include:
# Headless Linux
- os: linux
dist: trusty
jdk: oraclejdk8
env:
- CODACY_PROJECT_TOKEN=30ded88adc3944479b2de1780b26f01b
- _JAVA_OPTIONS="-Djava.awt.headless=true -Dtestfx.robot=glass -Dtestfx.headless=true -Dprism.order=sw -Dprism.text=t2k -Dtestfx.setup.timeout=2500"

after_success:
- java -cp ~/codacy-coverage-reporter-assembly-latest.jar com.codacy.CodacyCoverageReporter -l Java -r build/reports/jacoco/test/jacocoTestReport.xml
- java -cp ~/codacy-coverage-reporter-assembly-latest.jar com.codacy.CodacyCoverageReporter -l Java -r build-reporting/target/coverage-reports/jacoco.xml

notifications:
slack:
secure: XLI9vZQkaPSqxychm54I+u6dteSiDzp5aBcukX+CRpP8zF3QMLTYD4kEr0Mh45kWqbznB/Xb+tiDZNyDcU+O+nc+B2HrjU92ENrsWzpztbN6Nz/qGszjjhiU0JfD222qBeFATydJAutlU93PbX/hiIrCZCwSUKz9X0qpTV1XO/i5VvewONH4/yCE4lXo9XYZHKDCO9HxmxlLGxM7mdjlZMSywFYndmqK5Npz5SutPX9Ml7Ap0LpHwrRhuX1eNNkN4L16RKGbkSY1QGv+O7ab3l0Kwmfi4CT/4aeK46YZfb6yypNfQrxf2ZVesS2e11wFYiF4/rzDs/KMBX5fAN/vUv3ABEPj+5DnY29vMS2n3NwlxwD0eXdwdl+O5ScYRl20rZ9xQevlh07toxvOLttnifeocLxMM8M0LvRsaTXGiimjmWOR0yel3h5688I8diF8emUwF9XNfocaSQveU8vKCyYA+BVs2AVH3exGPOzYT66FtJEPAWp0Zu+zSwagPGnnjUUgubZOG2qAFPlnCnIDnPx4G6iEYi1BpjwkIGvSkyXEBVKLZqvMUeSYdEVOojUjSKJLPZ9zDoMEBHOWcY/lduPVotaYHy8C/pBAegqkX7dtM2p9BiHYjO44kYcFeTGgk4lr60DWJ2lpVM/KSyn9Lgtj03JWqoTXG+p2JjbkflY=

before_cache:
- rm -f $HOME/.gradle/caches/modules-2/modules-2.lock
- rm -fr $HOME/.gradle/caches/*/plugin-resolution/
- rm -f $HOME/.gradle/caches/*/fileHashes/fileHashes.bin
- rm -f $HOME/.gradle/caches/*/fileHashes/fileHashes.lock

cache:
directories:
- $HOME/.gradle/caches/
- $HOME/.gradle/wrapper/
92 changes: 61 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,39 +54,69 @@ Also I am using TDD approach to complete this project as I found this really con
- [x] Filter orders by date
- [x] Filter orders by total price

- [ ] As an optional part, I'd like to introduce GUI, which will make it easier to fill basket, see orders and etc
- [ ] Login screen
- [ ] Can login into system and gain appropriate rights
- [ ] Client GUI
- [ ] See all available products
- [ ] Add to basket
- [ ] Remove from basket
- [ ] Save basket
- [ ] Restore basket
- [ ] Make order
- [ ] Save order
- [ ] Complete order
- [ ] Restore order
- [ ] See all completed orders (client can see only his orders)
- [ ] Search for specific product by name
- [ ] Filter products by price, weight
- [ ] Filter orders by date/total price
- [ ] Administrator GUI
- [ ] See all available products
- [ ] See total cost of all products
- [ ] Add products to inventory
- [ ] Remove products from inventory
- [ ] See orders completed by any user
- [ ] See total cost of all completed orders
- [ ] Add user to the system
- [ ] Delete user from system
- [ ] Search for specific product by name
- [ ] Filter products by price, weight
- [ ] Filter orders by date/total price

- [x] As an optional part, I'd like to introduce GUI, which will make it easier to fill basket, see orders and etc
- [x] Login screen
- [x] Can login into system and gain appropriate rights
- [x] Client GUI
- [x] See all available products
- [x] Add to basket
- [x] Remove from basket
- [x] Complete order
- [x] See all completed orders (client can see only his orders)
- [x] See total cost of all completed orders
- [x] Sort orders by total price
- [x] Filter orders by total price
- [x] Search for specific product by name
- [x] Sort products by name, price and weight
- [x] Filter products by name, price and weight
- [x] Administrator GUI
- [x] See all available products
- [x] See total cost of all products
- [x] Add new product to inventory
- [x] Add products to inventory
- [x] Remove products from inventory
- [x] See orders completed by any user
- [x] See total cost of all completed orders
- [x] Sort orders by date, total price
- [x] Filter orders by date/total price
- [x] Add user to the system
- [x] Delete user from system
- [x] Search for specific product by name
- [x] Sort products by name, price
- [x] Filter products by price, weight
- [x] Bring all GUI components into one Stage
- [x] Create main stage with TabPane(tab panel, that contains scenes)
- [x] Main stage, which creates tab panels depending on the privilege level of the logged user
- [x] Save basket
- [x] Restore basket
- [x] Connect GUI to database
- [x] Login GUI
- [x] Client GUI
- [x] Highlighted parts
- [x] Administrator GUI
- [x] Highlighted parts

- [x] Finish up tests
- [x] Review tests for the project so far
- [x] MainStage GUI tests
- [x] Client GUI tests
- [x] Administrator GUI tests

- [ ] As an optional part, I'd like to write a service, which will keep shop's inventory in sync with DB and completed orders
- [ ] Once order is completed, service will update inventory in the DB and update local copy of the inventory
- [ ] It will also check, if order can be made, ie not enough products in the inventory
- [ ] If product(-s) not available to order, notify user and administrator

## Bug trace
- [x] When adding new product via form, product is added, but then I cannot change its amount
- [x] **METHOD DEPRECATED - WILL BE DELETED** restoreBasketFromDB method shall be fixed, as currently it's hardcoded to use fixed weight and price for all restored products in Basket
- [ ] When completing order as Client in GUI, basket is not reset. Should find a way to clear it
- [ ] When basket has been restored, restored products cannot be removed.

## Improvements, that could be done later
- [ ] At the moment user can add more products into basket than there exist in inventory -> make them talk to one another
- [ ] At the moment there is no column to sort orders by date, but could be added. Will require extra column in GUI and extra filed in Order class
- [ ] At the moment data in rows does not update straight after product has been added/removed to/from basket/inventory, but rather after row been clicked, table been sorted or Details button been clicked
- [ ] At the moment, when new product added to basket/inventory, in order to display it, I delete all entries from observable list + its underlying list

## Build and tested on MacOS and Java 1.8
## Build and tested on MacOS and Java 1.8 (_**required**_)
45 changes: 42 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@
<version>1.0</version>
<name>shop</name>
<url>https://example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.controlsfx/controlsfx -->
<dependency>
<groupId>org.controlsfx</groupId>
<artifactId>controlsfx</artifactId>
<version>8.40.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
Expand All @@ -20,17 +29,17 @@
<artifactId>postgresql</artifactId>
<version>42.1.4</version>
</dependency>
<!-- For testing DB related stuff -->
<!-- DB testing libraries -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.4.3</version>
<version>1.5.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>1.4.3</version>
<version>1.5.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -45,6 +54,36 @@
<version>2.0.0</version>
<scope>test</scope>
</dependency>
<!-- GUI testing libraries -->
<dependency>
<groupId>org.testfx</groupId>
<artifactId>testfx-core</artifactId>
<version>4.0.10-alpha</version>
</dependency>
<dependency>
<groupId>org.testfx</groupId>
<artifactId>testfx-junit</artifactId>
<version>4.0.10-alpha</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.loadui</groupId>
<artifactId>testFx</artifactId>
<version>3.1.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testfx</groupId>
<artifactId>openjfx-monocle</artifactId>
<version>8u76-b04</version> <!-- To run tests in Travis CI -->
<scope>test</scope>
</dependency>
<!-- <dependency>
<groupId>org.testfx</groupId>
<artifactId>testfx-core</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>-->
</dependencies>
<build>
<plugins>
Expand Down
23 changes: 16 additions & 7 deletions shop.iml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="jdk" jdkName="1.8-newer" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.controlsfx:controlsfx:8.40.14" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
<orderEntry type="library" name="Maven: org.postgresql:postgresql:42.1.4" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.testcontainers:testcontainers:1.4.3" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.testcontainers:testcontainers:1.5.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.slf4j:slf4j-api:1.7.25" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: com.kohlschutter.junixsocket:junixsocket-common:2.0.4" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: com.kohlschutter.junixsocket:junixsocket-native-common:2.0.4" level="project" />
Expand All @@ -28,6 +29,11 @@
<orderEntry type="library" scope="TEST" name="Maven: org.rnorth.duct-tape:duct-tape:1.0.6" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.jetbrains:annotations:13.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.slf4j:slf4j-ext:1.7.25" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: net.java.dev.jna:jna:4.5.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.testcontainers:postgresql:1.5.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.testcontainers:jdbc:1.5.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: com.zaxxer:HikariCP:2.7.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.rnorth.visible-assertions:visible-assertions:2.0.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: com.github.jnr:jnr-posix:3.0.41" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: com.github.jnr:jnr-ffi:2.1.4" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: com.github.jnr:jffi:1.2.15" level="project" />
Expand All @@ -39,9 +45,12 @@
<orderEntry type="library" scope="TEST" name="Maven: org.ow2.asm:asm-util:5.0.3" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: com.github.jnr:jnr-x86asm:1.0.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: com.github.jnr:jnr-constants:0.9.8" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.testcontainers:postgresql:1.4.3" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.testcontainers:jdbc:1.4.3" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: com.zaxxer:HikariCP:2.7.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.rnorth.visible-assertions:visible-assertions:2.0.0" level="project" />
<orderEntry type="library" name="Maven: org.testfx:testfx-core:4.0.10-alpha" level="project" />
<orderEntry type="library" name="Maven: org.testfx:testfx-internal-java8:4.0.10-alpha" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.testfx:testfx-junit:4.0.10-alpha" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.loadui:testFx:3.1.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: com.google.guava:guava:14.0.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-all:1.3" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.testfx:openjfx-monocle:8u76-b04" level="project" />
</component>
</module>
18 changes: 11 additions & 7 deletions src/main/java/com/molotkov/Basket.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import com.molotkov.interfaces.StringFormatter;
import com.molotkov.products.Product;

import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

import static com.molotkov.Utils.iterateSimultaneously;
import static com.molotkov.extras.Utils.iterateSimultaneously;

public class Basket implements ProductStorage {
private HashMap<Product, Integer> products;
Expand All @@ -25,7 +28,7 @@ public Basket() {

public void addProducts(final Product product, final int amount) throws BasketException {
if (product != null) {
final int currentAmount = this.products.getOrDefault(product,0);
final int currentAmount = this.products.getOrDefault(product, 0);
this.products.put(product, currentAmount + amount);
} else {
throw new BasketException("You cannot add Null objects to Basket!");
Expand All @@ -34,9 +37,9 @@ public void addProducts(final Product product, final int amount) throws BasketEx

public void removeProducts(final Product product, final int amount) throws BasketException {
if (this.products.get(product) > amount) {
this.products.replace(product,this.products.get(product)-amount);
this.products.replace(product, this.products.get(product) - amount);
} else if (this.products.get(product) == amount) {
this.products.remove(product);
this.products.replace(product, 0);
} else {
throw new BasketException(String.format("Cannot remove %d instances of product as there are only %d instances!",
amount, this.products.get(product)));
Expand All @@ -50,7 +53,7 @@ public HashMap<Product, Integer> getProducts() {
public double calculateTotal() {
return this.products.entrySet().
parallelStream().
mapToDouble(product -> product.getKey().getPrice()*product.getValue()).
mapToDouble(product -> product.getKey().getPrice() * product.getValue()).
sum();
}

Expand All @@ -66,7 +69,7 @@ public ArrayList<String> toDBFormat() {
final ArrayList<String> result = new ArrayList<>();
final String names = this.products.entrySet().
parallelStream().
map(p -> String.format("'%s'",p.getKey().getName())).
map(p -> p.getKey().getName()).
collect(Collectors.joining(","));
final String amounts = this.products.entrySet().
parallelStream().
Expand All @@ -78,6 +81,7 @@ public ArrayList<String> toDBFormat() {
return result;
}

@Deprecated
public void restoreFromDB(final String productsName, final String productsAmount) {
final List<String> names = Arrays.asList(productsName.split(","));
final List<String> amounts = Arrays.asList(productsAmount.split(","));
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/com/molotkov/Inventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import com.molotkov.interfaces.ProductStorage;
import com.molotkov.interfaces.StringFormatter;
import com.molotkov.products.Product;

import java.text.DecimalFormat;
import java.util.HashMap;

public class Inventory implements ProductStorage {
private HashMap<Product, Integer> products;
private StringFormatter stringFormatter;

Inventory() {
public Inventory() {
this.products = new HashMap<>();
this.stringFormatter = () -> {
final int productsSize = this.products.size();
Expand All @@ -23,7 +24,7 @@ public class Inventory implements ProductStorage {

public void addProducts(final Product product, final int amount) throws InventoryException {
if (product != null) {
final int currentAmount = this.products.getOrDefault(product,0);
final int currentAmount = this.products.getOrDefault(product, 0);
this.products.put(product, currentAmount + amount);
} else {
throw new InventoryException("You cannot add Null objects to products!");
Expand All @@ -32,9 +33,9 @@ public void addProducts(final Product product, final int amount) throws Inventor

public void removeProducts(final Product product, final int amount) throws InventoryException {
if (this.products.get(product) > amount) {
this.products.replace(product,this.products.get(product)-amount);
this.products.replace(product, this.products.get(product) - amount);
} else if (this.products.get(product) == amount) {
this.products.remove(product);
this.products.replace(product, 0);
} else {
throw new InventoryException(String.format("Cannot remove %d instances" +
" of product as there are only %d instances!", amount, this.products.get(product)));
Expand All @@ -48,7 +49,7 @@ public HashMap<Product, Integer> getProducts() {
public double calculateTotal() {
return this.products.entrySet().
parallelStream().
mapToDouble(product -> product.getKey().getPrice()*product.getValue()).
mapToDouble(product -> product.getKey().getPrice() * product.getValue()).
sum();
}

Expand Down
Loading