2017-10-26 6 views
0

私は新しいコーディングに慣れていて、私はScalaで学んでいます。私の練習は下ですが、私のコードはあまりにも冗長であると感じています。どうすれば改善できますか?冗長な条件付き実装が簡素化

演習:

*インターネット電話サービスプロバイダは、インターネットに接続されていないコンピュータや定期的な電話間の呼び出しサービスを提供しています。毎月の定期購読や有料のサービスを利用するかどうか、彼らがどの国を作るのかなど、1か月以内に話すことが予想される分数に基づいて通話コストを決定するのに役立つスクリプトを書くこれらの電話は、携帯電話と固定電話の両方に電話をかけているのか、固定電話だけに電話をかけているのか、あなたの仕事であなたを助けるために、以下の情報を使用します。

Table showing rates

ルイス、マーク・C; Lacher、Lisa。 Scala、第2版(コンピューティングのHall/CRC教科書)(ページ74)を使用したプログラミングと問題解決の入門。 CRC Press。ペーパーバック*

import scala.io.StdIn._ 

object callingCost { 
    def main(args: Array[String]): Unit = { 
//user input 
    print("Will you be using (1)Monthly or (2)Pay-As-You-Go ") 
    val planType = readInt() 
    print("What country will you be calling? ") 
    val country = readLine() 
    print("How many minutes would you talk per month? ") 
    val minsMonth = readInt() 
    print("Will you be calling both Mobile and Landlines as opposed to just landlines? (Y/N) ") 
    val lineType = readLine() 
//declare monthly subscription cost per country 
    val chinaCost120 = 1.19 
    val chinaCost400 = 3.89 
    val chinaCost800 = 7.79 
    val chinaUnltd = 13.99 
    val indiaCost120 = 1.42 
    val indiaCost800 = 8.99 
    val indiaUnltd = 19.99 
    val ukMobLand = 2.09 
    val ukLand = 1.19 
    val mexicoUnltd = 1.79 
    val usaUnltd = 2.99 
    //declare pay as you go cost per country 
    val chinaPaygI = 0.02 
    val indiaPaygI = 0.015 
    val usaPaygI = 0.023 
    val mexicoPaygMl = 0.035 
    val mexicoPaygL = 0.01 
    val ukPaygMl = 0.10 
    val ukPaygL = 0.023 
//monthly plan evaluation 
    val cost = if (planType == 1) { 
    if (country == "China") { 
     if (minsMonth <= 120) chinaCost120 
     else if (minsMonth > 120 && minsMonth <= 400) chinaCost400 
     else if (minsMonth > 400 && minsMonth <= 800) chinaCost800 
     else chinaUnltd 
    } 
    else if (country == "India") { 
     if (minsMonth <= 120) indiaCost120 
     else if (minsMonth > 120 && minsMonth <= 800) indiaCost800 
     else indiaUnltd 
    } 
    else if (country == "UK") { 
     if (lineType == "Y") ukMobLand 
     else ukLand 
    } 
    else if (country == "Mexico") mexicoUnltd else usaUnltd 
} else { //Pay-As-You-Go section 
    if (country == "China") minsMonth*chinaPaygI 
    else if (country == "India") minsMonth*indiaPaygI 
    else if (country == "USA") minsMonth*usaPaygI 
    else { 
    if (country == "Mexico" && lineType == "Y") minsMonth*mexicoPaygMl 
    else if (country == "Mexico" && lineType == "N") minsMonth*mexicoPaygL 
    else if (country == "UK" && lineType == "Y") minsMonth*ukPaygMl else minsMonth*ukPaygL 
    } 
} 
    println(s"Your projected cost per month is $cost") 
    } 
} 
+0

まあ、私はケースクラスは、私は検討したいあなたimporvements – Pavel

+0

ための良い出発点だと思いますケースクラスとパターンマッチングのデコンストラクションです。 – P3trur0

+0

https://codereview.stackexchange.com/で投稿してみることもできます。もっと詳細な情報を提供できるはずです。 – Shaido

答えて

0

あなただけの単純なパターンマッチングとかなり物事をクリーンアップすることができます。

object callingCost { 
    def main(args: Array[String]): Unit = { 
    //user input 
    print("Will you be using (1)Monthly or (2)Pay-As-You-Go ") 
    val planType = readInt() 
    print("What country will you be calling? ") 
    val country = readLine() 
    print("How many minutes would you talk per month? ") 
    val minsMonth = readInt() 
    print("Will you be calling both Mobile and Landlines as opposed to just landlines? (Y/N) ") 
    val lineType = readLine() 

    //declare monthly subscription cost per country 
    val cost = (planType, country, minsMonth, lineType) match { 
     case (1, "China", m, _) if m <= 120 => 1.19 
     case (1, "China", m, _) if m <= 400 => 3.89 
     case (1, "China", m, _) if m <= 800 => 7.79 
     case (1, "China", _, _)    => 13.99 
     case (1, "India", m, _) if m <= 120 => 1.42 
     case (1, "India", m, _) if m <= 800 => 8.99 
     case (1, "India", _, _)    => 19.99 
     case (1, "UK", _, "Y")    => 2.09 
     case (1, "UK", _, _)    => 1.19 
     case (1, "Mexico", _, _)   => 1.79 
     case (1, "USA", _, _)    => 2.99 
     case (2, "China", m, _)    => m * 0.02 
     case (2, "India", m, _)    => m * 0.015 
     case (2, "USA", m, _)    => m * 0.023 
     case (2, "Mexico", m, "Y")   => m * 0.035 
     case (2, "Mexico", m, "N")   => m * 0.01 
     case (2, "UK", m, "Y")    => m * 0.10 
     case (2, "UK", m, _)    => m * 0.023 
    } 

    println(s"Your projected cost per month is $cost") 
    } 
} 
+0

完璧です@ adrice727ありがとうございます!!!!また、私は今事件を理解することができます。本からちょうど学ぶのは簡単ではありません!あなたはそれに値する勉強のために私の夜を完全にしました。 – NoobScala

関連する問題