【发布时间】:2020-07-03 13:50:26
【问题描述】:
我正在尝试将值传递给名为“Account”的超级类,然后将每个 obj 存储到一个数组列表中。
我的班级账户代码
abstract class account{
String number;
String name;
int amount;
int newbalance;
static final int balance = 1000;
int bal;
public account(){
// used to store the parameter passed into constructor
}
int deposit(int i) {
return i;
}
void withdrawal(int i){
}
public void setData(String string, String string2, int i) {
// TODO Auto-generated method stub
}
public void showData() {
// TODO Auto-generated method stub
}
}
在我的主要方法中,我想创建一个对象,然后传入要存储在数组列表中的 name 、 number 和 amount 的值,因为我想要为它们存储多个具有不同名称的对象 number 和 amount .然后我想访问子类“Sbaccount”并更新“newbalance”变量
我的 main 代码
public class oopassignment {
public static void main(String[] args) {
String type;
Scanner input = new Scanner (System.in);
System.out.println("What type of account do you want to create? :");
type=input.nextLine();
Account sb;
sb = new sbaccount();
//sb.setData("Jospeh", "0563994",50000);
sb.setData("Joohn", "009734",50000);
sb.setData("hope", "05634",50000);
ArrayList <sbaccount> usera = new ArrayList<sbaccount>();
usera.add((sbaccount) sb);
usera.add((sbaccount) sb);
for (int i = 0; i < usera.size(); i++){
usera.get(i).showData();
//userb.
}
}
还有我的子类“sbaccount”的代码
final class sbaccount extends Account {
int total = 0;
public sbaccount(){
super ();
}
public void setData(String name, String number, int amount){
this.name = name;
this.number= number;
this.amount= amount;
}
public void showData(){
System.out.println("Account Owner: "+name);
System.out.println("Account number: "+ number);
System.out.println("Your New Amount is "+ newbalance);
System.out.println("Your Account Balance is: $"+ total);
return;
}
int deposit ( int money){
newbalance = money + amount;
System.out.println("Hi "+ name
+" \nAccountnumber: "+ number
+" \nYou have deposited :$"+money);
System.out.println("\nYour Account balance is now :"+ newbalance);
return newbalance;
}
void withdrawal(int withdraw){
int total = 0;
total = bal - withdraw;
if (total <= balance){
System.out.println("Your balance is too low for withdrawal!");
}
else{
System.out.println("\nHi "+ name
+" \nAccountnumber: "+ number
+" \nYou have Withdrawn :$"+withdraw);
System.out.println("\nYour Account balance is now :"+ total);
}
}
}
【问题讨论】:
-
我想你忘了说你的问题是什么。
-
嗨@Amongalen!我的问题是在我编译后,当我尝试从数组列表中检索数据时,我无法检索已经存储的数据。 1)我的对象似乎没有正确地存储在数组中?值它不断被覆盖。 2)如何从数组列表中检索数据?