I'm writing code in JavaScript and there is an condition to send the input. program must accept input from two sources: a filename passed as a command line argument, like ./myprogram input.txt; input read from STDIN, like ./myprogram < input.txt.
Input and Output:
Given the following input:
Add Tom 4111111111111111 $1000
Add Lisa 5454545454545454 $3000
Add Quincy 1234567890123456 $2000
Charge Tom $500
Charge Tom $800
Charge Lisa $7
Credit Lisa $100
Credit Quincy $200
program must produce the following output:
Lisa: $-93
Quincy: error
Tom: $500
I have this JS file which has three function add charge and credit but I'm not sure how to send input from command line to arguments to these function:
let obj = {};
function add(num, name, limit){
if(obj[num]){
console.log("alreday in the system");
return;
}
else {
obj[num] = {"name": name, "balance": 0, "limit": limit};
return obj;
}
}
function charge(num, chargeAmmount){
let cardBalance = obj[num].balance;
let newBalance = cardBalance+chargeAmmount;
if(obj[num] && obj[num].limit>newBalance){
obj[num].balance = newBalance;
return obj;
}
else {
console.log("cannot charge it, either this card is not in the system or canrge ammount is greater then the limit");
return;
}
}
function credit(num, creditBalance){
let cardBalance = obj[num].balance;
if(obj[num]){
let newBalance = cardBalance-creditBalance;
obj[num].balance = newBalance;
return obj;
} else {
console.log("not a valid card number");
return;
}
}