更新While循环中的变量

时间:2016-01-22 19:28:49

标签: java while-loop

我在家庭预算申请方面遇到了一些问题。在我插入新的收入或账单,检查预算后,该程序甚至会在客户代码中添加以前的账单(或收入)。

public class Client {
public static void main(String[] args) {
    Budget bud = new Budget();
    Bills[] expenses = new Bills[10];
    Income[] salary = new Income[4];
    boolean toContinue = true;
    String input = "";
    int menuItem = 0;
    int counter = 0;
    int salCount = 0;
    float budget = 0;
    while (toContinue) {
        input = JOptionPane
                .showInputDialog("(1) Add an expense\n(2) Add the salary\n(3) Display Current Budget\n(4) Exit");
        menuItem = Integer.parseInt(input);

        switch (menuItem) {
        case 1:       //Add expense
            String name = JOptionPane.showInputDialog("Enter the name of the expense");
            double amount = Double.parseDouble(JOptionPane.showInputDialog("Enter the amount for " + name));
            expenses[counter++] = new Bills(name, amount);
            break;
        case 2:     //Add salary
            String checkSal = JOptionPane.showInputDialog("Enter salary details");
            double checkAm = Double.parseDouble(JOptionPane.showInputDialog("Enter the amount of " + checkSal));
            salary[salCount++] = new Income(checkSal, checkAm);

            break;
        case 3:   // display current budget
            try {
                String expDisplay = "Expenditure\t             Amount\n";
                String salDisplay = "Salary\t                        Amount\n";
                float expenseSum = 0;
                float salarySum = 0;
                for (int i = 0; i < salary.length; i++) {
                    if (salary[i] != null)
                        salarySum += salary[i].Total();
                }

                for (int e = 0; e < expenses.length; e++) {
                    if (expenses[e] != null)

                        expenseSum += expenses[e].Total();
                }

                for (Bills e : expenses) {
                    if (e != null)
                        expDisplay += e.toString() + "\n";
                }

                for (Income p : salary) {
                    if (p != null)
                        salDisplay += p.toString() + "\n";

                }

                File fileBudget = new File("C:budget.txt");

                if (!fileBudget.exists()) {   //creating new txt
                    bud.setBudget(budget);
                    fileBudget.createNewFile();
                    System.out.println("TEXT File Created");
                    FileWriter fw = new FileWriter(fileBudget, false);
                    fw.write(bud.getBud() + "");
                    fw.flush();
                    fw.close();
                    budget = salarySum - expenseSum;  //calculating budget
                    JOptionPane.showMessageDialog(null, expDisplay + "\n\n" + salDisplay + "\n\n" + "You spent: "
                            + expenseSum + "\n" + "Budget: " + budget);
                } else {

                    BufferedReader re = new BufferedReader(new FileReader("C:budget.txt")); //reading budget on txt file
                    while (true) {
                        String line = re.readLine();
                        if (line == null) {
                            break;
                        } else {
                            float d = Float.valueOf(line);
                            bud.setBudget(d); //store the read value
                        }
                    }
                    re.close();    //end reading

                    float c = bud.getBud();   //taking budget value


                    // problem...there are even previous sum values!!
                    budget = salarySum - expenseSum + c;  // error 

                    FileWriter fw = new FileWriter(fileBudget, false);
                    fw.write(budget + "\n");
                    fw.flush();
                    fw.close();

                    JOptionPane.showMessageDialog(null, expDisplay + "\n\n" + salDisplay + "\n\n" + "You spent: "
                            + expenseSum + "\n" + "Budget: " + budget);

                }
            } catch (Exception e) {
                System.err.println("Error: " + e.getMessage());
            }

            break;
        case 4:    //close program
            JOptionPane.showMessageDialog(null, "You closed the Budget Program!");

            System.exit(0); }}}}

//budget object
public class Budget implements Serializable {
private static final long serialVersionUID = 1L;
public float bud;

Budget() 
{

}
public float getBud() {
    return bud;
}

public  void setBudget(float bud) {
    this.bud = bud;     }}

//Bill class
public class Bills {

private double amount;
private String name;

public Bills(double amount) {
    this.amount = amount;
}

public Bills(String name, double amount) {
    this.name = name;
    this.amount = amount;
}

public Bills() {
    this("New Expense", 0); }

public String toString() {
    return name + "\t     " + "\t     " + amount;   }

public double Total() {
    return amount;  }}

//Income class
public class Income {

private double amount;
private String Salary;

public Income(String Salary, double amount) {
    this.Salary = Salary;
    this.amount = amount;
}

public String toString() {
    return Salary + "\t     " + "\t             " + amount;
}

public double Total() {
    return amount;  }}

1 个答案:

答案 0 :(得分:1)

您的代码存在一些问题。

首先在这里:

for (int i = 0; i < salary.length; i++)
{
   if (salary[i] != null)
   {
      salarySum += salary[i].Total();
   }
}

for (int e = 0; e < expenses.length; e++)
{
   if (expenses[e] != null)
   {
      expenseSum += expenses[e].Total();
   }
}

每当您的用户输入[3]查看详细信息,添加这些数字,然后计算预算时,此预算就会添加到文件中。

现在在同一个会话中,用户再次单击[3]并重复上述步骤。

您应该找到一种方法来实际识别用户何时输入新费用或新工资,以避免重复计算预算,然后重复分配到文件。 (布尔变量可能?)

此外:

if (!fileBudget.exists()) {   //creating new txt
    bud.setBudget(budget);
    fileBudget.createNewFile();
    System.out.println("TEXT File Created");
    FileWriter fw = new FileWriter(fileBudget, false);
    fw.write(bud.getBud() + "");
    fw.flush();
    fw.close();
    budget = salarySum - expenseSum;  //calculating budget
    JOptionPane.showMessageDialog(null, expDisplay + "\n\n" + salDisplay + "\n\n" + "You spent: "
            + expenseSum + "\n" + "Budget: " + budget);
}

您始终为文件指定零值。即使要创建文件,也许用户已经提供了一些预算,您将丢失此数字。我想你也必须在那里计算预算。

编辑:

一个可能的解决方案,也就是简化事情,就是在用户输入新工资时实际添加到文件中,并在用户输入费用时从文件中减去,因此这两个操作将在前两个案例中进行select语句的(1和2)。 (这两个操作都要求您首先读取文本值,然后添加新工资,或者然后减去新费用)。这样,您的文件将包含最新的计算预算,在第三种情况下,您只会读取文件中的值,该值将包含最新的计算预算,而不是每次都计算第3种情况下的预算!