Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please convert the following programming code written in javascript into the code written in Perl, which may work at the CGI file. This code is

Please convert the following programming code written in javascript into the code written in Perl, which may work at the CGI file.

This code is for the inventry management system.

I would like to implement this system in the Apache with ec2 environment.

When each arguments have the unexpected value, the output should be "ERROR". The number of the characters of the product is less than or equal to 8.

Here is the conditions.

Below are the functions implemented for /stocker (1) Addition of inventory

  • argument:
    • function (required): addstock
    • name (required): Specify the name of the target product.
    • amount (arbitrary): Specify the number (positive integer) to add the target product to the inventory. The default is 1.
  • Output: None

(2) Inventory check

  • argument:
    • function (required): checkstock
    • name (optional): Specify the name of the target product.
  • Output:
    • If a name is specified, the number of items in stock with that name is output in the "[name]: [amount]" format. When there is no stock, amount is displayed as 0.
    • When name is not specified, the number of stocks of all products is sorted in ascending order with name as a key and output. Items with 0 inventory are not displayed.
  • Example output:
    • xxx: 12
    • yyy: 7

(3) Sales

  • argument:

    • function (required): sell
    • name (required): Specify the name of the target product.
    • amount (arbitrary): Specify the number (a positive integer) of the target product sold. The default is 1.
    • price (optional): Specify the price of the target product (a number greater than 0). Only when input, add price x amount to sales.
  • Output: None

(4) Sales check

  • argument:
    • function (required): checksales

    • Output: Displays the current sales in the format "sales: [sales]". For decimal numbers, round up to the second decimal place.

  • Example output:
    • sales: 400

(5) Delete all

  • argument:

    • function (required): deleteall
  • Output: None

[Example of Desired Result]

$ curl "http://1.2.3.4:8080/stocker?function=deleteall" $ curl "http://1.2.3.4:8080/stocker?function=addstock&name=aaa&amount=10" $ curl "http://1.2.3.4:8080/stocker?function=addstock&name=bbb&amount=10" $ curl "http://1.2.3.4:8080/stocker?function=sell&name=aaa&amount=4&price=100" $ curl "http://1.2.3.4:8080/stocker?function=sell&name=aaa&price=80" $ curl "http://1.2.3.4:8080/stocker?function=checkstock&name=aaa" aaa: 5 $ curl "http://1.2.3.4:8080/stocker?function=checksales" sales: 480

[GIVEN JS CODE]

const { Stack, inToPost, outPrec, inPrec, isOperator, isOperand, spaceSeparator, evaluatePostfix } = require('./Stack')

const { Stock, registerProduct } = require('./helper')
const express = require('express')

const app = express()

app.set('port', process.env.PORT || 8080)
app.set('host', process.env.HOST || '13.115.100.160')

app.get('/stocker', function(req, res) {

// parse req query

const parameter = {}

for (let [key, value] of Object.entries(req.query))

parameter[key] = value

if (parameter.function === 'deleteall') {

stockTable = new Stock()

res.end()

} else if (parameter.function === 'addstock') {

// + sign converts string into a number

if (!parameter.name || (parameter.amount && !(Number.isInteger(+parameter.amount))))

res.send('ERROR ')

else {

if (!(parameter.name in stockTable.names))

stockTable.names = Object.assign(registerProduct(parameter.name, parameter.amount),stockTable.names)

else {

if (!parameter.amount)

stockTable.names[parameter.name].amount += 1

else

stockTable.names[parameter.name].amount += parameter.amount

}

res.end()

}

} else if (parameter.function === 'checkstock') {

if (!parameter.name) {

// send all product list sorted by alpha

Object.keys(stockTable.names).sort().forEach(key => {

if (stockTable.names[key].amount != 0)

res.write(`${key}: ${stockTable.names[key].amount} `)

})

res.end()

} else {

if (!(parameter.name in stockTable.names))

res.send(`${parameter.name}: 0 `)

else {

res.send(`${parameter.name}: ${stockTable.names[parameter.name].amount} `)

}

}

} else if (parameter.function === 'sell') {

if (!parameter.name)

res.send('ERROR ')

else if (parameter.price && parameter.price <= 0)

res.send('ERROR ')

else if (parameter.price)

stockTable.addSales(parameter.price, parameter.amount)

else

stockTable.names[parameter.name].amount -= (parameter.amount) ? parameter.amount : 1

res.end()

} else if (parameter.function === 'checksales') {

if (!(Number.isInteger(stockTable.sales)))

res.send(`sales: ${stockTable.sales.toFixed(2)} `).end()

else

res.send(`sales: ${stockTable.sales} `).end()

} else

res.send('ERROR ')

})

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essential SQLAlchemy Mapping Python To Databases

Authors: Myers, Jason Myers

2nd Edition

1491916567, 9781491916568

More Books

Students also viewed these Databases questions