Использование массивов для отслеживания расходов в командировке - PullRequest
0 голосов
/ 30 января 2012

Как новичок в Java и языках программирования в целом, у меня есть вопрос. Я написал программу, которая должна отслеживать расходы на командировку. Однако я не уверен, какие конструкторы использовать. Я также не понимаю, как включить другие мои методы в мой основной метод. Вот мой код:

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();

  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...