issues with main class

i cant get the input in the main class to work properly

Page 1 of 1

4 Replies - 1453 Views - Last Post: 09 September 2009 - 01:43 PM Rate Topic: -----

#1 ksa5025   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 16-March 08

issues with main class

Posted 09 September 2009 - 09:31 AM

im trying to write this bank account program on java and it should be able to distinguish between the checking and savings account. if it is a savings account then the interest is 6% but if its checking then the interest is 0%. I decided to try writing the main class part and test out everything that i wrote but i cannot get the input functions to work properly.



import java.util.Scanner;

public class BankAccount
{
	Scanner input = new Scanner(System.in);

	private String name;	  // name of account holder
	private double balance;   // how much money is in account, $

	private static int numAccounts = 0;		 // how many accounts have been created

	public BankAccount()
	// POST: A default BankAccount object is created with the name set to a blank and
	//	   and the balance set to $0.00 with interest set to %0.
	{
	   this(0.0, 0);
	}

	public BankAccount(double balance, double interest)
	// PRE:  balance >= 0.00 and is in dollars
	// POST: A BankAccount object is created with the name set to a blank
	//	   and the class member balance set to balance
	{
	   name = " ";
	 if(interest == 0)
	 {
	   if(balance >= 0)			  // validate proposed initial balance
		  this.balance = balance;
	   else
		  this.balance = 0;
	   numAccounts++;
	 }
	 else
	 {
		   if(balance >= 0)
			   this.balance = balance + (balance*interest); //record the new
		   else											 //balance with
			   this.balance = 0;							//interest
		   numAccounts++;
	 }
	}

	public double DetermineInterest(int accountType)
	//Pre: The user must choose a 0 or 1 for checking or savings, respectivley.
	//Post: This will determine how much interest the user is capable of
	// getting.
	{
		double interest = 0;

		if(accountType == 1)
		   interest = 0.06;	 //set the interest to 6% for the savings account
		else
		   interest = 0.0;	  //set the interest to 0% for checking account

		return interest;

	}

	public void ResetAccount(String newName, double newBalance)
	// PRE:  newName has been assigned a value
	//	   && newBalance >= 0.00 and is in dollars
	// POST: The account object is reset with the name set to newName
	//	   and the balance set to newBalance
	{
		name = newName;			// Match up private variables with parameters
		balance = newBalance;	  // Could do error checking here with an if(balance >= 0)
	}

	public void Withdraw(double amount)
	// PRE:  amount >= 0.00 and is in dollars
	// POST: amount is deducted from the balance stored for the account
	{
		balance = balance - amount;	 // Could do error checking here
	}

	public double GetBalance()
	// PRE:  account object initialized
	// POST: Return the current balance of the account
	{
		return balance;
	}

	public void DisplayBalance()
	// PRE:  account object initialized
	// POST: Display the current balance of the account to the screen
	{
		System.out.printf("Your balance is currently $%.2f\n", balance);
	}

	public static int GetNumberOfAccounts()
	// POST: FCTVAL == how many accounts have been instantiated
	{
	   return numAccounts;
	}

	public String DetermineAccount()
	//Post: Determines what type of account the user wants. It then sends the
	// data to "DetermineInterest" where the interest will be determined.
	{
		int accntType;
		String typeAccount;
		System.out.print("Press 0 for checking and 1 for savings: ");
		accntType = input.nextInt();
		if(accntType == 0)
			typeAccount = "Checking";

		else
			typeAccount = "Savings";
		DetermineInterest(accntType);

		return typeAccount;

	}

	 public static void main(String[] args)
   {
	  int accountChoice;
	  String name;
	  name = new String();

	  System.out.println("Please enter your name: ");
	  name = nextLine();

	  System.out.println("Please enter 0 for checking or 1 for savings: ");
	  accountChoice = input.nextInt();
   }

}






Is This A Good Question/Topic? 0
  • +

Replies To: issues with main class

#2 AntonWebsters   User is offline

  • D.I.C Regular
  • member icon

Reputation: 89
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: issues with main class

Posted 09 September 2009 - 10:34 AM

I did some editings on it. Doesn't seem have any problems to me.

import java.util.Scanner;

