JavaScript之ATM机

//面向对象 版本 ATM机
// 分析一共两个对象 ATM对象和用户
// 清屏函数
let clear = () => process.stdout.write(process.platform === 'win32' ? '\x1Bc' : '\x1B[2J\x1B[3J\x1B[H');
//用户的构造函数
let User = function(userName,totalMoney){
this.userName = userName;
this.totalMoney = totalMoney;
}
//ATM 机构造函数
let ATM = function(){}
//ATM机 查询功能
ATM.prototype.checkMoney = function(user){
console.log("当前卡上的余额为:",user.totalMoney);
console.log("按回车键返回");
}
//ATM机 存钱功能
ATM.prototype.saveMoney = function(money,user){
user.totalMoney += money;
console.log("钱已经存入,当前卡上的余额为:",user.totalMoney);
console.log("按回车键返回");
}
//ATM机 取钱功能
ATM.prototype.outputMoney = function(money,user){
if(money > user.totalMoney)
{
console.log("卡上余额不足");
console.log("按回车键返回");
}
else{
user.totalMoney -= money;
console.log("钱已经取出,当前卡上的余额为:",user.totalMoney);
console.log("按回车键返回");
}
}
let main = function(){
let user = new User("老梁",100000);//实例化用户对象
let atm = new ATM();//实例化ATM机
let doWork = true;
while(doWork)
{
clear();
console.log("欢迎你,",user.userName);
console.log("请选择您要执行的功能: 1.查询 2.存钱 3.取钱 4.退出");
let choose = parseInt(readline.question(""));
switch(choose)
{
case 1:
{
clear();
atm.checkMoney(user);
readline.question("");
break;
}
case 2:
{
clear();
console.log("请输入您要存的金额:");
let money = parseInt(readline.question(""));
atm.saveMoney(money,user);
readline.question("");
break;
}
case 3:
{
clear();
console.log("请输入您要取的金额:");
let money = parseInt(readline.question(""));
atm.outputMoney(money,user);
readline.question("");
break;
}
case 4:
{
clear();
console.log("感谢您的使用,再见!");
doWork = false;
readline.question("");
clear();
break;
}
}
}
}
let readline = require("readline-sync");
main();
posted @ 2019-09-08 20:18  二狗子最萌哒  阅读(337)  评论(0编辑  收藏  举报