0

Hi Guys,

I really need help with my checkbook. My functions seem to be working. However when I can deposit, balance, or withdraw from the userinterface it doesn't seem to store the updates. When I check my balance it always says the balance is 0.00. Can someone help me fix this. Thanks!

import java.io.*;
import java.util.Scanner;

public class Checkbook2 {
	private String name;
	private double balance;
	private int checkNum;
	private double withdraw;
	private double deposit;
	private double withdrawTotal;
	double balanceTotal;
	//private double depositTotal;
	Scanner scan = new Scanner(System.in);

	// Default constructor
	Checkbook2() {
		name = null;
		balance = 0;
		withdraw = 0;
		deposit = 0;
	}//End Checkbook


	// Accessor methods
	public String getName(){
		return name;
	}//End getName
	
	private int getCheckNum() {
		return checkNum;
	}//End getCheckNum

	public double getBalance(){
		System.out.println("Your balance is : " + balance);
		return balance;
	}//End getBalance

	public double getWithdraw(){
		return withdraw;
	}//End getWithdraw

	public double getDeposit(){
		return deposit;
	}//End getDeposit

	public double getWithdrawTotal() {
		return withdrawTotal;
	}//End getWithdrawTotal

	// Mutator methods
	public void setName(String newName){
		name = newName;
	}//End setName

	public void setCheckNum(int newCheckNum){
		checkNum = newCheckNum;
	}//End setCheckNum
	
	public void setBalance(double newBalance){
		balance = newBalance;
	}//End setBalance

	public void setWithdraw(double newWithdraw){
		withdraw = newWithdraw;
	}//End setWithdraw

	public void setDeposit(double newDeposit){
		deposit = newDeposit;
	}//End setDeposit


	public double withdraw() {
		return balance - withdraw;
	}//End Withdraw
	
	public double withdraw(double withdraw){
		return balance - withdraw;
	}

	public double withdrawTotal(){
		withdrawTotal = balance - withdraw;
		return withdrawTotal;
	}
	
	public double balanceTotal(){
		double balanceTotal = balance + withdrawTotal + deposit;
		return balanceTotal;
	}
	public double deposit(){
		return withdrawTotal + deposit;
	}//End Deposit
	
	public double deposit(double deposit){
		return withdrawTotal + deposit;
	}

	// void method to display the account
	public void Registry(String name, int checkNum, double balance, double withdraw, double deposit) {
		Checkbook2 check = new Checkbook2();
		check.setName(name);
		check.setCheckNum(checkNum);
		check.setBalance(balance);
		check.setWithdraw(withdraw);
		check.setDeposit(deposit);

		// Displaying using the get methods
		System.out.println("Your name is:" + check.getName());
		System.out.println("Your check number is : " + check.getCheckNum());
		check.getBalance();
		System.out.println("Amount of withdrawal is:" + check.getWithdraw());
		System.out.println("The Balance after withdrawal is:" + check.withdrawTotal());
		System.out.println("Amount of deposit is:" + check.getDeposit());
		System.out.println("The balance after deposit is : " + check.deposit());
		
	}//End Registry

	public void fileWriter(){
		//Checkbook2 write = new Checkbook2();
		//System.out.println("Unavailable");
		try{
			BufferedWriter out = new BufferedWriter(new FileWriter("Checkbook"));	
			out.write(getName());
			out.write(getCheckNum());
			out.write((int) getBalance());
			out.close();
		}
		catch(IOException e){
			System.out.println("ERROR: Could not write to file!");
			System.exit(1);
		}
	}

	public void fileReader(){
		//System.out.println("Unavailable");
		try{
			BufferedReader in = new BufferedReader(new FileReader ("Checkbook"));
			String str;
			while((str = in.readLine()) != null){
				System.out.println(str);
			}
			in.close();
		}catch(IOException e){
			System.out.println("ERROR: Could not read from file!");
			System.exit(1);
		}
	}
	
	public static void main (String[] args){
		//Checkbook2 checking = new Checkbook2();
	}
}//End Main
import java.io.IOException;
import java.util.Scanner;

