简单的折扣

时间:2016-07-22 11:23:42

标签: java discount

参加考试并遇到问题,我是初学者。 它是一个折扣计算器。 一袋咖啡的价格为3.75欧元 10袋或以上5折优惠 20袋或以上10%折扣

到目前为止我有什么

grep -ir client_ *

} }

3 个答案:

答案 0 :(得分:3)

  

if(bag> = 10&&< 20)

  

if(bag> = 10&& bag < 20)

初学者容易犯的错误! 在学习基础知识的同时,我有点赞同编辑与编辑的讲师。您需要学习阅读编译器告诉您错误的内容。一旦你知道了这个问题,我想你会同意,上面的语法错误信息很好地说明了什么是错误的。

答案 1 :(得分:2)

我通常不赞成为Tom Sawyer学生做作业,但在这种情况下,我认为任何能够以不同方式研究问题的初学者都有这样的教育机会。

这是一个简单的课程,但如果你想成为一名程序员,养成良好的习惯是一个好主意。

非常注意代码格式和可读性。对类,方法和变量的名称进行长时间的考虑。如果名称不言自明,则需要较少的评论。

了解并关注Java coding standards。这将有助于提高可读性。

忘记输入和输出以及用户界面 - 首先获得正确的功能。

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * CoffeePriceCalculator
 * User: mduffy
 * Date: 7/22/2016
 * Time: 7:46 AM
 * @link http://stackoverflow.com/questions/38525213/simple-discount
 */
public class CoffeePriceCalculator {

    // Is double a good way to represent money?  What about units?  Conversions?
    public static final double DEFAULT_UNIT_PRICE = 3.75;  // British pounds

    private static final Map<Integer, Double> DISCOUNT_PERCENTAGE;

    static {
        DISCOUNT_PERCENTAGE = new LinkedHashMap<>();
        DISCOUNT_PERCENTAGE.put(10, 0.05);
        DISCOUNT_PERCENTAGE.put(20, 0.10);
    }

    public double calculatePrice(int numBags, double unitPrice) {
        if (numBags < 0) throw new IllegalArgumentException("Number of bags must be positive");
        if (unitPrice < 0.0) throw new IllegalArgumentException("Unit price must be positive");
        double price = numBags*unitPrice;
        price -= calculateDiscount(numBags, price);
        return price;
    }

    public double calculatePrice(int numBags) {
        return this.calculatePrice(numBags, DEFAULT_UNIT_PRICE);
    }

    public double calculateDiscount(int numBags, double price) {
        if (numBags < 0) throw new IllegalArgumentException("Number of bags must be positive");
        if (price < 0.0) throw new IllegalArgumentException("Total price must be positive");
        double discount = 0.0;
        for (int minBags : DISCOUNT_PERCENTAGE.keySet()) {
            if (numBags >= minBags) {
                discount = price*DISCOUNT_PERCENTAGE.get(minBags);
                break;
            }
        }
        return discount;
    }
}

了解JUnit和测试驱动开发还为时尚早。

import org.junit.Assert;
import org.junit.Test;

/**
 * Junit test for CoffeePriceCalculator
 * User: mduffy
 * Date: 7/22/2016
 * Time: 7:50 AM
 * @link http://stackoverflow.com/questions/38525213/simple-discount
 */
public class CoffeePriceCalculatorTest {

    public static final double TOLERANCE = 1.0e-3;

    @Test
    public void testCalculatePrice_NoDiscount() {
        // setup
        CoffeePriceCalculator coffeePriceCalculator = new CoffeePriceCalculator();
        int numBags = 5;
        // exercise
        double actual = coffeePriceCalculator.calculatePrice(numBags);
        // assert
        Assert.assertEquals(numBags * CoffeePriceCalculator.DEFAULT_UNIT_PRICE, actual, TOLERANCE);
    }

    @Test
    public void testCalculatorPrice_LowDiscount() {
        // setup
        CoffeePriceCalculator coffeePriceCalculator = new CoffeePriceCalculator();
        int numBags = 15;
        // exercise
        double actual = coffeePriceCalculator.calculatePrice(numBags);
        // assert
        Assert.assertEquals(numBags * CoffeePriceCalculator.DEFAULT_UNIT_PRICE*0.95, actual, TOLERANCE);
    }

    @Test
    public void testCalculatorPrice_HighDiscount() {
        // setup
        CoffeePriceCalculator coffeePriceCalculator = new CoffeePriceCalculator();
        int numBags = 25;
        // exercise
        double actual = coffeePriceCalculator.calculatePrice(numBags);
        // assert
        Assert.assertEquals(numBags * CoffeePriceCalculator.DEFAULT_UNIT_PRICE*0.90, actual, TOLERANCE);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testCalculatePrice_NegativeBags() {
        // setup
        CoffeePriceCalculator coffeePriceCalculator = new CoffeePriceCalculator();
        int numBags = -25;
        // exercise
        coffeePriceCalculator.calculatePrice(numBags);
    }
}

答案 2 :(得分:0)

希望您的考试不会过早出现,因为您需要查看大量语法。

我真的不喜欢直接回答家庭作业的问题,所以我会修复你现在的问题,也许这会指出你正确的方向。

Import.java.util.scanner;

public class discount {
  public static void main (String[] args){

    //Scanner input; Keep this as one line. It is good practice
    Scanner input = new Scanner(System.in);
    //Float bag;
   int bag;
    Float discount;
    Double cost = 3.75;

    //Input = new Scanner(System.ini);     combined with line above.
    System.out.println("Enter Numer of bag");
    bag = input.nextFloat();
    //If (bag ==>10&&<20) × .05  incorrect Java syntax

    //if(bag >= 10 && < 20) {
    if(bag >= 10 && bag < 20) {
     discount = 0.05;
    }
    else if(bag >= 20) {
      discount = 0.10;
    } 
    else {
      //discount = 0;
      discount = 0.0;
    }

    double finalPrice;
    finalPrice = (cost * bag) * (1 - discount);

    //System.out.println("You ordered " + bag + " bags of coffee.);
    System.out.println("You ordered " + bag + " bags of coffee.");
    System.out.println("Your dicount is " + discount + "%");
    System.out.println("Your total is: " + finalPrice);
  }
}

请记住保持代码清洁,这样就不会让自己感到困惑。确保缩进代码。

考虑一下你为变量命名的内容,如果你想出更好的东西,不要害怕重构它们。使用清晰的逻辑命名将减少您需要编写的注释数量。

正如我之前提到的,研究你的语法。您的代码中存在大量语法错误,这让我相信您可能正在编写记事本或命令行中的代码?如果是这样,我建议使用诸如IntelliJ,NetBeans或Eclipse之类的IDE。这些将强调您的语法错误,并指出您可能不会立即注意到的错误。