POS Machine Sample Code

The basic requirements and explanations of POS machines are as follows:

Description of Requirement

The cash register (POS) system is used in the shopping settlement in the store. This cash register is used for settlement and printing according to the items in the customer's shopping cart (Cart) and the ongoing promotions (Promotion) in the store. shopping list.

It is known that the store is conducting a "buy three get one free" promotion for some products, that is, if you buy three products, one of them will be sent for free, and the settlement will be based on the price of two products.

We need to implement a function called a printInventoryfunction that can take data in a specified format as a parameter and output the text of the checklist in the browser's console.

Input format (sample):

javascript

 

[
    'ITEM000001',
    'ITEM000001',
    'ITEM000001',
    'ITEM000001',
    'ITEM000001',
    'ITEM000003-2',
    'ITEM000005',
    'ITEM000005',
    'ITEM000005'
]

 Among them, for 'ITEM000003-2', the standard barcode before "-", and the quantity after "-". When we buy items that need to be weighed, such barcodes are generated by the weighing machine, and the cash register is responsible for identifying and generating small tickets. (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.)

 

List content (example, where the printing time is the actual time when printing):

 

***<No Money Shop>Shopping List***
Print time: August 04, 2014 08:09:05
----------------------
Name: Coca-Cola, Quantity: 3 bottles, Unit price: 3.00 (yuan), subtotal: 6.00 (yuan)
Name: badminton, quantity: 5, unit price: 1.00 (yuan), subtotal: 4.00 (yuan)
Name: Apple, Quantity: 2 Jin, Single: 5.50 (RMB), Subtotal: 11.00 (RMB)
----------------------
Tears give away products:
Name: Coca-Cola, Quantity: 1 bottle
Name: Badminton, Quantity: 1
----------------------
Total: 21.00 (yuan)
Saving: 4.00 (yuan)
**********************

 

Work requirements

  1. Please use object-oriented thinking as much as possible;
  2. Implement the function in the file according main-spec.jsto the test case in ;main.jsprintInventory
  3. Please use the minimum number of lines of code to complete the assignment under the premise of ensuring the readability of the code; Note: All punctuation marks are English symbols
  4. homework tips

    1. You can use loadAllItems()the method to get all the commodities, and the result of this method is an array containing commodity objects;
    2. See the structure of each commodity object item.js;
    3. You can use the loadPromotions()method to get all the promotion information, the method returns the result as an array containing the promotion information object;
    4. See the structure of each promotion information object promotion.js;
    5. Use console.log output (only once allowed);
    6. Should learn and be good at using the Console function in the developer tools included with various popular browsers;
    7. For the usage of Lo-Dash and moment.js , please refer to their respective official websites.

 code show as below

 

In this question, we constructed two classes. We first constructed an Order class to process and output the results.

 

function Order (items, promotions, inputs) {  
    this.items = {}; //Store the list of purchased items
    this.total = 0; //store the total price to be paid
    this.original = 0; //Store the total price before discount
    this.initiate(items, promotions, inputs); //Call the initialization function
 }

 

 Now initialize Oredr()

 

Order.prototype.initiate = function (items,promotions,inputs){
    var self = this; // The key point here, pay attention to the scope
    _.each(inputs,function(input){
        var rawed_barcode = input.substring(0,10);
        var item = {};
        if(self.items.hasOwnProperty(rawed_barcode)==true)
        {
            item = self.items[rawed_barcode];  
            //console.log(JSON.stringify(item));
        }else
        {
            var rawItem = _(items).findWhere({barcode: rawed_barcode});
         	var newItem = new Item(rawItem.barcode, rawItem.name, rawItem.unit, rawItem.price);
            item = newItem;
        }
       self.original += item.addCount(input);
       self.items[rawed_barcode] = item;
    });
    var two_with_one_list = _(promotions).findWhere({type: 'BUY_TWO_GET_ONE_FREE'}).barcodes;
    _(two_with_one_list).each(function (barcode) {
        self.items[barcode] && self.items[barcode].getPromotion();
      // console.log(JSON.stringify(self.items[barcode]));
    });
    this.total = _(self.items).reduce(function (sum, item){ return sum + item.subtotal;}, 0);
     //reduce summation method, please check for details and usage
}

 

 

The following is the prototype method of the Order class, which is used to get part of the item list

 

Order.prototype.getLists = function() {
    function getBoughtItem (item) {
        return 'name:' + item.name + ', quantity:' + item.count + item.unit
            + ', unit price:' + item.price.toFixed(2) + '(yuan)' + ', subtotal:' + item.subtotal.toFixed(2) + '(yuan)\n';
    }
    function getFreeItem (item) {
        if(item.free!==0){
        return 'name:' + item.name + ', quantity:' + item.free + item.unit +'\n';
        }
        else{
            return "";
        }
    }
    var first_title="***<No Money Shop>Shopping List***\n"
    var boughtList = '----------------------\n';
    var freeList = '----------------------\n' + 'Giving away the item with tears:\n';
    _(this.items).each(function (item) {
        boughtList += getBoughtItem(item);
        item.promotion && (freeList += getFreeItem(item));
    });
    this.boughtList = boughtList;
    this.freeList = freeList;
    this.first_title=first_title;
}

 

 

Order.prototype.getStats = function() {  
    return '----------------------\n' + 'Total:' + this.total.toFixed(2) + '(yuan)\n'
    + '节省:' + (this.original - this.total).toFixed(2) + '(元)\n'+"**********************";
}

 

 

Next we construct an Item class

 

function Item(barcode, name, unit, price) {
    this.barcode = barcode;
    this.name = name;
    this.unit = unit;
    this.price = price || 0.00;
    this.subtotal = 0;
    this.count = 0;
    this.promotion = false;
}

 

 Write the prototype method of Item as needed:

 

Calculate quantity information and subtotal etc.

 

Item.prototype.addCount = function(input) {  
    var bought_number = parseInt(input.substring(11)) || 1;
    this.count += bought_number;
    this.subtotal = this.count * this.price;
    return bought_number * this.price;
}

 

 

Calculate the discount part

 

Item.prototype.getPromotion = function() {  
    this.promotion = true;
    this.free = Math.floor(this.count / 3);
    this.subtotal = (this.count - this.free) * this.price;
}

 

 

Finally, we construct a prototype method of Order to get the time 

 

Order.prototype.getDateTime = function() {
      var myDate = new Date();
      var year=myDate.getFullYear();  
      var month=myDate.getMonth();
      var newMonth = (month+1)>9?(month+1):"0"+(month+1);
      var day=myDate.getDate();
      var newday = day>9?day:"0"+day;
      var hours=myDate.getHours();
      var newhours = hours>9?hours:"0"+hours;
      var minutes=myDate.getMinutes();  
      var newminutes = minutes>9?minutes:"0"+minutes;
      var seconds=myDate.getSeconds();
      var newseconds= seconds>9?seconds:"0"+seconds;
      var now_time='打印时间:' + year+"年"+ newMonth+"月"+newday+"日 "+ newhours+":"+newminutes+":"+newseconds+"\n";
      return now_time    
}

 

The time can be written according to your own needs, and different time formats can be written. The specific writing method can be extended according to the previous blog link as http://1397548794.iteye.com/admin/blogs/2411939

 

 

Finally we instantiate the Order method and output what we want

 

function printInventory(inputs) {
    var order = new Order(loadAllItems(),loadPromotions(),inputs);
    order.getLists();
    console.log(order.first_title+order.getDateTime()+order.boughtList + order.freeList + order.getStats());

}

 

 

 

By constructing prototype methods for Order() and Item(), we propose some invariant properties and functions separately, which saves memory, and realizes object-oriented writing through function encapsulation and inheritance.

 

We have not written the details of loadAllItems(), loadPromotions(), etc. Please refer to the webpage for comprehensive code and question details http://www.codefordream.com/courses/js-pos-boot-camp/sections/section_1/practices/normal /practice_6/items/code

Guess you like

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