Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.55 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6.  
  7.  
  8. public class GasStation {
  9.     //Constants to provide information that should probably be pulled from a DB
  10.     private static final float LEADED_FEDERAL_TAX = 0.10F;
  11.     private static final float UNLEADED_FEDERAL_TAX = 0.15F;
  12.     private static final float STATE_TAX = 0.05F;
  13.     private static final float LEADED_GALLON_COST = 3.0F;
  14.     private static final float UNLEADED_GALLON_COST = 3.5F;
  15.     private static final float FULL_SERVICE_CHARGE = 2.0F;
  16.     private static final float GASNGO_SERVICE_CHARGE = 2.0F;
  17.    
  18.     //Variable to hold the command line arguments so they're easier to read
  19.     private static String gasType = "Leaded";
  20.     private static float gallons = 10;
  21.     private static String serviceType = "FullService";
  22.    
  23.     private static float federalTax;
  24.     private static float gallonCost;
  25.     private static float serviceCharge;
  26.     private static float gasCharge;
  27.     private static float totalBill;
  28.  
  29.     public static void main(String[] args) {
  30.         //Create a connection object
  31.         Connection con = null;
  32.  
  33.         //Error handling try/catch
  34.         try {
  35.             //get the jdbc driver
  36.           Class.forName("com.mysql.jdbc.Driver").newInstance();
  37.           //try to connect to my db
  38.           con = DriverManager.getConnection("jdbc:mysql:///gascharges", "root", "14142135");
  39.  
  40.           //if the connection succeeded
  41.           if(!con.isClosed()) {
  42.             System.out.println("Successfully connected to MySQL server...");
  43.             //Make a statement object to hold our sql statement
  44.             Statement stmt = con.createStatement();
  45.             //Make a result set to hold the result of our sql statement
  46.             ResultSet rs = stmt.executeQuery("SELECT * FROM pumprecords;");
  47.             //Move to the first record
  48.             rs.next();
  49.            
  50.             while (!rs.isLast()) {
  51.                 //Set the main variables using the passed command line args.  Should catch invalid args
  52.                 //  but wanted to keep it simple.
  53.                 gasType = rs.getString("gasType");
  54.                 gallons = Float.valueOf(rs.getString("gallons")); //An argument is a string and must be forced to convert to a float
  55.                 serviceType = rs.getString("serviceType");
  56.                
  57.                 //Strings are objects unlike other data types.  Must use myString.equals("other string").  Can't
  58.                 //  use myString = "other string" since myString is a reference to a memory address,
  59.                 //  not an actual string.
  60.                 if (gasType.equals("Leaded")) {  
  61.                     federalTax = LEADED_FEDERAL_TAX;
  62.                     gallonCost = LEADED_GALLON_COST;
  63.                 } else {
  64.                     federalTax = UNLEADED_FEDERAL_TAX;
  65.                     gallonCost = UNLEADED_GALLON_COST;
  66.                 }
  67.                
  68.                 //Both these ifs should really uppercase both strings so the command line arguments
  69.                 //  aren't case sensitive
  70.                 if (serviceType.equals("FullService")) {
  71.                     serviceCharge = FULL_SERVICE_CHARGE;
  72.                 } else if (serviceType.equals("GasNGo")) {
  73.                     serviceCharge = GASNGO_SERVICE_CHARGE;
  74.                 } else {
  75.                     serviceCharge = 0F;
  76.                 }
  77.                
  78.                 gasCharge = gallonCost * gallons;
  79.                
  80.                 totalBill = ((gasCharge + serviceCharge) * (1.0f + STATE_TAX)) + (federalTax * gallons);
  81.                
  82.                 //This println should format totalBill to force $0.00 format.  Again, keeping it simple.
  83.                 System.out.println("Total Charge: $" + Float.toString(totalBill));
  84.                 rs.next();
  85.             }
  86.           }
  87.  
  88.         } catch(Exception e) {
  89.           System.err.println("Exception: " + e.getMessage());
  90.         } finally {
  91.           try {
  92.             if(con != null)
  93.               con.close();
  94.           } catch(SQLException e) {}
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement