2 Replies - 4215 Views - Last Post: 23 March 2007 - 11:18 AM Rate Topic: -----

#1 footsteps87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-March 07

Client class-how to create objects, call methods, etc

Posted 23 March 2007 - 07:45 AM

Hi, I am working on a program for an ATM pseudo banking machine..I am provided with the classes and methods that Im supposed to use, but Im getting stuck even though I understand what a class, method and object are, but I am not being able to apply them in my program..I will show you what ive done so far, so maybe someone could help by giving me some tips..I know that if i understand them fully and know how to use them, Ill be able to do my program on my own..
The program consists of 5 classes: SavingsAccount class, RspAccount class, Client class, Bank class and Machine class.
Im done with the SavingsAccount class and the RspAccount class and the class im having trouble with is the Client class.
The client class models a client, who can have at most one savings account and one Rsp account. (our program consists of only one client, since we havent covered arrays yet) Our class should have the following public constructor: Client (String name, int id, int pin) and it should have the following public methods:
String get_name() which gets the name of the client
int get_id() which gets the ID of the client
void set_id(int id) which sets the ID of the client
int get_pin() which gets the PIN of the client
void set_pin(int pin) which sets the PIN of the client
SavingsAccount get _savings() which gets savings account of the client & returns null if he hasnt one
void set_savings(SavingsAccount sa) which sets the savings account of the client
RspAccount get_rsp() which gets Rsp account of a client & returns null if he hasnt one
void set_rsp(RspAccount ra) which sets Rsp account of the client
boolean has_savings() which returns true if client has savings account, false if he hasnt
boolean has_rsp() which returns true if client has rsp account, false if he hasnt
double total_asset() returns the client's total assets in existing accounts

SO.. here is my client class (What ive done so far, which i know isnt enough, but im suck and i absolutely need help.. im not looking for code answer, just hints anything, i cant apply classes methods, i dont know how to call objects, to get my client class working, etc..Thank you so much in advance, to whoever will be the one helping me..thanks a lot)

 
import java.util.Scanner; 

public static void main (String [] args){
  

Scanner scanner = new Scanner (System.in); 
}


public class Client {
	String clientName = "";
	int clientID;
	int clientPIN;
	
 

  public Client (String name, int id, int pin){		   // this is the constructor (same name as class) 
	clientName = name;
	clientID = id;
	clientPIN = pin;
  }
  
	public String get_name(){							// gets the client's name

  
	  
	 System.out.println("Please enter your name:");
	 name = scan.next() + " " + scan.next();
	 return name;
	  
	}
	
	public int get_id (){								// gets the client's ID 
	  
	  System.out.println("Please enter your ID");
	  id = scan.nextInt();
	  return id;
	   
	  
	}
	
	public void set_id (int id){						// sets client's ID 
	  // what do we put here?
	}
	
	public int get_pin (){							  // gets client's PIN
	  
	  System.out.println("Please enter your PIN code");
	  pin = scan.nextInt();
	  return pin;
	}
	
	public void set_pin (int pin){					 // sets client's PIN
	  // what do we put here? 
	}
	
	public SavingsAccount get_savings (){			 
	  // gets SavingsAccount (the object which represents the client's savings account) of client if he has one, if 
	  // not, returns Null (how do u do that?)
	}
	
	public void set_savings (SavingsAccount sa){
	  // what do we put here? it sets the savingsaccount of client, if he has one. The set_savings() method does 
	 // the opposite. It sets the client's savings 
	 // account to the oject that is the parameter of the method. It was not specified what the method should do
	// if the client already has a savings account. You can do whatever you think is appropriate in that case
	}
	
	public void set_rsp (RspAccount ra){
	  // what do we put here? 
	}
	
	public boolean has_savings (){
	  // returns true if client has savingsaccount, false if not. How do we do that? 
	}
	
	public boolean has_rsp (){
	  // returns true if client has rsp account, false if not. How do we do that?
	}
	
	public double total_asset (){
	  // returns client's total asset in existing accounts. How do we do that?
	}
  }
  



thank you so much once again, to whoever will help..

Is This A Good Question/Topic? 0
  • +

Replies To: Client class-how to create objects, call methods, etc

#2 keems21  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 185
  • Joined: 03-February 07

Re: Client class-how to create objects, call methods, etc

