Coder Social home page Coder Social logo

aw-supermarket-checkout-stage-1-persistence's Introduction

Supermarket Checkout

We are now going to add persistency to this application.

The initial stage of the exercise now uses a database to store the prices of products, instead of a file.

Exercise 0: make it work

Install postgresql. On Mac, assuming that you have installed the "Homebrew" project, do

brew install postgresql

On Ubuntu, do

apt-get install postgresql postgresql-contrib

Then, in any case, create the databases by running

script/create_databases.sql

The database schema is defined in the file src/main/sql/000_create_products.sql. The initial state of the database is in file src/main/sql/seed.sql. If you open it, you will see that the price of product "A" is set at 111. Run the application, and verify that when we enter code "A" and press the button "Scan", the display should show "Price: 111".

Exercise 1: persist the total

We want to make it possible to persist the state of the checkout on the database. Even if the JVM process is shut down, we should retain the total. For this reason we get rid of the static checkout variable in the servlet. We will use the "repository" pattern, so that the repository will give us a new instance of the SupermarketCheckout at every request, initialized with the state that was saved in the database.

  1. Create a new table to store the total of our checkouts. In preparation for the moment when we will support more checkouts at the same time, you will define two columns: an "id" integer column, and a "total" integer column. You will need to add a new file src/main/sql/002_create_checkouts.sql. You will also need to update the seed.sql to add a row to this new table. Make it so that there is a checkout with id == 0. Remember to run script/create_databases.sh again!

  2. Change the SupermarketCheckoutRepository class so that the metdhods findById and save do the right thing.

  3. Test your work by proving that - after scanning a few codes, the total displayed by http://localhost:8080/total is correct. - kill the server with control-C. Restart it. The total should stay the same.

Exercise 2: more than one checkout

We want to support more than one checkout at the time. Define at least three checkouts in file seed.sql, with ids 0,1 and 2. Change the controller so that when we open http://localhost:8080/?checkout_id=1, when we scan codes, the total that is incremented is visible at http://localhost:8080/total?checkout_id=1.

Test that if we scan codes on http://localhost:8080/?checkout_id=2, we see the appropriate total at http://localhost:8080/total?checkout_id=2. The total at http://localhost:8080/total?checkout_id=1 should not change.

Update the "total" and "reset" buttons on the UI so that they all work on the correct checkout, using the parameter checkout_id on the query string.

aw-supermarket-checkout-stage-1-persistence's People

Contributors

xpmatteo avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.