当前位置:网站首页>A simple membership card management system based on Scala
A simple membership card management system based on Scala
2022-06-26 16:53:00 【JayDen2001】
Scala It's a multi paradigm programming language , A similar Java Programming language , The original intention of the design is to realize scalable language 、 And integration of object-oriented programming and functional programming features , With Scala Language to design a simple system to further consolidate and learn its characteristics .
import scala.io.StdIn
import scala.collection.mutable.{Map, Set}
// Can be introduced anywhere Variable set
class root {
var map1 =Map("Root"->0)
var map2 =Map("Root"->" The super user ")
val user_ac_numbers=Set("[email protected]")
def get_ac_numbers_Email: Unit ={// Because I found that the contents of the collection are add after , Not updated to call filter Object of function , So it's unnecessary
var ac_numbers_Email=user_ac_numbers.filter(x => x.contains("@"))// utilize filter Filter out all accounts registered by email
println(s" Account number existing in the system < Register with mailbox >:${ac_numbers_Email}")
}
def total_account_quota(p:user):Unit={
map1=map1+(p.name->p.Account_balance)
println(map1)
}
def set_account_level(p:user): Unit ={
total_account_quota(p)
map1.keys.foreach(i=>{// Use foreach Function to define the mapping of user types
if(map1(i)<0) map2+=i->" Arrearage users " else if(map1(i)<100000) map2+=i->" Ordinary users " else map2+=i->"VIP user "
})// Set the level according to the size of the account amount , This project is similar to the storage and withdrawal of credit cards , If you recharge, it is similar to the overpayment of a credit card , If you withdraw, it is similar to a consumer credit card
println(map2)
}
def get_user_level(): Array[String]= {//flatMap Function and related contents of the mapping query the level of the input user
print(" Please enter the user you want to query ( And separated by spaces ):")
val name:String = StdIn.readLine().toString
val names=name.split(" ")
names.flatMap(map2.get(_))
}
}
import scala.io.StdIn
// Users can choose to register by name and phone number , The system will take the phone number as the account number
// Select name and email information to register , The system will take the email information as the account number
// If you add your name, phone number and email information to register , The system takes the telephone number as the account number first
// If the user does not set a password when registering , The system will give the account a default password 123456
class user (val name:String,val telnumber:String="NULL",val email_number:String="NULL"){
var ac_number="NULL"
var passwd: String="123456"
var Account_balance=0
def this (name: String, telnumber:String,email_number:String,passwd:String) {
this(name,telnumber,email_number)
this.passwd=passwd
}
ac_number=if(this.telnumber=="NULL") email_number else telnumber
def select_self_information: Unit = {
println(s" Your name is :${name}// Your account number is :${ac_number}// Your password is :${passwd}")
}
def select_Account_balance: Unit = {// Check the amount left in the account
println(s" honorific ${name} user , Your account number :${ac_number}, The remaining amount in the current account is :${Account_balance} element ")
}
def Recharge: Unit = {// Recharge operation
println("-------------------------------------------------------------")
select_Account_balance
print(" Please input the amount you want to recharge :")
val v1:Int = StdIn.readLine().toInt// By using the knowledge content of closures, the account balance can be changed
val Account_balance_recharge = (a: Int) =>Account_balance=Account_balance+a
Account_balance_recharge(v1)
select_Account_balance
}
def Withdrawal: Unit = {// Withdrawal operation
select_Account_balance
print(" Please enter the amount you want to withdraw :")
val v2:Int = StdIn.readLine().toInt
val Account_balance_Withdrawal = (b: Int) =>Account_balance=Account_balance-b
Account_balance_Withdrawal(v2)
select_Account_balance
println("-------------------------------------------------------------")
}
}
import scala.collection.mutable.Map
import scala.io.StdIn
object run {
def main(args: Array[String]): Unit = {
println(" ----------------------------- Welcome to ------------------------------------------")
println("| 1、 Entry project |")
println("| 2、 Exit project |")
println(" ----------------------------- Welcome to ------------------------------------------")
println("------------------------- Display some registered users -------------------------------------")
val r = new root
var p1=new user("Nico","17396583307")// By name + Register users by telephone
p1.select_self_information// Users can query their own information after registration
r.user_ac_numbers.add(p1.ac_number)// Take advantage of the non repeatability of the set , To get the account information of all users in the system
var p2=new user("YOYO",email_number="[email protected]")// By name + Register users by email
r.user_ac_numbers.add(p2.ac_number)
var p3=new user("Alex","13459562539","[email protected]","demodemo")// full name + Telephone + Register users by email
r.user_ac_numbers.add(p3.ac_number)
p3.select_self_information
println(" Please select the block you want to operate :")
val e:String = StdIn.readLine()
e match{// Pattern matching for module selection
case "1"=>
println("----------------------------- User operation section ---------------------------------------")
p3.Recharge// User's recharge operation
p3.Withdrawal// Withdrawal operation of the user
p1.Recharge
p2.Recharge
p3.Recharge
println("----------------------------- Background operation section ---------------------------------------")
println(s" Account number existing in the system :${r.user_ac_numbers}")// Get the registered account in the system
r.get_ac_numbers_Email// Get the registered account in the system ( Take email as account number )
r.set_account_level(p1)// Set the user type according to the user's current account amount
r.set_account_level(p2)
r.set_account_level(p3)
println(r.get_user_level().mkString(","))// Enter the user to query , Displays its user type
case "2"=>
println(" bye ! Thank you for using !")
System.exit(-1)
}
}
}}
Screenshot of operation result :



