繁体   English   中英

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

[英]How to allow the user to access methods of a class

我正在从Java教科书中编程一个项目,说:

L&L银行最多可以处理30个拥有储蓄帐户的客户。 设计并实施一个程序来管理帐户。 跟踪关键信息,并让每个客户进行存款和取款。 为无效交易产生错误消息。 提示:您可能希望以第4章中的Account类为基础建立帐户。还提供一种方法,只要调用该方法,该方法就可以为所有帐户增加3%的利息。

我不确定这个问题到底要问什么,但是我的猜测是允许并允许用户添加帐户,存款,取款,增加利息,获取余额以及将要管理的帐户打印到一个数组中。 我并不完全确定我必须创建一个数组,但是整章都是关于数组的。

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

存钱(例如: acct1.deposit (25.85); ),

提款(例如: acct3.withdraw (800.00, 0.0); ),

增加兴趣(例如: 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");
  }


  }
}

在我看来,您在正确的道路上。 我从问题(书中)的措辞以及您发布的代码中不存在帐户的方式中推断出,在这种情况下,您需要允许系统用户创建帐户。 然后,在更改帐户时,用户首先必须提供帐号,以便您可以标识适当的“ Accounts对象。

我猜想由于本章是关于数组的,所以它可能还没有介绍Maps(否则这将是将帐号与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对象创建位置。 每次添加帐户时,该数字将增加一。

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

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类中创建新方法来进行存款等,但这至少显示了总体结构。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM