This site requires JavaScript, please enable it in your browser!
Greenfoot back
divinity
divinity wrote ...

2021/12/9

can someone tell why my program not running on how it supposed to run

divinity divinity

2021/12/9

#
hey pple, I have this banking program that I have build but it not running properly, somewhere along the codes, something is wrong, I dont what, I dont know if the calculation is wrong somewhere along the lilnes . I will post all of my classes so that you can get an idea what I am trying to say and I will also post the outcome when I run it and I will also explain it. what is supposed to be doing and it is not doing. so here goes. first one is the transaction account. after each class I will explain what each class supposed to do when it run transaction account
public class Transaction_Account{
    
    ArrayList <Transaction_Account> newtrans = new ArrayList();
    
    private Date date;
    private double balance;
    private String description;
    private char type;
    private double total_amt;
   
    public Transaction_Account(Date date, char type, double balance, double total_amt, String description){
        
        this.date = new Date();
        this.type = type;
        this.balance = balance;
        this.total_amt = total_amt;        
        this.description=description;
        
    }   

    public Date getDate(){     
        return date;
    }
    public char gettype(){
        return type;
    } 
    public double getbalance(){
        return balance;
    }
    public String getdescription(){
        return description;
    }
    public double gettotal_amt(){
        return total_amt;
    }
   
    public void setDate(Date date){
        this.date=date;
    }
    public void settype(char type){
        this.type=type;
    }
    public void setbalance(double balance){
        this.balance = balance;
    }
    public void setdescription(String description){
        this.description=description;
    }
    public void settotal_amt(double total_amt){
        this.total_amt = total_amt;
    }
    
    @Override
   public String toString(){
       return this.toString()+ "\n"+new Date()+" "+ "Type "+ this.type + " "+ "Account Balance"+ this.balance +" "+"Description"+ this.description;
   }
this transaction account supposed to gave the amt of transaction, gave a new balance after this transaction and the description of the transaction and it is not doing that. it also should be ablto show the date of this transaction, the type of transaction such as "W", for withdrawal, "D' for deposit second class is the saving account
import java.util.Date;


/**
 *
 * 
 */
public class Saving_Account extends Account{
    
    private  float interestRate = 500;
    double balance;
    
    
    public Saving_Account(String firstname, String acctnum,  double balance, float interestRate){
        super(firstname, acctnum, balance);
        this.interestRate=interestRate;
       
    }

     public float getinterestRate(){
        return interestRate;
    }
     
    public void setinterestRate(float interestRate){
        this.interestRate = interestRate/100;
        
    }
    
    private double getBalance(double balance) {
       return balance;
    }
    
    public void setbalance(double balance){
        this.balance = balance;
    }
    public void payInterest(){
      double total_amt = getbalance();      
      double balance = 0;
      balance=(total_amt + (total_amt*getinterestRate()));
      setbalance(balance);
       
      System.out.print("Account Balance with interest is %.2f"+balance);
      Transaction_Account newtrans = new Transaction_Account(new Date(), 'I', balance,0,"Interest Paid");
      transacct.add(newtrans);
      
    }
  
    //withdrawal method
    @Override
    public void withdraw(double total_amt){
        double balance = 0;
        double acctamt = 0;        
        if(acctamt < total_amt){
         System.out.println("Insufficient funds, balance is :$"+ acctamt);
        }
        else{
            balance = acctamt -= total_amt;
            setbalance(balance);
            System.out.print("The withdrawal of: $"+total_amt + " was succesful, the new balance is: $%.2f" + getBalance(balance));
            Transaction_Account newtrans = new Transaction_Account(new Date(), 'W', balance, 0, "Withdraw Made");
            transacct.add(newtrans);            
        }
       
    }
    @Override
      public void deposit(double total_amt){
        double balance;
        double acctamt = getbalance();
        balance = acctamt+=total_amt;
        setbalance(balance);
        System.out.println("Your deposit amount is: $"+ total_amt + "was successful, the new balance is"+ balance);
        Transaction_Account newtrans = new Transaction_Account(new Date(), 'D', balance, 0, "deposit made");
        transacct.add(newtrans);
        
     }  
    @Override
    public String toString(){
        
        return this.toString()+ "Interest Rate"+ interestRate;
    }

}
this saving account supposed calculate the interest rate . and the constructor should initialize all of the parent variables. as well as my own. next is the chequing account
public class Chequing_Account extends Account {
    
