Java Use throwing method
-
Good afternoon again, please don't kick hard and tell me where I'm making a mistake in teaching. There's a challenge.
Use
♪ Create MoneyTransactionUtil
♪ Create a private designer.
♪ Create a statistical method for sending money from one account to another: send To(Carta, Quarta, sum).
♪ The method should address several possible exceptional situations:
♪ If the number of the consignor's card and the recipient's map matches, need to throw RuntimeException away. (throw new RuntimeException())
♪ If the amount is zero, negative or more than 100 000, the RuntimeException must be discarded. (throw new RuntimeException())
♪ Add the keyword throws and RuntimeException to the proclamation of the method.
♪ If there are no exceptional situations, put it on screen: " Summa s, n is successfully transferred to m ' account "
public class MoneyTransactionUtil { private MoneyTransactionUtil() { }
public static double sendTo (int idSender, int idReciver, double money)throws RuntimeException {
try {
if (money > 0 & idReciver != idSender)
idReciver += money;
if (idReciver == idSender)
throw new RuntimeException("Отправитель равен получателю");
} catch (RuntimeException r) {
System.out.println(r.getMessage());
}
//return (int) idReciver;
return money;
}
I understand there's a slice to help me figure out what I'm thinking.
Thank you very much.
-
I think this code will help you:
public class MoneyTransactionUtil { public static void main(String[] args) { try { // Успешно sendTo(12345, 98765, 0.999); // Ошибка sendTo(12345, 12345, 55); } catch (RuntimeException r) { System.out.println(r.getMessage()); } }
public static void sendTo(int idSender, int idReceiver, double money) { if (idReceiver == idSender) throw new RuntimeException("Отправитель равен получателю"); if (money < 0 || money > 100_000) throw new RuntimeException("Некорректная сумма"); System.out.println("Сумма " + money + ", со счета " + idSender + " успешно переведена на счет " + idReceiver); } }
Here's the block.
try..catch
in the test methodmain
added check from assignment, function changed to method (voidnot the type returned. So you had a mistake trying to write the operator wrong. And (both) " instead "And also added money to the account number.I also want to add that
RuntimeException
Yes non-ver exceptThat's whythrows
Not necessarily after writing.