有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java获取ArrayList以保持每个帐户的余额和数量

我已经完成了我的计划,但在获取每个帐户余额方面遇到了困难

比如说

如果我输入第一个储蓄账户余额,它将正常工作。但是,当我打开另一个储蓄账户时,第一个账户的账户余额将显示我输入的最后一个账户余额。我想知道我必须做什么来更正此错误。任何帮助都可以。谢谢

package Object_1_Programs;
import  java.util.Date;
/**
 *
 * 
 */
public abstract class Account {

    //instance variables decleared
    private String c_name;
    private String acc_num;
    private double acc_balance;
   java.util.ArrayList transactions=new java.util.ArrayList();

   //default constructor 
    public Account(){


    }
    //Constructor

    public Account(String c_name,String acc_num,double  acc_balance){
        this.c_name=c_name;
        this.acc_num=acc_num;
        this.acc_balance=acc_balance;
    }

    //getters and setter method
    public String setName(){
        return this.c_name;
    }    

    public String getCname(){
        return this.c_name;
    }

    public String SetAccnum(String acc_number){
        return this.acc_num;
    }

    public String GetAccNum(){
        return this.acc_num;
    }

    public double setBalance(double balance){
        return this.acc_balance=balance;
    }

    public double getBalance(){
        return this.acc_balance;
    }
//end of getters an setters method

    //toString method decleared
    @Override
    public String toString(){
        return "Customer Name"+this.c_name   +"Account Number:"+this.acc_num; 
    }

    //beginning of withdrawal method
    public void  withdraw(double total){
        if(acc_balance<total){
            System.out.print("Not enough funds sorry your current balance is "+acc_balance);
        }
        else{
        this.acc_balance -= total;
        System.out.print("\n"+"Your Balance is"+ this.acc_balance);}

        Transaction t1 = new Transaction(new Date(), 'W', this.acc_balance, "Withdrawal Made");
        transactions.add(t1);

    }
    //end of withdrawal method


    //beginning of deposit method
   public void deposit(double total){
       this.acc_balance += total;
       System.out.printf("\n"+total+"Your Balance is"+ this.acc_balance);
        Transaction t1 = new Transaction(new Date(), 'D', this.acc_balance, "Deposit Made");
        transactions.add(t1);


   }
   //end of deposit method


}

This is the transaction class

package Object_1_Programs;

//Date decleared
import java.util.Date;

/**
 *
 * @
 */
public class Transaction {

    //instance variables decleared
    private Date date;
    private char type;
    private double amount;
    private double acc_balance;
    private String description;

    //Constructor
    public Transaction(Date date, char type, double acc_balance, String description) {
        this.acc_balance = acc_balance;
        this.date = new Date();
        this.type = type;

        this.description = description;
    }

//getters and setters method decleared
    public Date getDate() {
        return date;
    }

    public Date setDate() {
        return date;
    }

    public char getType() {
        return type;
    }

    public char setType() {
        return type;
    }

    public double getBalance() {
        return acc_balance;
    }

    public double setBalance() {
        return acc_balance;
    }

    public double getAmount() {
        return amount;
    }

    public double setAmount() {
        return amount;
    }

    public String getDescription() {
        return description;
    }

    public String setDescription() {
        return description;
    }
    //Tostring method 
    @Override
    public String toString() {  
        return "\n"+new Date()      + "Type : " + type     +      "Account Balance " + acc_balance + "Description " + description;
    }
}


This is the Saving Account Class

import java.util.Date;

/**
 *
 * @
 */
public class SavingsAccount extends Account {
// instance variables decleared
    private double int_rate;

    //default constrictor
    public SavingsAccount(){
      int_rate=0;  
    }
//main construtor
    public SavingsAccount(String c_name, String acc_num, double acc_balance, double int_rate) {
        super(c_name,acc_num, acc_balance);
        this.int_rate = int_rate;
    }
//getters and setters method decleared
    public double setRate(double rate) {
        return int_rate=rate/100;
    }

    public double getRate() {
        return int_rate;
    }


//payInterest method decleared
    public void payInterest() {
        double acc_amount=getBalance();
        double balance=0;
        balance = (acc_amount + (acc_amount *getRate()));
        setBalance(balance);

        System.out.printf("Account Balance With Interest is %.2f", setBalance(balance));
        Transaction t1 = new Transaction(new Date(),    'I'     ,        setBalance(balance), "Interest Paid");
        transactions.add(t1);

    }
//Withdrawal method override
    @Override
    public void withdraw(double total) {
        double acc_amount = getBalance();
        double balance = 0;

        if (acc_amount < total) {
            System.out.printf("Not enough funds your main account balance is :$%.2f", acc_amount);
        } else {

                    balance=acc_amount -= total;
                   setBalance(balance);
            System.out.printf("Your withdrawal of :$" + total + " was successful your new  account balance is :$%.2f",setBalance(balance));
        }
        Transaction t1 = new Transaction(new Date()     ,    'W'     ,       setBalance(balance), "Withdrawal Made");
        transactions.add(t1);


    }
//Deposit method override
    @Override
    public void deposit(double total) {
        double balance;
        double acc_amount = getBalance();
        balance= acc_amount+=total;
        setBalance(balance);
        System.out.printf("Your deposit of :$" + total + " was successful your new account balance is :$%.2f",setBalance(balance));
        Transaction t1 = new Transaction(new Date()     ,     'D'    , setBalance(balance),  "Deposit Made");
        transactions.add(t1);

    }

    @Override
    public String toString() {
        return this.toString() + "Interest Rate: " + int_rate;
    }


}


This is the Chequing Class

package Object_1_Programs;

import java.util.Date;

/**
 *
 * 
 */
public class ChequingAccount extends Account {
// instance variable decleared
    private double over_draft;
// default constructor
    public ChequingAccount() {
        this.over_draft = 500;
    }
    //end of default constructor


//Main Constructor begins
    public ChequingAccount(String c_name, String acc_num, double acc_balance) {
        super(c_name, acc_num, acc_balance);

    }
    //End of Main Method


// Getters and Setters method
    public double setOverDraft() {
        return over_draft = 500;

    }


//To string override
    @Override
    public String toString() {
        return this.toString() + "Over Draft Limit :" + over_draft;
    }

    //Withdrawal method override
    @Override
    public void withdraw(double amount) {
        double acc_amount = getBalance();
        double balance = 0;
        if (amount >balance &&  amount>  over_draft) {
            System.out.println("Sorry I cannot give you that amount choose a lower amount or tell d girl yuh cah remember yuh pin");
        } 
        else {
              balance= acc_amount-= amount;
              setBalance(balance);

        }
        System.out.printf(amount+ "Your new balance is %.2f", setBalance(balance));
        Transaction t1 = new Transaction(new Date()     , 'W',       setBalance(balance), "Withdrawal Made:");
        transactions.add(t1);

    }

    //Deposit Method override
    @Override
    public void deposit(double amount) {
        double acc_amount = getBalance();
        double balance=0;
        balance=acc_amount += amount;
        setBalance(balance);
        System.out.printf("Your deposit of :$" + amount + " was successful your new account balance is :$%.2f",setBalance(balance));
        Transaction t1 = new Transaction(new Date()     , 'D'    ,       setBalance(balance),      "Deposit Made:");
        transactions.add(t1);


    }


}


This is my test program

