Nowadays it is especially important to know what we eat and to keep track of the calories we consume.
Food Analyzer is a multi-threaded client-server application, which informs us about the ingredients, calories and products in our menu.
One of the fullest and most detailed resources for such information is the food database of U.S. Department of Agriculture. This information is publicly accessible via the free REST API, which is documented here.
Clone this repo to your local machine using https://github.com/Ivaylo-Georgiev/Food-Analyzer.git
-
The server can manage multiple clients simultaneously.
-
The server receives commands from the clients and returns the result.
-
The server fetches its data vie the RESTful API and caches the result in its local file system.
For example, when the server receives the command
get-food raffaello
, it makes the following HTTP GET request:https://api.nal.usda.gov/ndb/search/?q=raffaello&api_key=DEMO_KEY and receives HTTP response with status code 200 and body the following JSON:
{
"list": {
"q": "raffaello",
"sr": "1",
"ds": "any",
"start": 0,
"end": 1,
"total": 1,
"group": "",
"sort": "r",
"item": [
{
"offset": 0,
"group": "Branded Food Products Database",
"name": "RAFFAELLO, ALMOND COCONUT TREAT, UPC: 009800146130",
"ndbno": "45142036",
"ds": "LI",
"manu": "Ferrero U.S.A., Incorporated"
}
]
}
}
The requests to the REST API require authentication with an API key.
From the data about the product, we are interested in its full name (RAFFAELLO, ALMOND COCONUT TREAT
) and its unique number in the database, ndbno (45142036
). Some products, specifically those with group Branded Food Products Database
, also contain a producer (Ferrero U.S.A., Incorporated
) and a UPC code (009800146130
) in the element name
.
📓 Note: UPC, or Universal Product Code, is the dominant barcode standart in the USA. In other words, the UPC code is the number, encoded in the barcode of the package.
The server caches the received information in its file system. When it receives a request, it first the cache for information about the product. If the data is in the cache, it is directly returned to the server, instead of making a new request to the REST API.
The client connects to the Food Analyzer Server at a specified port, reads command from the standart input, forwards them to the server and prints the result to the standart output in human-readable format. The client can execute the following commands:
get-food <food_name>
- prints the information, described above for a particular product. If the server returns a set of products with a name, information is displayed for each of them. If there is no information for the product, an appropriate message is displayed.get-food-report <food_ndbno>
- by a unique number of a product (ndbno), prints the name of the product, ingredients, calories, proteins, fats, carbohydrates and fibres.get-food-by-barcode --upc=<upc_code>|--img=<barcode_image_file>
- prints data about a product by a barcode, if there is such in the cache (the REST API does not support searching for a product by a UPC code or barcode image!). It is required to pass one of the two parameters (UPC code or a barcode image as a full path to the file in the client's local file system). If both parameters are provided, the img parameter is ignored.
Searching by a barcod image uses the open source Java library ZXing "Zebra Crossing".
get-food butter
get-food-report 45142036
get-food-by-barcode --img=D:\User\Photos\BarcodeImage.jpg --upc=03600029142
Made with 🔥 for the Modern Java Technologies course at FMI 2019