Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class GasStation {
- //Constants to provide information that should probably be pulled from a DB
- private static final float LEADED_FEDERAL_TAX = 0.10F;
- private static final float UNLEADED_FEDERAL_TAX = 0.15F;
- private static final float STATE_TAX = 0.05F;
- private static final float LEADED_GALLON_COST = 3.0F;
- private static final float UNLEADED_GALLON_COST = 3.5F;
- private static final float FULL_SERVICE_CHARGE = 2.0F;
- private static final float GASNGO_SERVICE_CHARGE = 2.0F;
- //Variable to hold the command line arguments so they're easier to read
- private static String gasType = "Leaded";
- private static float gallons = 10;
- private static String serviceType = "FullService";
- private static float federalTax;
- private static float gallonCost;
- private static float serviceCharge;
- private static float gasCharge;
- private static float totalBill;
- public static void main(String[] args) {
- //Create a connection object
- Connection con = null;
- //Error handling try/catch
- try {
- //get the jdbc driver
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- //try to connect to my db
- con = DriverManager.getConnection("jdbc:mysql:///gascharges", "root", "14142135");
- //if the connection succeeded
- if(!con.isClosed()) {
- System.out.println("Successfully connected to MySQL server...");
- //Make a statement object to hold our sql statement
- Statement stmt = con.createStatement();
- //Make a result set to hold the result of our sql statement
- ResultSet rs = stmt.executeQuery("SELECT * FROM pumprecords;");
- //Move to the first record
- rs.next();
- while (!rs.isLast()) {
- //Set the main variables using the passed command line args. Should catch invalid args
- // but wanted to keep it simple.
- gasType = rs.getString("gasType");
- gallons = Float.valueOf(rs.getString("gallons")); //An argument is a string and must be forced to convert to a float
- serviceType = rs.getString("serviceType");
- //Strings are objects unlike other data types. Must use myString.equals("other string"). Can't
- // use myString = "other string" since myString is a reference to a memory address,
- // not an actual string.
- if (gasType.equals("Leaded")) {
- federalTax = LEADED_FEDERAL_TAX;
- gallonCost = LEADED_GALLON_COST;
- } else {
- federalTax = UNLEADED_FEDERAL_TAX;
- gallonCost = UNLEADED_GALLON_COST;
- }
- //Both these ifs should really uppercase both strings so the command line arguments
- // aren't case sensitive
- if (serviceType.equals("FullService")) {
- serviceCharge = FULL_SERVICE_CHARGE;
- } else if (serviceType.equals("GasNGo")) {
- serviceCharge = GASNGO_SERVICE_CHARGE;
- } else {
- serviceCharge = 0F;
- }
- gasCharge = gallonCost * gallons;
- totalBill = ((gasCharge + serviceCharge) * (1.0f + STATE_TAX)) + (federalTax * gallons);
- //This println should format totalBill to force $0.00 format. Again, keeping it simple.
- System.out.println("Total Charge: $" + Float.toString(totalBill));
- rs.next();
- }
- }
- } catch(Exception e) {
- System.err.println("Exception: " + e.getMessage());
- } finally {
- try {
- if(con != null)
- con.close();
- } catch(SQLException e) {}
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement