For this problem, you will maintain an associative array whose keys are people’s
ID: 3590386 • Letter: F
Question
For this problem, you will maintain an associative array whose keys are people’s custName. An associated value is an object with three properties: accounts (an array of a customer’s accounts objects), address (string literal, with just the city name), salary (the customer’s current salary) and loans (an array of a customer’s loan objects). We assume that a customer’s salary can start out with a zero. Write an HTML document prob1.html that loads a JavaScript file prob1.js. When the body is finishes loading, function go() is called. The body of the document consists of an empty div element with id value "custs". Function go() in prob1.js first calls function customer(), which prompts for the customer’s name. Each name becomes a key in an associative array, custs, and the associated value is an object with its accounts property initialized to [], loan property initialized to [], the address property initialized to an empty string and its salary property initialized to 0. Remember that the accounts property is an object with several properties, therefore a function account () is needed. This method should prompt the user for the account’s acctName and balance as account associative pair and then adds the account object to the array. The account () should prompt the user for more accounts until the user clicks Cancel. The customer()should continuously prompting the user more customers until the user clicks Cancel. You are limited to using Savings and Checking accounts name. Function go() next calls approveLoan(). This function prompts the user for a name a of customer that needs a loan then the name of the loan and the loan amount (> 0.0). If the name of the customer is not in the associative array for the customer, the user should be re-prompt saying user does not exist. Otherwise, approaveLoan () should loop through all the customer’s accounts for the account balances, each account balance should be added to customer’s salary then compared to the loan amount. If the loan amount is 3 times less than the salary + account balances, then approve the loan, otherwise disapprove the loan. The approaveLoan () should continuously prompting the user more names until the user clicks Cancel. You should store the loan name and amount as a pair in the loan property for the customer, with a status that says approved/disapproved. Special situation, if the customer already has any loans, loop through the loan array and add the loan amounts for that customer. Use the approved loans + the salary + total account balances to determine if the new loan should be approved or not. Finally, go() calls outCusts(), passing it custs. This function assigns to, say, custsRef a reference to the div element (with id " custs"). Its constructs in, say, custsStr a string with all the information in associative array custs, and this string is assigned to the innerHTML property of custsRef. For each element of custs, the string has one line with the name followed by a ‘:’. The next four lines have, respectively, the address, the salary, the array of accounts, the array of loans. Each of these four lines begins with two spaces. (Use the character entity reference to have the browser insert a space.) Use the toString() method of the arrays of accounts and COMP 322 Internet Systems Fall 2014 Assignment 4 2 loans to get a quick string representation of the contents of these arrays; do not spend time on appearances. (The contents of these arrays will be strings that have not been converted to numbers. These strings are converted to numbers when the balance is updated since that involves subtraction.) The screenshot at above shows the output when names “Jill” and “Jane” customer records were created.
Explanation / Answer
Prob1.html
<!DOCTYPE html>
<html>
<head>
<title>Problem</title>
</head>
<body>
<div id="custs">
</div>
<script type="text/javascript" src="prob1.js"></script>
</body>
</html>
Prob1.js
var custs = []; //an associative array, custs
// Finally, go() calls outCusts(), passing it custs. This function assigns to, say, custsRef a reference to the div element (with id " custs"). Its constructs in, say, custsStr a string with all the information in associative array custs, and this string is assigned to the innerHTML property of custsRef. For each element of custs, the string has one line with the name followed by a ‘:’. The next four lines have, respectively, the address, the salary, the array of accounts, the array of loans. Each of these four lines begins with two spaces.
function outCusts(){
var result="";
for (var i in custs){
result += "<br>"+i + ":<br>";
result += " Address: " +custs[i].address+ "<br>";
result += " Salary: " +custs[i].salary+ "<br>";
for (var j in custs[i].accounts){
result += " Accounts: "+Object.keys(custs[i].accounts[j])+" (Balance: "+Object.values(custs[i].accounts[j])+"),";
console.log(Object.keys(custs[i].accounts[j]));
}
for (var k in custs[i].loan){
result += "<br> Loans: "+Object.keys(custs[i].loan[k])+" (Balance: "+Object.values(custs[i].loan[k])+"),";
console.log(Object.keys(custs[i].loan[k]));
}
}
document.getElementById("custs").innerHTML = result;
}
//This function prompts the user for a name a of customer that needs a loan then the name of the loan and the loan amount (> 0.0).
//If the name of the customer is not in the associative array for the customer, the user should be re-prompt saying user does not exist.
//Otherwise, approveLoan () should loop through all the customer’s accounts for the account balances, each account balance should be added to customer’s salary then compared to the loan amount.
//If the loan amount is 3 times less than the salary + account balances, then approve the loan, otherwise disapprove the loan.
//The approveLoan () should continuously prompting the user more names until the user clicks Cancel.
//You should store the loan name and amount as a pair in the loan property for the customer, with a status that says approved/disapproved.
//Special situation, if the customer already has any loans, loop through the loan array and add the loan amounts for that customer.
//Use the approved loans + the salary + total account balances to determine if the new loan should be approved or not.
function approveLoan() {
while(1){
cname = prompt("Enter the name of customer that needs a loan: ");
loan = prompt("Enter the name of loan: ");
lamnt = prompt("Enter the loan amount: ");
if (cname == null) {
break;
}
else if(custs[cname] === undefined){
alert("customer does not exist "+custs[cname]);
}
else{
alert("customer exist, checking loan approval");
var sum;
for (var k in custs[cname].loans){
sum += parseInt(Object.values(custs[cname].loans[k]));
console.log(sum);
}
sum += parseInt(custs[cname].salary);
//Loan eligibility to be checked
amount = 3*parseInt(lamnt);
if( amount <= sum){
var l = [];
l[loan]=lamnt;
custs[cname].loan.push(l);
l["status"] = "Approved";
custs[cname].loan.push(l);
}
else{
var l = [];
l[loan]=lamnt;
custs[cname].loan.push(l);
l["status"] = "disapproved";
custs[cname].loan.push(l);
}
console.log(custs);
}
}
}
//the accounts property is an object with several properties, therefore a function account () is needed. This method should prompt the user for the account’s acctName and balance as account associative pair and then adds the account object to the array. The account () should prompt the user for more accounts until the user clicks Cancel.
function account(c){
while(1){
var accnt = prompt("Enter the account name - Savings/Checking: ");
if (accnt == null) {
return true;
}
else if (accnt.length==0) {
alert("You must enter a account name or cancel !!");
}
else{
var bal = prompt("Enter the balance: ");
if (accnt.length==0) {
alert("You must enter a account name or cancel !!");
}
else if (bal == null) {
alert("Enter balance ");
}
else{
//document.getElementById("custs").innerHTML = "Hello " + c;
var acc = [];
acc[accnt]=bal;
//custs[c]={accounts:[], address:'', salary:0, loan:0};
custs[c].accounts.push(acc);
console.log(custs);
}
}
}
}
//The customer()should continuously prompting the user more customers until the user clicks Cancel.
//Function go() first calls function customer(), which prompts for the customer’s name. Each name becomes a key in an associative array, custs, and the associated value is an object with its accounts property initialized to [], loan property initialized to [], the address property initialized to an empty string and its salary property initialized to 0.
function customer(){
while(1){
var c = prompt("Enter a customer name: ");
if (c == null) {
return false;
}
else if (c.length==0) {
alert("You must either enter a customer name or cancel !!");
}
else{
custs[c]={};
custs[c]={accounts:[], address:'', salary:0, loan:[]};
console.log(custs);
var a = prompt("Enter customer address: ");
var s = prompt("Enter customer salary: ");
custs[c].address=a;
custs[c].salary=s;
console.log(custs);
return c;
}
}
}
function go(){
while (x = customer()){
account(x);
}
approveLoan();
outCusts()
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.