JavaScript's ATM machine

// object-oriented version of the ATM machine
// analysis of a total of two ATM objects and user objects
// clear screen function
let clear = () => process.stdout.write(process.platform === 'win32' ? '\x1Bc' : '\x1B[2J\x1B[3J\x1B[H');
// constructor user
let User = function(userName,totalMoney){
this.userName = userName;
this.totalMoney = totalMoney;
}
// ATM agency constructors
let ATM = function(){}
// ATM machine inquiry
ATM.prototype.checkMoney = function(user){
console.log ( "the current balance on the card is:", user.totalMoney);
the console.log ( "Press ENTER to return");
}
// ATM machine saving function
ATM.prototype.saveMoney = function(money,user){
user.totalMoney += money;
console.log ( "money has been deposited, the current balance on the card is:", user.totalMoney);
the console.log ( "Press ENTER to return");
}
// ATM machine to withdraw money function
ATM.prototype.outputMoney = function(money,user){
if(money > user.totalMoney)
{
console.log ( "lack of balance on the card");
the console.log ( "Press ENTER to return");
}
else{
user.totalMoney -= money;
console.log ( "money has been taken out, the current balance on the card is:", user.totalMoney);
the console.log ( "Press ENTER to return");
}
}
let main = function(){
let user = new User ( "old beams", 100,000); // user object instance
let atm = new ATM (); // instantiate ATM machine
let doWork = true;
while(doWork)
{
clear();
console.log ( "You are welcome,", user.userName);
console.log ( "Please select the function you want to perform: 1. query 2. 3. withdraw money saving 4. Exit");
let choose = parseInt(readline.question(""));
switch(choose)
{
case 1:
{
clear();
atm.checkMoney(user);
readline.question("");
break;
}
case 2:
{
clear();
console.log ( "Please enter the amount you want to deposit:");
let money = parseInt(readline.question(""));
atm.saveMoney(money,user);
readline.question("");
break;
}
case 3:
{
clear();
console.log ( "Please enter the amount you want to take:");
let money = parseInt(readline.question(""));
atm.outputMoney(money,user);
readline.question("");
break;
}
case 4:
{
clear();
console.log ( "Thank you for your use, good-bye!");
doWork = false;
readline.question("");
clear();
break;
}
}
}
}
let readline = require("readline-sync");
main();

Guess you like

Origin www.cnblogs.com/fatingGoodboy/p/11488203.html