编写代码 - 类 - 这是正确的/评分我的代码

时间:2014-12-11 05:30:03

标签: java class methods

要求: 编写一个类BankAccount,它有两个实例变量用于所有者的名称(String类型)和balance(类型为double)。向此类添加以下方法:

  • 一个构造函数,它将BankAccount实例变量初始化为两个参数中的值,即所有者的名称和初始余额(> = 0.0)。
  • 存款:给定存款金额(> 0.0),此方法将其存入账户。
  • 撤销:给定要撤回的金额(> 0.0和<=当前余额),此方法将其从帐户中提取。
  • getName:此方法返回所有者名称。
  • getBalance:此方法返回第t个当前余额。 (不要尝试格式化数字 - 只需采用默认值。)

在您的方法中加入适当的检查,以确保存入和取出的金额满足指定的约束条件。使用良好的封装准则。

我是否正确地解释了这一点并正确地执行了它,如果没有,需要修复什么以及如何修复。任何帮助表示赞赏。

代码:

import java.util.*;
public class BankAccount {
static Scanner in = new Scanner (System.in);
private static String name;
private static double balance;


public BankAccount(String n, double b){
    System.out.println("Enter your name: ");
    n = in.nextLine();
    name = n;
    System.out.println("Enter your current balance: ");
    b = in.nextDouble();
    balance = b;
}



public void deposit(){
    System.out.println("Enter the amount you would like to deposit: ");
    double deposit = in.nextDouble();
    if(deposit > 0.0){
        balance = balance + deposit;
    }
}


    public void withdraw(){
        System.out.println("Enter the amount you would like to withdraw: ");
        double withdraw = in.nextDouble();
        if(withdraw > 0.0 && withdraw <= balance){
            balance = balance - withdraw;
        }
    }
    public static String getName(){
        return name;
    }
    public static double getBalance(){
        return balance;
    }

    }

3 个答案:

答案 0 :(得分:1)

  

一个构造函数,它将BankAccount实例变量初始化为两个参数中的值,即所有者的名称和初始余额(&gt; = 0.0)。

嗯,你有构造函数,但是你覆盖传递给它的值......

public BankAccount(String n, double b){
    System.out.println("Enter your name: ");
    n = in.nextLine();
    name = n;
    System.out.println("Enter your current balance: ");
    b = in.nextDouble();
    balance = b;
}

应该只是......

public BankAccount(String n, double b){
    name = n;
    balance = b;
}
  

存款:给定存款金额(> 0.0),此方法将其存入账户。

您的deposit方法不带参数......

public void deposit(double amount){
    if (amount > 0.0) {
        balance = balance + deposit;
    }
}
  

取款:给定要提取的金额(> 0.0和<=当前余额),此方法将其从帐户中提取。

deposit相同,不需要参数......

public void withdraw(double amount){
    if (amount > 0.0 && amount <= balance) {
        balance = balance - amount;
    }
}
  

getName:此方法返回所有者名称。

检查...

  

getBalance:此方法返回第t个当前余额。 (不要尝试格式化数字 - 只需采用默认值。)

检查...

答案 1 :(得分:0)

删除静态声明。除非您希望BankAccount的每个实例引用相同的balancename。如果不使用/设置它们,则重载的构造函数不需要参数,无论值如何。

public class BankAccount {
    private Scanner in = new Scanner (System.in);
    private String name;
    private double balance;

   public BankAccount(){
       String n = "";
       double b = 0.0;

       System.out.println("Enter your name: ");
       n = in.nextLine();

       // check for null string
       if(!n.equals("")) {
           name = n;
       }

       System.out.println("Enter your current balance: ");
        // ToDo: add a check for input
       b = in.nextDouble();
       balance = b;
    }

    ...

    public String getName(){
        return name;
    }

    public double getBalance(){
        return balance;
    }
}

您可以保留重载的构造函数并将其与默认构造函数结合使用。有点像...

public BankAccount(){

   String n = "";
   double b = 0.0;

   System.out.println("Enter your name: ");
   // ToDo: add a check for input
   n = in.nextLine();

   System.out.println("Enter your current balance: ");
   // ToDo: add a check for input
   b = in.nextDouble();

   this(n, b);
}

public BankAccount(String n, double b){
     // ToDo: add checks for parameter integrity
     this.name = n;
     this.balance = b;
}

答案 2 :(得分:0)

如果它是正常使用的正常类,那么这是可以的。

但是如果它在实时财务应用程序中使用那么方法应该同步化以进行线程安全操作并且还删除静态变量声明,因为静态变量属于类而不是实例。