Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Prog8 {
- public static void main(String[] args) {
- //variables
- char operation;
- Scanner reader = new Scanner (System.in);
- double amt = 0.0;
- Account acc = new Account();
- while (reader.hasNext()) {
- operation = reader.next(".").charAt(0);
- if (operation == 'd') {
- amt = reader.nextDouble();
- acc.deposit(amt);
- }
- else if (operation == 'w') {
- amt = reader.nextDouble();
- acc.withdraw(amt);
- }
- else if (operation == 'i') {
- acc.addInterest();
- }
- else if (operation == 'p') {
- acc.getBalance();
- }
- else {
- System.out.println("** Invalid Operation **");
- }
- }
- }
- }
- public class Account {
- //variables
- private int accountID;
- private double accountBalance;
- public Account() {
- accountID = (int)(Math.random() * (9000)) + 1000;
- accountBalance = 0.0;
- }
- //withdraw operation
- public void withdraw(double amount) {
- if(amount > accountBalance)
- System.out.printf("Error: cannot withdraw $%.2fn",amount);
- else {
- accountBalance -= amount;
- System.out.printf("$%.2f withdrawn from account %dn",amount,accountID);
- }
- }
- //deposit operation
- public void deposit(double amount) {
- accountBalance += amount;
- System.out.printf("$%.2f deposited into account %dn",amount,accountID);
- }
- //add interest
- public void addInterest() {
- double interest = accountBalance * 0.005;
- accountBalance += interest;
- System.out.printf("$%.2f interest added to accountn",interest,accountID);
- }
- //account balance
- public void getBalance() {
- System.out.printf("Current balance for account %d:
- $%.2fn",accountID,accountBalance);
- }
- }
Add Comment
Please, Sign In to add comment