使用数组来跟踪商务旅行中的费用

时间:2012-01-29 23:46:49

标签: java arrays printing methods constructor

作为Java和编程语言的新手,我有一个问题。我写了一个程序,应该跟踪商务旅行的费用。但是,我不确定使用什么样的构造函数。我也很困惑如何将我的其他方法合并到我的main方法中。这是我的代码:

import java.util.*;
import java.util.Scanner;

public class Expenses

{
  static String[] expenseType={"air travel","hotel","rental vehicle","toll","meal"};
  int numDaysTravel = 0;

public static int calcExpenses(int numDaysTravel, int[] anArray) 
  {
    int sumExpenses = (anArray[0] + anArray[1] + anArray[2] + anArray[3] + anArray[4]);
    int totalExpenses = sumExpenses * numDaysTravel;
    return totalExpenses;
  }

public static int[] anArray()
  {
    int[] anArray = new int[5];
    anArray[0] = 0;
    anArray[1] = 0;
    anArray[2] = 0;
    anArray[3] = 0;
    anArray[4] = 0;
    return anArray;
  }

  public void addExpense(int[] anArray)
  {
    Scanner keyboard = new Scanner(System.in);
    int inputType;
    System.out.println("What kind of expense should be added?");
    System.out.println("Press 1 to add an air travel expense.");
    System.out.println("Press 2 to add a hotel expense.");
    System.out.println("Press 3 to add a rental vehicle or taxi expense.");
    System.out.println("Press 4 to add a toll expense.");
    System.out.println("Press 5 to add a meal expense.");
    inputType = keyboard.nextInt();
    System.out.println("How much is this "+expenseType[inputType - 1]+"expense?");
    anArray[inputType] = keyboard.nextInt();

  }

  public static int Reimburse()
  {
    int mealReimburse = 50;
    int autoReimburse = 100;
    int hotelReimburse = 200;
    int totalReimburse = (mealReimburse + autoReimburse + hotelReimburse);
    return totalReimburse;
  }

  public void printExpense(int numDaysTravel, int totalExpenses, int totalReimburse, int[] anArray)
  {
    System.out.println("Number of days traveled: " + numDaysTravel);
    System.out.println("Air expenses: " + anArray[0]);
    System.out.println("Hotel expenses: " + anArray[1]);
    System.out.println("Vehicle expenses: " + anArray[2]);
    System.out.println("Tolls: " + anArray[3]);
    System.out.println("Meals: " + anArray[4]);    
    System.out.println("Total expenses: " + totalExpenses);
    System.out.println("Total reimbursed: " + totalReimburse);
  }

  public static void main(String[] args)
  {
    Scanner in = new Scanner(System.in);
    System.out.println("Would you like to add an expense? (y/n)");
    String input = in.nextLine();
    while (input == "y")
    {
      addExpense();
    }
    System.out.println("Finished with adding expenses!");
    printExpense();

  }
}

0 个答案:

没有答案