【发布时间】:2022-01-02 15:29:04
【问题描述】:
这是问题:为市场创建程序。系统可以根据以下规则向消费者发送家庭卡并接受 0%(无折扣)、5% 或 10% 的折扣:
- 默认折扣选项是“无折扣”。
- 未注册用户没有折扣。未向未注册用户提供家庭卡。
如果注册用户在过去的食品购买上花费超过 1000 美元且购物次数少于 19 次,他们将获得 5% 的折扣。
-
如果注册用户在之前的杂货店购物时段中花费超过 1000 美元并且至少有 20 次杂货店购物时段,他们将获得 10% 的折扣。
-
只有消费超过 1000 美元或至少进行 20 次购物的注册用户才会收到一张家庭卡。
下面是我的代码
package Market;
import java.util.Scanner;
public class Lab5
{
public static void main(String[] args)
{
int spent=0;
int market_sessions=0;
Scanner scan =new Scanner(System.in);
System.out.printf("Amount spent by customer: ");
spent=Integer.parseInt(scan.nextLine());
Scanner scan2 =new Scanner(System.in);
System.out.printf("Marketsessions by the customer: ");
market_sessions=Integer.parseInt(scan2.nextLine());
int a = 0;
int type=userType(a);
if(type==1)
{
int discount=discounts(spent, market_sessions);
System.out.println("Discount: "+discount);
if(spent>1000 || market_sessions>=20)
System.out.println("Family card is sent to the customer");
}
else
{
System.out.println("Family cards and discounts are not given to unregistered users.");
}
}
public static int discounts(int spent, int market_sessions)
{
int discount=0;
if(spent>1000)
{
if(market_sessions<=19)
discount=5;
else
discount=10;
}
return discount;
}
public static int userType(int a)
{
System.out.println("Enter 1 or 2");
System.out.println("1 = Registered User");
System.out.println("2 = Unregistered User");
Scanner scan3 =new Scanner(System.in);
a=Integer.parseInt(scan3.nextLine());
while(a<1 || a>2) {
System.out.println("Enter 1 or 2");
System.out.println("1 = Registered User");
System.out.println("2 = Unregistered User");
Scanner scan4 =new Scanner(System.in);
a=Integer.parseInt(scan4.nextLine());
}
return a;
}
}
我的程序运行良好。它满足问题中的条件。但我的问题是,无论我多么努力,我都无法运行 Junit 测试。因此,例如,我正在编写方法,但我不知道如何测试它。我尝试了很多,但我无法使您的 Junit 测试的逻辑。我的程序有问题吗? Junit 是否停止测试我的程序?因为我从键盘上得到数字,是这样吗?我不应该使用扫描仪吗?你能用我的程序写一个 Junit 测试吗?
【问题讨论】:
标签: java unit-testing testing junit