2016-11-08 9 views
-1

Javaを練習して学習するのに数時間しかかかりませんでしたので、まだ基本を学んでいます。Javaで文字列形式ですべての値を掛け合わせる

私が含まれているテキストファイルからの値を読んでいる:

シングル 112.50

マスター 2227.50

ペントハウス 5000.00

(だから、実行時と同じように表示されます) ルームタイプ:シングル、予約:6 0、部屋価格:£112.00、所得:£6,750.00、税金:1350.00 各部屋とも4番目。

すべての値を必要な文字列形式で出力しました。しかし、私の問題は本当に簡単です。

totalincaid変数にすべての収入を追加し、すべての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

そして、何が正確な問題のようですか?ヘルプが必要な場合は、少なくとも、問題のある箇所でコードの部分を分離する必要があります。 – MarsAtomic

+1

試行を見せて問題を説明できますか? "次のコードに[機能]を追加してください:"はそうではありません。http://stackoverflow.com/help/how-to-ask – Taylor

+1

私はちょうど、新しい変数を追加し、すべての値をpayTaxから新しい変数に追加します。 誰もが質問と混同しないように、コードを投稿しました。 問題はありません。私はすべての値を一緒に加える構文の助けを求めるだけです。あなたは2人が本当に簡単な質問を窓から吹き飛ばしたようです。 – Luke

答えて

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); 
        } 
        } 
    } 
    
関連する問題