2017-10-04 6 views
-2

업데이트 : 필요한 특정 형식으로 날짜를 출력하는 데 결코 문제가 해결되지 않았지만 나머지 프로그램은 작동합니다. 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 + "."); 
    } 
} 
+0

당신은 현재 레거시이며, java.time 클래스에 의해 대체 된 번거로운 오래된 날짜 - 시간 클래스를 사용하고 있습니다. '날짜'&'달력'을 피하십시오; 'Instant'와'ZonedDateTime'을 사용하십시오. 또한 돈 문제에 부동 소수점 유형을 사용하지 마십시오. 'BigDecimal'을 사용하십시오. 마지막으로 게시하기 전에 Stack Overflow를 철저히 검색하십시오. 모든 기본 날짜/시간 질문이 요청 및 응답되었습니다. –

+0

포인터를 가져 주셔서 감사합니다. 제가 질문을하는 이유는 저에게 의미있는 방식으로 대답을 찾는 데 어려움이 있기 때문입니다. 모든 교수님들이 코드 자체가 아닌 코드 뒤에있는 원칙을 가르치기 때문에 1 년 넘게 다양한 코딩 문제에 대한 답변을이 사이트에서 검색했습니다. 영어와 의사 코드로도 아이디어를 이해할 수 있기 때문에 이것은 나에게 아픈 주제이지만 코드 자체는 정부의 비밀처럼 취급합니다. 당신이 질문에 대답하고 싶지 않다면, 그렇게하지 마십시오. 무례 할 이유가 없습니다. –

+0

실제 문제는 스택 오버 플로우 검색이 이전 버전 질문을 필터링하지 않고 새로운 사용자에게 더 이상 작동하지 않거나 감가 상각 된 답변이 제공된다는 것입니다. –

답변

0

나는이 문제가이 줄에 있다고 생각한다.

withdrawl(balance, withdrawl); 

변수에 반환 된 값을 지정해야합니다.

double currentBalance = withdrawl(balance, withdrawl); 
print(id, currentBalance, MonthlyInterestRate, dateCreated); 
+0

정말 고마워. 나는 그게 단순한 걸 알았어. 나는 그걸 생각할 수 없었다. 그거야. –

0

나는 자신을 재구성하는 데 많은 시간을 들기 위해 wayyy wayyy를 보냈다. 나는 자바 8이 새로운 java.time을 가지고 있다는 것을 깨닫지 못했다.

여기에는 많은 변화가있다. 희망이 그래서 결국 추측 모든 변경 내 교수가 원하는 방식으로 날짜를 얻을 수있는 서식을 설정하는 방법을 정확하게 파악하지 않았다 결코

import java.time.format.*; 
import java.time.*; 
import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.*; 

public class Account { 

private static int id = 0; 
private static double balance = 0; 
private static double annualInterestRate = 0; 
private static Date dateCreated; 
private static double MonthlyInterestRate = annualInterestRate/12; 


public Account() 
{ 
    // empty constructor 
} 

/** 
* @param id 
* @param balance 
* @param annualInterestRate 
* @param dateCreated 
* regular constructors 
*/ 

public Account(int id, double balance, double annualInterestRate, Date dateCreated) { 
    super(); 
    Account.id = 0; 
    Account.balance = 0; 
    Account.annualInterestRate = 4.5; 
    Account.dateCreated = dateCreated; 
} 

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 LocalDate ConvertStringToDate(String dateNow) { 
    try 
{ 
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM d yyyy", Locale.ENGLISH); 
    LocalDate date = LocalDate.parse(dateNow, formatter); 
    System.out.println(date); 
    return date; 
} 
catch (DateTimeParseException e) 
{ 
    System.out.println(e); 
} 
LocalDate date = null; 
return date; 



} 



public static double getMonthlyInterestRate(double annualInterestRate2) { 

    double temp = annualInterestRate2/12; 

    MonthlyInterestRate = temp; 

    return MonthlyInterestRate; 

} 

public double getMonthlyInterest() { 

    double temp = 100/annualInterestRate; 

    double temp2 = getBalance(0)/temp; 

    double temp3 = balance + temp2; 

    System.out.println(temp3); 

    return temp3; 

} 

public static double deposit(double balance, double deposit) { 

    double temp = balance + deposit; 

    balance = temp; 

    return balance; 
} 

public static double withdrawl(double balance, double withdrawl) { 

    double temp = balance - withdrawl; 

    balance = temp; 

    return balance; 
} 

