0

プログラムを完了しましたが、各アカウントの残高を取得するのに行き詰まっています

例えば

最初の普通預金口座の残高を入力すると正常に機能しますが、別の普通預金口座を開くと、最初の口座の口座残高には、最後に入力した口座残高が表示されます。このエラーを修正するにはどうすればよいですか?助けてくれますありがとう

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
4

2 に答える 2

0

また、他にも多くの問題があります。たとえば、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()設定するだけでなく残高を返すように設計されているため、これらのコンテキストで使用できますが、残高が何であるかを出力しようとしているときに、残高を誤って別のものに設定するリスクがあります。

また、リファクタリングできる重複コードも多数あります。必要以上に複雑な設計についてよく考えるのが賢明です。これは、残高の報告と実行中のトランザクションが密接に組み合わされているためです。

于 2013-04-20T01:28:33.350 に答える
0

問題は、SavingsAccount オブジェクト (s1) を 1 つしか作成していないことです。「口座を開く」たびに、作成した 1 つの SavingsAccount が上書きされます。SavingsAccounts の ArrayList を次のように作成することをお勧めします: ArrayList accounts = new ArrayList();。口座を開くたびに、作成した SavingsAccount オブジェクトを「口座」に追加します。

「ArrayList name = new ArrayList();」ということも知っておく価値があります。構文は非推奨です。ここで、ArrayList 宣言に識別子を入れる必要があります。として示される識別子は、ArrayList が保持するオブジェクトのタイプを指定します。

于 2013-04-20T01:15:12.670 に答える