如何允许用户访问类的方法

时间:2016-12-11 23:07:14

标签: java arrays class methods java.util.scanner

我正在使用Java教科书编写一个编程项目:

  

L& L银行最多可以处理30个拥有储蓄账户的客户。设计并实施管理帐户的程序。跟踪关键信息,让每个客户进行存款和取款。为无效事务生成错误消息。提示:您可能希望将帐户建立在第4章的Account类上。还提供了一种方法,可以在调用方法时为所有帐户添加3%的利息。

我不确定问题的具体要求,但我的猜测是允许并允许用户添加帐户,存款,取款,增加兴趣,获得余额,并将管理的帐户打印到阵列中。我不完全确定我必须制作一个数组,但整个章节都在数组上。

我的问题是我不确定如何启用用户创建帐户  (EX:Account acct1 = new Account ("Ted Murphy", 72354, 102.56);),

存钱(EX:acct1.deposit (25.85);),

取钱(EX:acct3.withdraw (800.00, 0.0);),

添加兴趣(EX:acct1.addInterest();),

或为所有帐户打印数组。

以下是Java教科书中使用所有方法找到的Account类:

//********************************************************************
//  Account.java       Author: Lewis/Loftus/Cocking
//
//  Represents a bank account with basic services such as deposit
//  and withdraw.
//********************************************************************

import java.text.NumberFormat;

public class Accounts
{



private NumberFormat fmt = NumberFormat.getCurrencyInstance();

   private final double RATE = 0.035;  // interest rate of 3.5%

   private int acctNumber;
   private double balance;
   private String name;

   //-----------------------------------------------------------------
   //  Sets up the account by defining its owner, account number,
   //  and initial balance.
   //-----------------------------------------------------------------
   public Accounts (String owner, int account, double initial)
   {
      name = owner;
      acctNumber = account;
      balance = initial;
   }

   //-----------------------------------------------------------------
   //  Validates the transaction, then deposits the specified amount
   //  into the account. Returns the new balance.
   //-----------------------------------------------------------------
   public double deposit (double amount)
   {
      if (amount < 0)  // deposit value is negative
      {
         System.out.println ();
         System.out.println ("Error: Deposit amount is invalid.");
         System.out.println (acctNumber + "  " + fmt.format(amount));
      }
      else
         balance = balance + amount;
      return balance;
   }

   //-----------------------------------------------------------------
   //  Validates the transaction, then withdraws the specified amount
   //  from the account. Returns the new balance.
   //-----------------------------------------------------------------


     public double withdraw (double amount, double fee)
       {
          amount += fee;

      if (amount < 0)  // withdraw value is negative
      {
         System.out.println ();
         System.out.println ("Error: Withdraw amount is invalid.");
         System.out.println ("Account: " + acctNumber);
         System.out.println ("Requested: " + fmt.format(amount));
      }
      else
         if (amount > balance)  // withdraw value exceeds balance
         {
            System.out.println ();
            System.out.println ("Error: Insufficient funds.");
            System.out.println ("Account: " + acctNumber);
            System.out.println ("Requested: " + fmt.format(amount));
            System.out.println ("Available: " + fmt.format(balance));
         }
         else
            balance = balance - amount;

      return balance;
   }

   //-----------------------------------------------------------------
   //  Adds interest to the account and returns the new balance.
   //-----------------------------------------------------------------
   public double addInterest ()
   {
      balance += (balance * RATE);
      return balance;
   }

   public double addInterestAll ()// I made this method myself but I am not sure if it is correct
   {
       balance += (balance * 0.03);
       return balance;
   }

   //-----------------------------------------------------------------
   //  Returns the current balance of the account.
   //-----------------------------------------------------------------
   public double getBalance ()
   {
      return balance;
   }

   //-----------------------------------------------------------------
   //  Returns the account number.
   //-----------------------------------------------------------------
   public int getAccountNumber ()
   {
      return acctNumber;
   }