Posted 23 March 2007 - 10:42 AM

Alright, well it seems that you've got a good start and everything is your own work, so I'll be happy to give you some guidance.

The most important part of your code is your implementation of your class files. This is done in the main method which you have already declared:
public static void main (String [] args){
  

Scanner scanner = new Scanner (System.in);
}



When you compile your code and run it, this is what is going to be run, so this is where you have to put all of your instructions.

Creating objects and running methods are pretty much what you need to be worried about in your main method. Create an object from a class file, what you need to do is use the "new" operator.

For your given class- Client, this would be done as follows:
Client myClient = new Client("Bob", 33123, 555);



Alright, so let's break this down, on the left side of the equals sign, you have:
Client myClient

This is the same way that you would go about declaring any variable:
int int1;
String str;
etc.

You give the type of object you are creating, then its variable name. So, anytime you want to use your newly created Client object, you call the variable name myClient, similarly to as if you wanted to call the int int1, or the String str.

Now for the right side:
new Client("Bob", 33123, 555)

Without getting into too much detail, when you create an object, you need to declare it as new, because in essence, you are creating something from scratch.
Now, notice this part:
Client("Bob", 33123, 555)

this code fits the form for the constructor that you created for your Client object:
public Client (String name, int id, int pin)

The client has a String - "Bob"
an int - 33123
and another int - 555

When you create an object, you based on the constructor that you created for the class. In this way it acts like a kind of blueprint for what you wish to build.

So, put all of this together:
Client myClient = new Client("Bob", 33123, 555);


You have a Client object designated by the name myClient
it has a String variable- name: containing the info "Bob"
it has an int variable- id: containing the number 33123
and it has another int variable- pin: containing the number 555

In addition to all of this, your object also has a number of methods at its disposal (some of which you are about to write):

Quote

String get_name() which gets the name of the client
int get_id() which gets the ID of the client
void set_id(int id) which sets the ID of the client
int get_pin() which gets the PIN of the client
void set_pin(int pin) which sets the PIN of the client
SavingsAccount get _savings() which gets savings account of the client & returns null if he hasnt one
void set_savings(SavingsAccount sa) which sets the savings account of the client
RspAccount get_rsp() which gets Rsp account of a client & returns null if he hasnt one
void set_rsp(RspAccount ra) which sets Rsp account of the client
boolean has_savings() which returns true if client has savings account, false if he hasnt
boolean has_rsp() which returns true if client has rsp account, false if he hasnt
double total_asset() returns the client's total assets in existing accounts


Now, as for calling all of these methods, you are going to need to use the dot (.) operator.
Remember, the name of your client object is myClient, and you are trying to access methods that myClient has. Using the dot operator to do this would look something like this:
myClient.get_name();



Calling this will run your get_name() method under you Client class. Use a similar format for any other method you may have.


Alright, I know I jumped around a bit, but I hope that this at least helped a little bit. I'd be happy to help you with anything else you need, or to clarify any of the points I may not have explained too well.

Good luck.

This post has been edited by keems21: 23 March 2007 - 10:43 AM

Was This Post Helpful? 0
  • +
  • -

#3 footsteps87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-March 07

Re: Client class-how to create objects, call methods, etc

Posted 23 March 2007 - 11:18 AM

Hey there..thank you so much for your time, effort and help.. I do have a couple of questions.. Now im a bit confused about something.. okay so lets say we take the following method:
public String get_name(){   


you said that I would call the myClient object under that method. So basically, I'll have something like that?
public String get_name(){
myClient.get_name();  
}


so what would be the whole point of this method and object calling? You see, thats what im missing.. knowledge and practice on classes, objects, methods etc.. Its really sad that the prof I have at uni is really bad at explaining so im trying to study java on my own... Anyway, at first I had:
 public String get_name(){	
					  
System.out.println("Please enter your name:");
 name = scan.next() + " " + scan.next();
 return name;	 
}


I had all of this because I thought that this was where we ask the client for their name. So if i understand what u explained to me, it isnt the part where we ask the client for the name right? (because in my assignment, i still have two other clasess, one called bank and one called machine..and i think its there where we ask the client for the name)
So in this method, we are only writing the first code I showed you, so that the name gets stored in there and would be called anytime we ask for it? Is that right? I would appreciate any further help on this..thank you so much its so sweet of you :) God bless
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1