当前位置:网站首页>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 ”.
边栏推荐
- 去中心化NFT交易协议将击败OpenSea
- Calculate the average of N numbers in the group indexed by the formal parameter x, move the data less than the average in the group indexed to the front of the array, and move the data greater than or
- num[i]++
- In a bad mood, I just write code like this
- Apache APIs IX has the risk of rewriting the x-real-ip header (cve-2022-24112)
- 100+ data science interview questions and answers Summary - basic knowledge and data analysis
- proxy
- LeetCode Algorithm 24. Exchange the nodes in the linked list in pairs
- Stm32f103c8t6 realize breathing lamp code
- Greenplum数据库故障分析——semop(id=2000421076,num=11) failed: invalid argument
猜你喜欢

建立自己的网站(16)

MS | Xie Liwei group found that mixed probiotics and their metabolites could alleviate colitis

Make up the weakness - Open Source im project openim about initialization / login / friend interface document introduction

Kubecon China 2021 Alibaba cloud special session is coming! These first day highlights should not be missed

当一个程序员一天被打扰 10 次,后果很惊人!

Sandboxed container: container or virtual machine

牛客编程题--必刷101之动态规划(一文彻底了解动态规划)

C language --- basic function realization of push box 01

Classical synchronization problem

Kubernetes essential tools: 2021
随机推荐
并发编程整体脉络
GUI+SQLServer考试系统
C language -- legal identifier and integer
架构实战营毕业设计
QT 5.9.8 installation tutorial
【毕业季】致毕业生的一句话:天高任鸟飞,海阔凭鱼跃
Convert the decimal positive integer m into the number in the forward K (2 < =k < =9) system and output it in bits
国内首款开源 MySQL HTAP 数据库即将发布,三大看点提前告知
Calculate the average of N numbers in the index group of X, and return the number that is less than the average and closest to the average through formal parameters
What is the difference between digital collections and NFT
Dialogue with the senior management of Chang'an Mazda, new products will be released in Q4, and space and intelligence will lead the Japanese system
Call the random function to generate 20 different integers and put them in the index group of institute a
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
Data analysis - numpy quick start
C语言 头哥习题答案截图
Teach you to learn dapr - 1 The era of net developers
[matlab project practice] prediction of remaining service life of lithium ion battery based on convolutional neural network and bidirectional long short time (cnn-lstm) fusion
Memory partition model
num[i]++
建立自己的网站(16)