Skip to content

Commit b6a550a

Browse files
committed
added interface
1 parent c6f86e5 commit b6a550a

File tree

8 files changed

+43
-137
lines changed

8 files changed

+43
-137
lines changed

public/app.js

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,7 @@
1-
let me = {
2-
name: "tom",
3-
age: 20,
4-
speak(text) {
5-
console.log(text);
6-
},
7-
spend(amount) {
8-
console.log("spent:", amount);
9-
return amount;
10-
}
11-
};
12-
console.log(me);
13-
const greetPerson = (person) => {
14-
console.log("hello", person.name);
15-
};
16-
greetPerson(me);
17-
import { Invoice } from "./classes/Invoice.js";
18-
const invOne = new Invoice("tom", "work on the website", 123);
19-
const invTwo = new Invoice("bob", "work on the music", 34);
20-
console.log(invOne, invTwo);
21-
let invoices = [];
22-
invoices.push(invOne);
23-
invoices.push(invTwo);
24-
invoices.forEach(i => {
25-
console.log(i.client, i.amount, "|", i.format());
26-
});
27-
// inputs
28-
{
29-
const anchor = document.querySelector("a");
30-
if (anchor) {
31-
console.log(anchor.href);
32-
}
33-
const form = document.querySelector(".new-item-form");
34-
console.log(form.children);
35-
// inputs
36-
const type = document.querySelector("#type");
37-
const tofrom = document.querySelector("#tofrom");
38-
const details = document.querySelector("#details");
39-
const amount = document.querySelector("#amount");
40-
form.addEventListener("submit", (e) => {
41-
e.preventDefault();
42-
console.log(type.value, tofrom.value, details.value, amount.valueAsNumber);
43-
});
44-
}
1+
import { Invoice } from "./classes/Invoice";
2+
import { Payment } from "./classes/Payment";
3+
let doc1;
4+
let doc2;
5+
doc1 = new Invoice("tom", "tom's website", 250);
6+
doc2 = new Payment("bob", "bob's website", 300);
7+
console.log(doc1.format(), doc2.format());

public/classes/Payment.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ export class Payment {
44
this.details = details;
55
this.amount = amount;
66
}
7-
;
87
format() {
98
return `${this.recipient} is owed £${this.amount} for ${this.details}`;
109
}
File renamed without changes.

src/app.ts

Lines changed: 8 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,12 @@
1-
// interfaces
2-
interface IsPerson {
3-
name: string;
4-
age: number;
1+
import {Formattable} from "./interfaces/Formattable";
2+
import {Invoice} from "./classes/Invoice";
3+
import {Payment} from "./classes/Payment";
54

6-
speak(text: string): void;
5+
let doc1: Formattable;
6+
let doc2: Formattable;
77

8-
spend(amount: number): number;
9-
}
8+
doc1 = new Invoice("tom", "tom's website", 250);
9+
doc2 = new Payment("bob", "bob's website", 300);
1010

11-
let me: IsPerson = {
12-
name: "tom",
13-
age: 20,
14-
speak(text: string) {
15-
console.log(text);
16-
},
17-
spend(amount: number): number {
18-
console.log("spent:", amount);
19-
return amount;
20-
}
21-
}
22-
console.log(me);
11+
console.log(doc1.format(), doc2.format());
2312

24-
const greetPerson = (person: IsPerson) => {
25-
console.log("hello", person.name);
26-
}
27-
28-
greetPerson(me);
29-
30-
import {Invoice} from "./classes/Invoice.js";
31-
32-
const invOne = new Invoice("tom", "work on the website", 123);
33-
const invTwo = new Invoice("bob", "work on the music", 34);
34-
console.log(invOne, invTwo);
35-
36-
let invoices: Invoice[] = [];
37-
invoices.push(invOne);
38-
invoices.push(invTwo);
39-
40-
invoices.forEach(i => {
41-
console.log(i.client, i.amount, "|", i.format());
42-
})
43-
44-
// inputs
45-
{
46-
const anchor = document.querySelector("a");
47-
48-
if (anchor) {
49-
console.log(anchor.href);
50-
}
51-
52-
const form = document.querySelector(".new-item-form") as HTMLFormElement;
53-
console.log(form.children);
54-
55-
// inputs
56-
const type = document.querySelector("#type") as HTMLSelectElement;
57-
const tofrom = document.querySelector("#tofrom") as HTMLInputElement;
58-
const details = document.querySelector("#details") as HTMLInputElement;
59-
const amount = document.querySelector("#amount") as HTMLInputElement;
60-
61-
form.addEventListener("submit", (e: Event) => {
62-
e.preventDefault();
63-
console.log(
64-
type.value,
65-
tofrom.value,
66-
details.value,
67-
amount.valueAsNumber
68-
);
69-
})
70-
}

src/classes/Invoice.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import { HasFormatter } from '../interfaces/HasFormatter.js';
1+
import {Formattable} from "../interfaces/Formattable.js";
22

3-
export class Invoice implements HasFormatter {
4-
constructor(
5-
readonly client: string,
6-
private details: string,
7-
public amount: number,
8-
){}
3+
export class Invoice implements Formattable {
4+
constructor(
5+
readonly client: string,
6+
private details: string,
7+
public amount: number,
8+
) {
9+
}
910

10-
format() {
11-
return `${this.client} owes £${this.amount} for ${this.details}`;
12-
}
11+
format() {
12+
return `${this.client} owes £${this.amount} for ${this.details}`;
13+
}
1314
}

src/classes/Payment.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import { HasFormatter } from '../interfaces/HasFormatter.js';
2-
3-
export class Payment implements HasFormatter{
4-
constructor(
5-
readonly recipient: string,
6-
private details: string,
7-
public amount: number,
8-
){};
9-
10-
format() {
11-
return`${this.recipient} is owed £${this.amount} for ${this.details}`;
12-
}
13-
}
1+
import {Formattable} from "../interfaces/Formattable.js";
2+
3+
export class Payment implements Formattable {
4+
constructor(
5+
readonly recipient: string,
6+
private details: string,
7+
public amount: number,
8+
) {
9+
}
10+
11+
format() {
12+
return `${this.recipient} is owed £${this.amount} for ${this.details}`;
13+
}
14+
}

src/interfaces/Formattable.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface Formattable {
2+
format(): string;
3+
}

src/interfaces/HasFormatter.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)