/
digit
/
java-oop-polymorphism-abstract
Обзор
Документация
Войти
/
digit
/
java-oop-polymorphism-abstract
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
task1/Main.java
110 строк
4 KB
digit
Create: Account.java, CreditAccount.java, Main.java, SimpleAccount.java, Logger.java, Main.java, SimpleLogger.java, SmartLogger.java
09 июл 2026, 16:50
Верифицирован
09 июл 2026, 16:50
e09293a
Код
Авторство
О чём код?
package task1; public class Main { public static void main(String[] args) { testSimpleAccount(); testCreditAccount(); testTransfer(); testEdgeCases(); System.out.println("All tests completed."); } private static void testSimpleAccount() { System.out.println("=== task1.SimpleAccount ==="); SimpleAccount account = new SimpleAccount(); check("Initial balance is 0", account.getBalance() == 0); check("Add 100 succeeds", account.add(100)); check("Balance is 100 after add", account.getBalance() == 100); check("Pay 30 succeeds", account.pay(30)); check("Balance is 70 after pay", account.getBalance() == 70); check("Pay 1000 fails (insufficient funds)", account.pay(1000)); check("Balance unchanged after failed pay", account.getBalance() == 70); check("Pay negative amount fails", account.pay(-10)); check("Add negative amount fails", account.add(-10)); check("Balance still unchanged", account.getBalance() == 70); check("Pay exact balance succeeds (goes to 0)", account.pay(70)); check("Balance is 0", account.getBalance() == 0); System.out.println(); } private static void testCreditAccount() { System.out.println("=== task1.CreditAccount ==="); CreditAccount account = new CreditAccount(100); // credit limit 100, balance 0 check("Initial balance is 0", account.getBalance() == 0); check("Pay 50 succeeds (goes negative within limit)", account.pay(50)); check("Balance is -50", account.getBalance() == -50); check("Pay 50 more succeeds (hits exact limit)", account.pay(50)); check("Balance is -100 (at credit limit)", account.getBalance() == -100); check("Pay 1 more fails (exceeds credit limit)", account.pay(1)); check("Balance unchanged at -100", account.getBalance() == -100); check("Add 100 succeeds", account.add(100)); check("Balance is back to 0", account.getBalance() == 0); check("Add 50 succeeds (goes positive)", account.add(50)); check("Balance is 50", account.getBalance() == 50); check("Pay 150 succeeds (50 balance + 100 credit limit)", account.pay(150)); check("Balance is -100", account.getBalance() == -100); System.out.println(); } private static void testTransfer() { System.out.println("=== Transfer ==="); SimpleAccount simple = new SimpleAccount(200); CreditAccount credit = new CreditAccount(0, 100); // balance 0, limit 100 check("Transfer 150 from simple to credit succeeds", simple.transfer(credit, 150)); check("Simple balance is 50", simple.getBalance() == 50); check("Credit balance is 150", credit.getBalance() == 150); check("Transfer 100 back from credit to simple succeeds", credit.transfer(simple, 100)); check("Credit balance is 50", credit.getBalance() == 50); check("Simple balance is 150", simple.getBalance() == 150); check("Transfer more than simple has fails (150 > 50 limit reach)", !credit.transfer(simple, 200)); check("Credit balance unchanged after failed transfer", credit.getBalance() == 50); check("Simple balance unchanged after failed transfer", simple.getBalance() == 150); check("Transfer to null account fails", simple.transfer(null, 10)); check("Transfer negative amount fails", simple.transfer(credit, -10)); System.out.println(); } private static void testEdgeCases() { System.out.println("=== Edge cases ==="); SimpleAccount zeroLimitLike = new SimpleAccount(0); check("Pay 0 from empty task1.SimpleAccount succeeds (no-op)", zeroLimitLike.pay(0)); check("Add 0 succeeds (no-op)", zeroLimitLike.add(0)); check("Balance still 0", zeroLimitLike.getBalance() == 0); CreditAccount zeroCredit = new CreditAccount(0); // no credit allowed check("Pay 1 with zero credit limit fails", zeroCredit.pay(1)); check("Pay 0 with zero credit limit succeeds", zeroCredit.pay(0)); System.out.println(); } private static void check(String description, boolean condition) { System.out.println((condition ? "PASS" : "FAIL") + " - " + description); } }