TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
h dave
NA
50
4.2k
I have query regarding refactoring code
Sep 4 2019 11:45 AM
the website sells lawnmowers, phone cases and t-shirts. Their database is a mess but you have access to their variousproduct listings. The goal is to consolidate all of the client's products into a single list so that they can be sorted and displayed on the client's website.
I want to refactor code in AgularJS
Please find attached ZIP file
Is anyone there who can guide me and provide me solution for this that will be much Appreciated
Refactor Me :
function
ProductDataConsolidator() {
}
ProductDataConsolidator.get =
function
() {
var
l =
new
LawnmowerRepository().getAll();
var
p =
new
PhoneCaseRepository().getAll();
var
t =
new
TShirtRepository().getAll();
var
products = [];
for
(
var
i = 0; i < l.length; i++) {
products.push({
id: l[i].id,
name: l[i].name,
price: l[i].price.toFixed(2),
type:
"Lawnmower"
});
}
for
(
var
i = 0; i < p.length; i++) {
products.push({
id: p[i].id,
name: p[i].name,
price: p[i].price.toFixed(2),
type:
"Phone Case"
});
}
for
(
var
i = 0; i < t.length; i++) {
products.push({
id: t[i].id,
name: t[i].name,
price: t[i].price.toFixed(2),
type:
"T-Shirt"
});
}
return
products;
}
ProductDataConsolidator.getInUSDollars =
function
() {
var
l =
new
LawnmowerRepository().getAll();
var
p =
new
PhoneCaseRepository().getAll();
var
t =
new
TShirtRepository().getAll();
var
products = [];
for
(
var
i = 0; i < l.length; i++) {
products.push({
id: l[i].id,
name: l[i].name,
price: (l[i].price * 0.76).toFixed(2),
type:
"Lawnmower"
});
}
for
(
var
i = 0; i < p.length; i++) {
products.push({
id: p[i].id,
name: p[i].name,
price: (p[i].price * 0.76).toFixed(2),
type:
"Phone Case"
});
}
for
(
var
i = 0; i < t.length; i++) {
products.push({
id: t[i].id,
name: t[i].name,
price: (t[i].price * 0.76).toFixed(2),
type:
"T-Shirt"
});
}
return
products;
}
ProductDataConsolidator.getInEuros =
function
() {
var
l =
new
LawnmowerRepository().getAll();
var
p =
new
PhoneCaseRepository().getAll();
var
t =
new
TShirtRepository().getAll();
var
products = [];
for
(
var
i = 0; i < l.length; i++) {
products.push({
id: l[i].id,
name: l[i].name,
price: (l[i].price * 0.67).toFixed(2),
type:
"Lawnmower"
});
}
for
(
var
i = 0; i < p.length; i++) {
products.push({
id: p[i].id,
name: p[i].name,
price: (p[i].price * 0.67).toFixed(2),
type:
"Phone Case"
});
}
for
(
var
i = 0; i < t.length; i++) {
products.push({
id: t[i].id,
name: t[i].name,
price: (t[i].price * 0.67).toFixed(2),
type:
"T-Shirt"
});
}
return
products;
}
function
ProductDataRenderer() {
}
ProductDataRenderer.prototype.render =
function
() {
var
nzd =
'<table class="table table-striped">'
+
' <thead>'
+
' <tr><td colspan="3">Products (NZD)</td></tr>'
+
' <tr>'
+
' <td>Name</td>'
+
' <td>Price</td>'
+
' <td>Type</td>'
+
' </tr>'
+
' </thead>'
+
' <tbody>'
;
var
n = ProductDataConsolidator.get();
for
(
var
i = 0; i < n.length; i++) {
nzd +=
'<tr>'
+
'<td>'
+ n[i].name +
'</td>'
+
'<td>'
+ n[i].price +
'</td>'
+
'<td>'
+ n[i].type +
'</td>'
+
'</tr>'
;
}
nzd +=
'</tbody></table>'
;
document.getElementById(
"nzdProducts"
).innerHTML = nzd;
var
usd =
'<table class="table table-striped">'
+
' <thead>'
+
' <tr><td colspan="3">Products (USD)</td></tr>'
+
' <tr>'
+
' <td>Name</td>'
+
' <td>Price</td>'
+
' <td>Type</td>'
+
' </tr>'
+
' </thead>'
+
' <tbody>'
;
var
u = ProductDataConsolidator.getInUSDollars();
for
(
var
i = 0; i < u.length; i++) {
usd +=
'<tr>'
+
'<td>'
+ u[i].name +
'</td>'
+
'<td>'
+ u[i].price +
'</td>'
+
'<td>'
+ u[i].type +
'</td>'
+
'</tr>'
;
}
usd +=
'</tbody></table>'
;
document.getElementById(
"usdProducts"
).innerHTML = usd;
var
euro =
'<table class="table table-striped">'
+
' <thead>'
+
' <tr><td colspan="3">Products (Euro)</td></tr>'
+
' <tr>'
+
' <td>Name</td>'
+
' <td>Price</td>'
+
' <td>Type</td>'
+
' </tr>'
+
' </thead>'
+
' <tbody>'
;
var
e = ProductDataConsolidator.getInEuros();
for
(
var
i = 0; i < e.length; i++) {
euro +=
'<tr>'
+
'<td>'
+ e[i].name +
'</td>'
+
'<td>'
+ e[i].price +
'</td>'
+
'<td>'
+ e[i].type +
'</td>'
+
'</tr>'
;
}
euro +=
'</tbody></table>'
;
document.getElementById(
"euProducts"
).innerHTML = euro;
}
Do not refector me :
function
LawnmowerRepository() {
}
LawnmowerRepository.prototype.getAll =
function
() {
return
[
{
id: 1,
name:
"Hewlett-Packard Rideable Lawnmower"
,
fuelEfficiency:
"Very Low"
,
isVehicle:
true
,
price: 3000
}, {
id: 2,
name:
"Fisher Price's My First Lawnmower"
,
fuelEfficiency:
"Ultimate"
,
isVehicle:
false
,
price: 45
}, {
id: 3,
name:
"Volkswagen LawnMaster 39000B Lawnmower"
,
fuelEfficiency:
"Moderate"
,
isVehicle:
false
,
price: 1020
}
];
}
<!doctype html>
<html lang=
"en"
>
<head>
<title>Refactor Me</title>
<script type=
"text/javascript"
src=
"src/do-not-refactor/LawnmowerRepository.js"
></script>
<script type=
"text/javascript"
src=
"src/do-not-refactor/PhoneCaseRepository.js"
></script>
<script type=
"text/javascript"
src=
"src/do-not-refactor/TShirtRepository.js"
></script>
<script type=
"text/javascript"
src=
"src/refactor-me/ProductDataRenderer.js"
></script>
<script type=
"text/javascript"
src=
"src/refactor-me/ProductDataConsolidator.js"
></script>
<link rel=
"stylesheet"
type=
"text/css"
href=
"http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css"
></head>
<body>
<div style=
"max-width: 600px"
>
<div id=
"nzdProducts"
></div>
<div id=
"usdProducts"
></div>
<div id=
"euProducts"
></div>
</div>
<script type=
"text/javascript"
>
document.addEventListener(
"DOMContentLoaded"
,
function
() {
var
p =
new
ProductDataRenderer();
p.render();
});
</script>
</body>
<!DOCTYPE html>
<html>
<head>
<meta charset=
"utf-8"
>
<title>RefactorMe Tests</title>
<link rel=
"stylesheet"
href=
"http://code.jquery.com/qunit/qunit-1.17.1.css"
>
</head>
<body>
<div id=
"qunit"
></div>
<div id=
"qunit-fixture"
></div>
<script src=
"http://code.jquery.com/qunit/qunit-1.17.1.js"
></script>
<script src=
"src/tests.js"
></script>
</body>
</html>
function
TShirtRepository() {
}
TShirtRepository.prototype.getAll =
function
() {
return
[
{
id: 1,
colour:
"Blue"
,
name:
"Xamarin C# T-Shirt"
,
price: 15.0,
shirtText:
"C#, Xamarin"
}, {
id: 2,
colour:
"Black"
,
name:
"New York Yankees T-Shirt"
,
price: 8.0,
shirtText:
"NY"
}, {
id: 3,
colour:
"Green"
,
name:
"Disney Sleeping Beauty T-Shirt"
,
price: 10.0,
shirtText:
"Mirror mirror on the wall..."
}
];
}
function
PhoneCaseRepository() {
}
PhoneCaseRepository.prototype.getAll =
function
() {
return
[
{
id: 1,
name:
"Amazon Fire Burgundy Phone Case"
,
colour:
"Burgundy"
,
material:
"PVC"
,
targetPhone:
"Amazon Fire"
,
price: 14.0
}, {
id: 2,
name:
"Nokia Lumia 920/930/Icon Crimson Phone Case"
,
colour:
"Red"
,
material:
"Rubber"
,
targetPhone:
"Nokia Lumia 920/930/Icon"
,
price: 10.0
}
]
}
Reply
Answers (
1
)
How to increase performance of Angular 7 Project ?
Regarding install wysiwyg editor in Angular 7.