Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hands On OOP submission by Jimly Firdaus #84

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 13 additions & 60 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,65 +1,18 @@
# Hands On OOP
# Merchant Service Simulation

## Deskripsi Tugas
Buatlah suatu program yang *perlu* mengimplementasikan konsep-konsep OOP, seminimalnya
- Inheritance
- Interface dan/atau abstract class
- Polimorfisme
- Mengimplementasikan (minimal 1) design pattern
- Mengimplementasikan (minimal 1) prinsip SOLID
# Deskripsi Program
Program ini berbasis CLI yang mensimulasikan bagaimana user sebagai customer dari sebuah merchant dapat berinteraksi seperti membeli barang, top up balance, dan menu-menu lain.

Tugas ini sangat membebaskan kalian untuk berkreasi. Konsep-konsep lain yang kalian gunakan dan keunikan program akan sangat dihargai dan dihitung sebagai bonus nilai (jangan lupa tulis di penjelasan program). Silakan berkreasi! :D
# Design Pattern
Creational Pattern (Prototype): pada program ini, terdapat abstract class (pada class `baseUser`) yang digunakan untuk membangun class-class yang lain.

### Beberapa Bonus yang Direkomendasikan
- Stream API dan/atau functional programming
- Multilevel inheritance
- Interactive program (Menerima input dan mengeluarkan output sesuai input)

## Penjelasan Program
Selain membuat program, kalian perlu menuliskan penjelasan program kalian, dengan seminimalnya berisikan
- Deskripsi program
- Penjelasan design pattern yang dipilih
- Letak implementasi design pattern tersebut
- Alasan pemilihan design pattern tersebut
- Cara menjalankan program
- Versi bahasa dan dependency (jika ada) yang digunakan
- Konsep-konsep lain yang kalian gunakan dan keunikan program (jika ada)

## Pengumpulan
- Pengumpulan tugas ini mirip dengan tugas sebelumnya.
- Fork ke repository github kalian masing - masing
- Buka repository pada repo yang telah di fork sebelumnya
- Clone repository tersebut
- Buat program sesuai deskripsi di atas di dalam folder dengan format `Nama_Univ`
- Tulis penjelasan program kalian pada file `README.md` di dalam folder tersebut
- Add folder tersebut ke dalam staging
- Letakkan folder tersebut sejajar dengan file README.md
- Setelah itu push kembali ke repository kalian
- Pull request kedalam repository GDSC yang sudah anda fork tadi
- Isi judul pull request dengan "Hands on OOP submission by < Nama kalian >"

## Bahasa Pemrograman
Tidak semua bahasa pemrograman dapat mengimplementasikan OOP. Untuk tugas ini, ada bahasa yang dapat digunakan dan tidak dapat digunakan.

### Bahasa yang dapat digunakan
Selain bahasa di bawah ini, kalian dapat me-request bahasa lain, dengan persetujuan tim kurikulum GDSC ITB

- C++
- Java
- Kotlin
- Typescript
- Go (Golang)
- C#

### Bahasa yang tidak dapat digunakan
Karena keterbatasan di bahasa-bahasa berikut, bahasa di bawah ini tidak digunakan di tugas ini
- Python
- PHP
- C

## Deliverable
- Kumpulkan link github fork repository dan link pull request kalian ke gform yang dibagikan

## Deadline
- Jumat, 27 Januari 2023, pukul 23.59
# Cara menjalankan program
```shell
cd src
tsc index.ts
node index.js
```

# Versi Bahasa
- Typescript 4.8.4 (compatible untuk versi ^3.0.0)
24 changes: 24 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"@types/node": "^18.11.18"
}
}
144 changes: 144 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import { sleep } from "./lib/time_sleep";
import { inputData } from "./lib/readline";
import { storeItem, adminCredentials } from "./lib/constant/store";
import {
User,
Merchant,
Auth,
} from "./lib/user";
import * as console from "console";

