繁体   English   中英

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

[英]Getting The ArrayList To Keep Each Account Balance And Number

我已经完成了计划,无法获得每个帐户的余额

例如

如果我输入第一个储蓄帐户余额,则可以正常工作,但是当我打开另一个储蓄帐户时,第一个帐户的账户余额将显示我输入的最后一个账户余额,我想知道该怎么做才能更正此错误帮助会做的谢谢

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

您的问题是您仅创建了一个SavingsAccount对象(s1)。 每次“开设帐户”都将覆盖您创建的一个SavingsAccount。 我建议像这样创建一个SavingsAccounts的ArrayList:ArrayList accounts = new ArrayList();。 每次打开帐户时,请将您创建的SavingsAccount对象添加到“帐户”中。

还应该知道“ ArrayList名称= new ArrayList();”。 不推荐使用语法。 现在,您应该在ArrayList声明中放入一个标识符。 标识为的标识符指定ArrayList持有的对象的类型。

还有许多其他问题。 例如,在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() 后一个问题也发生在withdraw()

尽管setBalance()可以在这些情况下使用,因为它已被设计为返回余额并设置余额,但有可能在尝试仅打印出余额时将其错误地设置为其他余额。

还可以重构很多重复的代码,明智地考虑使设计变得比必需的更为复杂的设计是明智的,因为它将报告余额与实际交易紧密结合在一起。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM