LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-07-2012, 10:05 AM   #1
Treeware
LQ Newbie
 
Registered: Jun 2012
Location: 80 beautiful acres in the Chicago metro area
Posts: 4

Rep: Reputation: Disabled
Creating Object Relationships using Java 7


Hi Everyone:

I am new to the forum. This is my first post. I am currently working on a banking app. It has been running fine UNTIL I implemented subclassing.

My relationships are
BANKS have CUSTOMERS.
CUSTOMERS have 1 or more ACCOUNTS.

The code executed as expected until I created 2 subclasses . These subclasses are extended from ACCOUNTS.

public class CheckingAccount extends ACCOUNTS {

-and-

public class SavingsAccount extends ACCOUNTS {


Here's My Question: I'd like some help pushing through the compile errors. I believe the problem is not syntactical, but conceptual. I seem to not fully comprehend some concept at play here. Any seasoned Java programmers want to enlighten me about subclassing?


Thank you so much for looking at my subclassing error(s). I realize that conceptually subclassing is a snap. BUT programming code for subclassing is hard for me to hammer down. I'd love seasoned reasoning that will help this logically click for me. Thank you sooo much!


***COMPILE ERROR***
the TestingChecking.java has 22 errors.

**6 (Identifier) expected.**
line28- customer.setAccount(new CheckingAccount(871.7));
line29- CheckingAccountBalance=CheckingAccount.getBalance();

**5 Illegal start of type.**
line28- (see syntax above)
line33- System.out.println("New Account..." + customer.getAccount());


**5 class, interface, or enum expected**
line37- if
line40- System.out.println("\n\tCreating Checking Account -overdraft waived."); error indicator points to close parenthesis??



***COMPILE ERROR***


===================================================================================
/* code for Account.java Status: Running As Expected */



public class Account {
private double globalBalance;
public boolean status;
public boolean SUCCESS = true;
public boolean FAILURE = false;

public Account(double initialBalance) {
if (initialBalance > 0) {
globalBalance = initialBalance;
System.out.println("New Account Created. Balance is " + globalBalance);
}
}


//method: getBalance
public double getBalance() {
return globalBalance;
}


//method: WITHDRAWL

public boolean Withdrawl(double withdrawlAmount) {
if(globalBalance > withdrawlAmount) {
globalBalance = globalBalance - withdrawlAmount;
status = SUCCESS;

} else status = FAILURE;
// System.out.println("current balance cannot accomodate your request");

return status;
}


//method: DEPOSIT

public boolean Deposit (double depositAmount) {
if(depositAmount >0) {
globalBalance = globalBalance + depositAmount;
status = SUCCESS;
} else status = FAILURE;
// System.out.println("Error in Deposit. Transaction not complete.");

return status;
}


}
========================================================================================
/* source for CheckingAccount.java Status: DOA */
public class CheckingAccount {
public boolean status=false;
public boolean SUCCESS = true;
public boolean FAILURE = false;

private double globalBalance;
protected double overdraftLimit;




public CheckingAccount(double initialBalance) {

if (initialBalance > 0) {
globalBalance = initialBalance;
System.out.println("New Account Created. Balance is " + globalBalance);
status = true;

}
System.out.println("New Checking Account Error: Cannot open CheckingAccount with less than $1.00.
globalBalance= " + globalBalance + " Overdraft Limit= " +overdraftLimit);

}



//method: getBalance
public double getBalance() {
return globalBalance;
}



//method: WITHDRAWL
public boolean Withdrawl(double withdrawlAmount) {
boolean status;

if(globalBalance > withdrawlAmount) {
globalBalance = globalBalance - withdrawlAmount;
status = true;

} else status = false;
// System.out.println("current balance cannot accomodate your request");

return status;
}



//method: DEPOSIT
public boolean Deposit (double depositAmount) {
if(depositAmount >0) {
globalBalance = globalBalance + depositAmount;
status = true;
} else status = false;
System.out.println("Error in Deposit. Transaction not complete.");


====================================================================================
/* source for testCode.java Status: Not Running

public class TestingChecking {
private int Status2;

public static void main(String[] args) {

String firstName = "Kole";
String lastName = "Strepek";

boolean transactionStatus = false;

Bank bank = new Bank();
Customer customerObjectReference; // **initialize "customer" container**
CheckingAccount checkingAccountObjectReference;

double CheckingAccountBalance;


System.out.println("\n\t Creating Kole Strepek Customer Object");
bank.addCustomer("Kole","Strepek");
customerObjectReference = bank.getCustomer(0); //CRITICAL REFERENCE
System.out.println("\n\tCustomer has been established: " + customer.getFirstName() + " " + customer.getLastName() );
}


customer.setAccount(new CheckingAccount(871.7));
CheckingAccountBalance=CheckingAccount.getBalance();
checkingAccountObjectReference=CheckingAccount.getAccount();

System.out.println("\n\t Created Checking with OverDraft Protection FOR KOLE. TARGET HIT" );
System.out.println("New Account has been established: " + customer.getAccount() );


}
if //checking without overdraft
(AccountType == 2) {
System.out.println("\n\n\n\n Need Code to Create Checking without OverDraft Protection ");
System.out.println("\n\tCreating Checking Account -overdraft protection waived.");

}
if //savings with annual interest calculation
(AccountType == 3) {
System.out.println("\n\n\n\n Need Code to Create Savings with Annual Interest Rate");

System.out.println("\n\tCreating Savings Account. Interest is calculated annually. The current rate is 3%.");
}
}
}
}



Thank you so much for looking at my subclassing error(s). I realize that conceptually subclassing is a snap. BUT programming code for subclassing is hard for me to hammer down. I'd love seasoned reasoning that will help this click for me. Thank you much!

~Tree

Last edited by Treeware; 06-07-2012 at 12:09 PM. Reason: insert code snippets
 
Old 06-08-2012, 07:14 AM   #2
Treeware
LQ Newbie
 
Registered: Jun 2012
Location: 80 beautiful acres in the Chicago metro area
Posts: 4

Original Poster
Rep: Reputation: Disabled
Subclassing/ Inheritance: Detective Work

The issues with this code are many.

#1: When creating a 'setter' method to create a new CheckingAccount(subclass of Account), create the 'CheckingAccount' object WITHIN the Data type declaration of the parent(in this case Account).
=============
customer.setAccount(new CheckingAccount(5000.0, 5699.2);
where the setAccount method accepts Account data.


=> public void setAccount(Account acct) {
account = acct;
}
=======================================

I would like deeper understanding about why the CheckingAccount object is passed under the data type 'Account' in the setter method.
 
Old 06-27-2012, 06:45 PM   #3
Treeware
LQ Newbie
 
Registered: Jun 2012
Location: 80 beautiful acres in the Chicago metro area
Posts: 4

Original Poster
Rep: Reputation: Disabled
A deeper Understanding

I am ...

Last edited by Treeware; 06-27-2012 at 07:03 PM. Reason: post incomplete
 
Old 06-27-2012, 06:58 PM   #4
Treeware
LQ Newbie
 
Registered: Jun 2012
Location: 80 beautiful acres in the Chicago metro area
Posts: 4

Original Poster
Rep: Reputation: Disabled
Lightbulb A deeper Understanding

I am writing on June 27th. I now have a deeper understanding of subclassing, "super" & "this" keywords, overriding...+

When building relationships in Java code(much like DBMS relationships)I find it important to build from the bottom up. Knowing I wanted to create a banking app-> I knew I would be working with Banks -> (which have) Customers -> (who own/ operate) Accounts.

As I know the account object will need to be created inside the customer class to create the object relationship I will WRITE the ACCOUNT CLASS FIRST. Work hard to not get overwhelmed about the inter-workings of all the objects. Work with tunnel vision highlighting the needs of the Account class alone.

Next focus on the customer class. Define the instance and local variables & customer behavior. Before exiting the customer class-> create a class variable to represent the account object that will soon be linked "Account account;". Just by creating the class variable alone AN ACCOUNT object is instantiated within the customer class. This is what is desired.

Within the CUSTOMER CLASS, 3 steps are required to create a relationship between CUSTOMERS && ACCOUNTS.
-------------------------------------------------------------------------------------------------------
1.) define Account variable "Account acct;"
2.) define a CREATE ACCOUNT method within the customer class. This method will create the ACCOUNT object, but is only 1/2 the process to work with the new ACCOUNT object linked to CUSTOMER x. "customer.createAccount(new Account(345.0, 500.0) );"
Where

public void createAccount(Account acct) {
ACCOUNT = acct;
}


3.) set the pointer for the "acct" variable to reference the new ACCOUNT object linked to the CUSTOMER instance.
" acct = customer.setAccount(); "
Where

public Account getAccount() {
return ACCOUNT;
}

===================================================
My Advice. Work from the bottom up using the logic steps I detailed above. It worked for me. It took me almost 2 weeks to grasp this concept fully. Hope this helps!

Treeware
 
Old 06-27-2012, 10:03 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Moved: This thread is more suitable in <PROGRAMMING> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Error creating library file from object files using xlc montylee AIX 3 03-04-2024 11:21 AM
pygame: example of creating a Sound from an object eantoranz Programming 1 10-26-2009 10:12 AM
creating a shared object vin_pll Linux - General 1 12-18-2008 12:43 PM
java: java.util.Hashmap doesn't work for a custom object kpachopoulos Programming 2 04-15-2007 03:25 PM
LXer: Creating a Web service app object using WebSphere Portal V6 LXer Syndicated Linux News 0 02-24-2007 01:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration