2017-10-04 10 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クラスに取って代わられています。 'Date'と' Calendar'を避けてください。 'Instant'と' ZonedDateTime'を使います。また、金額に関する浮動小数点型を使用しないでください。 'BigDecimal'を使います。最後に、投稿する前にStack Overflowを完全に検索します。すべての基本日時の質問が尋ねられ、答えられました。 –

+0

ありがとうございます。私が質問をする理由は、私が私に合った方法で答えを見つけることが困難なためです。すべての教授がコードの原則を教えるのであって、コードそのものを教えるわけではないので、私はこのサイトを検索してさまざまなコーディングの質問に答えました。私は英語や擬似コードでもアイデアを理解できるので、私にとっては辛いテーマですが、コード自体は政府の秘密のように扱います。あなたが質問に答えることを望まないなら、しないでください。無礼な理由はありません。 –

+0

実際の問題は、スタックオーバーフローによる検索で古いバージョンの質問がフィルタリングされず、新規ユーザーには機能しなくなった、または償却された回答が提示されることです。 –

答えて

0

私はこの問題がメインであると信じています。

withdrawl(balance, withdrawl); 

メソッドの戻り値を変数に割り当てる必要があります。

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

ありがとう、私はそれが何か単純であることを知っていた、私はそれを考えることができなかった。それは動作します。 –

0

私は多くの時間を自分自身に再学習するためにwayyy wayyyを費やしました。私はJava 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です。date 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 + "."); 
    } 
} 
関連する問題