public class BankAccount
{
	static Scanner input = new Scanner(System.in);

	private String name;	  // name of account holder
	private double balance;   // how much money is in account, $

	private static int numAccounts = 0;		 // how many accounts have been created

	public BankAccount()
	// POST: A default BankAccount object is created with the name set to a blank and
	//	   and the balance set to $0.00 with interest set to %0.
	{
	   this(0.0, 0);
	}

	public BankAccount(double balance, double interest)
	// PRE:  balance >= 0.00 and is in dollars
	// POST: A BankAccount object is created with the name set to a blank
	//	   and the class member balance set to balance
	{
	   name = " ";
	 if(interest == 0)
	 {
	   if(balance >= 0)			  // validate proposed initial balance
		  this.balance = balance;
	   else
		  this.balance = 0;
	   numAccounts++;
	 }
	 else
	 {
		   if(balance >= 0)
			   this.balance = balance + (balance*interest); //record the new
		   else											 //balance with
			   this.balance = 0;							//interest
		   numAccounts++;
	 }
	}

	public static double DetermineInterest(int accountType)
	//Pre: The user must choose a 0 or 1 for checking or savings, respectively.
	//Post: This will determine how much interest the user is capable of
	// getting.
	{
		double interest = 0;

		if(accountType == 1)
		   interest = 0.06;	 //set the interest to 6% for the savings account
		else if(accountType == 0)
		   interest = 0.0;	  //set the interest to 0% for checking account

		return interest;

	}

	public void ResetAccount(String newName, double newBalance)
	// PRE:  newName has been assigned a value
	//	   && newBalance >= 0.00 and is in dollars
	// POST: The account object is reset with the name set to newName
	//	   and the balance set to newBalance
	{
		name = newName;			// Match up private variables with parameters
		balance = newBalance;	  // Could do error checking here with an if(balance >= 0)
	}

	public void Withdraw(double amount)
	// PRE:  amount >= 0.00 and is in dollars
	// POST: amount is deducted from the balance stored for the account
	{
		balance = balance - amount;	 // Could do error checking here
	}

	public double GetBalance()
	// PRE:  account object initialized
	// POST: Return the current balance of the account
	{
		return balance;
	}

	public void DisplayBalance()
	// PRE:  account object initialized
	// POST: Display the current balance of the account to the screen
	{
		System.out.printf("Your balance is currently $%.2f\n", balance);
	}

	public static int GetNumberOfAccounts()
	// POST: FCTVAL == how many accounts have been instantiated
	{
	   return numAccounts;
	}

	public static String DetermineAccount()
	//Post: Determines what type of account the user wants. It then sends the
	// data to "DetermineInterest" where the interest will be determined.
	{
		int accntType;
		String typeAccount = "";
		System.out.print("Press 0 for checking and 1 for savings: ");
		accntType = input.nextInt();
		if(accntType == 0)
			typeAccount = "Checking";

		else if(accntType == 1)
			typeAccount = "Savings";
		DetermineInterest(accntType);

		return typeAccount;

	}

	 public static void main(String[] args)
   {
	  int accountChoice;
	  String name;
	  name = new String();

	  System.out.println("Please enter your name: ");
	  name = input.nextLine();

	  System.out.println(DetermineAccount());
   }

}



Was This Post Helpful? 0
  • +
  • -

#3 ksa5025   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 16-March 08

Re: issues with main class

Posted 09 September 2009 - 12:53 PM

View PostAntonWebsters, on 9 Sep, 2009 - 09:34 AM, said:

I did some editings on it. Doesn't seem have any problems to me.

import java.util.Scanner;

public class BankAccount
{
	static Scanner input = new Scanner(System.in);

	private String name;	  // name of account holder
	private double balance;   // how much money is in account, $

	private static int numAccounts = 0;		 // how many accounts have been created

	public BankAccount()
	// POST: A default BankAccount object is created with the name set to a blank and
	//	   and the balance set to $0.00 with interest set to %0.
	{
	   this(0.0, 0);
	}