    public static void main(String[] args) throws ParseException { 
     //create Scanner 1 time. no need to create over and over 
     Scanner stdin = new Scanner(System.in); 
     id = 1122; 
     balance = 20000; 
     MonthlyInterestRate = .375; 

     System.out.println("SAMPLE: Account ID " + id + " with a balance of " + balance + " " 
       + "and a monthly interest rate of " + MonthlyInterestRate + " opened on " + 
       dateCreated + "."); 

     System.out.println("Please enter the ID number:"); 

     id = stdin.nextInt(); 
     // consume the /n 
     stdin.nextLine(); 

     System.out.println("Typically, the original balance will be $20,000.00.\nPlease enter the balance:"); 



     balance = stdin.nextInt(); 
     // consume the /n 
     stdin.nextLine(); 

     double newBalance = balance; 

     Account.getBalance(20000.00); 

     System.out.println("Please enter the date created like: MM d yyyy"); 


     // assign your string to your scanned object 
     String dateNow = stdin.nextLine(); 
     //catch your returned date 
     LocalDate dateCreated = ConvertStringToDate(dateNow); 

     System.out.println("Please enter the deposit amount:"); 



     double deposit = 0.00; 

     deposit = stdin.nextDouble(); 
     // consume the /n 
     stdin.nextLine(); 

     newBalance = deposit(balance, deposit); 

     System.out.println("Please enter the withdrawl amount:"); 



     double withdrawl = 0.00; 

     withdrawl = stdin.nextDouble(); 
     // consume the /n 
     stdin.nextLine(); 

     double newBalance2 = withdrawl(newBalance, withdrawl); 

     double annualInterestRate2 = 4.5; 

     double MonthlyInterestRate2 = getMonthlyInterestRate(annualInterestRate2); 

     // pass your local date 
     print(id, newBalance2, MonthlyInterestRate2, dateCreated); 
    } 

    public static void print(int id, double balance, double MonthlyInterestRate2, LocalDate dateCreated) 
    { 
      System.out.println("To verify: the Account ID is " + id + " with a balance of $" + balance + " " 
        + "and a monthly interest rate of " + MonthlyInterestRate2 + "% opened on " + 
        dateCreated + "."); 
    } 
} 
+0

이 부분 중 하드 코드는 무엇이며 프로그램마다 어떤 부분이 있습니까? 분명 int와 string은 있지만, 첫 번째 줄은 어느 것이 변수이고 어느 구문인지는 확실치 않습니다. 내 get 메소드는 변수로 dateCreated를 사용하여 getDateCreated이며, 날짜는 dateCreated2 = new dateCreated입니까? 나는 지금 내 코드에 액세스 할 수 없다. –

+0

나는 실제로 질문을 놓친다. 나는 당신이 날짜 데이터 형을 가져 와서 그것의 문자열 조각들로 변환하기를 원한다고 생각했다. –

+0

https://stackoverflow.com/questions/6510724/how-to-convert-java-string-to-date-object –

0

코드에서 주석 처리하지만 100 %를 가지고하는 데 도움이 그렇게 중요하지 않았다. Jeremiah Stillings 덕분에 모든 도움에 감사드립니다. 이것은 내가 결국에 끝난 것입니다 :

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 
} 

/** 
* @param id 
* @param balance 
* @param annualInterestRate 
* @param dateCreated 
* regular constructors 
*/ 

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 + "."); 
    } 
} 
관련 문제