Google Answers Logo
View Question
 
Q: java (study question for a beginner) ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: java (study question for a beginner)
Category: Computers > Programming
Asked by: brittlehorse-ga
List Price: $12.00
Posted: 04 Nov 2002 13:12 PST
Expires: 04 Dec 2002 13:12 PST
Question ID: 98564
This is a study question similar to the one that i will need to be
ready to answer.
**********************
You are currently working for a company that is going to produce
ATM’s.  Please create a program, using at least two different classes,
that does the following:
·The program should ask the user to input their account number,
username, and password.
·The program should ask the user to input their initial balance (I
know this is a little weird, but since we can’t retrieve this we have
to enter it)
·The program allows the user to deposit or withdraw money from their
account.
·Accounts cannot have a balance of under $50.  
·For every transaction the system should print:
Initial Balance
Amount withdrawn or deposited 
Current Balance

*********very important!!!***************
The code cannot have any short-cuts like "gpa>=0? (""+gpa):" OR
"gpa=-1; //for unknown GPA"
ALSO no fancy stuff like keyword "this" because we have not used it
yet.  Just very plane-jane code.

Request for Question Clarification by rbnn-ga on 04 Nov 2002 13:56 PST
Should the Atm class hold a list of all its users and their passwords,
and check that the passwords are correct? Or does it just work for one
user?

Clarification of Question by brittlehorse-ga on 04 Nov 2002 13:58 PST
Not hold a list of the users.  Too complicated.  Just check to see if
the password is correct, but i would take just about anything right
now.  Speed is of the essence my friend.
ps glad your answering this for me.

Request for Question Clarification by rbnn-ga on 04 Nov 2002 14:06 PST
I still don't understand how to check that the password is correct
unless we have a list of users here. Are you saying the Bank only has
one user?

I could just have the Bank class do something like
String[]usernames
String[]passwords
double[] balances

and when a username/password pair comes in, it could check for that .
But this would require constructs like arrays and for loops, and some
if statements here. is that ok?

Request for Question Clarification by rbnn-ga on 04 Nov 2002 14:10 PST
Another thing I do not understand here is this.

You say the user input his own initial balance because we cannot
retrieve it.

But if we cannot retrieve the balance, then how can we retrieve the
password to check it is right?

Perhaps if you typed in what you would like the output of the program
to look like a bit.

Clarification of Question by brittlehorse-ga on 04 Nov 2002 14:11 PST
i would say that the bank has only one user.  I did like the level of
code you just displayed.  I think it's a go.  I can use some simple
loops however.  Like do while and for.  You got it bro.

Clarification of Question by brittlehorse-ga on 04 Nov 2002 14:20 PST
the user just inputs the beginning balance and then it is used in the
rest of the program.  The only reason this is weird is because you
can't just tell an ATM how much you have, but in this case you do.

ATM:
what is:
your name
your acctNumber
your Password

User:
my acctNumber is:
my name is:
my beginning balance is:___

deposit()
widthdraw()

******
i think that is it.

Request for Question Clarification by rbnn-ga on 04 Nov 2002 14:28 PST
I appreciate your attempts to clarify what is going on here, but try
as I might I just cannot figure out what is required.

For instance, I don't understand what the password is to be validated
against; I don't understand what it means for a bank to have one user,
I'm just not clear on this.

You might try as I mentioned, typing out what the program should *do*,
perhaps?

Actually if there are other researchers who feel they can tackle this
please feel free to help out here!

Request for Question Clarification by rbnn-ga on 04 Nov 2002 14:29 PST
I just saw your sample output. OK, I will try to post something along these lines.

Clarification of Question by brittlehorse-ga on 04 Nov 2002 14:38 PST
I guess the bank could store more than one user if makes it easier. 
I'll take your best stab at it and be VERY happy.  Speed is more
important than accuracy at this point.

The program should:
Have Two classes 
Keep track of a verified users account balance
Let them deposit and withdraw.  
Make it look like an idiot did it
Get done fast (yup)
Answer  
Subject: Re: java (study question for a beginner)
Answered By: rbnn-ga on 04 Nov 2002 15:14 PST
Rated:5 out of 5 stars
 
OK, I did my best here to try and write code "like an idiot did it".

There are two things though that are a little tricky but there is no
way around it.

Thing 1: No easy way in java to just "read a line". You have to create
a "Reader" like at the beginning of the ATM class. Everyone just
copies a line like this at the beginning of all java programs that
read lines from standard input.

Thing 2: Every I/O operation in java (like reading a line) might
produce an Exception, that is, an error. All these have to be
"caught". The only way to do this is to wrap a big so-called
"try/catch" around all the code.

