It's been a while since I did some Java so I decided to have a dabble.
There are a couple of ways to restructure the classes. I'll show one of the ways and then also describe the other. It's important to understand a few concepts; it's important to know the difference between class variables and method variables (I forget their proper names, it's been a while since I did any serious Java development). In my example I've deliberately called the variables in the Customer class the same thing. But you can see where class variables are used because they are prefixed with "this.".
FIRST WAY
In my example I've tried to keep fairly close to your structure by using
getter and
setter methods. What this means is that I've made the Customer class variables to be scoped as
private. So no other classes can refer to them directly. Instead the only way to get them, or to set them, is to use the getter and setter methods. I feel this is generally safer than allowing other classes to access the variables directly because this way the Customer class has complete control over how valkues are accessed or referenced. The only way to change them or to retrieve them is to use the Customer class itself.
1) The Acccountancy class instantiates a Customer object.
2) The Accountancy class then uses the Customer class
setter methods to set all of the customer data.
3) When the Accountancy class wants to retrieve those values again it uses the
getter methods.
4) Several variables are calculated and not stired directly. There are specific methods to retrieve them (I called them all
calculateXXXXX). I don't store them in variables, but calculate them every time, because it's possible that the variables which the calculation methods depend on could be changed at any time via a setter method.
Customer Class:
Code:
public class Customer {
private String customerName;
private double openingBalance;
private double debitPurchases;
private double creditPayment;
private double closingBalance;
private double hoursWorked;
private double hourlyRate;
private double totalWage;
private double highTaxRate;
private double totalTaxPaid;
//
// Getters and setters - These are pairs of methods used to get or set private variables
//
public String getCustomerName() {
return this.customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public double getOpeningBalance() {
return this.openingBalance;
}
public void setOpeningBalance(double openingBalance) {
this.openingBalance = openingBalance;
}
public double getDebitPurchases() {
return this.debitPurchases;
}
public void setDebitPurchases(double debitPurchases) {
this.debitPurchases = debitPurchases;
}
public double getCreditPayment() {
return this.creditPayment;
}
public void setCreditPayment(double creditPayment) {
this.creditPayment = creditPayment;
}
public double getClosingBalance() {
return this.closingBalance;
}
public void setClosingBalance(double closingBalance) {
this.closingBalance = closingBalance;
}
public double getHoursWorked() {
return this.hoursWorked;
}
public void setHoursWorked(double hoursWorked) {
this.hoursWorked = hoursWorked;
}
public double getHourlyRate() {
return this.hourlyRate;
}
public void setHourlyRate(double hourlyRate) {
this.hourlyRate = hourlyRate;
}
public double getTotalWage() {
return this.totalWage;
}
public void setTotalWage(double totalWage) {
this.totalWage = totalWage;
}
public double getHighTaxRate() {
return this.highTaxRate;
}
public void setHighTaxRate(double highTaxRate) {
this.highTaxRate = highTaxRate;
}
public double getTotalTaxPaid() {
return this.totalTaxPaid;
}
public void setTotalTaxPayed(double totalTaxPaid) {
this.totalTaxPaid = totalTaxPaid;
}
//
// Methods to return calculated values
//
public double calculateCustomerBalance() {
return this.openingBalance + this.creditPayment - this.debitPurchases;
}
public double calculateWeeklyWage() {
return this.hoursWorked * this.hourlyRate;
}
public double calculateTax() {
return calculateWeeklyWage() * this.highTaxRate;
}
}
Accountancy Class:
Code:
public class Accountancy {
public static void main(String[] args) {
Customer a3 = new Customer();
a3.setCustomerName("David OSullivan");
a3.setOpeningBalance(700);
a3.setDebitPurchases(500);
a3.setCreditPayment(100);
a3.setClosingBalance(0);
a3.setHoursWorked(65.4);
a3.setHourlyRate(15.0);
a3.setHighTaxRate(42.0);
System.out.println("Your name is: " + a3.getCustomerName());
System.out.println("Your opening Balance is: " + a3.getOpeningBalance());
System.out.println("Your Closing Balance is: " + a3.calculateCustomerBalance());
System.out.println("This week you worked: " + a3.getHoursWorked());
System.out.println("Your Hourly Rate is: " + a3.getHourlyRate());
System.out.println("You pay the tax rate of 42%!");
System.out.println("Your total wage this week is: " + a3.calculateWeeklyWage());
System.out.println("The amount of tax you pay weekly is: " + a3.calculateTax());
System.out.println();
}
}
SECOND WAY
Another way of doing it would be to have a constructor where you pass all data into it. When the object is created all of the class variables are assigned within this constructor. You then don't need setter methods because you've already set the values when the object was created. You would still need some of the getter methods, or to expose the class variables directly again (public or protected). The slight disadvantage of this approach would be that once you've created the Customer object then you can't ever change the values in it nless you create a new object.
Of course it would be possible to do both; allow the values to be set by the constructor and also create every setter method. This way you would have a choice of which way you want to set the values.