Hi i have the next task to solve.
A bank holds different types of accounts for
its customers:
– Deposit accounts
– Loan accounts
– Mortgage accounts
Customers could be individuals or
companies. All accounts have customer,
balance and interest rate (monthly based).
Deposit accounts are allowed to deposit and
withdraw money. Loan and mortgage
accounts can only deposit money.
All accounts can calculate their interest
amount for a given period (in months). In the
common case its is calculated as follows:
number_of_months * interest_rate.
Loan accounts have no interest for the first 3
months if are held by individuals and for the
first 2 months if are held by a company.
Deposit accounts have no interest if their
balance is positive and less than 1000.
Mortgage accounts have ½ interest for the
first 12 months for companies and no interest
for the first 6 months for individuals.
Your task is to write a program to model the
bank system by abstract and concrete
classes. You should identify the classes, base
classes and abstract actions and implement
the calculation of the interest functionality.
Implement a test program that shows how
different types of accounts have different
interest rates.

Because i`am new and classes are still hard for me can u explain to me where am`i doing mistakes and how should i finish the task.
How can i do that part with setting and getting separate inviduals and company balances, and how can i do the part with "no interest for companies for X many months" and "no interest for inviduals for X many months".
Here is my creation so far.
I hope someone will take the time to see it all. Thanks in advance.

package zadacha_6;

public class customer {
		private String name;

		public customer (){
			
		}
		
		public customer(String name) {
			this.name = name;
		}

		
		
		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}
		
}
package zadacha_6;

public class Inviduals extends customer{
	private String personName;
	
	public Inviduals() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Inviduals(String name) {
		super(name);
		this.personName = name;
		// TODO Auto-generated constructor stub
	}

	public String getPersonName() {
		return personName;
	}

	public void setPersonName(String personName) {
		this.personName = personName;
	}
	
	
	
	
}
package zadacha_6;

public class companies extends customer {
	private String companyName;

	public companies() {
		super();
		// TODO Auto-generated constructor stub
	}

	public companies(String name) {
		super(name);
		this.companyName = name;
		// TODO Auto-generated constructor stub
	}

	public String getCompanyName() {
		return companyName;
	}

	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}
	
	
	
	
}
package zadacha_6;

public class accounts extends customer {
	private double balance;
	private double rate; //towa tr da e monthly rate
	public accounts() {
		
	}
	public accounts(String name, double balance, double rate) {
		super(name);
		this.balance = balance;
		this.rate = rate;
		// TODO Auto-generated constructor stub
	}
	public double getBalance() {
		return balance;
	}
	public void setBalance(double balance) {
		this.balance = balance;
	}
	public double getRate() {
		return rate;
	}
	public void setRate(double rate) {
		this.rate = rate;
	}
	
	
}
package zadacha_6;

public class deposit_accounts extends accounts{
	private double deposit;
	private double depositRate = (double)20/(double)100;
	private int months;
	private double withdraw;
	
	public deposit_accounts() {
		super();
		// TODO Auto-generated constructor stub
	}

	public deposit_accounts(String name, double balance, double rate) {
		super();
		// TODO Auto-generated constructor stub
	}
	public void deposit (String name, double deposit){
		
		setBalance(getBalance() + deposit);
		
	}
	public void calcRate (double balance, double rate){
		if (getBalance()>0 && getBalance() <1000){
			rate = 1;
		}
		else {
		rate = (getBalance()*depositRate)*months;
		}
	}
	public void withdraw (String name, double withdraw){
		setBalance(getBalance() - withdraw); 
	}
	

}
package zadacha_6;

public class loan_accaunts extends accounts {
		private double loanRate = (double)5/(double)100;
		
		private double desposit;
		private int months;
		public loan_accaunts() {
			super();
			// TODO Auto-generated constructor stub
		}
	
		public loan_accaunts(String name, double balance) {
			super();
		
			// TODO Auto-generated constructor stub
		}
		
		public void deposit (String name, double deposit){
			
			setBalance(getBalance() + deposit);
			
		}
		public void calcRate (double balance, double rate){
			
			rate = (getBalance()*loanRate)*months;
		}

}
package zadacha_6;

public class morgage_accounts extends accounts {
		private double morgageRate = (double)10/(double)100;
		private double deposit;
		private int months;
		public morgage_accounts() {
			super();
			// TODO Auto-generated constructor stub
		}
		public morgage_accounts(String name, double balance) {
			super();
			// TODO Auto-generated constructor stub
		}
		public void deposit (String name, double deposit){
			
			setBalance(getBalance() + deposit);
			
		}
		public void calcRate (double balance, double rate){
			rate = (getBalance()*morgageRate)*months;
		}
}

Recommended Answers

All 21 Replies

A few quick observations:
Class names: should begin with a capital letter and shouldn't be plural when they represent a single instance of something. Eg use Account rather than accounts.
An Account is not a kind of Customer, so it shouldn't extend that class.
Do you need personName and companyName? Why not just use the inherited name variable from Customer?
The no-args constructor for Customer (etc) is dangerous - it allows the creation of a Customer with no name, and therefore no way to refer to it or find it later. (Typically you would have a unique customerID field that is mandatory in all the constructors.)
Customer and Account should be abstract - you cannot instantiate one of there, only their concrete sub-classes.

And to continue the suggestions, the Account class could look like this:

public abstract class Account {
  private Custome cust = null;
  private double rate = 0.0;
  private double balance = 0.0;
}

You can implement any methods you want (get/set). It doesn't matter that it is abstract. Create any methods you want.

And:

public class Deposit extends Account {
}

i have changed the names of the classes with capital letter and not to be in plural.
Account was inheriting customer because iam tring to have different accounts for invidual customers and for companies. There are (as it says in the assignment) different rates and calculations for different types of customers.

i
Account was inheriting customer because iam tring to have different accounts for invidual customers and for companies. There are (as it says in the assignment) different rates and calculations for different types of customers.

You will need to attach Account instances to Customer instances to represent a customer opening an account. This is how you will access the account for a given customer, and the method for calculating interest will need the Customer passed as a paameter so it can use the right formula.
In any case, Account extends Customer is wrong - Accounts are not Customers, they don't have an address or a date of birth, or any other attributes or behaviours of Customers.

You will need to attach Account instances to Customer instances to represent a customer opening an account. This is how you will access the account for a given customer, and the method for calculating interest will need the Customer passed as a paameter so it can use the right formula.
In any case, Account extends Customer is wrong - Accounts are not Customers, they don't have an address or a date of birth, or any other attributes or behaviours of Customers.

How can i attach Customer instances to Account instances?

And should i make Customer class abstract as I did with Account.

i have changed the names of the classes with capital letter and not to be in plural.
Account was inheriting customer because iam tring to have different accounts for invidual customers and for companies. There are (as it says in the assignment) different rates and calculations for different types of customers.

Then customers should have an attribute: rateType.
Also when Account extended Customer is like saying that Account is a Customer, whish is not.

The assignment says:

All accounts have customer,
balance and interest rate

So the Account class would be like this:

public class Account {
  private Custome cust = null;
  private double rate = 0.0;
  private double balance = 0.0;

// Constructor and get/set methods
}

Then, as the assignment says, you will have Deposit, Loan, Mortgage classes that extends Account and each one of them will have the attributes described at the assignment.

