将所有值以java

时间:2016-11-08 16:24:23

标签: java

我只练习和学习Java几个小时,所以我还在学习基础知识。

我正在从文本文件中读取值,其中包含:

单 60 112.50

主 70 2227.50

顶层 五 5000.00

(因此在运行时显示) 房间类型:单人间,预订:60,房价:£112.00,收入:£6,750.00,税:1350.00 每个房间都排在第四位。

我已经以字符串格式打印了所有值,这是必需的。但是,我的问题非常简单。

我只想将所有收入一起添加到totalincome变量中,并将所有paidTax一起添加到totalpaidTax变量中,然后继续打印出来,基本上显示所有房间的总税额和总收入。 虽然,我只是不知道如何写它。我尝试了多次,但没有运气。 这是我目前的代码。

import java.io.FileReader;
import java.util.Scanner;
public class WagesCalculator {
public static void main(String[] args) throws Exception {
   Scanner input = new Scanner(System.in);
   Scanner file = new Scanner(new FileReader("task3.txt"));
   Scanner sc = new Scanner(System.in);

   //Current tax variable value 
   double tax = 20;

   //User Input Y or N to change tax variable value
   System.out.println("- - Hotel Tax System - -");
   System.out.print("Do you want to specify a custom Tax Rate? [Y|N]: ");

   //if statement to change tax variable value subject to Y or N
   if (sc.next().equalsIgnoreCase("Y")) {
       System.out.print("Please enter the new tax value: ");
       tax = new Scanner(System.in).nextInt();
       }

   //Prints out current tax value
   System.out.println("The current tax rate is " + tax+".");

   while (file.hasNext()) {
       String name = file.next();
       int numberOfBookings = file.nextInt();
       double price = file.nextDouble();
       double income = numberOfBookings * price;
       double paidTax = income*(tax/100);
       //String format print out final calculations
       System.out.printf("Room Type: %s, Bookings: %d, Room Price:  £%.2f, Income: £%.2f, Tax: %.2f %n", name, numberOfBookings, price, income, paidTax);

       }
   file.close();
   }

}

1 个答案:

答案 0 :(得分:0)

对象是你的朋友。

  • 为输入中的每个房间创建一个对象。
  • 将房间存储在列表中。
  • 汇总列表中的值。
  • 相应打印。

    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    public class WagesCalculator
    {
    
      public static void main(String[] args)
      throws Exception
      {
        WagesCalculator wc = new WagesCalculator();
        wc.calculate();
      }
    
      public void calculate()
      throws FileNotFoundException
      {
        Scanner file = new Scanner(new FileReader("task3.txt"));
        Scanner sc = new Scanner(System.in);
    
        // Current tax variable value
        double tax = 20;
    
        // User Input Y or N to change tax variable value
        System.out.println("- - Hotel Tax System - -");
        System.out.print("Do you want to specify a custom Tax Rate? [Y|N]: ");
    
        // if statement to change tax variable value subject to Y or N
        if (sc.next().equalsIgnoreCase("Y"))
        {
          System.out.print("Please enter the new tax value: ");
          tax = new Scanner(System.in).nextInt();
        }
    
        // Prints out current tax value
        System.out.println("The current tax rate is " + tax + ".");
    
        List<Room> rooms = new ArrayList<Room>();
    
        while (file.hasNext())
        {
          String name = file.next();
          int numberOfBookings = file.nextInt();
          double price = file.nextDouble();
          rooms.add(new Room(tax, name, numberOfBookings, price));
        }
    
        file.close();
    
        rooms.stream().forEach(e -> System.out.println(e));
        double totalIncome = rooms.stream().map(r -> r.income)
        .reduce((a, b) -> a + b).orElse(0.0);
        double totalTax = rooms.stream().map(r -> r.tax).reduce((a, b) -> a + b)
        .orElse(0.0);
        System.out.printf("Total income was: %d\nTotal tax was %d\n", totalIncome,
        totalTax);
      }
    
      class Room
      {
        double tax;
    
        String name;
    
        int numberOfBookings;
    
        double price;
    
        double income;
    
        double paidTax;
    
        public Room(double tax, String name, int numberOfBookings, double price)
        {
          this.tax = tax;
          this.name = name;
          this.numberOfBookings = numberOfBookings;
          this.price = price;
          this.income = numberOfBookings * price;
          this.paidTax = income * (tax / 100);
        }
    
        @Override
        public String toString()
        {
          return String.format(
          "Room Type: %s, Bookings: %d, Room Price:  £%.2f, Income: £%.2f,         Tax: %.2f %n",
              name, numberOfBookings, price, income, paidTax);
        }
      }
    }