Guest User

Untitled

a guest
Jul 10th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var uid = 0;
  2. var users = [];
  3. var transactions = [];
  4.  
  5. function User(uid) {
  6.  
  7.     // If the uid is an object, we copy the properties of
  8.     // that object into this new object.
  9.     if( typeof uid === 'object') {
  10.         for(var prop in uid) {
  11.             this[prop] = uid[prop];
  12.         }
  13.     } else {
  14.         // The unique identifier for this game.
  15.         this.uid = this.uid || uid;
  16.  
  17.         /*
  18.          * Identification
  19.          */
  20.  
  21.         this.username = 'admin';
  22.         // default username
  23.         this.password = 'admin';
  24.         // default password
  25.  
  26.         this.dob = -1;
  27.         // placeholder dob
  28.         this.doc = -1;
  29.         // placeholder doc
  30.  
  31.         /*
  32.          * Financial
  33.          */
  34.  
  35.         this.balance = -1;
  36.         this.knownTransactions = [];
  37.  
  38.     }
  39. }
  40.  
  41. //********************************************************************************//
  42.  
  43. function Transaction() {
  44.     this.amount = 0;
  45.     this.to
  46.     this.from
  47. }
  48.  
  49. //********************************************************************************//
  50.  
  51. checkPassword = function(user, pass) {
  52.     for(var i = 0; i < users.length; i++) {
  53.         if(users[i].username === user) {
  54.             return (users[i].password === pass);
  55.         }
  56.     }
  57.     return false;
  58. }
  59. //********************************************************************************//
  60. //********************************************************************************//
  61. //********************************************************************************//
  62.  
  63. /*
  64.  * Methods used by the user
  65.  */
  66.  
  67. User.prototype.transfer = function(to, amount) {
  68.     var tran = new Transaction(to, this.username, amount);
  69.     var found = 0;
  70.     var complete = 0;
  71.    
  72.    
  73.     for(var i = 0; i < users.length; i++) {
  74.         if(users[i].username === to)
  75.         {
  76.             found = 1;
  77.             if (this.balance - amount < 0)
  78.             {
  79.             this.balance -= amount;
  80.             users[i].balance += amount;
  81.             complete = 1;
  82.             }          
  83.         }
  84.     }
  85.    
  86.  
  87.     if (found === 0)
  88.     {
  89.         // transaction incomplete, invalid id
  90.         // quitting
  91.     }
  92.    
  93.     if (complete === 0)
  94.     {
  95.         // inssufficient founds
  96.     }
  97.  
  98.     /*
  99.      * TODO:
  100.      *
  101.      * three states:
  102.      * return:successfully withdrawn
  103.      * return: failure to withdrawn: inssufficient funds
  104.      * return: failure to transfer: invalid recipient
  105.      * */
  106.  
  107.     path.exists(transDB, function(exists) {
  108.         if(!exists) {
  109.  
  110.             var transactions = newUser ? [tran] : [];
  111.             var json = JSON.stringify(transactions);
  112.  
  113.             saveTransDB(transactions, function() {
  114.                 res.render('transfer', {
  115.                     'title' : 'transfer'
  116.                 });
  117.             });
  118.         } else {
  119.             loadTransDB(function(transactions) {
  120.  
  121.                 transactions.push(tran);
  122.  
  123.                 saveTransDB(transactions, function() {
  124.                     res.render('transfer', {
  125.                         'title' : 'transfer'
  126.                     });
  127.                 });
  128.             });
  129.         }
  130.     });
  131. }
  132.  
  133. User.prototype.withdraw = function(amount) {
  134.     var tran = new Transaction(-1, this.username, amount);
  135.     if (this.balance - amount < 0)
  136.     {
  137.         // return, inssufficient funds
  138.     }
  139.     this.balance -= amount;
  140.    
  141.  
  142.     path.exists(transDB, function(exists) {
  143.         if(!exists) {
  144.  
  145.             var transactions = newUser ? [tran] : [];
  146.             var json = JSON.stringify(transactions);
  147.  
  148.             saveTransDB(transactions, function() {
  149.                 res.render('withdraw', {
  150.                     'title' : 'withdraw'
  151.                 });
  152.             });
  153.         } else {
  154.             loadTransDB(function(transactions) {
  155.  
  156.                 transactions.push(tran);
  157.  
  158.                 saveTransDB(transactions, function() {
  159.                     res.render('withdraw', {
  160.                         'title' : 'Withdraw'
  161.                     });
  162.                 });
  163.             });
  164.         }
  165.     });
  166.     /*
  167.      * TODO:
  168.      * add to list of the known transactions
  169.      * return: withdrawn $x
  170.      * return: failure to withdraw: inssufucient funds
  171.      */
  172.  
  173. }
  174.  
  175. User.prototype.deposit = function(amount) {
  176.     var temp = new Transaction(this.username, -1, amount);
  177.     this.balance += amount;
  178.    
  179.     path.exists(transDB, function(exists) {
  180.         if(!exists) {
  181.  
  182.             var transactions = newUser ? [tran] : [];
  183.             var json = JSON.stringify(transactions);
  184.  
  185.             saveTransDB(transactions, function() {
  186.                 res.render('deposit', {
  187.                     'title' : 'deposit'
  188.                 });
  189.             });
  190.         } else {
  191.             loadTransDB(function(transactions) {
  192.  
  193.                 transactions.push(tran);
  194.  
  195.                 saveTransDB(transactions, function() {
  196.                     res.render('deposit', {
  197.                         'title' : 'deposit'
  198.                     });
  199.                 });
  200.             });
  201.         }
  202.     });
  203.     /*
  204.      * TODO:
  205.      * add to the list of the known transactions
  206.      *
  207.      */
  208.  
  209. }
  210. //********************************************************************************//
  211. //********************************************************************************//
  212. //********************************************************************************//
  213.  
  214. /*
  215.  * Methods used backend
  216.  */
  217.  
  218. User.prototype.createUser = function(username, password, dob) {
  219.     if (typeof(username) !== 'string' || typeof(password) !== 'string')
  220.     {
  221.         // username or password are not strings
  222.     }
  223.     var patt = new RegExp("\d{1,2}\/\d{1,2}\/\d{1,4}"); // not used yet, very simple regex
  224.    
  225.     this.username = username;
  226.     this.password = password;
  227.     this.dob = dob;
  228.     this.balance = 0;
  229.  
  230.     var currentTime = new Date();
  231.     var month = currentTime.getMonth() + 1;
  232.     var day = currentTime.getDate();
  233.     var year = currentTime.getFullYear();
  234.  
  235.     this.doc = month + "/" + day + "/" + year;
  236.  
  237.     /*
  238.      * TODO:
  239.      * check date of birth to be in the appropriate format
  240.      *
  241.      * return: user sucessfully created
  242.      * return: error: user already exists
  243.      * return: error: inccorrect format for date of birth
  244.      */
  245. }
  246.  
  247. User.prototype.updateTransactions = function() {
  248.     /*
  249.      * TODO:
  250.      * figure out how to implement this function
  251.      */
  252. }
  253.  
  254. User.prototype.getTransactions = function() {
  255.     var ret = '';
  256.     /*
  257.      * TODO:
  258.      * possibly update transactions
  259.      * return an html object with alternating colors (list items, css) of the list of the transactions
  260.      */
  261. }
  262.  
  263. User.prototype.getName = function() {
  264.     return this.username;
  265. }
  266.  
  267. User.prototype.addTransaction = function(id) {
  268.     /*
  269.      * TODO: given transaction ID add it to array of known transactions
  270.      */
  271. }
  272. //********************************************************************************//
  273. //********************************************************************************//
  274. //********************************************************************************//
  275.  
  276. /*
  277.  * Transactions
  278.  */
  279.  
  280. Transaction.prototype.transfer = function(to, from, amount) {
  281.     for(var i = 0; i < users.length; i++) {
  282.         if(users[i].username === to)
  283.             users[i].balance += amount;
  284.     }
  285.  
  286.     for(var i = 0; i < users.length; i++) {
  287.         if(users[i].username === from)
  288.             users[i].balance -= amount;
  289.     }
  290.  
  291.     // notify if fail
  292.  
  293.     /*
  294.      * TODO:
  295.      * find the appropriate users and add transaction to list of transactions
  296.      * change their money
  297.      */
  298.  
  299. }
  300.  
  301. Transaction.prototype.toString = function() {
  302.     var ret = '';
  303.     if(this.to !== -1 && this.from !== -1)
  304.         ret = 'Transferred $' + this.amount + ' from ' + this.from + ' to ' + this.to;
  305.     else if(this.to === -1 && this.from !== -1)
  306.         ret = 'Withdrawn $' + amount + ' from ' + this.from;
  307.     else if(this.to !== -1 && this.from === 1)
  308.         ret = 'Deposited $' + amount + ' to ' + this.to;
  309.     return ret;
  310. }
  311. //********************************************************************************//
  312. //********************************************************************************//
  313. //********************************************************************************//
  314.  
  315. /*
  316.  * views
  317.  */
  318.  
  319. exports.readme = function(req, res) {
  320.     // The instructions for the assignment.
  321.     res.render('readme', {
  322.         title : 'Midterm'
  323.     });
  324. };
  325. //********************************************************************************//
  326.  
  327. exports.create = function(req, res) {
  328.  
  329.     var uname = req.query.username;
  330.     var pw = req.query.password;
  331.     var dob = req.query.dob;
  332.  
  333.     var newUser = new User(uid++);
  334.     newUser.createUser(uname, pw, dob);
  335.  
  336.     path.exists(usersDB, function(exists) {
  337.         if(!exists) {
  338.  
  339.             var users = newUser ? [newUser] : [];
  340.             var json = JSON.stringify(users);
  341.  
  342.             saveUsersDB(users, function() {
  343.                 res.render('create', {
  344.                     'title' : 'Create New User'
  345.                 });
  346.             });
  347.         } else {
  348.             loadUsersDB(function(users) {
  349.                 if(newUser) {
  350.                     var dupe = 0;
  351.                     for(var i = 0; i < users.length; i++) {
  352.                         if(users[i].username === uname) {
  353.                             dupe = 1;
  354.                         }
  355.                     }
  356.                     if(dupe === 0)
  357.                         users.push(newUser);
  358.                 }
  359.                 saveUsersDB(users, function() {
  360.                     res.render('create', {
  361.                         'title' : 'Create'
  362.                     });
  363.                 });
  364.             });
  365.         }
  366.     });
  367. };
  368. //********************************************************************************//
  369.  
  370. exports.login = function(req, res) {
  371.     // req has properties username and password
  372.  
  373.     path.exists(usersDB, function(exists) {
  374.         if(!exists) {
  375.             var users = [];
  376.             var json = JSON.stringify(users);
  377.             saveUsersDB(users, function() {
  378.                 res.render('login', {
  379.                     'title' : 'No userdatabase'
  380.                 });
  381.             });
  382.         } else {
  383.             loadUsersDB(function(users) {
  384.                 var uname = req.query.username;
  385.                 var pw = req.query.password;
  386.                 for(var i = 0; i < users.length; i++) {
  387.                     if(users[i].username === uname) {
  388.                         if(users[i].password === pw)
  389.                         ;
  390.                         req.session.user = users[i];
  391.                     }
  392.                 }
  393.                 res.render('login', {
  394.                     'title' : 'logged in as ' + req.session.user.username
  395.                 });
  396.             });
  397.         }
  398.         // TODO: user login
  399.     });
  400. }
  401. //********************************************************************************//
  402.  
  403. exports.logout = function(req, res) {
  404.     req.session.destroy();
  405.     // TODO: user logout
  406. };
  407. //********************************************************************************//
  408.  
  409. exports.transactions = function(req, res) {
  410.     res.render('transactions', {
  411.         title : 'Transactions',
  412.         msg : 'Logged in as ' + req.session.username
  413.     });
  414.     // TODO: transaction list
  415. };
  416. //********************************************************************************//
  417.  
  418. exports.deposit = function(req, res) {
  419.     res.render('deposit', {
  420.         title : 'Deposit',
  421.         user: req.session.user.username
  422.     });
  423.     var amount = req.query.amt;
  424.     var user = new User(req.session.user);
  425.     // TODO: account deposit
  426. };
  427. //********************************************************************************//
  428.  
  429. exports.withdraw = function(req, res) {
  430.     var user = new User(req.session.user);
  431.     res.render('withdraw', {
  432.         title : 'Withdraw'
  433.     });
  434.     // TODO: account withdrawal
  435. };
  436. //********************************************************************************//
  437.  
  438. exports.transfer = function(req, res) {
  439.     res.render('transfer', {
  440.         title : 'Transfer'
  441.     });
  442.     // TODO: account transfer
  443. };
  444. //********************************************************************************//
  445.  
  446. exports.report = function(req, res) {
  447.     res.render('report', {
  448.         title: 'Report'
  449.     });
  450. }
  451. //********************************************************************************//
  452. //********************************************************************************//
  453. //********************************************************************************//
  454.  
  455. /*
  456.  * Persistance
  457.  */
  458.  
  459. var fs = require('fs');
  460. var path = require('path');
  461.  
  462. var usersDB = 'users.json';
  463. var transDB = 'trans.json';
  464.  
  465. function loadUsersDB(cb) {
  466.     fs.readFile(usersDB, function(err, data) {
  467.         if(err) {
  468.             throw new Error('Error reading database: ' + err);
  469.         }
  470.         cb(JSON.parse(data));
  471.     });
  472. }
  473.  
  474. function loadTransDB(cb) {
  475.     fs.readFile(trans, function(err, data) {
  476.         if(err) {
  477.             throw new Error('Error reading database: ' + err);
  478.         }
  479.         cb(JSON.parse(data));
  480.     });
  481. }
  482.  
  483. function saveUsersDB(users, cb) {
  484.     var json = JSON.stringify(users);
  485.     fs.writeFile(usersDB, json, function(err) {
  486.         if(err) {
  487.             throw new Error('Could not write ' + dbfile);
  488.         }
  489.         cb();
  490.     });
  491. }
  492.  
  493. function saveTransDB(transactions, cb) {
  494.     var json = JSON.stringify(transactions);
  495.     fs.writeFile(transDB, json, function(err) {
  496.         if(err) {
  497.             throw new Error('Could not write ' + dbfile);
  498.         }
  499.         cb();
  500.     });
  501. }
  502.  
  503. //********************************************************************************//
  504. //********************************************************************************//
  505. //********************************************************************************//
  506.  
  507. // Random stuff
  508.  
  509. // Generate list of Users
  510.  
  511. function toList(users) {
  512.     var s = '<ul>';
  513.     for(var i = 0; i < users.length; i++)
  514.     s += '<li>' + users[i].username + '</li>';
  515.     s += '</ul>';
  516.     return s;
  517. }
Add Comment
Please, Sign In to add comment