当前位置:网站首页>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 ”.
边栏推荐
- 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
- The student record consists of student number and academic performance. The data of n students have been stored in the a structure array to find out the student record with the lowest performance
- Gui+sqlserver examination system
- Qt 5.9.8 安装教程
- The function keeps the value of variable H to two decimal places and rounds the third digit
- Kubecon China 2021 Alibaba cloud special session is coming! These first day highlights should not be missed
- What is the preferential account opening policy of securities companies now? Is it safe to open an account online now?
- 108. simple chat room 11: realize client group chat
- How can I get the stock account opening discount link? Is online account opening safe?
- 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
猜你喜欢

GUI+SQLServer考试系统

5G未平6G再启,中国引领无线通信,6G的最大优势在哪里?

Développer un opérateur basé sur kubebuilder (démarrer)

Web3 decentralized storage ecological landscape

pybullet机器人仿真环境搭建 5.机器人位姿可视化

5g is not flat and 6G is restarted. China leads wireless communication. What is the biggest advantage of 6G?

Apache APIs IX has the risk of rewriting the x-real-ip header (cve-2022-24112)

Sandboxed container: container or virtual machine

建立自己的网站(16)
Scala Foundation (2): variables et types de données
随机推荐
Apache APIs IX has the risk of rewriting the x-real-ip header (cve-2022-24112)
When a programmer is disturbed 10 times a day, the consequences are amazing!
Scala 基础 (二):变量和数据类型
Swap two numbers
Junit单元测试
Day10 daily 3 questions (1): sum gradually to get the minimum value of positive numbers
What is the preferential account opening policy of securities companies now? Is it safe to open an account online now?
Community ownership of NFT trading market is unstoppable
TCP congestion control details | 1 summary
Cuckoo filter for Chang'an chain transaction
Vibrating liquid quantity detecting device
Can Luo Yonghao succeed in entering the AR field this time?
# 补齐短板-开源IM项目OpenIM关于初始化/登录/好友接口文档介绍
Fgetc() reads content from file
The function keeps the value of variable H to two decimal places and rounds the third digit
Day10 daily 3 questions (3): String Matching in array
安信证券排名第几位?开户安全吗?
Redis overview
Toupper function
JS tutorial electron JS is a good tool for designing powerful multi platform desktop applications