gpt4 book ai didi

java - 找不到符号 ArrayList

转载 作者:行者123 更新时间:2023-12-01 13:17:24 27 4
gpt4 key购买 nike

尝试编译此文件,但无法找出问题所在。几天来一直在为此苦苦挣扎。是的,我是新手...有人可以帮助我吗?获取代码底部方法中的错误。

import java.util.ArrayList;
import java.util.Scanner;

public class BankLogic
{
private long pNr;
private int accountId;
private double amount;
private double balance;
private double rate;

private ArrayList<Customer> customerlist;
private ArrayList<SavingsAccount> accounts;

Scanner in = new Scanner(System.in);

//----------------------------------------------------------------------
//----------------------------------------------------------------------
public BankLogic(){

customerlist = new ArrayList<Customer>();
accounts = new ArrayList<SavingsAccount>();
}

//----------------------------------------------------------------------
//----------------------------------------------------------------------
public String toString(){

String info = "Personnummer: " + pNr + "\nKontonummer: " + accountId
+ "\nSaldo: " + amount;
}
//----------------------------------------------------------------------
// Beskrivning: returnerar presentation av alla kunder(pers.nr och namn)
// Returvärde: String
//----------------------------------------------------------------------
public String infoBank(){

return customerlist.toString();
}

//----------------------------------------------------------------------
//----------------------------------------------------------------------
public boolean addCustomer(String name, long pNr){

for (Customer a : customerlist)
{
if (a.getPCode() == pNr)
return false;
else
{
Customer aCust = new Customer(name, pNr);
customerlist.add(aCust);
}
}
}

//----------------------------------------------------------------------
//----------------------------------------------------------------------
public String infoCustomer(long pNr){

for (Customer a : customerlist)
{
if (a.getPCode() == pNr)
{
System.out.println(a);
}
}
}

//----------------------------------------------------------------------
//----------------------------------------------------------------------
public boolean changeCustomerName(String name, long pNr){

for (Customer a : customerlist)
{
if (a.getPCode() == pNr)
{
Customer aCust = new Customer(name, pNr);
customerlist.add(aCust);
}
else
return false;
}
}

//----------------------------------------------------------------------
//----------------------------------------------------------------------
public String removeCustomer(long pNr){

for (int i = 0; i < customerlist.length; i++)
{
Customer a = customerlist.get(i);
if (a.getPCode() == pNr)
{
customerlist.remove(i);
}
else
i++;
}

}


//----------------------------------------------------------------------
//----------------------------------------------------------------------
public int addSavingsAccount(long pNr){

for (Customer a : customerlist)
{
if (a.getPCode() == pNr)
{
boolean added = false;
for (int i = 0; !added && i < accounts.size(); i++)
{
added = accounts.get(i).addAccount(a);
if(added)
{
System.out.println("Kontonummer: " + accounts.get(i).getAccountId();
}
else
return "Kontot skapades inte.";
}
}
}
}

//----------------------------------------------------------------------
//----------------------------------------------------------------------
public String infoAccount(long pNr, int accountId)
{
for (Customer a : customerlist)
{
if (a.getAccountId() == accountId && a.getPCode() == pNr)
return a.infoCust();
}
return null;
}

//----------------------------------------------------------------------
//----------------------------------------------------------------------
public boolean deposit(long pNr, int accountId, double amount){

for (Customer a : customerlist)
{
if ((a.SavingsAccount.getAccountId() == accountId) && (a.getPCode() == pNr))
{
a.SavingsAccount.getBalance() = balance + amount;
}
else
return false;
}
}

//----------------------------------------------------------------------
//----------------------------------------------------------------------
public boolean withdraw(long pNr, int accountId, double amount){

for (Customer a : customerlist)
{
if ((a.getAccountId() == accountId) && (a.getPCode() == pNr))
{
a.getBalance() = balance - amount;
return true;
}
else
return false;
}
}

}

最佳答案

上面的代码我一眼就看出了各种各样的错误:

  • toString() 不返回任何内容(它只是声明一个局部变量,该变量会在方法结束时立即被垃圾回收)
  • addCustomer() 并不总是返回某些内容。
  • infoCustomer()removeCustomer() 永远不会返回任何内容,尽管它们应该在方法 header 中声明
  • addSavingsAccount() 应返回一个 int(根据方法 header ),但有时只返回一个字符串。
  • 除非 Customer 有一个名为 SavingsAccount 的公共(public)字段(从很多方面来看,这都是非常糟糕的设计),然后 a.SavingsAccount.getAccountId() 和类似的方法是错误的,您可能需要 Customer 上的方法来获取其特定的储蓄帐户。

您需要养成以更小的增量检查代码的习惯,至少直到您掌握了事物的基本语法方面。这样,您就不会陷入必须修复大量错误而又不真正了解如何修复这些错误的情况。

每次添加(例如)一个方法时:停止、编译、检查它是否按您希望的方式工作,然后才继续进行下一个操作。从长远来看,它会为您节省大量时间。

关于java - 找不到符号 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22357869/

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