Why cannot we use static keyword inside a method in java?

Java help. What code/method should i use for this program to work?

  • i have this program it's kinda like a bank program i can deposit and withdraw. and at the same time, i can show my current balance. 1 is for deposit 2 is for withdraw 3 is for current balance and 0 is for Exit in 'Exit', the program shows the current balance after one or more instances of deposit or withdrawal. (now here's what i don't get) in Exit, it's also supposed to display the total withdrawal amount and the total deposit amount. How do i read my input from all the amounts for deposit/withdrawal that i've entered in the program? And how will I be able to add them all? separately (deposit and withdrawal). CLASS ONE: RUNNER package app.bank; import java.util.Scanner; public class Runner { public static void main(String []args){ int choice=0; double deposit=0; double withdrawal=0; Transaction transact = new Transaction(); do { Scanner s = new Scanner(System.in); System.out.print("1.)Deposit 2.)Withdrawal 3.)Current Balance 0.)Exit -> "); choice = s.nextInt(); if (choice == 1){ System.out.print("Enter amount to deposit -> "); deposit = s.nextDouble(); transact.setAmount((transact.getAm… + deposit)); } if (choice == 2){ System.out.print("Enter withdrawal amount -> "); withdrawal = s.nextDouble(); transact.setAmount((transact.getAm… - withdrawal)); } if (choice==3){ System.out.println("Current Balance-> " + transact.getAmount()); } }while(choice !=0); System.out.println("Current Balance -> " + transact.getAmount()); System.out.println("Total withdrawal amount -> " + transact.getWithdrawal()); System.out.println("Total deposit amount -> " + transact.getDeposit()); } } CLASS TWO: TRANSACTION package app.bank; public class Transaction { private double amount; private double withdrawal; private double deposit; public Transaction(){ amount=0; withdrawal=0; deposit=0; } public void setAmount(double amount){ this.amount=amount; } public double getAmount(){ return amount; } } I guess Class Two: Transaction is lacking the code for that part. But I dont really know how to use that. Hoping you could help. Thanks in advance.

  • Answer:

    You need a datastore for Transaction. Datastore can be an array or it can be ArrayList<Transaction> I don't know how far along your studies are. With a datastore you could do methods like... private String showBalance() { Double total = 0.0; for( Transaction t : transactions ) { total += t.getAmount(); return String.format( "$%.2f", total ); } and that method goes into that first Runner class Transaction really should be subclassed Deposit extends Transaction Check extends Transaction put specific behavior methods in the subclasses that block an overdraft, for instance

Alyssa at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.