   //-----------------------------------------------------------------
   //  Returns a one-line description of the account as a string.
   //-----------------------------------------------------------------
   public String toString ()
   {
      return (acctNumber + "\t" + name + "\t" + fmt.format(balance));
   }
}

这是正在建设的主要方法,我不确定我是否走在正确的轨道上:

import java.util.Scanner;
public class SixSix
{



public static void main (String[] args)
   {
      Scanner scan = new Scanner(System.in);
      System.out.println("Input (0) to add account, (1) to deposit,");
      System.out.println("(2) to withdraw, (3) to add interest, (4) to add interest to all");
      System.out.println("(5) to get balance, (6) to get account number, (7) to print");
      int input = scan.nextInt();

  while (input == 0){
      System.out.println("To create an account, please enter your name");
      String name = scan.nextLine();
      System.out.println("Please enter your account number");
      int accNum = scan.nextInt();
      System.out.println("Please Enter account balance");
      double accBalance = scan.nextDouble();

      //System.out.format


  }

  while (input == 1)
  {
      System.out.println("To deposit money to an account");

  }

  while (input == 2)
  {
      System.out.println("To withdraw money from an account");

  }

  while (input == 3)
  {
      System.out.println("To add Interest");

  }

  while (input == 4)
  {
      System.out.println("To add Interest to all");
  }

  while (input == 5)
  {
      System.out.println("To get balance");

  }

  while (input == 6)
  {
      System.out.println("To get account number");
  }

  while (input == 7)
  {
      System.out.println("Printing account");
  }


  }
}

1 个答案:

答案 0 :(得分:1)

在我看来,你似乎正走在正确的轨道上。我推断问题(在书中)的措辞和你发布的代码尚未存在的代码,在这种情况下你需要允许系统的用户创建它们。然后,在更改帐户时,用户首先必须提供帐号,以便您可以识别正确的Accounts对象。

我猜测由于这一章是关于数组的,所以它可能还没有涵盖地图(否则这将是一种将帐号与Accounts个对象相关联的便捷方式)。如果你使用数组,那么账号从0到29似乎是一个好主意。

以下是一个如何实现AccountsManager类的示例,该类可帮助您从一组帐户中添加和检索帐户。

public class AccountsManager {
    private Accounts[] accounts;
    private final int capacity;
    private int current;

    public AccountsManager(int capacity) {
        this.capacity = capacity;
        accounts = new Accounts[capacity];
        current = 0;
    }

    // returns the account number of the new account
    // or -1 if no account could be made
    public int addAccount(String name) {
        if (current >= capacity) {
            return -1;
        }
        accounts[current] = new Accounts(name, current, 0);
        return current++;
    }

    public Accounts getAccount(int number) {
        if (number >= current || number < 0) {
            return null;
        }
        return accounts[number];
    }
}

在上面,capacity属性只是数组的大小,这是可以创建的Accounts个对象的最大数量(根据练习,这应该是30)。 current属性(随意重命名为更具信息性的内容!)跟踪数组中应创建下一个Accounts对象的位置。每次添加帐户时,这一增长为1。

在您的代码中,您现在可以执行以下操作:

AccountsManager manager = new AccountsManager(30);

// ...

if (input == 0) {
    // Create new account
    System.out.println("To create an account, please enter your name");
    String name = scan.nextLine();

    int accountNumber = manager.addAccount(name);
    if (accountNumber == -1)
        System.out.println("The bank can't handle any more accounts.");
    else
        System.out.println("Your account number is "+accountNumber);

} else if (input == 1) {
    // Deposit money to account
    System.out.println("What is your account number?");
    int accountNumber = scan.nextInt();

    // Check if account exists
    if (manager.getAccount(accountNumber) == null) {
        System.out.println("That account doesn't exist!");
    } else {
        System.out.println("How much do you want to deposit?");
        double amount = scan.nextDouble();
        manager.getAccount(accountNumber).deposit(amount);
    }
}

也许最好在AccountsManager类中创建新方法以进行存款等,但这至少表明了一般结构可能是什么样的。