public class userInterface  extends Checkbook2{
	public void driver () throws IOException{
		//Variable records the user's number entered to navigate the menu
		int option = 0 ;
		Scanner scanner = new Scanner(System.in);
		//Menu prompt
		System.out.println("Choose an option :");
		System.out.println("1. Store check information ");
		System.out.println("2. Make a deposit ");
		System.out.println("3. Make a withdrawal ");
		System.out.println("4. Check Balance ");
		System.out.println("5. Save Account ");
		System.out.println("6. Read Account ");
		System.out.println("7. Quit ");

		//Take in choice variable to navigate the menu
		option = scanner.nextInt();
		System.out.println("");
		String answer = null;
		//Switch controlling the menu system
		switch (option){
		//add to the registry
		case 1:
			//System.out.println("Unavailable");
			System.out.println("Please enter your name");
			String name = scan.next();
			System.out.println("Please enter your check number");
			int checkNum = scan.nextInt();
			System.out.println("Balance");
			double balance = scan.nextDouble();
			System.out.println("Withdraw");
			double withdraw = scan.nextDouble();
			System.out.println("Deposit");
			double deposit = scan.nextDouble();
			Registry(name,checkNum, balance, withdraw, deposit);
			System.out.println("Would you like to make another transaction ? Y/N");
			answer = scanner.next();
			if(answer.equalsIgnoreCase("y")|| answer.equalsIgnoreCase("yes")){
				driver();
			}else if(answer.equalsIgnoreCase("n")|| answer.equalsIgnoreCase("no")){
				System.out.println("Goodbye!");
				System.exit(0);
			}else{
				System.out.println("Invalid Selection!");
				System.exit(1);
			}
			break;
			//makes a deposit
		case 2:
			//System.out.println("Unavailable");
			System.out.println("Enter the amount you wish to deposit");
			deposit = scan.nextDouble();
			deposit(deposit);
			System.out.println("Would you like to make another transaction ? Y/N");
			answer = scanner.next();
			if(answer.equalsIgnoreCase("y")|| answer.equalsIgnoreCase("yes")){
				driver();
			}else if(answer.equalsIgnoreCase("n")|| answer.equalsIgnoreCase("no")){
				System.out.println("Goodbye!");
				System.exit(0);
			}else{
				System.out.println("Invalid Selection!");
				System.exit(1);
			}
			break;
			//withdraw from account
		case 3:
			//System.out.println("Unavailable");
			System.out.println("Enter the amount you wish to withdraw : ");
			withdraw = scan.nextDouble();
			withdraw(withdraw);
			System.out.println("Would you like to make another transaction ? Y/N");
			answer = scanner.next();
			if(answer.equalsIgnoreCase("y")|| answer.equalsIgnoreCase("yes")){
				driver();
			}else if(answer.equalsIgnoreCase("n")|| answer.equalsIgnoreCase("no")){
				System.out.println("Goodbye!");
				System.exit(0);
			}else{
				System.out.println("Invalid Selection!");
				System.exit(1);
			}
			break;
			//get account balance
		case 4:
			//System.out.println("Unavailable");
			System.out.println("Your balance is : " + balanceTotal());
			System.out.println("Would you like to make another transaction ? Y/N");
			answer = scanner.next();
			if(answer.equalsIgnoreCase("y")|| answer.equalsIgnoreCase("yes")){
				driver();
			}else if(answer.equalsIgnoreCase("n")|| answer.equalsIgnoreCase("no")){
				System.out.println("Goodbye!");
				System.exit(0);
			}else{
				System.out.println("Invalid Selection!");
				System.exit(1);
			}
			break;
			//write to file
		case 5:
			//System.out.println("Unavailable");
			fileWriter();
			System.out.println("Would you like to make another transaction ? Y/N");
			answer = scanner.next();
			if(answer.equalsIgnoreCase("y")|| answer.equalsIgnoreCase("yes")){
				driver();
			}else if(answer.equalsIgnoreCase("n")|| answer.equalsIgnoreCase("no")){
				System.out.println("Goodbye!");
				System.exit(0);
			}else{
				System.out.println("Invalid Selection!");
				System.exit(1);
			}
			break;
			//Read from file
		case 6:
			//System.out.println("Unavailable");
			fileReader();
			System.out.println("Would you like to make another transaction ? Y/N");
			answer = scanner.next();
			if(answer.equalsIgnoreCase("y")|| answer.equalsIgnoreCase("yes")){
				driver();
			}else if(answer.equalsIgnoreCase("n")|| answer.equalsIgnoreCase("no")){
				System.out.println("Goodbye!");
				System.exit(0);
			}else{
				System.out.println("Invalid Selection!");
				System.exit(1);
			}
			break;
			//Terminate the program
		case 7:
			System.out.println("Thank You for banking with us!");
			System.exit(0);
			break;

			//Default
		default:
			option = scanner.nextInt();
			scanner.nextLine();	
		}//End Switch
	}

	public static void main (String [] args) {
		userInterface ui = new userInterface();
		try {
			ui.driver();
		} catch (IOException e){
			// throws IOException
			e.printStackTrace();
		}

	}
}
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by tbone2sk
0

I just look at the code in the Checkbook2 class, and it needs to be reviewed. To get started I updated the fileWriter to print out the correct lines to the file.

public void fileWriter(){
		//Checkbook2 write = new Checkbook2();
		//System.out.println("Unavailable");
		try{
			BufferedWriter out = new BufferedWriter(new FileWriter("Checkbook.txt"));	
			out.write(getName()+"\n");
			out.write(getCheckNum()+"\n");
			out.write((int) getBalance()+"\n");
			out.close();
		}
		catch(IOException e){
			System.out.println("ERROR: Could not write to file!");
			System.exit(1);
		}
	}

Is there a reason that you are casting the balance to an int?
System.exit(1) is not needed.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.