私はセッターとゲッターのメインクラスの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;
}
}
あなたの具体的な質問は何ですか?何か試しましたか?ここの人々はおそらくあなたのために単体テストを書かないでしょう... – home
Junitを書いている最初の問題は、コードがメインブロックにあり、2番目にスキャナクラスがユーザ入力を使用していることです。 –