How this ratetype is gonna work?
Iam not getting a lot of help, probably its too long to read it all and figure out what directions to post.
I`ll try in a few days

may be of I put private static final boolean isInvidual = true; for inviduals and private static final boolean isCompany = true; i might be able to separate the different interests for different customers. But i still cant see it all put together

may be of I put private static final boolean isInvidual = true; for inviduals and private static final boolean isCompany = true; i might be able to separate the different interests for different customers. But i still cant see it all put together

I think this is going in the right direction - Customers should know what kind of customer they are, and Accounts should know about interest rates. Rather than a private boolean variable you can simply define a public method in the Customer class:

public boolean isIndividual();

then override it as appropriate in the subclasses:

class Individual {
   public boolean isIndividual() {return true;}
etc

Then in the Account classes' interest calculations you can use them like this:

if (customer.isIndividual() {
   // do calc for individuals here
} else {
   // do calc for companies here
}

Ok I`am still stuck with this task but i want it done.

here is whats done (u can see the code in the beginning of the thread):

Class Customer - i have here only string name - do i need it at all. What suppost to be in there. Should it be abstract.

Class Invidual - should i have String personName in here? Or i just have to extend it from customer abstract class with super(name). Here i have a boolean isInvidual that hopefully will help me with determining which method for calulating interest to use later on.

Class Company should be the same like invidual - super(name) from Customer.

Class Account - should it be abstract, so the other 3 classes loan, morgage and deposit should develop it? what should i have in here?

Class deposit - i have to get somehow the company or invidual balance, and have the operations deposit, withdraw, and no interest if balance is <1000.

Give me directions till here i think i can make the other 2 classes after that.

Yes, use inherited name.
Account should be abstract - you can't instantiate it, only one of the subclasses.
Balance variable and deposit/withdraw methods should be defined in the Account class then overidden as necessary in the subclasses.
General rule of thumb: anything that occurrs in more than one subclass should be defined in the superclass, even if the details differ between subclasses

ok in Account class i`ll define all methods and predefine them in the subclasses.
Tell me how can change the balance for some customer without account extending customer?

P.S. By mistake i marked the thread as solved how can i undo this?

A customer has one or more Accounts, and so will have one or more balances, one per Account. Balance is an attribute of an Account, not a Customer.
(Maybe you are confusing the class Account with the individual instances of that class?)
Every (instance of an) Account is for exactly one customer. The attributes of an Account will include the Customer and the balance.
Account does not extend Customer; an Account is not a kind of Customer.
Re: ps: Sorry, don't know.

here is what i got now:

package zadacha_6;

public class Customer {
	private String name;

	public Customer() {

	}

	public Customer(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}
package zadacha_6;

public class Inviduals extends Customer {
	private String personName;

	public Inviduals() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Inviduals(String name) {
		super(name);
		this.personName = name;
		// TODO Auto-generated constructor stub
	}

	public boolean isInvidual() {
		return true;
	}

	public String getPersonName() {
		return personName;
	}

	public void setPersonName(String personName) {
		this.personName = personName;
	}

}
package zadacha_6;

public class Companies extends Customer {
	private String companyName;

	public Companies() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Companies(String name, String companyName) {
		super(name);
		this.companyName = name;
		// TODO Auto-generated constructor stub
	}
	
	public boolean isInvidual() {
		return false;
	}

	public String getCompanyName() {
		return companyName;
	}

	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}
	
	
	
	
}
package zadacha_6;

public abstract class Account {
	private double balance = 0.0;
	private double rate = 0.0;
	private Customer customer = null;
	private double depositAmount = 0.0;
	private double withdrawAmount = 0.0;

	public Account(Customer customer, double balance, double rate) {
		super();
		this.balance = balance;
		this.rate = rate;
		this.customer = customer;
	}

	public Account() {
		// TODO Auto-generated constructor stub
	}

	public String getName() {
		return customer.getName();
	}

	public void setName(String name) {
		customer.setName(name);
	}

	public double deposit(Customer customer) {
		balance = depositAmount + balance;
		return balance;
	}

	public double withdraw(Customer customer) {
		balance = balance - withdrawAmount;

		return balance;
	}

}
package zadacha_6;

public class Deposit_account extends Account {
	private double deposit;
	private double depositRate = (double) 20 / (double) 100;
	private int months;
	private double withdraw;

	public Deposit_account() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Deposit_account(Customer customer, double balance, double rate) {
		super(customer, balance, rate);
		this.depositRate = rate;
		// TODO Auto-generated constructor stub
	}

	public void deposit(Customer customer, double depositAmount) {
		super.deposit(customer);
	}

	public double calcRate(double balance, double rate) {
		rate = balance * depositRate * months;
		return rate;
	}

	public void withdraw(Customer customer, double withdrawAmount) {
		super.withdraw(customer);
	}

}

i`ll paste the other classes later on