    private  float overdraft ;
      double balance;
    
    
    public Chequing_Account(String firstname,  String acctnum, double balance, float overdraft){
        super(firstname, acctnum,  balance);
        this.overdraft = overdraft;
    }

    
    public float getoverdraft(){
        return overdraft=500;
    }
    
    public void setoverdraft(float overdraft){
        if(balance > overdraft){
            System.out.println("Sorry but the limit for the overdraft cannot be exceeded:$"+ overdraft);
        }
                
    }
    
    public void withdrawal(double total_amt){
        double acctamt = getbalance();
        double balance=0;
        
       if(total_amt > balance){
            System.out.println("You can withdraw more than you have");
        }else{
           balance = acctamt-= total_amt;
            setbalance(balance);
            System.out.println("Insufficient funds available ");
            Transaction_Account newtrans= new Transaction_Account(new Date(), 'W', balance, 0, "Withdrawal made");
            transacct.add(newtrans);
       }

    }
     
    @Override
    public void deposit(double total_amt){
        double acctamt = getbalance();
        double balance = 0;
        balance = acctamt+=total_amt;
        setbalance(balance);
        System.out.println("You have deposited: $"+total_amt+ "have been added to your account balance"+ "Your new balance is: $"+total_amt);
        Transaction_Account newtrans = new Transaction_Account(new Date(), 'D', balance,0,"Deposit deposited");
        transacct.add(newtrans);
        
    }
    
    @Override
   public String toString(){
       return this.toString()+ "Over Draft Limit"+ this.overdraft;
   } 
}
this chequing account supposed to have an overdraft limit to which u can widthdraw more that you have and also override the widthdrawal and deposit methods that is to cater for the overdraft limit. also if the overdraft limit is 500, this means that once the balance is 0, the user has the ability to withdraw up to 500 however the balance should show up as -500,for e.g if the user deposit 300, the balance is -200 and to which it is not doing next is the customer class
public class Customer {
    
    private String firstname;
    private String lastname;
    private String email_add;
    private String home_add;
    private String cellnum;
    
    
    
   public Customer(String firstname, String lastname, String email_add, String home_add, String cellnum){
        
        
        this.firstname = firstname;
        this.lastname = lastname;
        this.email_add = email_add;
        this.home_add = home_add;
        this.cellnum= cellnum;
    }

    public String getfirstname(){
        return firstname;
    }
    
     public String getlastname(){
         return lastname;
     }
     
     public String getemail_add(){
         return email_add;
     }
     
     public String gethome_add(){
         return home_add;
     }
     
     public String getcellnum(){
         return cellnum;
     }
     
     public void setfirst_name(String firstname){
         this.firstname=firstname;
     }
     
     public void setlast_name(String lastname){
         this.lastname=lastname;
     }
     
     public void setemail_add(String email_add){
         this.email_add = email_add;
     }
this class should stored all of the information of the customer. up next is the account class
public abstract class  Account{
    
      ArrayList <Transaction_Account> transacct = new ArrayList <>();
      
      private double balance;
      String acctnum;
      String firstname;
      String lastname;
      
     public Account(String firstname, String acctnum, double balance){
         
       this.firstname = firstname;
       this.acctnum = acctnum;
       this.balance = balance;
                
    }
     
    public String getfirstname(){
      return firstname;
    }
    public String getlastname(){
      return lastname;
    }
   
   public String acctnum(){
       return acctnum;
   }
   
   public double getbalance(){
       return balance;
   }
   public void setfirstname(String firstname){
       this.firstname = firstname;
   }
   public void setlastname(String lastname){
       this.lastname = lastname;
   }
  
   public void setacctnum(String acctnum){
       this.acctnum = acctnum;
   }
   public void setbalance(double balance){
       this.balance = balance;
   }
   
    public void withdraw(double total_amt){
        if(balance < total_amt){
            System.out.print("Sorry insufficient funds: $"+ balance);
        }
        else{
            this.balance -= total_amt;
           System.out.println("\n "+ "Your current balance is"+this.balance);           
           Transaction_Account newtrans = new Transaction_Account(new Date(), 'W', this.balance, 0, "Withdrawal made");
           transacct.add(newtrans);
        }
        //end of the withdraw method
    }
    
