gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:08:05 24 4
gpt4 key购买 nike

我正在开发 Java 教科书中的一个编程项目,内容如下:

The L&L bank can handle up to 30 customers who have savings accounts. Design and implement a program that manages the accounts. Keep track of key information and let each customer make deposits and withdrawals. Produce error messages for invalid transactions. Hint: You may want to base your accounts on the Account class from Chapter 4. Also provide a method to add 3 percent interest to all accounts whenever the method is invoked.

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

我的问题是我不知道如何让用户创建帐户 (例如:帐户 acct1 = 新帐户(“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 对象。

我猜测,由于这一章是关于数组的,因此它可能还没有涵盖 map (否则这将是一种将帐号与 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 类中创建新方法来进行存款等,但这至少显示了一般结构可能是什么样的。

关于java - 如何允许用户访问类的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41092152/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com