/
GermanezZ
/
JavaExercises
Обзор
Документация
Войти
/
GermanezZ
/
JavaExercises
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
AllMetanitLessons/Generics/Program.java
154 строки
3 KB
germa
Уроки по Metanit
04 дек 2024, 20:39
04 дек 2024, 20:39
48d575a
Код
Авторство
О чём код?
package AllMetanitLessons.Generics; import java.util.Objects; public class Program { /* public static void main(String[] args) { Account acc1 = new Account("Stepashka",50_000); Account acc2 = new Account("GeRman",0); Transaction<Account> trans = new Transaction<Account>(acc1,acc2,20_000); trans.execute(); trans = new Transaction<>(acc2,acc1,10_000); trans.execute(); } } class Account{ private String id; private int sum; Account(String id, int sum){ this.id = id; this.sum = sum; } public String getId() { return id; } public int getSum() { return sum; } public int setSum(int sum) { return this.sum = sum; } } class DepositAccount <T> extends Account{ private T type; public T getType(){ return type; } DepositAccount(String id, T type, int sum) { super(id, sum); this.type = type; } } class Transaction<T extends Account>{ private T from; private T to; private int sum; Transaction(T from, T to, int sum){ this.from = from; this.to = to; this.sum = sum; } public void execute(){ if(from.getSum()>sum){ from.setSum(from.getSum() - sum); to.setSum(to.getSum() + sum); System.out.printf("Operation successful\nAccount %s: %d \nAccount %s: %d \n", from.getId(), from.getSum(),to.getId(), to.getSum()); } else{ System.out.println("Operation is invalid"); } } }*/ public static void main(String[] args) { Account<String> Stepasha = new Account<>("Stepasha",90_000); Account<Integer> Unknown = new Account<>(1234,0); Account<String> German = new Account<>("German", 80_000); try { Transaction transaction = new Transaction(Stepasha,Unknown,50_000); transaction.execute(); transaction.Info(); Transaction transaction1 = new Transaction(Unknown,German,50_000); transaction1.execute(); transaction1.Info(); }catch (Exception ex){ System.out.println(ex.getMessage()); } } } class Account<T>{ private T id; private int sum; public Account(T id, int sum) { this.id = id; this.sum = sum; } public T getId() { return id; } public void setId(T id) { this.id = id; } public int getSum() { return sum; } public void setSum(int sum) { this.sum = sum; } } class Transaction<T extends Account<T>>{ private T from; private T to; private int sum; public Transaction(T from, T to, int sum) { this.from = from; this.to = to; this.sum = sum; } public void execute() throws Exception { if (from.getSum()>= sum){ from.setSum(from.getSum()- sum); to.setSum(to.getSum() + sum); }else throw new Exception("Недостаточно денег на счёте!"); } public void Info(){ System.out.printf("Account %s has %d\tAccount %s has %d\n",from.getId(), from.getSum(), to.getId(), to.getSum()); } }