2017-06-11 4 views
0

私はセッターとゲッターのメインクラスのBasket1とItemを持っています。私はプロジェクトをテストするためにJUNITを書く必要があります。 JUnitテスト関数は、スキャンテストまたはゲッターセッターと、Itemの合計になります。このバスケット用のJunitの書き方1クラスはMAINクラスのスキャナを含む

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; 
    } 
} 
+0

あなたの具体的な質問は何ですか?何か試しましたか?ここの人々はおそらくあなたのために単体テストを書かないでしょう... – home

+0

Junitを書いている最初の問題は、コードがメインブロックにあり、2番目にスキャナクラスがユーザ入力を使用していることです。 –

答えて

0

の代わりにあなたが(Basketクラスに関連する他の方法と一緒に)addItem()getSum()のような計算方法を持つ新しいクラスBasketを追加する必要がありますmain()メソッド内バスケットのコードを書きます。この新しいクラスには、ユーザの入力や出力メッセージはなく、アイテムや計算のためのコンテナだけです。

あなたが実際に何かを持っているときは、JUnitテストを書くことができます。 1つのテスト方法は、「新しいバスケットを作成し、いくつかの時間を追加して、アイテムの合計が期待されるものと同じであることを確認する」ことです。他のテストは、「量が正でないときに例外をスローする」、または1つの引数がnullのときに例外をスローするようなものです。

関連する問題