当前位置:网站首页>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 ”.

原网站

版权声明
本文为[JayDen2001]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202170502389216.html