0
try-catch 블록으로이 코드를 묶으려고합니다. 그러나, 많은 실패한 시도에 나는 아직도 사용자가 0보다 적은 균형을 입력 할 때를 위해 나의 주문 예외 메시지를 던지기 위하여 그것을 기울인다 얻는다. 그것을 배우기 목적으로 극단적으로 간단하게 유지하는 것을 시도하는 Im. IOException 및 InvalidException 던지는 내 주요 던지지만 사용자 지정 예외 메시지를 호출하는 방법을 알아낼 것. 코드를 삭제하여 시청자가 코드를보다 단순하게 보입니다. 이 문제를 해결하는 방법에 대한 모든 논리 조언? 모든 의견을 보내 주시면 감사하겠습니다.try-catch 블록, 파일 작성자 및 인쇄기를 사용하여 Java에서 사용자 정의 클래스 만들기
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
public class TestAccountWithException
{
public static void main(String[] args) throws IOException, InvalidBalanceException
{
// variable declaration
String fileName;
String firstName;
String lastName;
double balance;
int id = 1122;
final double RATE = 4.50;
Scanner input = new Scanner(System.in);
System.out.print("Please enter file name: ");
fileName = input.next();
File fw = new File(fileName);
// check if file already exists
while(fw.exists())
{
System.out.print("File already exists, enter valid file name: ");
fileName = input.next();
fw = new File(fileName);
}
System.out.print("Enter your first name: ");
firstName = input.next();
System.out.print("Enter your last name: ");
lastName = input.next();
// combine first and last name
String fullName = firstName.concat(" " + lastName);
System.out.print("Input beginnning balance: ");
balance = input.nextDouble();
// pass object to printwriter and use pw to write to the file
PrintWriter pw = new PrintWriter(fw);
// print to created file
pw.println(firstName);
pw.println(lastName);
pw.println(balance);
pw.println(id);
pw.println(RATE);
AccountWithException acctException = new AccountWithException(fullName, balance, 1122, 4.50);
System.out.println(acctException.toString());
} // end main
} // end class
// Account with exception class
public class AccountWithException {
private int id;
private double balance;
private static double annualInterestRate;
private java.util.Date dateCreated;
// CRE additions for lab assignment
private String name;
// no-arg constructor to create default account
public AccountWithException() {
this.dateCreated = new java.util.Date();
}
// constructor for test account with exception that takes in arguments
public AccountWithException(String newName, double newBalance, int newId, double newRate) {
this.name = newName;
this.balance = newBalance;
this.id = newId;
AccountWithException.annualInterestRate = newRate;
this.dateCreated = new java.util.Date();
}
// accessor methods
public int getId() {
return this.id;
}
public double getBalance() {
return this.balance;
}
public static double getAnnualInterestRate() {
return annualInterestRate;
}
public double getMonthlyInterest() {
return this.balance * (this.annualInterestRate/1200);
}
public java.util.Date getDateCreated() {
return this.dateCreated;
}
public String getName() {
return this.name;
}
// mutator methods
public void setId(int newId) {
this.id = newId;
}
public void setBalance(double newBalance) throws InvalidBalanceException {
if(balance >= 0) {
this.balance = newBalance;
}
else {
throw new InvalidBalanceException(balance);
}
}
public static void setAnnualInterestRate(double newAnnualInterestRate) {
annualInterestRate = newAnnualInterestRate;
}
public void setName(String newName) {
this.name = newName;
}
// balance modification methods
public void withdraw(double amount) {
this.balance -= amount;
}
public void deposit(double amount) {
this.balance += amount;
}
// override of Object method
public String toString() {
// return string with formatted data
// left-align 20 character column and right-align 15 character column
return String.format("%-20s%15d\n%-20s%15tD\n%-20s%15s\n%-20s%15.2f%%\n%-20s%,15.2f\n",
"ID:", this.id,
"Created:", this.dateCreated,
"Owner:", this.name,
"Annual Rate:", this.annualInterestRate,
"Balance:", this.balance);
}
}
public class InvalidBalanceException extends Exception {
private double balance;
public InvalidBalanceException(double balance) {
// exceptions constructor can take a string as a message
super("Invalid Balance " + balance);
this.balance = balance;
}
public double getBalance() {
return balance;
}
}