Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PRIME CHECKER
- // Main.java
- public class Main {
- public static void main(String[] args) {
- System.out.println("Is 97 prime? " + PrimeChecker.isPrime(97));
- System.out.println("Is 12 prime? " + PrimeChecker.isPrime(12));
- System.out.println("");
- ShoppingCartTest.main(null);
- }
- }
- =============================================================================
- -----------------------------------------------------------------------------
- =============================================================================
- // PrimeChecker.java
- public class PrimeChecker {
- public static boolean isPrime(Integer digit) {
- if (digit == null || digit <= 1) {
- return false;
- }
- for (int i = 2; i <= Math.sqrt(digit); i++) {
- if (digit % i == 0) {
- return false;
- }
- }
- return true;
- }
- }
- =============================================================================
- -----------------------------------------------------------------------------
- =============================================================================
- // PrimeCheckerTest.java
- public class PrimeCheckerTest {
- public static void main(String[] args) {
- testNullInput();
- testNegativeInput();
- testZeroInput();
- testOneInput();
- testPrimeInput();
- testLargePrimeInput();
- testCompositeInput();
- }
- public static void testNullInput() {
- Integer input = null;
- boolean result = PrimeChecker.isPrime(input);
- assert !result : "Failed: Null input should return false";
- }
- public static void testNegativeInput() {
- Integer input = -10;
- boolean result = PrimeChecker.isPrime(input);
- assert !result : "Failed: Negative input should return false";
- }
- public static void testZeroInput() {
- Integer input = 0;
- boolean result = PrimeChecker.isPrime(input);
- assert !result : "Failed: Zero input should return false";
- }
- public static void testOneInput() {
- Integer input = 1;
- boolean result = PrimeChecker.isPrime(input);
- assert !result : "Failed: One input should return false";
- }
- public static void testPrimeInput() {
- Integer input = 2;
- boolean result = PrimeChecker.isPrime(input);
- assert result : "Failed: Prime input should return true";
- }
- public static void testLargePrimeInput() {
- Integer input = 97;
- boolean result = PrimeChecker.isPrime(input);
- assert result : "Failed: Large prime input should return true";
- }
- public static void testCompositeInput() {
- Integer input = 10;
- boolean result = PrimeChecker.isPrime(input);
- assert !result : "Failed: Composite input should return false";
- }
- }
- KOSZYK ZAKUPOWY
- // ShoppingCartTest.java
- import java.util.List;
- public class ShoppingCartTest {
- public static void main(String[] args) {
- testAddProduct();
- testRemoveProduct();
- testUpdateQuantity();
- testGetProducts();
- testGetTotalPrice();
- testApplyDiscountCode();
- testCheckout();
- System.out.println("All tests passed successfully.");
- }
- private static void testAddProduct() {
- ShoppingCart shoppingCart = new ShoppingCart();
- boolean result = shoppingCart.addProduct("Product1", 10.0, 2);
- // Assert
- assert result == true : "testAddProduct failed";
- System.out.println("testAddProduct passed");
- }
- private static void testRemoveProduct() {
- ShoppingCart shoppingCart = new ShoppingCart();
- shoppingCart.addProduct("Product1", 10.0, 2);
- boolean result = shoppingCart.removeProduct("Product1");
- // Assert
- assert result == true : "testRemoveProduct failed";
- System.out.println("testRemoveProduct passed");
- }
- private static void testUpdateQuantity() {
- ShoppingCart shoppingCart = new ShoppingCart();
- shoppingCart.addProduct("Product1", 10.0, 2);
- boolean result = shoppingCart.updateQuantity("Product1", 3);
- // Assert
- assert result == true : "testUpdateQuantity failed";
- System.out.println("testUpdateQuantity passed");
- }
- private static void testGetProducts() {
- ShoppingCart shoppingCart = new ShoppingCart();
- shoppingCart.addProduct("Product1", 10.0, 2);
- List<String> products = shoppingCart.getProducts();
- // Assert
- assert products.size() == 1 && products.contains("Product1") : "testGetProducts failed";
- System.out.println("testGetProducts passed");
- }
- private static void testGetTotalPrice() {
- ShoppingCart shoppingCart = new ShoppingCart();
- shoppingCart.addProduct("Product1", 10.0, 2);
- double totalPrice = shoppingCart.getTotalPrice();
- // Assert
- assert totalPrice == 20.0 : "testGetTotalPrice failed";
- System.out.println("testGetTotalPrice passed");
- }
- private static void testApplyDiscountCode() {
- ShoppingCart shoppingCart = new ShoppingCart();
- boolean result = shoppingCart.applyDiscountCode("DISCOUNT");
- // Assert
- assert result == true : "testApplyDiscountCode failed";
- System.out.println("testApplyDiscountCode passed");
- }
- private static void testCheckout() {
- ShoppingCart shoppingCart = new ShoppingCart();
- boolean result = shoppingCart.checkout();
- // Assert
- assert result == true : "testCheckout failed";
- System.out.println("testCheckout passed");
- }
- }
- =============================================================================
- -----------------------------------------------------------------------------
- =============================================================================
- // ShoppingCart.java
- import java.util.ArrayList;
- import java.util.List;
- class ShoppingCart implements ShoppingCartOperation {
- private List<String> products = new ArrayList<>();
- private double totalPrice = 0.0;
- @Override
- public boolean addProduct(String productName, double price, int quantity) {
- products.add(productName);
- totalPrice += price * quantity;
- return true;
- }
- @Override
- public boolean removeProduct(String productName) {
- if (products.contains(productName)) {
- products.remove(productName);
- return true;
- }
- return false;
- }
- @Override
- public boolean updateQuantity(String productName, int newQuantity) {
- return true;
- }
- @Override
- public List<String> getProducts() {
- return products;
- }
- @Override
- public double getTotalPrice() {
- return totalPrice;
- }
- @Override
- public boolean applyDiscountCode(String discountCode) {
- return true;
- }
- @Override
- public boolean checkout() {
- System.out.println("Zamówienie zrealizowane. Potwierdzenie zostanie wysłane na podany adres email.");
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement