1 Replies - 168 Views - Last Post: 24 April 2019 - 12:08 PM Rate Topic: -----

#1 L4WR3NC3   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 24-April 19

BANK ACCOUNT

Posted 24 April 2019 - 11:03 AM

So down below is my bank account code. I've got 3 classes for the program itself. What I'm stuck on is on the main class, "Bank".
What I'm stuck on is once I put in my name and it asks me what I would like to choose. If I choose number 2, or 3 it would always start with number 1 that is "deposit". I'm pretty sure I'm messing up in misusing the if statement and the scanner. Thanks to whoever helps me out in advance!
import java.util.Scanner;
import java.text.NumberFormat;

public class Bank {
	
	private static Scanner input;

	public static void main(String[] args) {
		int num;
		Scanner scan = new Scanner(System.in);
		Account munozAccount = new Account(250, "Lawrence", "Richmond", "", "4/2/2019", "FL", "33445");
		
		input = new Scanner(System.in);
		double data;
		NumberFormat money = NumberFormat.getCurrencyInstance();
		
		System.out.println(munozAccount);
		
		
		System.out.print("Please enter your name: ");
		String name = scan.nextLine();
		
		System.out.println("Welcome, " + name + "!" );
		System.out.println("Please choose one of the following: ");
		System.out.println("1. Deposit \n2. Withdraw \n3. Check Balance");
		
		String number = scan.nextLine(); 
		 
		
		if (number.contentEquals("1")); 
			{
			System.out.print("Please, enter your deposit amount:  ");
			data = input.nextDouble();
			munozAccount.deposit(data);
		 }
		
		  if (number.contentEquals("2")); 
					 {
		System.out.print("Please, enter you withdrawal amount: ");
		data = input.nextDouble();
		munozAccount.withdrawal(data);
		System.out.println("Your balance is: " + money.format(munozAccount.getBalance()));
					 }
		 if  (number.contentEquals("3")); 
		 {
		System.out.println("Your balance is: " + money.format(munozAccount.getBalance()));
		}
	}
}


~~~~~~~~~ END OF FIRST CLASS ~~~~~~~~~~~
import java.text.NumberFormat;

public class Account {
private double balance;
private Customer cust;

/**
* constructor
* pre: none
* post: An account created. Balance and
* customer data initialized with parameters.
 */

public Account(double bal, String fName, String lName, String str, String city, String st, String zip) {
	balance = bal;
	cust = new Customer(fName, lName, str, city, st, zip);
}

/**
 * constructor
 * pre: none
 * post: An account created. Balance and
 * customer data initialized with parameters.
 */
public double getBalance() {
	return(balance);
	}
/**
 * A deposit is made to the account.
 * pre: none
 * post: The balance has been increased by the amount of the deposit.
 */
public void deposit(double amt) {
	balance += amt;
}
/**
 * A withdrawal is made from the account if there is enough money.
 * pre: none
 * post: The balance has been decreased by the amount withdrawn.
 */
public void withdrawal(double amt) {
	if (amt <= balance) {
		balance -= amt;
	} else {
		System.out.println("Not enough money in account.");
	}
}

/**
 * Returns a String that represents the Account object.
 * pre: none
 * postL A string representing the Account object has been returned.
 */
public String toString() {
	String accountString;
	NumberFormat money = NumberFormat.getCurrencyInstance();
	
	accountString = cust.toString();
	accountString += "Current balance is " + money.format(balance);
	return(accountString);
	}


	// TODO Auto-generated method stub	
}


~~~~~~~~~~ END OF 2nd CLASS ~~~~~~~~~~~~~~~~~
public class Customer {
	private String firstName, lastName, street, city, state, zip;
	
	/**
	 * constructor
	 * pre: none
	 * post: A Customer object has been created.
	 */
	public Customer(String fName, String lName , String str, String c, String s, String z) {
		firstName = fName;
		lastName = lName;
		street = str;
		city = c;
		state = s;
		zip = z;
	}
	
	/**
	 * Returns a String that represents the Customer object.
	 * pre: none
	 * post: A string represntin the Account object has been returned.
	 */
	public String toString() {
		String custString;
		
		custString = firstName + " " + lastName + "\n";
		custString += street + "\n";
		custString += city + ", " + state + "  " + zip + "\n";
		return(custString);
	}
}
:code:

~~~~~~~~~~~~~~ END OF 3rd CLASS ~~~~~~~~~~~~~~~~~

This post has been edited by modi123_1: 24 April 2019 - 11:39 AM
Reason for edit:: In the future, please use the [code] tag button in the editor.


Is This A Good Question/Topic? 0
  • +

Replies To: BANK ACCOUNT

#2 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3623
  • View blog
  • Posts: 16,656
  • Joined: 20-September 08

Re: BANK ACCOUNT

Posted 24 April 2019 - 12:08 PM

You're not using your Customer class and you probably should be. Also, the main direction of dependence should probably be Customer has an Account, not the other way around

Only one Scanner on System.in too

This post has been edited by g00se: 24 April 2019 - 12:09 PM
Reason for edit:: Clarification

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1