如何为这个Basket1类编写Junit包括MAIN类中的扫描程序

时间:2017-06-11 11:45:23

标签: java junit

我有一个主要的课程篮子1和项目的setter和getter,请帮我写这个项目的Junit。我必须写一个JUNIT来测试我的项目。 JUnit测试函数可以是Scan测试或getter setter,也可以是Item的总和。

package Basket;

import java.util.ArrayList;
import java.util.Scanner;
import java.text.NumberFormat;

class Basket1 {

    public static void main(String[] args) {

        ArrayList<Item> Cart = new ArrayList<Item>();
        Item item;
        String itemName;
        double itemPrice;
        int quantity;
        Scanner scan = new Scanner(System.in);

        double totalPrice = 0.0;
        double sum = 0.0;

        String keepShopping = "y";

        do {
            System.out.print("Enter the name of the item: ");
            itemName = scan.nextLine();
            System.out.print("Enter the unit price: ");

            Double n1 = 0.0;
            boolean bError = true;
            while (bError) {
                if (scan.hasNextDouble())
                    n1 = scan.nextDouble();
                else {
                    scan.next();
                    System.out.print("Please Enter valid Price");
                    continue;
                }

                bError = false;
            }
            itemPrice = n1;
            System.out.print("Enter the quantity: ");

            int n2 = 0;
            boolean tError = true;
            while (tError) {
                if (scan.hasNextInt())
                    n2 = scan.nextInt();
                else {
                    scan.next();
                    System.out.print("Please Enter valid Unit");
                    continue;
                }

                tError = false;
            }
            quantity = n2;

            item = new Item(itemName, itemPrice, quantity);
            Cart.add(item);

            for (int i = 0; i < Cart.size(); i++) {
                Item itm = Cart.get(i);
                System.out.println(itm);
            }

            System.out.print("You want to continue? Y/N ");
            scan.nextLine();
            keepShopping = scan.nextLine();
        } while (keepShopping.equals("y"));

        scan.close();
        for (int i = 0; i < Cart.size(); i++) {
            Item itm = Cart.get(i);
            System.out.println(itm);
            totalPrice = itm.getQuantity() * itm.getPrice();
            sum += totalPrice;
        }

        NumberFormat fmt = NumberFormat.getCurrencyInstance();

        System.out.println("The total price is: " + fmt.format(sum));

    }

}


package Basket;

import java.text.NumberFormat;

public class Item {
    private String name;
    private double price;
    private int quantity;

    public Item(String itemName, double itemPrice, int numPurchased) {
        name = itemName;
        price = itemPrice;
        quantity = numPurchased;
    }

    public String toString() {
        NumberFormat fmt = NumberFormat.getCurrencyInstance();

        return (name + "\t" + fmt.format(price) + "\t" + quantity + "\t" + fmt.format(price * quantity));
    }

    public double getPrice() {
        return price;
    }

    public String getName() {
        return name;
    }

    public int getQuantity() {
        return quantity;
    }
}

1 个答案:

答案 0 :(得分:0)

您不应在main()方法中编写购物篮代码,而应添加新的类Basket,其中包含addItem()getSum()等计算方法(以及其他相关方法)对于Basket类)。这个新类不会有任何用户输入或输出消息,只是您的项目和计算的容器。

当你有这样一个类时,你实际上可以编写JUnit测试。一种测试方法是&#34;创建一个新的篮子,添加一些时间,检查项目的总和是否与预期的一个相同&#34;。其他测试就像&#34;当数量不是正数时会抛出异常&#34;或者&#34;当一个参数为null&#34;。

时抛出异常