Looking better: you still don't need things like companyName (use the inherited name variable) or depositRate(use the inherited rate variable) or the accessor methods for these vairables (use the inherited accessors).
Get/set name for Accounts doesn't make sense. It's Customers that have names.
Deposit doesn't need a customer parameter - you already know the customer for this account, ditto balance, rate, and name in the next 2 methods.

Here is fixed .. I hope. Tell me what else is wrong?

package zadacha_6;

public class Customer {
	private String name;

	public Customer() {

	}

	public Customer(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}
package zadacha_6;

public class Inviduals extends Customer {
	private String personName;

	public Inviduals() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Inviduals(String name) {
		super(name);
		this.personName = name;
		// TODO Auto-generated constructor stub
	}

	public boolean isInvidual() {
		return true;
	}

	public String getPersonName() {
		return personName;
	}

	public void setPersonName(String personName) {
		this.personName = personName;
	}

}
package zadacha_6;

public class Companies extends Customer {
	
	public Companies() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Companies(String name) {
		super(name);
		
		// TODO Auto-generated constructor stub
	}
	
	public boolean isInvidual() {
		return false;
	}

	public String getCompanyName() {
		return getName();
	}

	public void setCompanyName(String name) {
		setName(name);
	}
	
	
	
	
}
package zadacha_6;

public abstract class Account {
	private double balance = 0.0;
	private double rate = 0.0;
	private Customer customer = null;
	private double depositAmount = 0.0;
	private double withdrawAmount = 0.0;
	private int months;


	public Account(Customer customer, double balance, double rate, int months) {
		super();
		this.balance = balance;
		this.rate = rate;
		this.customer = customer;
		this.setMonths(months);
		
	}

	public Account() {
		// TODO Auto-generated constructor stub
	}

	public String getName() {
		return customer.getName();
	}

	public void setName(String name) {
		customer.setName(name);
	}

	public double deposit(Customer customer) {
		balance = depositAmount + balance;
		return balance;
	}

	public double withdraw(Customer customer) {
		balance = balance - withdrawAmount;
		return balance;
	}

	public void setMonths(int months) {
		this.months = months;
	}

	public int getMonths() {
		return months;
	}
	
	

}
package zadacha_6;

public class Deposit_account extends Account {

	
	public Deposit_account() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Deposit_account(Customer customer, double balance, double rate, int months) {
		super(customer, balance, rate, months);
		rate =(double) 20 / (double) 100;
		
		// TODO Auto-generated constructor stub
	}

	public void deposit(Customer customer, double depositAmount) {
		super.deposit(customer);
	
	}

	public double calcRate(double balance, double rate) {
		if (balance > 0 && balance <1000){
			System.out.println ("No interest for account under 1000.");
			return rate = 0; 
		}
		rate = balance * rate * getMonths();
		return rate;
	}

	public void withdraw(Customer customer, double withdrawAmount) {
		super.withdraw(customer);
	}

}
package zadacha_6;



public class Loan_account extends Account {
	


	public Loan_account() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Loan_account(Customer customer, double balance, double rate, int months) {
		super(customer, balance, rate, months);
		rate = (double) 5 / (double) 100;
		// TODO Auto-generated constructor stub
	}

	public void deposit(Customer customer, double depositAmount) {
		super.deposit(customer);
	}

