Thursday, December 15, 2011

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 -%26gt; ");


choice = s.nextInt();


if (choice == 1){


System.out.print("Enter amount to deposit -%26gt; ");


deposit = s.nextDouble();


transact.setAmount((transact.getAm鈥?+ deposit));


}


if (choice == 2){


System.out.print("Enter withdrawal amount -%26gt; ");


withdrawal = s.nextDouble();


transact.setAmount((transact.getAm鈥?- withdrawal));


}


if (choice==3){


System.out.println("Current Balance-%26gt; " + transact.getAmount());


}


}while(choice !=0);


System.out.println("Current Balance -%26gt; " + transact.getAmount());


System.out.println("Total withdrawal amount -%26gt; " + transact.getWithdrawal());


System.out.println("Total deposit amount -%26gt; " + 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.|||You need a datastore for Transaction. Datastore can be an array or it can be ArrayList%26lt;Transaction%26gt; 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

No comments:

Post a Comment