Work out what changed between two versions of a product catalogue, describing each product's changes with a utility type.
What to build
A program that lists the products added, removed and changed between two catalogues.
Requirements
- Declare
interface Product { id: number; name: string; price: number; stock: number }and describe a product's changes asPartial<Omit<Product, "id">>. - For each id in either catalogue, in increasing order of id: print
+ ID NAMEif only in the new one,- ID NAMEif only in the old one, and for each changed field of a product in both,~ ID FIELD: OLD -> NEW, fields in the order name, price, stock. - Finally print
added A, removed R, changed C(C counts products with at least one changed field), orno changesif all three are 0.
Input
Line 1: n. Then n lines id;name;price;stock (the old catalogue). Next line: m. Then m lines (the new catalogue).
Output
The change lines, then the summary.
Constraints
0 ≤ n, m ≤ 100; ids are distinct within each catalogue.
Example
Input:
3
1;Pen;20;100
2;Book;350;12
3;Bag;900;4
3
1;Pen;25;100
3;Bag;900;4
4;Lamp;1200;7
Output:
~ 1 price: 20 -> 25
- 2 Book
+ 4 Lamp
added 1, removed 1, changed 1
Why it works
Deriving the change type from Product with Omit and Partial means a new field in Product is automatically a field that can change, and the list of fields to compare is typed as real keys, so a misspelt field name is a compile error.
Examples
3 1;Pen;20;100 2;Book;350;12 3;Bag;900;4 3 1;Pen;25;100 3;Bag;900;4 4;Lamp;1200;7
~ 1 price: 20 -> 25 - 2 Book + 4 Lamp added 1, removed 1, changed 1
1 5;Mug;150;30 1 5;Mug;150;30
no changes
Hints
Hint 1
type Changes = Partial<Omit<Product, "id">> describes what changed in one product.
Hint 2
Loop over the field names as (keyof Omit<Product, "id">)[] to compare the two versions field by field.
Solution
Show one correct solution (try it yourself first)
const read = (): string => prompt() ?? "";
interface Product {
id: number;
name: string;
price: number;
stock: number;
}
type Changes = Partial<Omit<Product, "id">>;
const FIELDS: (keyof Omit<Product, "id">)[] = ["name", "price", "stock"];
function readCatalogue(): Map<number, Product> {
const result = new Map<number, Product>();
const count = Number(read());
for (let i = 0; i < count; i++) {
const [id, name, price, stock] = read().split(";");
result.set(Number(id), { id: Number(id), name, price: Number(price), stock: Number(stock) });
}
return result;
}
function changesBetween(a: Product, b: Product): Changes {
const changes: Changes = {};
for (const field of FIELDS) {
if (a[field] !== b[field]) Object.assign(changes, { [field]: b[field] });
}
return changes;
}
const before = readCatalogue();
const after = readCatalogue();
const ids = [...new Set([...before.keys(), ...after.keys()])].sort((x, y) => x - y);
let added = 0;
let removed = 0;
let changed = 0;
for (const id of ids) {
const a = before.get(id);
const b = after.get(id);
if (!a && b) {
added++;
console.log("+ " + id + " " + b.name);
} else if (a && !b) {
removed++;
console.log("- " + id + " " + a.name);
} else if (a && b) {
const changes = changesBetween(a, b);
const fields = FIELDS.filter((f) => f in changes);
if (fields.length > 0) changed++;
for (const f of fields) console.log("~ " + id + " " + f + ": " + a[f] + " -> " + b[f]);
}
}
console.log(added + removed + changed === 0 ? "no changes" : "added " + added + ", removed " + removed + ", changed " + changed);