pos machine

POS Project v0.2
POS cash register version: v0.2

teaching objectives
Learn to convert basic descriptions into programs;
be able to control three cycles;
description of requirements The cash register (POS) system will be used in the
store for shopping settlement. This cash register opportunity At checkout, checkout and print the shopping list according to the items in the customer's shopping cart (Cart).

We need to implement a function called printInventory, which can take the data in the specified format as a parameter, and then output the text of the inventory in the browser's console.

Input format (sample):

[
    'ITEM000000',
    'ITEM000000',
    'ITEM000000',
    'ITEM000000',
    'ITEM000000',
    'ITEM000001',
    'ITEM000001',
    'ITEM000004'
]


(When the "Save and Submit Code" button is clicked, we will call the function printInventory and pass the above data as parameters (inputs) to the function.)

Inventory content (sample):

***<No Money Earning Store> Shopping List***
Name: Coca-Cola, Quantity: 5 Bottles, Unit Price: 3.00 (RMB), Subtotal: 15.00 (RMB)
Name: Sprite, Quantity: 2 Bottles, Unit Price: 3.00 (RMB), Subtotal: 6.00 (RMB)
Name: battery, quantity: 1, unit price: 2.00 (yuan), subtotal: 2.00 (yuan)
----------------------
total: 23.00 (yuan ) )
*********************
The implementation process is as follows:
//Function function: count the number of commodities (count) and construct a new object form.
function get_goods_count(inputs){                            
    var goods_count_list={};            
    for(var i=0;i<inputs.length;i++){ //Use the for loop inputs array.
        if(goods_count_list.hasOwnProperty(inputs[i])){ //Determine whether there is an element with the key name inputs[i]
            goods_count_list[inputs[i]].count++                     //含有直接count++
        }
        else{
            goods_count_list[inputs[i]]={count:1}; //Does not contain: define the key name as inputs[i], and the key value as {count:1}
        }
    }
   return goods_count_list;                                                 
}
//The function of the function: construct a key called the product number, and the key value is the product information (including the count value)
function get_goods_list(goods_count_list,AllItems){       
    var goods_list={};                                                                
    for(var goods_barcode in goods_count_list){ // Traverse the goods_count_list object to get the number of shopping, and the code of the goods
        for(var i=0;i<AllItems.length;i++){ // Traverse the items array to get product information
            if(goods_barcode==AllItems[i].barcode){ //Get the unit price name information of the purchased goods
                 
                AllItems[i].count=goods_count_list[goods_barcode].count //Plant count for each item in items
                goods_list[AllItems[i].name]=AllItems[i]
            }
        }
        }
    return goods_list;
}
function print_goods_list(goods_list){ //Concatenate strings
    var new_goods_list="";
    var total = 0;
    for(var goods_name in goods_list){                                                       
        total+=goods_list[goods_name].count*goods_list[goods_name].price;
        new_goods_list+="名称:"+goods_list[goods_name].name+",数量:"+goods_list[goods_name].count+goods_list[goods_name].unit+",单价:"+goods_list[goods_name].price+".00(元),小计:"+goods_list[goods_name].count*goods_list[goods_name].price+".00(元)\n"
    }
    var goods_list_title="***<No Money Shop>Shopping List***\n"
    var goods_list_late="----------------------\n总计:"+total+".00(元)\n**********************"
    new_goods_list=goods_list_title+new_goods_list+goods_list_late
    return new_goods_list;  
}
function call
function printInventory(inputs) { // call the number of lines
    var AllItems = loadAllItems ();                                              
    
    var goods_count_list=get_goods_count(inputs);
    var goods_list=get_goods_list(goods_count_list,AllItems)
    var new_goods_list=print_goods_list(goods_list);
   
    console.log(new_goods_list)
}



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326204452&siteId=291194637