	public double calcRate(double balance, double rate, boolean isInvidual) {
		if ((getMonths() <=3) && (isInvidual)) {
			rate = 0;
			return rate;
		}
		else if ((getMonths() <=2) && (!isInvidual)){
			rate = 0;
			return rate;
		}
		else rate = (balance * rate) * getMonths();
		return rate;
	}
	

}
package zadacha_6;

public class Morgage_account extends Account {
	
		public Morgage_account() {
			super();
			// TODO Auto-generated constructor stub
		}
		public Morgage_account(Customer customer, double balance, double rate, int months) {
			super(customer, balance, rate, months);
			rate = (double) 20 / (double) 100;
			// TODO Auto-generated constructor stub
		}
		public void deposit (Customer customer, double deposit){
			super.deposit(customer);
		}
		public double calcRate(double balance, double rate, boolean isInvidual) {
			if ((getMonths() <=6) && (isInvidual)) {
				rate = 0;
				return rate;
			}
			else if ((getMonths() <=12) && (!isInvidual)){
				rate = rate/2;
				return rate;
			}
			else rate = (balance * rate) * getMonths();
			return rate;
		}
}

You did some of the things I suggested, but not all. That's OK, it's your project.
Good luck.
James

only in the Accounts class i have to remove customer as argument in the deposit method. I think everything else i did by your suggestion.
Now if everything looks fine - how can i test it :)?

rate, balance etc are already variables in the class, you shouldn't pass them as parameters.
isIndividual is an accessible method of the customer, so that also should not be passed as a parameter.
So: deposit & withdraw just need an amount. calcRate needs no parameters.
To test, create a main(...) method that creates a new Customer of some sort, creaqtes an Account or two for that Customer, and calls the methods.
Use Syetem.out.println to display the results.

I'm going out now, so no more posts tonight.

i cleaned the project as u said, but something isnt right. i cant deposit and probably there are more mistakes.

package zadacha_6;

public class Companies extends Customer {
	
	public Companies() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Companies(String name) {
		super(name);
		
		// TODO Auto-generated constructor stub
	}
	
	public boolean isInvidual() {
		return false;
	}

}
package zadacha_6;

public class Inviduals extends Customer {
	
	public Inviduals() {
		super();
	}

	public Inviduals(String name) {
		super(name);
	
	}

	public boolean isInvidual() {
		return true;
	}

}
package zadacha_6;

public class Companies extends Customer {
	
	public Companies() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Companies(String name) {
		super(name);
		
		// TODO Auto-generated constructor stub
	}
	
	public boolean isInvidual() {
		return false;
	}

}
package zadacha_6;

public abstract class Account {
	protected double balance = 0.0;
	protected double rate = 0.0;
	protected Customer customer = null;
	protected double depositAmount = 0.0;
	protected double withdrawAmount = 0.0;
	protected int months;


	public Account(Customer customer) {
		super();
		this.balance = balance;
		this.rate = rate;
		this.customer = customer;
		this.setMonths(months);
		
	}

	public Account() {
		// TODO Auto-generated constructor stub
	}

	public Account(Customer customer, double balance, int months) {
		// TODO Auto-generated constructor stub
	}

	public double deposit() {
		balance = depositAmount + balance;
		return balance;
	}

	public double withdraw(Customer customer) {
		balance = balance - withdrawAmount;
		return balance;
	}

	public void setMonths(int months) {
		this.months = months;
	}

	public int getMonths() {
		return months;
	}
	
	

}
package zadacha_6;

public class Deposit_account extends Account {

	
	public Deposit_account() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Deposit_account(Customer customer) {
		super(customer);
		this.rate =(double) 20 / (double) 100;
		
		// TODO Auto-generated constructor stub
	}

	public double deposit(double depositAmount) {
		super.deposit();
		return balance;
	}

	public double calcRate() {
		if (balance > 0 && balance <1000){
			System.out.println ("No interest for account under 1000.");
			return rate = 0; 
		}
		rate = balance * rate * getMonths();
		return rate;
	}