The knowledge points used in this project include building multiple auxiliary constructors 、 Closure 、 aggregate 、 Higher order function 、 Mapping, etc , Make use of their respective characteristics to achieve the target function most effectively , Especially in the process of using high-order functions to solve the realization of target functions , By constantly using a variety of higher-order functions , It not only reduces the code volume of the overall project , And it is more convenient to call . for example foreach、flatMap、filter These three common higher-order functions , It is very fast and convenient for data traversal and data filtering , For the deficiency of this system , I personally think there is a lack of abstract classes and abstract methods 、 Implicit transformation, etc , Although the general process of the whole project is not too wrong , But the control over the details of the whole system is still not in place , It gives people a feeling that they can't flexibly use various functions of the system , The function of the system is missing “ flexibility ”.
边栏推荐
- Qt 5.9.8 安装教程
- Count the number of words in a line of string and take it as the return value of the function
- proxy
- Teach you to learn dapr - 6 Publish subscription
- 《软件工程》期末重点复习笔记
- Greenplum database fault analysis - semop (id=2000421076, num=11) failed: invalid argument
- Calculate a=1, a2=1/1=a1
- Junit单元测试
- [Li Kou brush questions] 11 Container holding the most water //42 Rain water connection
- 建立自己的网站(16)
猜你喜欢

Cloud platform monitoring system based on stm32+ Huawei cloud IOT design

防火 疏散 自救…这场安全生产暨消防培训干货满满!

GUI+SQLServer考试系统

经典同步问题

Niuke programming problem -- dynamic programming of must brush 101 (a thorough understanding of dynamic programming)

7 views on NFT market prospect

无需人工先验!港大&同济&LunarAI&旷视提出基于语义分组的自监督视觉表征学习,显著提升目标检测、实例分割和语义分割任务!...

R329 (maix-ii-a (M2A) data summary

内存分区模型

进军AR领域,这一次罗永浩能成吗?
随机推荐
Summary of all knowledge points of C language
牛客编程题--必刷101之动态规划(一文彻底了解动态规划)
【小5聊】毕业8年,一直在追梦的路上
板卡的分级调试经验
[from deleting the database to running] the end of MySQL Foundation (the first step is to run.)
Develop operator based on kubebuilder (for getting started)
Stm32f103c8t6 realize breathing lamp code
QT 5.9.8 installation tutorial
Leetcode 1169. Query invalid transactions (if the amount of data is small, this problem still needs to be solved by violent enumeration)
Scala 基础 (二):变量和数据类型
对NFT市场前景的7个看法
Scala Foundation (2): variables et types de données
Niuke programming problem -- dynamic programming of must brush 101 (a thorough understanding of dynamic programming)
Kubecon China 2021 Alibaba cloud special session is coming! These first day highlights should not be missed
Find out the maximum value of each column element of NxN matrix and store it in the one-dimensional array indicated by formal parameter B in order
Codeforces Round #802 (Div. 2)
Teach you to learn dapr - 4 Service invocation
TCP congestion control details | 1 summary
MS | Xie Liwei group found that mixed probiotics and their metabolites could alleviate colitis
Multiply the values of the upper triangular elements of the array by M