public class Account_Records_Test {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);


        ArrayList Savings =new ArrayList();
        ArrayList Chequing=new ArrayList();



        String name;
        char type;
        char selection;
        double balance;
        double rate;
        String acc_num;       
        double total;


        SavingsAccount s1 = new SavingsAccount();
        ChequingAccount c1 = new ChequingAccount();



        System.out.println("\n" + "Please select one of the following options");
        System.out.println("|  o-To Open Account       |");
        System.out.println("|  d-To Make A Deposit     |");
        System.out.println("|  w-To Make A Withdraw    |");
        System.out.println("|  i-To Pay Interest       |");
        System.out.println("|  t-To View Transactions  |");
        System.out.println("|  q-To Quit               |");
        type = input.next().trim().toUpperCase().toLowerCase().charAt(0);

        while (type != 'q') {

            if (type == 'o') {
                System.out.print("Please select the type of account you will like to open s-Saving or c-Checking :");
                selection = input.next().trim().toUpperCase().toLowerCase().charAt(0);
                if (selection == 's') {
                    System.out.print("Please enter Account Holder Name");
                    name=input.next();

                    System.out.print("Please enter a Savings Account Number : ");
                    acc_num = input.next();
                    Savings.add(acc_num);

                    System.out.print("Please enter starting balance :$");
                    balance = input.nextDouble();
                    s1.setBalance(balance);



                    System.out.print("Please enter the courrent interest rate :");
                    rate = input.nextDouble();
                    s1.setRate(rate); 
                    Savings.add(acc_num);





                } else if (selection == 'c') {
                    System.out.print("Please enter a Chequing Account Number :");
                    acc_num = input.next();
                    Chequing.add(acc_num);
                    System.out.print("Please enter starting balance :$");
                    balance = input.nextDouble();
                    c1.setBalance(balance);
                }


            } else if (type == 'd') {
                System.out.print("Please enter the account number you wish to make a deposit to :");
                acc_num= input.next();

                if (Savings.contains(acc_num)) {
                    System.out.print("Please enter how much you will like to deposit to your Savings Account :$");
                    total = input.nextDouble();
                    s1.deposit(total);
                } else if (Chequing.contains(acc_num)) {
                    System.out.print("Please enter how much you will likt to deposit to your Chequing Account :$");
                    total = input.nextDouble();
                    c1.deposit(total);
                }

            } else if (type == 'w') {
                System.out.print("Please enter an account number  :");
                acc_num = input.next();
                if (Savings.contains(acc_num)) {
                    System.out.print("please enter the amount you wish to withdraw from your Saving Account");
                    total = input.nextDouble();
                    s1.withdraw(total);
                } else if (Chequing.contains(acc_num)) {
                    System.out.print("Please enter the amount you wish to withdraw from your Chequing Account");
                    total = input.nextDouble();
                    c1.withdraw(total);
                }
            } else if (type == 'i') {
                System.out.print("Please enter your account");
                acc_num = input.next();
                if (Savings.contains(acc_num)) {
                    s1.payInterest();
                } else {
                    System.out.print("Sorry This account isnt a Savings Account");
                }
            } else if (type == 't') {
                System.out.print("Please enter a account number");
                acc_num = input.next();
                if (Savings.contains(acc_num)) {
                    System.out.printf("\n" + "Transaction History for Account :%s", acc_num);
                    System.out.println(s1.transactions.toString());

                }
                else if(Chequing.contains(acc_num)) {
                    System.out.printf("\n" + "Transaction History for Account :%s", acc_num);
                    System.out.println(c1.transactions.toString());

                }

            }
            else if(type=='q'){
                break;
            }

            System.out.println("\n" + "Please select one of the following options");
            System.out.println("|  o-To Open Account       |");
            System.out.println("|  d-To Make A Deposit     |");
            System.out.println("|  w-To Make A Withdraw    |");
            System.out.println("|  i-To Pay Interest       |");
            System.out.println("|  t-To View Transactions  |");
            System.out.println("|  q-To Quit               |");
            type = input.next().trim().toUpperCase().toLowerCase().charAt(0);
        }

    }
}


Any help will do thanks

共 (2) 个答案

  1. # 1 楼答案

    您的问题是您只创建了一个SavingsAccount对象(s1)。每次你“开户”时,你都会覆盖你的储蓄账户。我建议创建一个存储帐户的ArrayList,如下所示:ArrayList accounts=new ArrayList();。每次开户时,将创建的SavingsAccount对象添加到“accounts”中

    还值得一提的是,“ArrayList name=new ArrayList();”语法已弃用。现在应该在ArrayList声明中添加一个标识符。标识符(表示为)指定ArrayList保存的对象类型

  2. # 2 楼答案

    还有许多其他问题。例如,在deposit()中:

    balance=acc_amount += amount;
    

    。。。应该是:

    balance = acc_amount + amount;
    

    。。。以及:

    System.out.printf("Your deposit of :$" + amount + " was successful your new account balance is :$%.2f",setBalance(balance));
    

    。。。就像在withdraw()中一样,setBalance()应该是getBalance()。后一个问题也发生在{}

    虽然setBalance()可以在这些上下文中使用,因为它被设计用于返回余额和设置余额,但在尝试打印出余额时,存在将余额错误设置为其他值的风险

    还有许多重复的代码可能是refactored,明智的做法是仔细考虑一个比必要的更复杂的设计,因为它将报告余额与行动交易紧密结合在一起