	public double withdraw(double withdrawAmount) {
		super.withdraw(customer);
		return balance;
	}

}
package zadacha_6;



public class Loan_account extends Account {
	


	public Loan_account() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Loan_account(Customer customer) {
		super(customer);
		rate = (double) 5 / (double) 100;
		// TODO Auto-generated constructor stub
	}

	public double deposit(double depositAmount) {
		super.deposit();
		return balance;
	}

	public double calcRate(boolean isInvidual) {
		if ((getMonths() <=3) && (isInvidual)) {
			rate = 0;
			return rate;
		}
		else if ((getMonths() <=2) && (!isInvidual)){
			rate = 0;
			return rate;
		}
		else rate = (balance * rate) * getMonths();
		return rate;
	}
	

}
package zadacha_6;

public class Morgage_account extends Account {
	
		public Morgage_account() {
			super();
			// TODO Auto-generated constructor stub
		}
		public Morgage_account(Customer customer) {
			super(customer);
			rate = (double) 20 / (double) 100;
			// TODO Auto-generated constructor stub
		}
		public double deposit (double depositAmount){
			super.deposit();
			return balance;
		}
		
		public double calcRate(boolean isInvidual) {
			if ((getMonths() <=6) && (isInvidual)) {
				rate = 0;
				return rate;
			}
			else if ((getMonths() <=12) && (!isInvidual)){
				rate = rate/2;
				return rate;
			}
			else rate = (balance * rate) * getMonths();
			return rate;
		}
}

here is the test program with some issues.

package zadacha_6;

public class TestBank {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Customer human = new Customer("human");
		Inviduals invidual = new Inviduals();
		
		//deposit account test for invidual - works
		Deposit_account human_deposit = new Deposit_account(invidual);
		human_deposit.deposit(1500);
		System.out.println ("The balance for human deposit account is: " + human_deposit.balance);
		human_deposit.setMonths(3);
		System.out.println ("The rate for human for " + human_deposit.getMonths() + " months is " + human_deposit.calcRate());
		human_deposit.withdraw(1000);
		System.out.println ("Human deposit account balance after withdraw " + human_deposit.balance);
		System.out.println ("The rate for human for " + human_deposit.getMonths() + " months is " + human_deposit.calcRate());
		System.out.println ("---------------------------------------");
		
		//loan account test for invidual - problem with boolean
		Loan_account human_loan = new Loan_account(invidual);
		System.out.println ("The balance for human loan account is: " + human_loan.balance);
		human_loan.deposit(5000);
		System.out.println ("The balance for human loan account after depositing 5000 is: " + human_loan.balance);
		human_loan.setMonths(2);
		System.out.println ("the rate for " + human_loan.getMonths() + " months for human loan acc is :" + human_loan.calcRate(true));
		// why in human_loan.calcRate method i should specify if its true or false. why it doesnt get it by itself from the account class where ist specified already
		human_loan.setMonths(8);
		System.out.println ("the rate for " + human_loan.getMonths() + " months for human loan acc is :" + human_loan.calcRate(true));
		// why it doesnt calculate the rate its in the constructor for loan account.
		System.out.println("---------------------------------");
		
		//test for human morgage account
		Morgage_account human_morgage = new Morgage_account(invidual);
		System.out.println ("The balance for human morgage account is: " + human_morgage.balance);
		human_morgage.deposit(10000);
		System.out.println ("The balance for human morgage account after depositing 10000 is: " + human_morgage.balance);
		human_morgage.setMonths(5);
		System.out.println ("the rate for " + human_morgage.getMonths() + " months for human morgage acc is :" + human_morgage.calcRate(true));
		human_morgage.setMonths(10);
		System.out.println ("the rate for " + human_morgage.getMonths() + " months for human morgage acc is :" + human_morgage.calcRate(true));
		// here the rate doesnt work too and i have to specify its invidual
		
	}

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.