嵌入式Switch Case语句

时间:2014-08-08 17:29:58

标签: java switch-statement case

所以我正在尝试测试我的程序,看它是否会累计我添加的支出。

所以我使用选项2,添加两个费用,以测试是否总计。然后我转到选项5(费用选项),然后转到案例2(列出总费用),这是它重置并重新打开原始Joption屏幕

package homebudget;

import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JOptionPane;

/**
*
* @author Derek
*/
public class HomeBudget 
{
//Features to add:
//Reminder 2 days before an auto deduction




public static void main(String[] args) throws Exception
{
    // TODO code application logic here

List<Bills> deductions = new ArrayList();
String billName, deductDate, resp;
double amount, totalAmount;
int cmd, year, month, date;
totalAmount = 0;

List<Spending> expenses = new ArrayList();
String type;

List<Income> deposits = new ArrayList();
String incomeType;

String fname = JOptionPane.showInputDialog("Enter the name of the budget file, none if no file");
    if (fname.compareTo("none") !=0)
    {
        FileInputStream ist = new FileInputStream(fname);
        ObjectInputStream ifile = new ObjectInputStream(ist);
        deductions = (ArrayList<Bills>) ifile.readObject();

    }
    boolean done = false;
    while(!done)
    {
        resp = JOptionPane.showInputDialog("Enter a command from: \n" 
                + "\t1:Add a new deduction\n"  //think its done
                + "\t2:Add a new expense\n"  
                + "\t3:Add a deposit\n"  
                + "\t4:Deduction options\n"  
                + "\t5:Expense Options\n"
                + "\t6:Total balances in bank\n"
                + "\t7:quit");
        cmd = Integer.parseInt(resp);
        switch(cmd)
        {
            case 1:

            billName = JOptionPane.showInputDialog("Enter the name of the bill:");
            deductDate = JOptionPane.showInputDialog("Enter the deduct date:");
            resp = JOptionPane.showInputDialog("Enter the deduct amount");
            amount = Double.parseDouble(resp);

            Bills d = new Bills(billName, deductDate, amount);
            deductions.add(d);
            break;

            case 2:
            //Give the option to add new spending occurence.
            //Give option to choose from array of spending types.

            type = JOptionPane.showInputDialog("Enter the type of the expense:"); 

            resp = JOptionPane.showInputDialog("Enter the amount of the expense:");   
            amount = Double.parseDouble(resp);
            resp = JOptionPane.showInputDialog("Enter the year of the expense:");
            year = Integer.parseInt(resp);
            resp = JOptionPane.showInputDialog("Enter the month of the expense:");
            month =  Integer.parseInt(resp);
            resp = JOptionPane.showInputDialog("Enter the date of the expense:");
            date =  Integer.parseInt(resp);
            Spending s = new Spending(amount, type, year, month, date);
            expenses.add(s);

            break;

            //Income reporting    
            case 3:

            resp = JOptionPane.showInputDialog("Enter the type of deposit:"); 
            incomeType = resp;
            resp = JOptionPane.showInputDialog("Enter the amount of the deposit:");   
            amount = Double.parseDouble(resp);
            resp = JOptionPane.showInputDialog("Enter the year of the deposit:");
            year = Integer.parseInt(resp);
            resp = JOptionPane.showInputDialog("Enter the month of the deposit:");
            month =  Integer.parseInt(resp);
            resp = JOptionPane.showInputDialog("Enter the date of the deposit:");
            date =  Integer.parseInt(resp);        
            Income i = new Income(amount, incomeType, year, month, date);

            break;

            case 4:

            //deduction options

            break;

            case 5:

            //expense options
            resp = JOptionPane.showInputDialog("Enter a command from: \n" 
                + "\t1: List all expenses in a month\n"  //done
                + "\t2: List the total spent on an expense for the month\n"  
                + "\t3: List all expenses in a year\n"  
                + "\t4: List all expenses by type in a year\n"
                + "\t5:  \n"
                + "\t6:  \n"
                + "\t7:quit");
                switch(cmd){

                    case 1:


                        resp = JOptionPane.showInputDialog("What year of expenses would you like to see?:"); 
                        year = Integer.parseInt(resp);
                        resp = JOptionPane.showInputDialog("What month of expenses would you like to see?:"); 
                        month =  Integer.parseInt(resp);

                        boolean found = false;

                        Iterator<Spending> spendIter = expenses.iterator();

                        while(!found && spendIter.hasNext())
                        {
                         s = spendIter.next();
                         if(s.getYear() == year && s.getMonth() == month)
                        {
                       //How to print out an element of this Array List?
                       System.out.println("Here is the expense list for the month number " + month + ": \n") ;
                       System.out.println("Expense Type: " + s.getType() + "Amount" + s.getAmount() + "Date" + s.getDay());


                                found = true;

                        }
                         else  
                         {
                            System.out.println("There are no expenses entered for this month.");
                            }

                        }  
                    break;

                    case 2:
                        //How to list total spending on an expense per month.
                        type = JOptionPane.showInputDialog("Enter the type of the expense:"); 
                        resp = JOptionPane.showInputDialog("Enter the month of the expense:");
                        month = Integer.parseInt(resp);
                        found = false;

                        spendIter = expenses.iterator();
                        double totalSpent = 0;
                        while(spendIter.hasNext())
                        {
                         s = spendIter.next();
                         if(s.getType() == type && s.getMonth() == month)
                        {

                         totalSpent = totalSpent + s.getAmount();
                        }
                        JOptionPane.showMessageDialog(null, "Total money spent on " + type + ": $" 
                                + totalSpent);
                        }
                break;





    }


}

}
}

1 个答案:

答案 0 :(得分:2)

在内置交换机中,您仍然在尝试打开cmd,这是外部交换机正在打开的内容。因此,在第二次切换之前,您应该执行以下操作:

int cmd2 = Integer.parseInt(resp);

然后:

switch (cmd2) {

我认为您在某处也错过了break,但这不是主要问题。