Skip to content

Instantly share code, notes, and snippets.

@amoozeshbebin
Created November 4, 2023 10:29
Show Gist options
  • Select an option

  • Save amoozeshbebin/2d6df57e8c24e13f1970a39ed4ffc3d0 to your computer and use it in GitHub Desktop.

Select an option

Save amoozeshbebin/2d6df57e8c24e13f1970a39ed4ffc3d0 to your computer and use it in GitHub Desktop.
inventory console project
file Main.kt:
var productList = mutableListOf<Product>()
fun main(args: Array<String>) {
val handler = ProductHandler()
val reader = ReadFileCoroutine()
val writer = WriteFileCoroutine()
reader.read {
handler.fromJson(it)
}
try {
while (true) {
when (Menu.getMenu()) {
1 -> {
handler.showAllProducts()
}
2 -> {
Actions.insertProduct(handler,writer)
}
3 -> {
Actions.increaseProductCount(handler,writer)
}
4 -> {
Actions.decreaseProductCount(handler,writer)
}
5 -> {
Actions.deleteProduct(handler,writer)
}
0 -> {
println("You are logged out")
return
}
}
}
}
catch (e:NotFoundExeption){
println(e.message)
}
catch (e:Exception){
println(e.message)
}
}
class ReadFileCoroutine:
class ReadFileCoroutine {
private var fileName = "C:\\\\Users\\PC\\intellij projects\\inventory\\1.txt"
fun read(callback: (String) -> Unit){
GlobalScope.launch(Dispatchers.IO) {
FileUtility.readFile(fileName,callback)
}
}
}
class Actions:
r description = readLine()!!
println("Price : ")
var price = readLine()?.toInt()!!
println("Quantity : ")
var quantity = readLine()?.toInt()!!
var newProduct = Product(sku, name, description, price, quantity)
handler.addProduct(newProduct)
println("Product inserting dose Successfully!")
writer.write(handler.toJson()!!)
}
fun increaseProductCount(handler: ProductHandler, writer: WriteFileCoroutine) {
println("SKU : ")
var sku = readLine()!!
println("Count : ")
var count = readLine()?.toInt()!!
if (handler.increaseQuantity(sku, count)) {
println("increased Product count dose Successfully")
writer.write(handler.toJson()!!)
} else {
println("Error in product increased:0x0000001 ")
}
}
fun decreaseProductCount(handler: ProductHandler, writer: WriteFileCoroutine) {
println("SKU : ")
var sku = readLine()!!
println("Count : ")
var count = readLine()?.toInt()!!
if (handler.decreaseQuantity(sku, count)) {
println("decreased Product count dose Successfully")
writer.write(handler.toJson()!!)
} else {
println("Error in product decreased:0x0000001 ")
}
}
fun deleteProduct(handler: ProductHandler, writer: WriteFileCoroutine) {
println("SKU : ")
var sku = readLine()!!
handler.removeProduct(sku)
println("Product delete successfully!")
writer.write(handler.toJson()!!)
}
}
}
class ProductHandler:
class ProductHandler {
companion object {
private var productList = mutableListOf<Product>()
}
fun showAllProducts() {
if (productList.isEmpty()) println("There are no products")
productList.forEach { println(it) }
}
fun toJson(): String? {
val gson = Gson()
return gson.toJson(productList)
}
fun fromJson(json: String): List<Product> {
val gson = Gson()
val listType: Type = object : TypeToken<MutableList<Product>>() {}.type
var result = gson.fromJson<List<Product>>(json, listType)
productList.clear()
productList.addAll(result)
return productList
}
fun addProduct(product: Product) {
productList.add(product)
}
fun removeProduct(sku: String) {
productList.removeIf { it.sku == sku }
}
fun getProduct(sku: String): Product? {
return productList.first { it.sku == sku }
}
fun increaseQuantity(sku: String, count: Int): Boolean {
var product = getProduct(sku) ?: throw Exception("There is no product with this SKU")
product.quantity += count
return true
}
fun decreaseQuantity(sku: String, count: Int): Boolean {
var product = getProduct(sku) ?: throw NotFoundExeption("There is no product with this SKU")
if (product.quantity - count < 0) {
throw Exception("تعداد موجود در انبار از $count کمتر است ")
}
product.quantity -= count
return true
}
}
data class Product:
data class Product(
var sku:String,
var name:String,
var description:String,
var price:Int,
var quantity:Int
){
fun toJson(): String? {
var gson = Gson()
return gson.toJson(this)
}
companion object{
fun fromJson(json: String): Product {
var gson = Gson()
return gson.fromJson(json, Product::class.java)
}
}
}
class NotFoundExeption:
class NotFoundExeption(messege:String) : Exception(messege) {
}
class WriteFileCoroutine:
class WriteFileCoroutine {
private var fileName = "C:\\\\Users\\PC\\intellij projects\\inventory\\1.txt"
fun write(text:String){
GlobalScope.launch(Dispatchers.IO) {
FileUtility.write(fileName,text)
}
}
fun append(text:String){
GlobalScope.launch(Dispatchers.IO) {
FileUtility.appendToFile(fileName,text)
}
}
}
class Menu:
class Menu {
companion object{
fun getMenu():Int {
println("select options")
println("[1] -> Show Available Products")
println("[2] -> Add New product")
println("[3] -> increased inventory Product")
println("[4] -> decreased inventory Product")
println("[5] -> delete Product")
println("[0] -> Exit the Console")
return readLine()?.toInt()!!
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment