Facebook
From Flying Earthworm, 4 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 329
  1. import java.util.InputMismatchException;
  2. import java.util.Scanner;
  3.  
  4. public class Main {
  5.  
  6.     public static void main(String[] args) {
  7.         /* The mission, create a salary calculator
  8.  
  9.         Ideas before starting: Copy paste format for try catch for the inputs, all in a while loop.
  10.        
  11.         After coding thoughts: This program was really simple, it just took me some time to get right the arrays
  12.         but I managed to have a lot of variables or at least more than I have by using an array and a for loop
  13.          */
  14.  
  15.         System.out.println("Welcome to your salary calculator");
  16.         Scanner keyboard = new Scanner(System.in);
  17.         int x=0;
  18.         int rh;
  19.         double eh;
  20.         double p1, p2, p3, p4, p5, p6, p7;
  21.  
  22.         while(x<1){
  23.             try{
  24.                 double[] workDays = new double[7]; // Here I define the length of the array, so 7 days means 7 slots
  25.                 System.out.println("How many regular hours do you work on Mondays?");
  26.                 rh = keyboard.nextInt();
  27.                 if(rh>8){ //You can't work more than 8 regular hours, I do this for everyday
  28.                     System.out.println("You can't work more than 8 regular hours");
  29.                     System.out.println("Terminating program");
  30.                     System.exit(0);
  31.                 }
  32.                 System.out.println("How many extra hours do you work?");
  33.                 eh = keyboard.nextDouble();
  34.                 if(eh>16) { // Assuming you work 8 regular hours then if you work say 17 extra hours you would work 25
  35.                     // Hours in a day of 24 hours so yeah
  36.                     System.out.println("You can't work 25 hours in one day");
  37.                     System.out.println("Terminating program");
  38.                     System.exit(0);
  39.                 }
  40.                 p1 = (rh*10) + (eh*15);
  41.                 workDays[0]=p1; // Here I assign the total pay value to slot number 1, but its actually 0, and same for
  42.                 // Each day
  43.                 System.out.println("How many regular hours do you work on Tuesdays?");
  44.                 rh = keyboard.nextInt();
  45.                 if(rh>8){
  46.                     System.out.println("You can't work more than 8 regular hours");
  47.                     System.out.println("Terminating program");
  48.                     System.exit(0);
  49.                 }
  50.                 System.out.println("How many extra hours do you work?");
  51.                 eh = keyboard.nextDouble();
  52.                 if(eh>16){
  53.                     System.out.println("You can't work 25 hours in one day");
  54.                     System.out.println("Terminating program");
  55.                     System.exit(0);
  56.                 }
  57.                 p2 = (rh*10) + (eh*15);
  58.                 workDays[1]=p2;
  59.                 System.out.println("How many regular hours do you work on Wednesdays?");
  60.                 rh = keyboard.nextInt();
  61.                 if(rh>8){
  62.                     System.out.println("You can't work more than 8 regular hours");
  63.                     System.out.println("Terminating program");
  64.                     System.exit(0);
  65.                 }
  66.                 System.out.println("How many extra hours do you work?");
  67.                 eh = keyboard.nextDouble();
  68.                 if(eh>16){
  69.                     System.out.println("You can't work 25 hours in one day");
  70.                     System.out.println("Terminating program");
  71.                     System.exit(0);
  72.                 }
  73.                 p3 = (rh*10) + (eh*15);
  74.                 workDays[2]=p3;
  75.                 System.out.println("How many regular hours do you work on Thursdays?");
  76.                 rh = keyboard.nextInt();
  77.                 if(rh>8){
  78.                     System.out.println("You can't work more than 8 regular hours");
  79.                     System.out.println("Terminating program");
  80.                     System.exit(0);
  81.                 }
  82.                 System.out.println("How many extra hours do you work?");
  83.                 eh = keyboard.nextDouble();
  84.                 if(eh>16){
  85.                     System.out.println("You can't work 25 hours in one day");
  86.                     System.out.println("Terminating program");
  87.                     System.exit(0);
  88.                 }
  89.                 p4 = (rh*10) + (eh*15);
  90.                 workDays[3]=p4;
  91.                 System.out.println("How many regular hours do you work on Fridays?");
  92.                 rh = keyboard.nextInt();
  93.                 if(rh>8){
  94.                     System.out.println("You can't work more than 8 regular hours");
  95.                     System.out.println("Terminating program");
  96.                     System.exit(0);
  97.                 }
  98.                 System.out.println("How many extra hours do you work?");
  99.                 eh = keyboard.nextDouble();
  100.                 if(eh>16){
  101.                     System.out.println("You can't work 25 hours in one day");
  102.                     System.out.println("Terminating program");
  103.                     System.exit(0);
  104.                 }
  105.                 p5 = (rh*10) + (eh*15);
  106.                 workDays[4]=p5;
  107.                 System.out.println("How many regular hours do you work on Saturdays?");
  108.                 rh = keyboard.nextInt();
  109.                 if(rh>8){
  110.                     System.out.println("You can't work more than 8 regular hours");
  111.                     System.out.println("Terminating program");
  112.                     System.exit(0);
  113.                 }
  114.                 System.out.println("How many extra hours do you work?");
  115.                 eh = keyboard.nextDouble();
  116.                 if(eh>16){
  117.                     System.out.println("You can't work 25 hours in one day");
  118.                     System.out.println("Terminating program");
  119.                     System.exit(0);
  120.                 }
  121.                 p6 = (rh*30) + (eh*30);
  122.                 workDays[5]=p6;
  123.                 System.out.println("How many regular hours do you work on Sundays?");
  124.                 rh = keyboard.nextInt();
  125.                 if(rh>8){
  126.                     System.out.println("You can't work more than 8 regular hours");
  127.                     System.out.println("Terminating program");
  128.                     System.exit(0);
  129.                 }
  130.                 System.out.println("How many extra hours do you work?");
  131.                 eh = keyboard.nextDouble();
  132.                 if(eh>16){
  133.                     System.out.println("You can't work 25 hours in one day");
  134.                     System.out.println("Terminating program");
  135.                     System.exit(0);
  136.                 }
  137.                 p7 = (rh*30) + (eh*30);
  138.                 workDays[6]=p7;
  139.                 double pt = 0;
  140.                 // For loops are specially handy to sum values in long arrays
  141.                 for (double workDay : workDays) {
  142.                     pt = pt + workDay;
  143.                 }
  144.                 System.out.println("Your total pay for this week is: " + pt + "$");
  145.                 x=1;
  146.             }catch(InputMismatchException ime){
  147.                 System.out.println("You cant input letters, terminating program");
  148.                 System.exit(0);
  149.             }
  150.         }
  151.     }
  152. }
captcha