const main = async () => {
let running = true;
console.log("Welcome~!");

let auth = new Auth();
let merchant = new Merchant(
adminCredentials.username,
adminCredentials.password,
);

while (running) {
console.log("1. Login");
console.log("2. Sign up");
console.log("3. Exit")

let userChoice: any = await inputData("choice");
let validInput = false;

switch (userChoice){
case "1":
// login
await auth.login();
if (auth.currentUserIndex !== -1) {
let data = auth.getData();
let currentUserIndex = auth.currentUserIndex;
// found login credentials
console.log(`\nHi ${data[auth.currentUserIndex].username}!`);

let currentUser = new User(
data[currentUserIndex].username,
data[currentUserIndex].password,
data.length + 1
);

let quitMenu = false;
do {
// Menu
do {
console.log("\nMenu")
console.log("1. Check Basket");
console.log("2. Check Store");
console.log("3. Top Up Balance");
console.log("4. Exit");
userChoice = await inputData("choice");

if (userChoice === "1" || userChoice === "2" || userChoice === "3" || userChoice === "4") {
validInput = true;
}

} while (!validInput)
validInput = false;

switch (userChoice) {
case "1":
currentUser.getBasketItem();
console.log("\nWant to checkout things?");
console.log("1. Yes");
console.log("2. No");
userChoice = await inputData("choice");

switch (userChoice){
case "1":
let chosenItem = await currentUser.checkoutPayment();
if (chosenItem !== -1) {
merchant.markPayment(currentUser.getUsername(), chosenItem);
}
break;
default:
}

break;
case "2":
console.log("\nSelect item to buy:");
storeItem.forEach((item) => {
console.log(`\t${item.item_id}. cost: ${item.cost}`);
})
userChoice = parseInt(await inputData("choice")) - 1;
if (userChoice > 5) {
console.log("Invalid input!");
}
else {
currentUser.makePayment(
storeItem[userChoice].cost,
storeItem[userChoice].item_id
)
merchant.addCustomer(
{
username: currentUser.getUsername(),
item_id: storeItem[userChoice].item_id,
total_cost: storeItem[userChoice].cost,
status: "unpaid",
}
)
console.log(`Successfully bought item with id ${storeItem[userChoice].item_id}\n`);
}
break;
case "3":
currentUser.topUpBalance();
break;
case "4":
quitMenu = true;
running = false;
break;
default:
}

} while (!quitMenu)

}
else {
console.log("Account not found!");
}
break;
case "2":
await auth.signUp();
break;
case "3":
running = false;
break;
default:
}

}
}

main().then(() => {
sleep(1000).then(() => {
console.log("See you next time!");

})
});

// solved TS2451
export {};
13 changes: 13 additions & 0 deletions src/lib/constant/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const storeItem = [
{item_id: 1, cost: 20000},
{item_id: 2, cost: 30000},
{item_id: 3, cost: 50000},
{item_id: 4, cost: 100000},
{item_id: 5, cost: 200000},
];

export const adminCredentials = {
username: "admin",
password: "test12345",
}

21 changes: 21 additions & 0 deletions src/lib/readline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as readline from "readline"
import { toCapitallize } from "./string_modifier";

/**
* Readline module built in Node
*/
export const stdin = readline.createInterface({
input: process.stdin,
output: process.stdout
});

/**
* input from console
* @async true
* @param user_data captured string from console
* @returns full string of user input
*/
export const inputData = async (user_data: string) => {
let userInput: string = await new Promise(resolve => stdin.question(`Enter ${user_data.toLowerCase()}: `, resolve));
return toCapitallize(userInput);
}
17 changes: 17 additions & 0 deletions src/lib/string_modifier.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Takes in full string and return the capitalized version
* @param str input string
* @returns capitalized string
* */
export const toCapitallize = (str: string) => {
let tempStr: string;
let rawStrArr = str.split(" ");
rawStrArr.forEach((ele, index) => {
tempStr = ele.toLowerCase();
let strArr = tempStr.split("");
strArr[0] = strArr[0].toUpperCase();
rawStrArr[index] = strArr.join("");
})
tempStr = rawStrArr.join(" ");
return tempStr;
}
10 changes: 10 additions & 0 deletions src/lib/time_sleep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Set time sleep for given milliseconds
* @param ms millis
* @returns timeout Promise
*/
export const sleep = async (ms: number) => {
return new Promise (resolve => setTimeout(resolve, ms));
}


Loading