Reputation: 44
The final code looks like this. Fixed the errors and understood the concepts of inheritance. Thanks to you guys who responded to my post. Your replies were helpful.
Account Class
public class Account {
String name;
String number;
public double balance;
public class Account
{
public String acctnum;
public String accttitle;
public Double acctbal;
public String accttitle2;
public Account()
{
}
public void withdraw(Double amount)
{
acctbal=acctbal-amount;
}
public void deposit(Double amount)
{
acctbal=acctbal+amount;
}
public double zakat() /*In Islam Zakat is deducted every year from saving
accounts in holy month of Ramadan */
{
Double z=acctbal*0.025;
return z;
}
public void showdetail()
{
System.out.println("The Account Number ="+acctnum);
System.out.println("The Account Title ="+accttitle);
System.out.println("The Account Balance ="+acctbal);
}
}
Current Account Class
public class CurrentAccount extends Account
{
public CurrentAccount(String Num,String Title,double Bal)
{
acctnum = Num;
accttitle = Title;
acctbal = Bal;
}
public double tax()
{
double t= acctbal*0.05;
return t;
}
}
Joint Account Class
public class JointAccount extends Account
{
public String accttitle2;
public JointAccount(String Num,String Title,String accttitle_1,double Bal)
{
acctnum = Num;
accttitle=Title;
accttitle2=accttitle_1;
acctbal =Bal;
}
public void showdetail() //Overrding
{
System.out.println("The Account Number = " + acctnum);
System.out.println("The Account Title = " + accttitle + " and " + accttitle2);
System.out.println("The Account Balance = " + acctbal);
}
public double calculatetax()
{
double t= acctbal*0.09;
return t;
}
}
SavingAccount Class
public class savingAccount extends Account
{
public savingAccount(String Num,String Title,Double Bal)
{
super.acctnum =Num;
super.accttitle=Title;
super.acctbal =Bal;
}
public double tax()
{
double t= acctbal*0.07;
return t;
}
public void withdraw(double amount)
{
super.withdraw(amount);
if(acctbal<1000){
acctbal=acctbal-150;
}
}
}
Following is testApp class
public class testApp
{
public static void main(String args[])
{
savingAccount obj=new savingAccount("01-102-33","Ali",30000.0);
System.out.println("My Balance is="+obj.acctbal);
obj.withdraw(5000.0);
System.out.println("After Withdraw The Balanc is="+obj.acctbal);
obj.deposit(10000.0);
System.out.println("After deposit balance is ="+obj.acctbal);
double z= obj.zakat();
System.out.println("zakat is="+z);
obj.withdraw(z);
System.out.println("after draw balance is="+obj.acctbal);
double t=obj.tax();
System.out.println("Tax is="+t);
obj.withdraw(t);
System.out.println("Remaining balance is ="+obj.acctbal);
obj.withdraw(25000.0);
obj.showdetail();
CurrentAccount obj1=new CurrentAccount("01-102-33","Ali",10000.0);
System.out.println("My Balance is="+obj1.acctbal);
t=obj1.tax();
System.out.println("Tax is ="+t);
obj1.withdraw(t);
System.out.println("Remaining bal is="+obj1.acctbal);
obj1.showdetail();
JointAccount obj2= new JointAccount("11-101-23","Amjad","Usman",20000.0);
System.out.println("My Balance is="+obj2.acctbal);
t=obj2.calculatetax();
System.out.println("Tax is ="+t);
obj2.showdetail();
}
}
Upvotes: 1
Views: 3383
Reputation: 4843
Several suggestions:
Abstract
(you have not defined any method that has only a signature (name and parameters) and no code implementing the methodConsider the following replacements
TestApp
public class TestApp {
public static void main(String[] args) {
SavingAccount sa = new SavingAccount();
sa.open("Amjad", "5440442378", 1000.00d);
System.out.println("Account name: " + sa.getName() + ", number: " + sa.getNumber());
System.out.println("Account balance: " + sa.getBalance());
System.out.println("Account tax: " + sa.calculate(.05d));
}
}
Account
public class Account {
String name;
String number;
public double balance;
public Account() {
};
public void open(String accountName, String accountNumber, double openingDeposit) {
name = accountName;
number = accountNumber;
balance = openingDeposit;
}
public void withdraw(double amount) {
if (balance > amount) {
balance -= amount;
}
}
public void deposit(double amount) {
balance = balance + amount;
}
public String getName() {return name;}
public String getNumber() {return number;}
public double getBalance() {return balance;}
}
SavingAccount (I suspect you wanted "tax" to be "rate", and that you may want to establish the rate of return when you "open" the SavingAccount (i.e. use a variable that is part of the Class)
public class SavingAccount extends Account {
public double calculate(double rate) {
return balance * rate;
}
}
Upvotes: 1
Reputation: 5638
string
should be String
public Account()
not public account()
accoutTitle(String) is undefined for the type saving_Account
: because there is no such this method , you need to define it , and also accountNumber(String)
class saving_Account extends Account {
public double calculate(double tax) {
tax = balance * 0.05;
return tax;
}
void accoutTitle(String accountTitle) {
this.accountTitle=accountTitle;
}
void accountNumber(String accountNumber) {
this.accountNumber=accountNumber;
}
}
Upvotes: 0
Reputation: 18123
You have many problems in your code:
First, Java is case sensitive, so string
should be String
, similar for your constructor. Name of the constructor should match exactly with name of your class.
String accountTitle;
String accountNumber; //Notice string has been changed to String
Second, you don't have method named accoutTitle
, accountNumber
Upvotes: 0
Reputation: 4176
string should have uppercase S. change string
to String
. String class name starts with 'S'
String accountTitle;
String accountNumber;
Upvotes: 1