需要后续帮助/建议---Java BankAccount应用程序-通过switch语句在案例中重用变量
后续问题,需要案例5的帮助-使用此代码和joptionpane显示输出。如何在有意义的结果中从arraylist(database=db/deleted)调用对象?我能够显示对对象的引用。我读过关于重载tostring方法的文章,但我不确定我的方向是否正确??任何帮助或建议都将不胜感激!!提前谢谢 另请注意,我应该在回答之前的问题后将其删除,还是将其留在黑板上需要后续帮助/建议---Java BankAccount应用程序-通过switch语句在案例中重用变量,java,variables,switch-statement,case,Java,Variables,Switch Statement,Case,后续问题,需要案例5的帮助-使用此代码和joptionpane显示输出。如何在有意义的结果中从arraylist(database=db/deleted)调用对象?我能够显示对对象的引用。我读过关于重载tostring方法的文章,但我不确定我的方向是否正确??任何帮助或建议都将不胜感激!!提前谢谢 另请注意,我应该在回答之前的问题后将其删除,还是将其留在黑板上 package bankaccount; import javax.swing.JOptionPane; public cl
package bankaccount;
import javax.swing.JOptionPane;
public class BankAccountTest
{
public static void main (String[] args)
{
Database db = new Database();
Database deleted = new Database();
boolean done = false;
while (!done)
{
int activity = IO.getInt("Please choose one of the following:"+
"\n 1 to create new account"+ "\n 2 to delete an account"+
"\n 3 to withdraw from an account"+"\n 4 to deposit to an account"+
"\n 5 to list all customers"+"\n 6 to list all deleted customers"+
"\n 7 to display single account "+"\n 8 to exit this program");
switch(activity)
{
case 1:
//Create new account
String LastName = IO.getString("Please type last name: ");
String FirstName = IO.getString("Please type first name: ");
Name n = new Name (LastName,FirstName);
//Create address object
String street = IO.getString("Please type your address: ");
String city = IO.getString("Please type your city: ");
String state = IO.getString("Please type your state: ");
String zipcode = IO.getString("Please type your zipcode: ");
Address addr = new Address (street,city,state,zipcode);
/* //Create Account number
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(2000000000);
AccountNum accno = new AccountNum(randomInt);
*/ //Create customer object
String accno = IO.getString("Please enter the account number: ");
Customer c = new Customer(n,addr,accno);
//Create bankaccount object
double amt = IO.getDouble("Please type the opening account balance: ");
BankAccount b = new BankAccount(c, amt);
db.add(b);
break;
case 2:
// Delete an account / copy account info to deleted database
String key = IO.getString("Enter account number to delete: ");
db.search(key);
if (db.inlist())
{
deleted.add(db.remove(db.getindex()));
} else
{
//Display not found
IO.notFound();
}
break;
case 3:
// withdraw from an account
key = IO.getString("Enter account number to withdraw from: ");
db.search(key);
if (db.inlist())
{
double amount = IO.getDouble("Enter an amount to withdraw : ");
b.withdraw(amount);
JOptionPane.showMessageDialog(null, "Current balance is: "+b.balance);
} else
{
IO.notFound();
}
break;
案例5:
//Display single account
if(db.list.isEmpty())
{
String s = "The list is empty";
JTextArea text = new JTextArea(s, 6, 20);
JScrollPane pane = new JScrollPane(text);
JOptionPane.showMessageDialog(null, pane,
"Current Customers", JOptionPane.INFORMATION_MESSAGE);
}
for (int i = 0; i < db.list.size(); i++)
{
// JOptionPane.showMessageDialog(null, " "+db.list.size());
String s = "These exists in the list: ";
JTextArea text = new JTextArea(s, 6, 20);
JScrollPane pane = new JScrollPane(text);
JOptionPane.showMessageDialog(null, pane,
"Current Customers List", JOptionPane.INFORMATION_MESSAGE);
}
break;
case 8:
// exit program
done=true;
break;
default:
JOptionPane.showMessageDialog(null, "Invalid choice, please choose again ",
"ERROR",JOptionPane.ERROR_MESSAGE);
}
}
}
}
&
&
您必须在switch子句之外定义变量,否则无法在案例3中定义它 编辑 请尝试以下操作:
db.getaccount().withdraw(amount);
而不是:
b.withdraw(amount);
通过这一行,您可以从数据库中获取帐户并在那里进行编辑。获得了-但是这段代码需要案例1中的变量/输入来定义该对象b为什么不将该对象b设置为类的属性?我是新的,我将研究它,不确定这意味着什么。你能给出一个快速的代码示例吗?谢谢!完美,成功。。。我验证了使用joptionpane来显示。我想不出那部分!!!后续问题-使用此代码和joptionpane显示输出。如何在有意义的结果中从arraylist(database=db/deleted)调用对象?我能够显示对对象的引用。我读过关于重载tostring方法的文章,但我不确定我的方向是否正确??
package bankaccount;
import java.util.ArrayList;
public class Database
{
int index;
boolean found;
ArrayList<BankAccount> list;
BankAccount acc;
Database()
{
list = new ArrayList<BankAccount>();
}
void add(BankAccount b)
{
list.add(b);
}
BankAccount remove (int i)
{
return list.remove(i);
}
BankAccount getaccount()
{
return acc;
}
ArrayList getlist()
{
return list;
}
int getindex()
{
return index;
}
boolean inlist()
{
return found;
}
void search (String key)
{
found = false;
int i = 0;
//int.length = list.size();
while (i < list.size() && !found)
{ BankAccount b = list.get(i);
if (key.equals(b.getcustomer().accno))
{
acc = b; found = true; index = i;
}
else
{
i++;
}
}
}
}
package bankaccount;
public class Address
{
String street;
String city;
String state;
String zipcode;
Address (String str, String cty, String st, String zip)
{
street = str;
city = cty;
state = st;
zipcode = zip;
}
static String getstreet(String street)
{
return street;
}
public String getcity()
{
return city;
}
public String getstate()
{
return state;
}
public String getzip()
{
return zipcode;
}
}
db.getaccount().withdraw(amount);
b.withdraw(amount);