업데이트 : 필요한 특정 형식으로 날짜를 출력하는 데 결코 문제가 해결되지 않았지만 나머지 프로그램은 작동합니다. time.format 클래스를 망친 후 다른 형식으로 날짜를 출력하는 여러 가지 다른 방법을 찾았고 목록의 마지막 부분을 제외하고 모두 작업 할 수있었습니다. time.format 클래스하지만 불행히도 그것을 구현하는 방법을 알아낼 수 없었습니다. 그러나, 이것은 (내 생각에) 관심을 계산하는 간단한 프로그램의 좋은 예입니다. 나는 게터와 세터에 대한 많은 비판을 읽었지 만,이 프로그램을 위해 잘 작동하는 것처럼 보였다. Java 및 프로그래밍 전체를 배우는 중입니다.getter 및 setter를 사용하여 특정 날짜 및 시간을 인쇄하는 방법
package accountproject;
// two imports needed for date and time
import java.time.format.*;
import java.time.*;
// import standard exception error text
import java.text.ParseException;
// import EVERYTHING!
import java.util.*;
public class Account {
private static int id = 0;
private static double balance = 0;
private static double annualInterestRate = 0;
private static ZonedDateTime dateCreated;
private static double MonthlyInterestRate = annualInterestRate/12;
public Account()
{
// empty constructor
}
public Account(int id, double balance, double annualInterestRate, ZonedDateTime dateCreated) {
super();
Account.id = 0;
Account.balance = 0;
Account.annualInterestRate = 4.5;
}
public int getId() {
return id;
}
public void setId(int id) {
Account.id = id;
}
public static double getBalance(double d) {
return balance;
}
public void setBalance(double balance) {
Account.balance = balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
Account.annualInterestRate = annualInterestRate;
}
public static ZonedDateTime ConvertStringToDate(String dateNow) {
try
{
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
ZonedDateTime date = ZonedDateTime.parse(dateNow, formatter);
return date;
}
catch (DateTimeParseException e)
{
System.out.println(e);
}
ZonedDateTime date = null;
return date;
}
public static double getMonthlyInterestRate(double annualInterestRate2) {
double temp = annualInterestRate2/12;
MonthlyInterestRate = temp;
return MonthlyInterestRate;
}
public static double getMonthlyInterest(double newBalance2) {
double temp = 100/MonthlyInterestRate;
double temp2 = newBalance2/temp;
double temp3 = newBalance2 + temp2;
newBalance2 = temp3;
return temp2;
}
public static double deposit(double balance, double deposit) {
double temp = balance + deposit;
balance = temp;
return balance;
}
public static double withdrawal(double balance, double withdrawal) {
double temp = balance - withdrawal;
balance = temp;
return balance;
}
public static void main(String[] args) throws ParseException {
// establish a scanner and set example values
Scanner stdin = new Scanner(System.in);
id = 1122;
balance = 20000;
MonthlyInterestRate = .375;
double withdrawal = 2500;
double deposit = 3000;
double balanceExp = deposit(balance,deposit);
balanceExp = withdrawal(balanceExp,withdrawal);
double balanceExp2 = getMonthlyInterest(balanceExp);
double monthlyInterest = balanceExp2;
String dateExp = "Fri Oct 06 16:10:59 GMT 2017";
dateCreated = ConvertStringToDate(dateExp);
System.out.println("SAMPLE: Account ID " + id + " with a balance of $" + balanceExp
+ ",\nhas accrued $" + monthlyInterest + " in interest and was opened on "
+ dateCreated + ".");
System.out.println("Please enter the ID number:");
// get the id number input
id = stdin.nextInt();
stdin.nextLine();
System.out.println("Typically, the original balance will be $20,000.00.\nPlease enter the balance:");
// get the starting balance input
balance = stdin.nextInt();
stdin.nextLine();
double newBalance = balance;
Account.getBalance(20000.00);
System.out.println("Please enter the deposit amount:");
// ensure deposit is set to 0 before getting input
deposit = 0.00;
// get the deposit amount from input
deposit = stdin.nextDouble();
stdin.nextLine();
newBalance = deposit(balance, deposit);
System.out.println("Please enter the withdrawal amount:");
// ensure withdrawal is set to 0 before getting input
withdrawal = 0.00;
// get the deposit amount from input
withdrawal = stdin.nextDouble();
stdin.nextLine();
double newBalance2 = withdrawal(newBalance, withdrawal);
double newBalance3 = getMonthlyInterest(newBalance2);
double MonthlyInterest = newBalance3;
print(id, newBalance2, MonthlyInterest, dateCreated);
stdin.close();
}
public static void print(int id, double newBalance2, double MonthlyInterest, ZonedDateTime dateCreated2)
{
System.out.println("To verify: the Account ID is " + id + " with a balance of $" + newBalance2
+ ",\nhas accrued $" + MonthlyInterest + " in interest, and was opened on " + dateCreated2 + ".");
}
}
당신은 현재 레거시이며, java.time 클래스에 의해 대체 된 번거로운 오래된 날짜 - 시간 클래스를 사용하고 있습니다. '날짜'&'달력'을 피하십시오; 'Instant'와'ZonedDateTime'을 사용하십시오. 또한 돈 문제에 부동 소수점 유형을 사용하지 마십시오. 'BigDecimal'을 사용하십시오. 마지막으로 게시하기 전에 Stack Overflow를 철저히 검색하십시오. 모든 기본 날짜/시간 질문이 요청 및 응답되었습니다. –
포인터를 가져 주셔서 감사합니다. 제가 질문을하는 이유는 저에게 의미있는 방식으로 대답을 찾는 데 어려움이 있기 때문입니다. 모든 교수님들이 코드 자체가 아닌 코드 뒤에있는 원칙을 가르치기 때문에 1 년 넘게 다양한 코딩 문제에 대한 답변을이 사이트에서 검색했습니다. 영어와 의사 코드로도 아이디어를 이해할 수 있기 때문에 이것은 나에게 아픈 주제이지만 코드 자체는 정부의 비밀처럼 취급합니다. 당신이 질문에 대답하고 싶지 않다면, 그렇게하지 마십시오. 무례 할 이유가 없습니다. –
실제 문제는 스택 오버 플로우 검색이 이전 버전 질문을 필터링하지 않고 새로운 사용자에게 더 이상 작동하지 않거나 감가 상각 된 답변이 제공된다는 것입니다. –