質問

I'm very new to java and to get my feet a little wet I'm trying to build a simple budget program for my wife and me. I have quite a few classes, which I'm still learning to organize, but my primary question for this thread is with regards to my interface.

It looks like this:

package com.budgeter;

public interface Category {

void setPersonalLoan(int budget);
void setDonationsGifts(int budget);
void setRecreation(int budget);
void setHouseHoldItems(int budget);
void setUtilities(int budget);
void setSavings(int budget);
void setGroceries(int budget);
void setAutoInsurance(int budget);
void setAutoPayment(int budget);
void setHomeInsurance(int budget);
void setHomePayment(int budget);

}

I have no idea if this interface is done according to a best practice or not, but my goal is to have a way to have a defined set of categories. Next, I have another class called BillCategory which I can use to add a custom category. Again, not sure if this is a correct way of using an interface.

So I'm using this interface this way:

package com.budgeter;

public class Bills implements Category {

private Double amount;
private Double totalBills;

public Bills() {}
public Bills(double amount) {
    setTotal(amount);
}

private void setTotal(Double amount) {
    this.amount = amount;
    totalBills = amount;
}

public Double getTotal() {
    return totalBills;
}
@Override
public void setPersonalLoan(int budget) {
    // TODO Auto-generated method stub

}
@Override
public void setDonationsGifts(int budget) {
    // TODO Auto-generated method stub

}
@Override
public void setRecreation(int budget) {
    // TODO Auto-generated method stub

}
@Override
public void setHouseHoldItems(int budget) {
    // TODO Auto-generated method stub

}
@Override
public void setUtilities(int budget) {
    // TODO Auto-generated method stub

}
@Override
public void setSavings(int budget) {
    // TODO Auto-generated method stub

}
@Override
public void setGroceries(int budget) {
    // TODO Auto-generated method stub

}
@Override
public void setAutoInsurance(int budget) {
    // TODO Auto-generated method stub

}
@Override
public void setAutoPayment(int budget) {
    // TODO Auto-generated method stub

}
@Override
public void setHomeInsurance(int budget) {
    // TODO Auto-generated method stub

}
@Override
public void setHomePayment(int budget) {
    // TODO Auto-generated method stub

}
}

Here is my BillCategory class which I use to add a new category, but not completely sure how to relate this to my interface:

package com.budgeter;

import java.util.HashMap;
import java.util.Map;

public class BillCategory {

private static Map<String, Integer> categories = new HashMap<String, Integer>();

public void addCategory(String categoryName, int frequency) {
    categories.put(categoryName, frequency);
}

public Map<String, Integer> getCategories() {
    return categories;
}

}
  • What is the most effective way to check if an item was set and then calculate all of them for my getTotal() method?
  • Am I using an interface properly?
  • Am I using a good naming convention (e.g. Category)?
  • The setTotalAmount() method I wasn't sure if I should pass a reference to amount or set the amount using this.amount = amount and then access it globally. Is there a best practice for this?
  • What is the best way to relate my BillCategory class to the interface?
役に立ちましたか?

解決

The only thing I would suggest for your interface would be to add some sort of visibility (public, private, protected) to your method declarations. You have that in the implementation but not in the interface. The code in the implementation looks fine, just the two variables seem to be doing the same thing. I do not know if I fully understand why you called it Category but having upper case Interface names is fine convention in java.

In set methods you can do exactly what you did this.amount = amount, calling that method from the constructor is fine practice.

In regards to your first question if you add a class variable for each of your set methods and just set that variable to the value passed in then you can add up the total in the getTotal() method and return that value.

private int personalLoan;
private int donationGifts;
private int recreation;
private int houseHoldItems;
...

@Override
public void setPersonalLoan(int budget) {
    this.personalLoan = budget;
}
@Override
public void setDonationsGifts(int budget) {
    this.donationGifts = budget;
}
@Override
public void setRecreation(int budget) {
    this.recreation = budget;
}
@Override
public void setHouseHoldItems(int budget) {
    this.houseHoldItems = budget;
}
...


public double getTotal() {
    double total = personalLoan + donationGifts + recreation + houseHoldItems + utilities + savings + groceries....
    return total;
}

You can also have all of those variables as parameters in your constructor and set them using those methods instead of taking in one value for your total.

Note I did catch that you have your budgets as int's as such you cannot have decimal values for your budget. You may want to change that in your interface and then returning a double for the getTotal() makes more sense.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top