Skip to content

Commit

Permalink
Sample of classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jskonst committed Oct 23, 2023
1 parent c8965a3 commit 4b70e3b
Show file tree
Hide file tree
Showing 13 changed files with 200 additions and 11 deletions.
20 changes: 20 additions & 0 deletions rpgsaga/saga/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@ Diagonal
DeclineCall()
sendText()
readText()

drive
cdDrive dwdDrive
(write) (write)
combo()
write()???

class Combo(cdDrive, dvdDrive)

Animal
Mamal
canine feline
dog wolf cat, tiger
voice()
display()





BabushkaPhone SmartPhone LandLinePhone

AndroidPhone IOSPhone Symbian
Expand Down
19 changes: 19 additions & 0 deletions rpgsaga/saga/src/EShop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { IDisplayable } from './IDisplayable';
import { Phone } from './phone';

export class Eshop {
private stuff: IDisplayable[];
constructor() {
this.stuff = [];
}

addStuff(item: IDisplayable) {
this.stuff.push(item);
}

showStuff() {
for (const itm of this.stuff) {
console.log(itm.display());
}
}
}
5 changes: 5 additions & 0 deletions rpgsaga/saga/src/IAndroidApp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { IApplication } from './IApplication';

export interface IAndroidApp extends IApplication {
checkSecurity(): void;
}
6 changes: 6 additions & 0 deletions rpgsaga/saga/src/IApplication.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface IApplication {
readonly name: string;
install(): void;
run(): void;
uninstall(): void;
}
3 changes: 3 additions & 0 deletions rpgsaga/saga/src/IDisplayable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface IDisplayable {
display(): string;
}
5 changes: 5 additions & 0 deletions rpgsaga/saga/src/IIOsApp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { IApplication } from './IApplication';

export interface IIosApp extends IApplication {
checkSignature(): void;
}
3 changes: 3 additions & 0 deletions rpgsaga/saga/src/babushkaphone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { CellPhone } from './cellphone';

export class Babushkaphone extends CellPhone {}
30 changes: 30 additions & 0 deletions rpgsaga/saga/src/cellphone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Phone } from './phone';

export abstract class CellPhone extends Phone {
diagonal: number;

display(): string {
return `i'm cellphone ${this.name}`;
}

constructor(number: string, year: number, diagonal: number, public name?: string) {
console.log('Creating cellphone');
super(number, year, name);
this.diagonal = diagonal;
}

call(number: string): void {
console.log(`making call by cellphone ${number}`);
}

declaineCall() {
console.log('declaing call');
}
sendMessage(text: string, num: number) {
console.log(`sending ${text} to ${num}`);
}

readMessage() {
console.log('reading incoming messages');
}
}
72 changes: 64 additions & 8 deletions rpgsaga/saga/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,72 @@
import { Phone } from './phone';
import { CellPhone } from './cellphone';
import { LandlinePhone } from './landlinephone';
import { Babushkaphone } from './babushkaphone';
import { Eshop } from './EShop';

const first = new Phone('+7900-000 000 (123)', 1990, 'Телефон 1');
const first = new LandlinePhone('+7900-000 000 (123)', 1990, 'Телефон 1');
first.year = 1998;

first.year = -1998;
try {
first.year = -1998;
} catch (e) {
console.log(e);
}
first.call('12345');
first.endCall();

const second = new Phone('+799900000', -5);
// second.name = 'Телефон 2';
console.log(second.year);
second.call('12345');
second.endCall();
try {
const second = new LandlinePhone('+799900000', -5);
// second.name = 'Телефон 2';
console.log(second.year);
second.call('12345');
second.endCall();
} catch (e) {
console.log(e);
}
const cp1: CellPhone = new Babushkaphone('+79000000000', 2010, 5, 'samsung');
cp1.call('12345');
cp1.endCall();
cp1.sendMessage('some message', 1234);

console.log(first, second, Phone.phoneCount);
const cp2 = new Babushkaphone('+79000000000', 2010, 5, 'samsung');
cp2.call('1234111111');
cp2.endCall();
console.log(`${cp2}`);

const tmp = `
/\\ /\\ Todd Vargo
//\\\\_//\\\\ ____
\\_ _/ / /
/ * * \\ /^^^]
\\_\\O/_/ [ ]
/ \\_ [ /
\\ \\_ / /
[ [ / \\/ _/
_[ [ \\ /_/
`;
console.log(tmp);

const phones: Array<Phone> = [cp2, cp1, first];
for (const phone of phones) {
console.log(phone.display());
}

const someReq = () => {
throw new Error('Something bad happend');
};

try {
someReq();
console.log('success');
} catch (e) {
console.log(`failed ${e}`);
}
console.log('after finally');

const shop = new Eshop();

shop.addStuff(cp1);
shop.addStuff(cp2);
shop.addStuff(first);
shop.showStuff();
7 changes: 7 additions & 0 deletions rpgsaga/saga/src/landlinephone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Phone } from './phone';

export class LandlinePhone extends Phone {
display(): string {
return `i'm landline phone`;
}
}
11 changes: 11 additions & 0 deletions rpgsaga/saga/src/pen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { IDisplayable } from './IDisplayable';

export class Pen implements IDisplayable {
name: string;
constructor(name: string) {
this.name = name;
}
display(): string {
return `stylus ${this.name}`;
}
}
19 changes: 16 additions & 3 deletions rpgsaga/saga/src/phone.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
export class Phone {
import { IDisplayable } from './IDisplayable';

export abstract class Phone implements IDisplayable {
private aYear: number;
phoneNumber: string;

static phoneCount = 0;

constructor(number: string, year: number, public name?: string) {
console.log('Creating phone');
Phone.phoneCount += 1;
this.phoneNumber = number;
this.year = year;
Expand All @@ -19,10 +22,20 @@ export class Phone {
}

set year(year: number) {
this.aYear = year >= 1900 && year < 2023 ? year : this.aYear ?? 1900;
if (year >= 1900 && year < 2023) {
this.aYear = year;
return;
}
throw new Error(`incorrect year`);
}

get year(): number {
return this.aYear;
}
}

toString(): string {
return `this is phone ${this.name}`;
}

abstract display(): string;
}
11 changes: 11 additions & 0 deletions rpgsaga/saga/src/smartphone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { IApplication } from './IApplication';
import { CellPhone } from './cellphone';

export abstract class Smartphone extends CellPhone {
apps: IApplication[];

constructor(number: string, year: number, diagonal: number, public name?: string) {
super(number, year, diagonal, name);
this.apps = [];
}
}

0 comments on commit 4b70e3b

Please sign in to comment.