	public BankAccount(double balance, double interest)
	// PRE:  balance >= 0.00 and is in dollars
	// POST: A BankAccount object is created with the name set to a blank
	//	   and the class member balance set to balance
	{
	   name = " ";
	 if(interest == 0)
	 {
	   if(balance >= 0)			  // validate proposed initial balance
		  this.balance = balance;
	   else
		  this.balance = 0;
	   numAccounts++;
	 }
	 else
	 {
		   if(balance >= 0)
			   this.balance = balance + (balance*interest); //record the new
		   else											 //balance with
			   this.balance = 0;							//interest
		   numAccounts++;
	 }
	}

	public static double DetermineInterest(int accountType)
	//Pre: The user must choose a 0 or 1 for checking or savings, respectively.
	//Post: This will determine how much interest the user is capable of
	// getting.
	{
		double interest = 0;

		if(accountType == 1)
		   interest = 0.06;	 //set the interest to 6% for the savings account
		else if(accountType == 0)
		   interest = 0.0;	  //set the interest to 0% for checking account

		return interest;

	}

	public void ResetAccount(String newName, double newBalance)
	// PRE:  newName has been assigned a value
	//	   && newBalance >= 0.00 and is in dollars
	// POST: The account object is reset with the name set to newName
	//	   and the balance set to newBalance
	{
		name = newName;			// Match up private variables with parameters
		balance = newBalance;	  // Could do error checking here with an if(balance >= 0)
	}

	public void Withdraw(double amount)
	// PRE:  amount >= 0.00 and is in dollars
	// POST: amount is deducted from the balance stored for the account
	{
		balance = balance - amount;	 // Could do error checking here
	}

	public double GetBalance()
	// PRE:  account object initialized
	// POST: Return the current balance of the account
	{
		return balance;
	}

	public void DisplayBalance()
	// PRE:  account object initialized
	// POST: Display the current balance of the account to the screen
	{
		System.out.printf("Your balance is currently $%.2f\n", balance);
	}

	public static int GetNumberOfAccounts()
	// POST: FCTVAL == how many accounts have been instantiated
	{
	   return numAccounts;
	}

	public static String DetermineAccount()
	//Post: Determines what type of account the user wants. It then sends the
	// data to "DetermineInterest" where the interest will be determined.
	{
		int accntType;
		String typeAccount = "";
		System.out.print("Press 0 for checking and 1 for savings: ");
		accntType = input.nextInt();
		if(accntType == 0)
			typeAccount = "Checking";

		else if(accntType == 1)
			typeAccount = "Savings";
		DetermineInterest(accntType);

		return typeAccount;

	}

	 public static void main(String[] args)
   {
	  int accountChoice;
	  String name;
	  name = new String();

	  System.out.println("Please enter your name: ");
	  name = input.nextLine();

	  System.out.println(DetermineAccount());
   }

}




ok thanks..im not exactly sure what you changed but it works so thanks. im new to java so i probably missed something really stupid lol.
Was This Post Helpful? 0
  • +
  • -

#4 ksa5025   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 16-March 08

Re: issues with main class

Posted 09 September 2009 - 01:30 PM

ok i added some more code in but now im getting this error when i try to call some of the methods. i dont really understand the reasoning behind why it wont work. it says non-static method cannot be referenced from static context. its happening on displayBalance as well as ResetAccount.


import java.util.Scanner;

public class BankAccount
{
	static Scanner input = new Scanner(System.in);

	private String name;	  // name of account holder
	private double balance;   // how much money is in account, $

	private static int numAccounts = 0;		 // how many accounts have been created

	public BankAccount()
	// POST: A default BankAccount object is created with the name set to a blank and
	//	   and the balance set to $0.00 with interest set to %0.
	{
	   this(0.0, 0);
	}

	public BankAccount(double balance, double interest)
	// PRE:  balance >= 0.00 and is in dollars
	// POST: A BankAccount object is created with the name set to a blank
	//	   and the class member balance set to balance
	{
	   name = " ";
	 if(interest == 0)
	 {
	   if(balance >= 0)			  // validate proposed initial balance
		  this.balance = balance;
	   else
		  this.balance = 0;
	   numAccounts++;
	 }
	 else
	 {
		   if(balance >= 0)
			   this.balance = balance + (balance*interest); //record the new
		   else											 //balance with
			   this.balance = 0;							//interest
		   numAccounts++;
	 }
	}

	public static double DetermineInterest(int accountType)
	//Pre: The user must choose a 0 or 1 for checking or savings, respectively.
	//Post: This will determine how much interest the user is capable of
	// getting.
	{
		double interest = 0;

		if(accountType == 1)
		   interest = 0.06;	 //set the interest to 6% for the savings account
		else if(accountType == 0)
		   interest = 0.0;	  //set the interest to 0% for checking account

		return interest;

	}

	public void ResetAccount(String newName, double newBalance)
	// PRE:  newName has been assigned a value
	//	   && newBalance >= 0.00 and is in dollars
	// POST: The account object is reset with the name set to newName
	//	   and the balance set to newBalance
	{
		name = newName;			// Match up private variables with parameters
		balance = newBalance;	  // Could do error checking here with an if(balance >= 0)
	}

	public void Withdraw(double amount)
	// PRE:  amount >= 0.00 and is in dollars
	// POST: amount is deducted from the balance stored for the account
	{
		balance = balance - amount;	 // Could do error checking here
	}

	public double GetBalance()
	// PRE:  account object initialized
	// POST: Return the current balance of the account
	{
		return balance;
	}

	public void DisplayBalance()
	// PRE:  account object initialized
	// POST: Display the current balance of the account to the screen
	{
		System.out.printf("Your balance is currently $%.2f\n", balance);
	}

	public static int GetNumberOfAccounts()
	// POST: FCTVAL == how many accounts have been instantiated
	{
	   return numAccounts;
	}

	public static String DetermineAccount()
	//Post: Determines what type of account the user wants. It then sends the
	// data to "DetermineInterest" where the interest will be determined.
	{
		int accntType;
		double interest;
		String typeAccount = "";
		System.out.print("Press 0 for checking and 1 for savings: ");
		accntType = input.nextInt();
		if(accntType == 0)
			typeAccount = "Checking";

		else if(accntType == 1)
			typeAccount = "Savings";
		interest = DetermineInterest(accntType);
		
		System.out.println("you selected savings");
		System.out.println("Your interest is: " + interest);

		return typeAccount;

	}
	
	public static int DisplayMenu()
	{
		int userChoice;
		
		System.out.println("What would you like to do: ");
		System.out.println("1) Display Balance");
		System.out.println("2) Get Number of accounts");
		System.out.println("3) Reset your account");
		System.out.println("4) Withdraw cash");
		System.out.println("5) Exit");
		userChoice = input.nextInt();

		return userChoice;
	}

	 public static void main(String[] args)
   {
	  int usersChoice;
	  double balance;
	  double withdraw;
	  String name;
	  name = new String();

	  System.out.print("Please enter your name: ");
	  name = input.nextLine();
	  System.out.println(DetermineAccount());
	  
	  usersChoice = DisplayMenu();
	  
	  if(usersChoice == 1)	  //Display Balance
	  {
		  (DisplayBalance());
	  }
	  else if(usersChoice == 2) //Get number of accounts
	  {
		  GetNumberOfAccounts();
	  }
	  else if(usersChoice == 3) //Reset your account
	  {
		  System.out.println("Please enter your name: ");
		  name = input.nextLine();
		  System.out.println("Please enter your initial balance: ");
		  balance = input.nextDouble();
		  
		  ResetAccount(name, balance);		  
	  }
	  else if (usersChoice == 4)	//Withdraw Cash
	  {
		  System.out.println("Please enter the amount to withdraw: ");
		  withdraw = input.nextDouble();
		  Withdraw(withdraw);
	  }
	  else
	  {
		  System.out.println("Thank you and have a good day");
	  }


   }

}



This post has been edited by ksa5025: 09 September 2009 - 01:31 PM

Was This Post Helpful? 0
  • +
  • -

#5 AntonWebsters   User is offline

  • D.I.C Regular
  • member icon

Reputation: 89
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: issues with main class

Posted 09 September 2009 - 01:43 PM

I think you need to declare these methods as static methods, just like what I did with the previous code you posted.
Please refer to this site if you need to know more about static methods or static classes.

This post has been edited by AntonWebsters: 09 September 2009 - 01:45 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1