Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.60 KB | None | 0 0
  1. // Empty Lab Exam 2 template
  2. // Program Id - Name - Description here
  3.  
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.util.Scanner;
  7.  
  8. public class NeilanDavidLabEx2{
  9.     public static void main(String[] args) throws FileNotFoundException{
  10.         // Constants
  11.         final double UNIT_COST = 0.10;
  12.  
  13.         // File Objects/Variables
  14.         Scanner usageFile = new Scanner(new FileReader("/home/david/IdeaProjects/2014NeilanDavidLabEx2/src/WaterUsage.dat"));
  15.  
  16.         // Record Layout
  17.         int custId;
  18.         char custStatus;
  19.         String custFirstName;
  20.         String custLastName;
  21.         char custPlan;
  22.         double custStandingCharge;
  23.         int custFreeUnits;
  24.         int customerUsage;
  25.  
  26.         // Other Variables
  27.         String planName;
  28.         double cost;
  29.         int totalUsage, activeCustomers=0, inactiveCustomers=0, singleCustomers=0, pendingCustomers=0,
  30.             doubleCustomers=0, concessionCustomers=0, famCustomers=0, bigFamCustomers=0, suspendedCustomers=0;
  31.  
  32.         // Screen/Report Header
  33.         System.out.println("  Id      Name              Plan         Stand  Free   Q1   Q2   Q3   Q4     Sum     Avg    Cost");
  34.         System.out.println("================================================================================================");
  35.  
  36.         // Main while loop file Input until EOF
  37.         while (usageFile.hasNext()){
  38.             custId = usageFile.nextInt();
  39.             custStatus= usageFile.next().charAt(0);
  40.             custFirstName = usageFile.next();
  41.             custLastName = usageFile.next();
  42.             custPlan = usageFile.next().charAt(0);
  43.             custStandingCharge = usageFile.nextDouble();
  44.             custFreeUnits = usageFile.nextInt();
  45.  
  46.             totalUsage = 0;
  47.             cost = custStandingCharge;
  48.  
  49.             if (Character.toUpperCase(custStatus) == 'A'){
  50.                 activeCustomers++;
  51.  
  52.                 switch (Character.toLowerCase(custPlan)){
  53.                     case '1':
  54.                         planName = "Single";
  55.                         singleCustomers++;
  56.                         break;
  57.                     case '2':
  58.                         planName = "Double";
  59.                         doubleCustomers++;
  60.                         break;
  61.                     case 'c':
  62.                         planName = "Concession";
  63.                         concessionCustomers++;
  64.                         break;
  65.                     case 'f':
  66.                         planName = "Family";
  67.                         famCustomers++;
  68.                         break;
  69.                     case 'b':
  70.                         planName = "Big Family";
  71.                         bigFamCustomers++;
  72.                         break;
  73.                     default:
  74.                         planName = "Pending";
  75.                         pendingCustomers++;
  76.                 }
  77.  
  78.                 System.out.printf("%-9d %-17s %-12s %5.2f %5d ", custId, (custLastName + ',' + custLastName), planName, custStandingCharge, custFreeUnits);
  79.                 for (int i=0;i<4;i++){
  80.                     customerUsage = usageFile.nextInt();
  81.                     totalUsage += customerUsage;
  82.                     System.out.printf("%4d ", customerUsage);
  83.                 }
  84.  
  85.                 if (totalUsage > custFreeUnits){
  86.                     cost += (totalUsage-custFreeUnits)*UNIT_COST;
  87.                 }
  88.                 System.out.printf("%7d %7.2f %7.2f%n", totalUsage, totalUsage/4.0, cost);
  89.             } else{
  90.                 usageFile.nextLine();
  91.                 switch (Character.toUpperCase(custStatus)){
  92.                     case 'X':
  93.                         inactiveCustomers++;
  94.                         break;
  95.                     case 'S':
  96.                         suspendedCustomers++;
  97.                         break;
  98.                     }
  99.             }
  100.         }
  101.  
  102.         // Output Footer details to Screen and Report
  103.         System.out.println();
  104.         System.out.printf("Customers:%4d ", activeCustomers+inactiveCustomers+suspendedCustomers);
  105.         System.out.printf("Inactive:%4d " , inactiveCustomers);
  106.         System.out.printf("Suspended:%4d " , suspendedCustomers);
  107.         System.out.printf("Active:%4d%n" , activeCustomers);
  108.         System.out.printf("Single:%4d " , singleCustomers);
  109.         System.out.printf("Double:%4d " , doubleCustomers);
  110.         System.out.printf("Concession:%4d%n" , concessionCustomers);
  111.         System.out.printf("Family:%4d " , famCustomers);
  112.         System.out.printf("Big Family:%4d ", bigFamCustomers);
  113.         System.out.printf("Pending:%4d", pendingCustomers);
  114.         // Close files
  115.  
  116.     }  // main
  117.  
  118. } // LastNameFirstNameLabEx2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement