var db = require('./database.js')
var format = require('./format.js')

function run(command, input){
  if (command === 'product-list'){
    if(!input){
      console.log('You must enter a valid grocery section.')
    } else {
        db.productList(input)
        .then(function(result){
          console.log(`|------------------+------------------+`)
          console.log(`|   Product Name   |      Section     |`)
          console.log(`|------------------+------------------+`)
          format(result)
          process.exit()
        })
        .catch(function(error){
          console.log(error)
          process.exit()
        })
      }
  } else if (command === 'shopper-orders'){
    if(!input || typeof input !== 'number'){
      console.log('You must enter a valid shopper id.')
    } else {
      db.shopperOrders(input)
      .then(function(result){
        console.log(`|------------------+------------------+`)
        console.log(`|     order id     |     total cost   |`)
        console.log(`|------------------+------------------+`)
        format(result)
        process.exit()
      })
      .catch(function(error){
        console.log(error)
        process.exit()
      })
    }
  } else if (command === 'real-shoppers'){
      db.realShoppers()
      .then(function(result){
        console.log(`|------------------+------------------+`)
        console.log(`|   shopper name   | number of orders |`)
        console.log(`|------------------+------------------+`)
        format(result)
        process.exit()
      })
      .catch(function(error){
        console.log(error)
        process.exit()
      })
  } else {
    console.log("Not a valid command. Please use 'product-list', 'shopper-orders', or 'real-shoppers'.")
    process.exit()
  }
}

try{
  var command = process.argv[2].toLowerCase()
  var input = process.argv.splice(3).join(' ')
  run(command, input)
} catch(error){
  if(error instanceof TypeError){
    console.log(`You must add a command after the file name. Please use of of these:
  | command        | description                                               | example usage                            |
  |----------------|-----------------------------------------------------------|------------------------------------------|
  | product-list   | lists all products which belong to the given section      | ./store product-list <product-section>   |
  | shopper-orders | lists the orders for a given shopper                      | ./store shopper-orders <shopper-id>      |
  | real-shoppers  | lists the names of all shoppers who have at least 1 order | ./store real-shoppers                    |
    `);
  }
}

@@ -682,32 +682,34 @@
 r-orders')%7B%0A    
+  
 if(!input %7C%7C typ
@@ -709,36 +709,33 @@
  %7C%7C 
-typeof input !== 'number'
+isNaN(Number(input))
 )%7B%0A
+  
     
@@ -782,32 +782,34 @@
 opper id.')%0A    
+  
 %7D else %7B%0A      d
@@ -797,24 +797,26 @@
    %7D else %7B%0A
+  
       db.sho
@@ -831,32 +831,34 @@
 rs(input)%0A      
+  
 .then(function(r
@@ -857,32 +857,34 @@
 nction(result)%7B%0A
+  
         console.
@@ -930,32 +930,34 @@
 ----+%60)%0A        
+  
 console.log(%60%7C  
@@ -991,24 +991,26 @@
  cost   %7C%60)%0A
+  
         cons
@@ -1060,32 +1060,34 @@
 ----+%60)%0A        
+  
 format(result)%0A 
@@ -1077,32 +1077,34 @@
  format(result)%0A
+  
         process.
@@ -1108,35 +1108,39 @@
 ss.exit()%0A      
+  
 %7D)%0A
+  
       .catch(fun
@@ -1153,32 +1153,34 @@
 error)%7B%0A        
+  
 console.log(erro
@@ -1174,32 +1174,34 @@
 sole.log(error)%0A
+  
         process.
@@ -1209,27 +1209,31 @@
 xit()%0A      
+  
 %7D)%0A
+  
     %7D%0A  %7D el
@@ -2683,9 +2683,8 @@
   %60)
-;
 %0A  %7D

Fix parseInt bug