    //start of the deposit method
    public void deposit(double total_amt){
        this.balance +=total_amt;
        System.out.print("\n"+total_amt+"Your balance is:"+this.balance);
        Transaction_Account newtrans = new Transaction_Account(new Date(), 'D', balance,0, "Deposit deposited");
        transacct.add(newtrans);
    }
    //end of the deposit method
    
    
    //toString method
    @Override
    public String toString(){
        return "Customers details" + this.firstname+" "+this.lastname+" "+this.acctnum+" "+
                +this.balance;
    }
}
the acct class should be able to stored the transaction on the account stored in arraylist, withdraw and deposit methods to withdraw and deposit funds. and this is my main
public class BankAccount {
    
    public static void main(String[]args){
        Scanner keyin = new Scanner(System.in);
        
        ArrayList savings = new ArrayList();
        ArrayList chequing = new ArrayList();
        
        String firstname = null;
        String lastname;
        String home_add;
        String email_add;
        String cellnum;
        char type;
        double balance = 0;
        float interestRate = 0;
        String acctnum = null;
        double total_amt;
        char mychoice ;
        boolean choice = true;
        float overdraft = 0;
        
        Saving_Account sa = new Saving_Account(firstname,  acctnum,  balance, interestRate);
        Chequing_Account ca = new Chequing_Account(firstname, acctnum,  balance, overdraft);
        
        System.out.println("\n"+ "Please select one of the following option");
        System.out.println(" o - To open an account");
        System.out.println(" d - To make a deposit");
        System.out.println(" w - To make a withdraw");
        System.out.println(" i - To pay the interest");
        System.out.println(" t - To view the transactions");
        System.out.println(" q - To quit");
        type = keyin.next().trim().toUpperCase().toLowerCase().charAt(0);
this part of the codes let the user choose which type of transaction they want to do but when I clicked on any one ot the letter the result is not what is supposed to do 
        
        
        while(type != 'q'){
            if(type == 'o'){
                System.out.print("Enter  which type of account(Savings-s)(Chequing-c)");
                mychoice = keyin.next().trim().toUpperCase().toLowerCase().charAt(0);
                if(mychoice == 's'){
                    System.out.print("Enter your first name");
                    firstname = keyin.next();
                    
                    System.out.print("Enter last name");
                    lastname = keyin.next();
                    
                    System.out.print("Enter your address");
                    home_add = keyin.next();
                    
                    System.out.println("Enter your email address");
                    email_add = keyin.next();
                    
                    System.out.print("Enter your cell number");
                    cellnum = keyin.next();
                    
                    System.out.print("Enter account number");
                    acctnum = keyin.next();
                    savings.add(acctnum);
                    
                    System.out.print("Enter initial balance:$"+ balance );
                    balance = keyin.nextDouble();
                    sa.setbalance(balance);
                    
                    System.out.print("Enter the interest rate");
                    interestRate = keyin.nextFloat();
                    sa.getinterestRate();
                    savings.add(acctnum);
                    
                }else if(mychoice == 'c'){
                    System.out.print("Enter your first name");
                    firstname = keyin.next();
                    
                    System.out.print("Enter your last name");
                    lastname = keyin.next();
                    
                    System.out.print("Enter your home address");
                    home_add = keyin.next();
                    
                    System.out.print("Enter your email address");
                    email_add = keyin.next();
                    
                    System.out.print("Enter your contact number ");
                    cellnum = keyin.next();
                    
                    System.out.print("Enter chequing account number");
                    acctnum =  keyin.next();                    
                    chequing.add(acctnum);
                    System.out.print("Enter the initial balance: $"+balance);
                    balance= keyin.nextDouble();
                    ca.setbalance(balance);
                    
                    System.out.print("Enter overdraft limit");
                    overdraft = keyin.nextFloat();
                }
               
                }else if(type == 'd'){
                    System.out.print("Which account to you wish to deposit");
                    if(savings.contains(acctnum)){
                        System.out.print("Enter the amount you want to deposit");
                        total_amt = keyin.nextDouble();
                        sa.deposit(total_amt);
                        System.out.print("Total balance account is:$"+balance);
                        balance = keyin.nextDouble();
                    }
                    
                }else if(type == 'w'){
                    System.out.print("Which account you want to withdraw from");
                    acctnum = keyin.next();
                    if(savings.contains(acctnum)){
                        System.out.print("Enter the amount that you want to withdraw");
                        total_amt = keyin.nextDouble();
                        ca.withdraw(total_amt);
                    }
                }else if(savings.contains(acctnum)){
                    sa.payInterest();
                }else{
                    System.out.print("");
                }
            }if(type == 't'){
                System.out.print("Enter an account number");
                acctnum = keyin.next();
                if(savings.contains(acctnum)){
                    System.out.print("\n"+ "List of transaction done on this savings account");
                    System.out.print(sa.transacct.toString());
                    
                }else if(chequing.contains(acctnum)){
                    System.out.print("\n" + "List of transaction done on this chequing account");
                    System.out.print(ca.transacct.toString());
                }
               
            }else if(type == 'q'){
                
            }
              
         }
                 
     }         
for e.g if the user clicked on the 'o' to open an account ( it keep loopin) here is the outcome
Enter  which type of account(Savings-s)(Chequing-c)
c
Enter your first name
jen
Enter your last name
zen
Enter your home address
port of spain
Enter your email addressEnter your contact number Enter chequing account number
jzen@mail.com
Enter the initial balance: $0.0
200
Enter overdraft limit
500
Enter  which type of account(Savings-s)(Chequing-c)
it keep looping when u click on "d" to deposit, the outcome is this
divinity divinity

2021/12/9

#
continuing from my post above when u click on "d" to deposit, the outcome is this which account u wish to deposit; and this line is to long.( the result is give u a long line across the console (cant get to copy and paste it, it keep freezing up when I try to copy it. when i click the 'W" to withdraw this is the result w Which account you want to withdraw from s Which account you want to withdraw fromc (it keep looping) Which account you want to withdraw from when I click "i" to pay the interest, ths result is this nothing and also if i clcik the 't' to view the transaction on anyone of the acct, i got not as the result. can someone view my codes and class and tell me where i have gone wrong. plz and thanks and yes I know it is kinda long
danpost danpost

2021/12/9

#
divinity wrote...
it keep looping when u click on "d" to deposit
In BankAccount class, change line 132 to:
}
Add the following at line 133:
if (type == 'q') break;
type = ' ';
Then change line 37 to:
while (true)
divinity divinity

2021/12/10

#
hey danpost, i tried implement what u have there but all I am getting is a set of errors. I will put in the errors later when I reach home, not getting the internet while I am at work here at the moment
divinity divinity

2021/12/29

#
hey danpost, I implement what u say but all I am getting is a set of errors saying, break outside switch or loop. I really need these codes to work, can someone really take a good looks at my codes and tell me what is my mistakes, where i have gone wrong and how i can fix it. plz
divinity divinity

2021/12/29

#
please take a look through the savings account class, chequeing account class, transaction account class, account class, customer class, look at my arraylist in both the account, transaction classes, and take a look at the on in my main, and plz tell me if there is a mistake some where. In the account class, (abstract class) it say that it should store the customer details such as an account number (string), balance (double value), and transactions on the account that is stored in an arraylist. tell me if what I have is the correct thing. In the chequing account, which has an overdraft limit but the user can withdraw more than u have but with that, it has an overdraft limit of 500 which means once your balance is 0, the user has the ability to withdraw up to 500 but the balance should show up as -500. and if you deposit 300, the user balance should also show up as -200. additional specification: is I have to use an arraylist to store the accounts and in that each element can store a reference to a saving account object or chequing account object. with this additional information, I hope that I can get some help. thanks in advance
danpost danpost

2021/12/29

#
Please show your updated BankAccount class codes.
danpost danpost

2021/12/29

#
Account class appears to have the required fields. Chequing_Account class overdraft implementation does not look good. Will have to work on that. ArrayLists in BankAccount class can be improved upon. Use just one arraylist:
ArrayList<Account> accounts = new ArrayList<Account>();
If you need to determine if an account is a checking or a savings account, you can use the instanceof conditional. The following checks if an account, reference by acc, is a Saving_Account object:
if (acc instanceof Saving_Account)
divinity divinity

2021/12/30

#
hey danpost
ArrayList savings = new ArrayList();
        ArrayList chequing = new ArrayList();
        
these two arraylist is to check to see if the savings and chequing account contain the account numbers
danpost danpost

2021/12/30

#
In your BankAccount class codes above, lines 59 and 68 seem to be duplicates. Also, what is to prevent the same account number from being entered in twice (or more times)?
divinity divinity

2021/12/30

#
hi danpost, yes I see my mistakes there, thanks for pointing that out to me, is there anything that i miss
You need to login to post a reply.