I have a little password protection: the password must be "brittle" .

The balance cannot go below 50.

Here are some sample runs:

Input name: 
me
Input password: 
brittle
Input initial balance
78
withdraw or deposit or exit?
withdraw
How much to withdraw?
9
Withdraw succeeded
Initial balance: 78.0
Amount withdrawn: 9.0
Current balance: 69.0
withdraw or deposit or exit?
deposit
How much to deposit?
10
Deposit succeeded
Initial balance: 69.0
Amount deposited: 10.0
Current balance: 79.0
withdraw or deposit or exit?
exit
/cygdrive/c/google: !!
java Atm
Input name: 
ffoo
Input password: 
badpassword
Input initial balance
99
Failed to add user
/cygdrive/c/google: !!
java Atm
Input name: 
fum
Input password: 
brittle
Input initial balance
10
Failed to add user
/cygdrive/c/google: !!
java Atm
Input name: 
me
Input password: 
brittle
Input initial balance
100
withdraw or deposit or exit?
withdraw
How much to withdraw?
60
Withdrawal attempt failed, sorry
withdraw or deposit or exit?
withdraw
How much to withdraw?
10
Withdraw succeeded
Initial balance: 100.0
Amount withdrawn: 10.0
Current balance: 90.0
withdraw or deposit or exit?
exit


-----Cut here for Bank.java-----
public class Bank{ //A bank with one user 
    String Username;
    String Password; //must equal "brittle"
    double Balance;

    public boolean addUser(String name, String password, double
balance){
	if(balance<50)
	    return false;
	if (!password.equals("brittle"))
	    return false;
	Username=name;
	Password=password;
	Balance=balance;
	return true; 
    }

    public double getBalance(){
	return Balance;
    }

    public boolean withdraw(double amount){
	double newbalance=Balance-amount;
	if (newbalance<50) return false;
	Balance=newbalance;
	return true;
    }

    public boolean deposit(double amount){
	Balance=Balance + amount;
	return true;
    }

}
-----cut here for Atm.java-----
import java.io.* ;  //This line is neaded for BufferedReader and
BufferedInputStream
public class Atm{ // A not-very-secure ATM
    public static void main(String[] args){
	try{ 
	    BufferedReader  reader = new BufferedReader(new InputStreamReader
( System.in));

	    Bank bank=new Bank();
	    System.out.println("Input name: ");
	    String username=reader.readLine();
	    System.out.println("Input password: ");
	    String password=reader.readLine();

	    System.out.println("Input initial balance");
	    double balance=Double.parseDouble(reader.readLine());

	    boolean ok;
	    ok=bank.addUser(username,password,balance);
	
	    if (!ok){
		System.out.println("Failed to add user");
		System.exit(0);
	    }

	    while(true) { //Until the user wants to stop
		double initialBalance=bank.getBalance();
		System.out.println("withdraw or deposit or exit?");
		String answer=reader.readLine();
	    
		//Case 1: a withdrawael
		if (answer.equals("withdraw")){
		    System.out.println("How much to withdraw?");
		    double withdrawAmount=Double.parseDouble(reader.readLine());
		    ok=bank.withdraw(withdrawAmount);
		    if (ok){
			System.out.println("Withdraw succeeded");
			System.out.print("Initial balance: ");
			System.out.println(initialBalance);
			System.out.print("Amount withdrawn: ");
			System.out.println(withdrawAmount);
			System.out.print("Current balance: ");
			System.out.println(bank.getBalance());
		    }
		    else {
			System.out.println("Withdrawal attempt failed, sorry");
		    }
		}

		//Case 2: a deposit
		else if (answer.equals("deposit")){
		    System.out.println("How much to deposit?");
		    double depositAmount=Double.parseDouble(reader.readLine());
		    bank.deposit(depositAmount);
		    System.out.println("Deposit succeeded");
		    System.out.print("Initial balance: ");
		    System.out.println(initialBalance);
		    System.out.print("Amount deposited: ");
		    System.out.println(depositAmount);
		    System.out.print("Current balance: ");
		    System.out.println(bank.getBalance());
		}

		//Case 3: user wants to leave
		else 
		    System.exit(0);
	    } //end the while loop, get next command
	} // end the try/catch
	catch(Exception e){
	    System.out.println("Invalid input");
	    System.exit(0);
	}
    }
}
brittlehorse-ga rated this answer:5 out of 5 stars
Perfection!  You're the greatest rbnn-ga!!! Don't forget it. ever.

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy