Advertisement
mmayoub

java-Exercise01-apartment rent

Jun 21st, 2017
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.89 KB | None | 0 0
  1. package class170619;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class tester01 {
  6.  
  7.     public static void main(String[] args) {
  8.         /*
  9.          * ברחוב דירות עם 3 עד 5 חדרים, שחלקן הן דופלקסים. תשלומי ועד הבית נקבעו
  10.          * כך: דירת 3 חדרים: 120 ש"ח דירת 4 חדרים: 150 ש"ח דירת 5 חדרים: אם
  11.          * דופלקס: 200 ש"ח אחרת: 180 ש"ח יש להציג את הסכום לתשלום.
  12.          */
  13.  
  14.         int numberOfRooms; // input: number of rooms in the apartment
  15.         int price; // output: the rent of the apartment
  16.  
  17.         // create a scanner to get input from the user
  18.         Scanner s = new Scanner(System.in);
  19.  
  20.         // ask the user to enter the number of rooms
  21.         System.out.println("Enter number of rooms (3, 4 or 5) : ");
  22.  
  23.         // get and save data from the user
  24.         numberOfRooms = s.nextInt();
  25.  
  26.         // check the number of rooms
  27.         switch (numberOfRooms) {
  28.         case 3:
  29.             // three rooms in apartment --> price is 120
  30.             price = 120;
  31.             break;
  32.         case 4:
  33.             // four rooms in apartment --> price is 150
  34.             price = 150;
  35.             break;
  36.         case 5:
  37.             // five rooms in apartment
  38.             // should get and check if apartment is duplex
  39.  
  40.             boolean isDuplex; // input: is the apartment duplex?
  41.  
  42.             // ask the user to input data
  43.             System.out.println("Is the apartment duplex (true / false) ? ");
  44.             // get and save data from the user
  45.             isDuplex = s.nextBoolean();
  46.  
  47.             // if duplex then price is 200, else price is 180
  48.             price = isDuplex ? 200 : 180;
  49.             break;
  50.         default:
  51.             // number of rooms is invalid
  52.             System.out.println("Error: Invalid number of rooms ("
  53.                     + numberOfRooms + ") !");
  54.  
  55.             // to indicate an error
  56.             price = 0;
  57.         }
  58.  
  59.         // if input data was valid and price has been claculated
  60.         if (price > 0) {
  61.             // display the price on the screen for the user
  62.             System.out.println("the price you should pay is : " + price);
  63.         }
  64.  
  65.         s.close();
  66.     }
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement