Java日期方法?类?joptionpane/从数据库输出/显示arraylist中的对象?
需要案例5和7的帮助 5显示所有对象和相关变量=(日期/账户#/名称在1行+地址在下四行+余额在最后一行 我不知道如何嵌套方法来执行上述操作/格式化输出 这看起来应该是这样的,但一直给我java语言空指针异常Java日期方法?类?joptionpane/从数据库输出/显示arraylist中的对象?,java,swing,date,arraylist,nest,Java,Swing,Date,Arraylist,Nest,需要案例5和7的帮助 5显示所有对象和相关变量=(日期/账户#/名称在1行+地址在下四行+余额在最后一行 我不知道如何嵌套方法来执行上述操作/格式化输出 这看起来应该是这样的,但一直给我java语言空指针异常 if(!db.list.isEmpty()) { for (int i = 0; i < db.list.size(); i++) { String s = " "; s = "hello
if(!db.list.isEmpty())
{
for (int i = 0; i < db.list.size(); i++)
{
String s = " ";
s = "hello 123 "+db.getaccount().getcustomer().getAccountNumber()
+"\nBalance is: "+db.getaccount().balance;
JTextArea text = new JTextArea(s, 6, 20);
JScrollPane pane = new JScrollPane(text);
JOptionPane.showMessageDialog(null, pane,
"Current Customers List", JOptionPane.INFORMATION_MESSAGE);
}}break;
&
&
&
您可以使用
java.util.Calendar
类来处理日期。
您还可以查看java.util.Date
,但许多日期方法都已弃用,不应使用
public class Main {
public static void main(String [] args){
System.out.println(Calendar.getInstance().getTime());
}
}
非常感谢!这解决了我的日期问题。关于如何循环数据库中的所有对象并让它们返回,有什么建议吗?代码位于我的循环的顶部-如果您能帮助的话,我很感激=)
package bankaccount;
public class BankAccount
{
Customer cust;
double balance;
BankAccount (Customer c,double b)
{
cust = c;
balance = b;
}
void deposit (double amt)
{
balance = balance + amt;
}
void withdraw (double amt)
{
balance = balance - amt;
}
double getbalance ()
{
return balance;
}
Customer getcustomer ()
{
return cust;
}
}
package bankaccount;
public class Customer
{
Name name;
Address addr;
String accno;
Customer (Name n, Address addy, String acc)
{
name = n;
addr = addy;
accno = acc;
}
Name getname()
{
return name;
}
Address getAddress()
{
return addr;
}
String getAccountNumber()
{
return accno;
}
void changeAccountNumber(String acc)
{
accno = acc;
}
}
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;
}
}
package bankaccount;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JOptionPane;
public class IO
{
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
static double getDouble(String s)
{
return Double.parseDouble(getString(s));
}
static int getInt(String s)
{
return Integer.parseInt(getString(s));
}
static String getString(String s)
{
return JOptionPane.showInputDialog(s);
}
static void notFound() //Display not found
{
JOptionPane.showMessageDialog(null, "Account has not been found, "
+ "please try again");
}
void date()
{
JOptionPane.showMessageDialog(null, "Current date is: ")+dateFormat.format(cal.getTime());
}
}
public class Main {
public static void main(String [] args){
System.out.println(Calendar.getInstance().getTime());
}
}