GithubHelp home page GithubHelp logo

11-futures-cli's Introduction

This is an assignment to the Software Architecture class at the Technische Hochschule Nürnberg.

Assignment 11: Futures - CLI variant

This assignment covers the more advanced multithreading topics futures and future chaining. Futures are a feature of Java 8 and can be compared to the concept of promises in JavaScript.

The internet contains lots of good articles about Future<> and CompletableFuture<> in Java. For example this one covers everything you need to know for this assignment.

This is the CLI variant of the assignment. There's also an Android variant which covers the same concepts. You don't have to implement both! If you want to switch between them you should be able to copy most of the code you already wrote (except a few platform adoptions).

Retrofit and CompletableFutures

Inspect the interface ohm.softa.a11.openmensa.OpenMensaAPI. The method return types have been changed from Call<> to CompletableFuture<>, a built-in way to leverage asynchronous programming via futures.

Retrieving all canteens

The given CLI application has a menu which asks the user which action he wants to perform. The following actions are available:

  • Show canteens - retrieves all canteens and prints them (including the id) to the STDOUT
  • Set canteen - reads an integer as canteen id
  • Set date - reads a date string and updates the currently selected date
  • Show meals - retrieves all meals of the currently selected canteen and date and prints them to the STDOUT

The first step is to complete the printCanteens() method. The following flow chart shows how to proceed :

Canteen retrieval

Retrieve the first page of canteens

Use the method getCanteens() of the OpenMensaAPI to retrieve the fist page of canteens (without index). The future returns an instance of Response<List<Canteen>>. That might be a little bit confusing but the OpenMensaAPI does not expose a dedicated pagination endpoint to retrieve the total count of items or the total count of pages but exposes this information in the response headers (X-Total-Pages, X-Total-Count, ...). To be able to extract this information you need the Response<> wrapper because the wrapper includes a reference to the headers. There's a given utility class PageInfo which extracts the pagination information from a generic Response<> object. To create a PageInfo instance use the static factory method PageInfo.extractFromResponse(...).

Retrieve the remaining pages

When you have the pagination information extracted you can retrieve the remaining pages with the method getCanteens(int pageNumber) of the OpenMensaAPI. As always there's not one solution but different ways to accomplish this! But no matter which approach you're chosing you have to wait for all responses to be retrieved to display a complete list of canteens before you leave the method because otherwise the main menu is printed in the middle of the list of canteens.

Retrieve the meals of a day

The second (and last) step is to complete the method printMeals(). This method should retrieve the meals of specific day served at a specified canteen.

The following flow chart shows how to proceed:

Meals retrieval

Retrieve the canteen state

At first you have to validate that there's a canteen selected (i.e. the currentCanteenId may not be less or equal 0!). Afterwards you should fetch the state of the canteen at the given date (default date is today, dateFormat.format(currentDate.getTime()) formats the currently selected date as required by the API). The required method to fetch a state is already present in the OpenMensaAPI.

Retrieve the meals

If you retrieved the state of the canteen at it is open at the specified date you have to retrieve the meals for the same params. Keep in mind to block the main thread to display the retrieved meals after you retrieved them. If the canteen is not open at the specified date a meaningful message should be printed to inform the user, that the date has to be changed.

11-futures-cli's People

Contributors

prskr avatar sikoried 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.