Skip to content

Commit 51e2c02

Browse files
committed
lesson-16
1 parent e23b4f6 commit 51e2c02

File tree

7 files changed

+62
-59
lines changed

7 files changed

+62
-59
lines changed

public/app.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
1-
const me = {
2-
name: 'shaun',
3-
//age: 30,
4-
speak(text) {
5-
console.log(text);
6-
},
7-
spend(amount) {
8-
console.log('I spent ', amount);
9-
return amount;
10-
},
11-
};
12-
console.log(me);
13-
me.speak('hello, world');
14-
const greetPerson = (person) => {
15-
console.log('hello ', person.name);
16-
};
17-
greetPerson(me);
18-
//greetPerson({name: 'shaun'});
1+
import { Invoice } from './classes/Invoice.js';
2+
import { Payment } from './classes/Payment.js';
3+
// let docOne: HasFormatter;
4+
// let docTwo: HasFormatter;
5+
// docOne = new Invoice('yoshi', 'web work', 250);
6+
// docTwo = new Payment('mario', 'plumbing', 200);
7+
// let docs: HasFormatter[] = [];
8+
// docs.push(docOne);
9+
// docs.push(docTwo);
1910
const form = document.querySelector('.new-item-form');
2011
console.log(form.children);
2112
// inputs
@@ -25,5 +16,12 @@ const details = document.querySelector('#details');
2516
const amount = document.querySelector('#amount');
2617
form.addEventListener('submit', (e) => {
2718
e.preventDefault();
28-
console.log(type.value, tofrom.value, details.value, amount.valueAsNumber);
19+
let doc;
20+
if (type.value === 'invoice') {
21+
doc = new Invoice(tofrom.value, details.value, amount.valueAsNumber);
22+
}
23+
else {
24+
doc = new Payment(tofrom.value, details.value, amount.valueAsNumber);
25+
}
26+
console.log(doc);
2927
});

public/classes/Invoice.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
export class Invoice {
2-
// readonly client: string;
3-
// private details: string;
4-
// public amount: number;
52
constructor(client, details, amount) {
63
this.client = client;
74
this.details = details;

public/classes/Payment.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export class Payment {
2+
constructor(recipient, details, amount) {
3+
this.recipient = recipient;
4+
this.details = details;
5+
this.amount = amount;
6+
}
7+
;
8+
format() {
9+
return `${this.recipient} is owed £${this.amount} for ${this.details}`;
10+
}
11+
}

src/app.ts

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,16 @@
11
import { Invoice } from './classes/Invoice.js';
2+
import { Payment } from './classes/Payment.js';
3+
import { HasFormatter } from './interfaces/HasFormatter.js';
24

3-
// interfaces
4-
export interface IsPerson {
5-
name: string;
6-
age?: number;
7-
speak(a: string): void;
8-
spend(a: number): number;
9-
}
5+
// let docOne: HasFormatter;
6+
// let docTwo: HasFormatter;
107

11-
const me: IsPerson = {
12-
name: 'shaun',
13-
//age: 30,
14-
speak(text: string): void {
15-
console.log(text);
16-
},
17-
spend(amount: number): number {
18-
console.log('I spent ', amount);
19-
return amount;
20-
},
21-
};
8+
// docOne = new Invoice('yoshi', 'web work', 250);
9+
// docTwo = new Payment('mario', 'plumbing', 200);
2210

23-
console.log(me);
24-
me.speak('hello, world');
25-
26-
const greetPerson = (person: IsPerson): void => {
27-
console.log('hello ', person.name);
28-
}
29-
30-
greetPerson(me);
31-
//greetPerson({name: 'shaun'});
11+
// let docs: HasFormatter[] = [];
12+
// docs.push(docOne);
13+
// docs.push(docTwo);
3214

3315
const form = document.querySelector('.new-item-form') as HTMLFormElement;
3416
console.log(form.children);
@@ -42,10 +24,11 @@ const amount = document.querySelector('#amount') as HTMLInputElement;
4224
form.addEventListener('submit', (e: Event) => {
4325
e.preventDefault();
4426

45-
console.log(
46-
type.value,
47-
tofrom.value,
48-
details.value,
49-
amount.valueAsNumber
50-
);
27+
let doc: HasFormatter;
28+
if (type.value === 'invoice') {
29+
doc = new Invoice(tofrom.value, details.value, amount.valueAsNumber);
30+
} else {
31+
doc = new Payment(tofrom.value, details.value, amount.valueAsNumber);
32+
}
33+
console.log(doc);
5134
});

src/classes/Invoice.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
export class Invoice {
2-
// readonly client: string;
3-
// private details: string;
4-
// public amount: number;
1+
import { HasFormatter } from '../interfaces/HasFormatter.js';
52

3+
export class Invoice implements HasFormatter {
64
constructor(
75
readonly client: string,
86
private details: string,

src/classes/Payment.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
}

src/interfaces/HasFormatter.ts

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

0 commit comments

Comments
 (0)