public class Customer { protected String name; protected String ID; protected String location; public Customer(String n, String i, String l) { setName(n); setID(i); setLocation(l); } public void setName(String n) { name = n; } public void setID(String i) { ID = i; } public void setLocation(String l) { location = l; } public String getName() { return name; } public String getID() { return ID; } public String getLocation() { return location; } public String toString() { return "Name: " + name + "\n" + "ID: " + ID + "\n"; } }//class ///////////////////////////////////NEW FILE//////////////////////////////// import java.util.*; import java.io.*; import java.sql.*; public class Customers { private Vector customer;; public Customers() throws Exception { customer = new Vector(); PatsDatabase pdb = new PatsDatabase(); ResultSet rs = pdb.getData("customer"); while (rs.next()) { String type = rs.getString(1); String name = rs.getString(2); String id = rs.getString(3); String location = rs.getString(4); String mOrD = rs.getString(5); Customer temp = null; if (type.equals("S")) { temp = new Student(name,id,location,mOrD); } else { temp = new Staff(name,id,location,mOrD); } customer.addElement(temp); } pdb.close(); } public Customer getCustomer(String id) { for (int i = 0; i < customer.size(); i++) { Customer c = (Customer)customer.elementAt(i); if (id.equals(c.getID())) { return c; } } return null; } public String toString() { String out = ""; for (int i = 0; i < customer.size(); i++) { out = out + "\n" + ((Customer)customer.elementAt(i)).toString(); } return out; } public void addCustomer(String type,String name,String id, String location,String mOrD) throws Exception { Customer temp = null; if (type.equals("S")) { temp = new Student(name,id,location,mOrD); } else { temp = new Staff(name,id,location,mOrD); } customer.addElement(temp); PatsDatabase pdb = new PatsDatabase(); pdb.addCustomer(type, name, id, location, mOrD); pdb.close(); } }//CLASS ///////////////////////////////////NEW FILE//////////////////////////////// import java.util.*; import java.io.*; import java.sql.*; public class Inventory { private Vector plants; private Vendors vendors; private String[] p = {"Lilly,14.50,6,Sam", "Ivy,12.35,10,Lilly", "Tulip,17.85,3,Daisy"}; public Inventory(Vendors v) throws Exception { vendors = v; plants = new Vector(); PatsDatabase pdb = new PatsDatabase(); ResultSet rs = pdb.getData("inventory"); while (rs.next()) { String name = rs.getString(1); double price = rs.getDouble(2); int days = rs.getInt(3); Vendor vend = v.getVendor(rs.getString(4)); plants.addElement(new Plant(name, price, days, vend)); } pdb.close(); } public Plant getPlant(String name) { for (int i = 0; i < plants.size(); i++) { Plant temp = (Plant)plants.elementAt(i); if (name.equals(temp.getName())) { return temp; } } return null; } public String toString() { String out = ""; for (int i = 0; i < plants.size(); i++) { out = out + "\n" + (Plant)plants.elementAt(i); } return out; } public void addPlant(String name, double price, int onHand, String vendor) throws Exception { plants.addElement(new Plant(name,price,onHand,vendors.getVendor(vendor))); PatsDatabase pdb = new PatsDatabase(); pdb.addInventory(name, price, onHand, vendor); pdb.close(); } }//class ///////////////////////////////////NEW FILE//////////////////////////////// import java.util.*; import java.text.*; import java.net.*; import java.io.*; public class Order implements Runnable { private Customer customer; private Plant plant; private Date date; private PatsUserInterface pui; private Thread thread; public Order(Customer cust, Plant pl, PatsUserInterface p) { customer = cust; plant = pl; date = new Date(); pui = p; thread = new Thread(this, "OrderThread"); thread.start(); } public Order(Customer cust, Plant pl, Date d) { customer = cust; plant = pl; date = d; } public void run() { int days = plant.getVendor().getAverageDelivery(); try { URL doIt = new URL("http://komar.cs.stthomas.edu/servlet/PlantInStock?plant=" + plant.getName()); BufferedReader in = new BufferedReader( new InputStreamReader( doIt.openStream())); String status = in.readLine(); if (status.equalsIgnoreCase("out")) { days = days + 2; pui.backorder(this); } } catch (Exception e) { System.out.println("Error: getting data from server"); } try { Thread.sleep(days * 1000); } catch (InterruptedException ie) { System.out.println("Error: thread interrupted"); } pui.orderDone(this); } public String toString() { DateFormat form = DateFormat.getDateInstance(); String out = "Customer Order Date: " + form.format(date) + "\n" + customer + "\n" + plant; return out; } public Customer getCustomer() {return customer;} public Plant getPlant() {return plant;} public Date getDate() {return date;} public String getDateString() { DateFormat form = DateFormat.getDateInstance(3); return form.format(date); } } ///////////////////////////////////NEW FILE//////////////////////////////// import java.util.*; import java.sql.*; public class Orders { private Vector orders = null; private Customers customers; private Inventory inventory; public Orders(Customers cust, Inventory inv) throws Exception { orders = new Vector(); customers = cust; inventory = inv; PatsDatabase pdb = new PatsDatabase(); ResultSet rs = pdb.getData("orders"); while (rs.next()) { String customer = rs.getString(1); String plant = rs.getString(2); java.util.Date newDate = rs.getDate(3); Order o = new Order(customers.getCustomer(customer), inventory.getPlant(plant), newDate); orders.addElement(o); } } public void addOrder(String cust, String plant, PatsUserInterface pui) throws Exception { Order o = new Order(customers.getCustomer(cust), inventory.getPlant(plant), pui); orders.addElement(o); PatsDatabase pdb = new PatsDatabase(); String dateString = o.getDateString(); pdb.addOrder(cust, plant, dateString); pdb.close(); } public String toString() { String out = ""; for (int i = 0; i < orders.size(); i++) { out = out + (Order)orders.elementAt(i) + "\n"; } return out; } public Vector getOrders(Customer cust) { Vector temp = new Vector(); for (int i = 0; i < orders.size(); i++) { Order temp2 = (Order)orders.elementAt(i); if ((temp2.getCustomer().getID()).equals(cust.getID())) {temp.addElement(temp2);} } return temp; } }//class ///////////////////////////////////NEW FILE//////////////////////////////// import java.io.*; import java.sql.*; public class PatsDatabase { Connection connection = null; ResultSet rs = null; Statement statement = null; public PatsDatabase() throws Exception { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); connection = DriverManager.getConnection( "jdbc:odbc:Pats","",""); statement = connection.createStatement(); } catch (Exception e) { System.err.println("Connection error: " + e.getMessage()); e.printStackTrace(); throw e; } }//constructor public ResultSet getData (String table) throws Exception { try { String sql = "select * from " + table; rs = statement.executeQuery( sql ); } catch (Exception e) { System.err.println("Query error: " + e.getMessage()); e.printStackTrace(); throw e; } return rs; } public int addVendor(String name, String address, int delivery) throws Exception { String sql = null; int number = 0; try { sql = "insert into vendors values('" + name + "','" + address + "'," + delivery + ")"; number = statement.executeUpdate(sql); } catch (Exception e) { System.err.println("Insert error: " + e.getMessage()); e.printStackTrace(); throw e; } return number; } public int addCustomer(String type, String name,String id, String location, String mOrd) throws Exception { String sql = null; int number = 0; try { sql = "insert into customers values('" + type + "','" + name + "','" + id + "','" + location + "','" + mOrd + "')"; number = statement.executeUpdate(sql); } catch (Exception e) { System.err.println("Insert error: " + e.getMessage()); e.printStackTrace(); throw e; } return number; } public int addInventory(String name, double price, int onHand, String vendor) throws Exception { String sql = null; int number = 0; try { sql = "insert into inventory values('" + name + "'," + price + "," + onHand + ",'" + vendor + "')"; number = statement.executeUpdate(sql); } catch (Exception e) { System.err.println("Insert error: " + e.getMessage()); e.printStackTrace(); throw e; } return number; } public int addOrder(String customer, String plant, String date) throws Exception { String sql = null; int number = 0; try { sql = "insert into orders values('" + customer + "','" + plant + "','" + date + "')"; number = statement.executeUpdate(sql); } catch (Exception e) { System.err.println("Insert error: " + e.getMessage()); e.printStackTrace(); throw e; } return number; } public void close() throws Exception { try { connection.close(); statement.close(); } catch (Exception e) { System.err.println("Insert error: " + e.getMessage()); e.printStackTrace(); throw e; } } }// class ///////////////////////////////////NEW FILE//////////////////////////////// public class PatsPottedPlants { private Vendors vendors; private Inventory inventory; private Customers customers; private Orders orders; public PatsPottedPlants() throws Exception { vendors = new Vendors(); inventory = new Inventory(vendors); customers = new Customers(); orders = new Orders(customers, inventory); } public Vendors getVendors() { return vendors; } public Inventory getInventory() { return inventory; } public Customers getCustomers() { return customers; } public Orders getOrders() { return orders; } }//class ///////////////////////////////////NEW FILE//////////////////////////////// public interface PatsUserInterface { void orderDone(Order o); void backorder(Order o); }public class Plant { private String name; private double price; private int numberOnHand; private Vendor vendor; public Plant(String n, double p, int num, Vendor v) { setName(n); setPrice(p); setNumberOnHand(num); setVendor(v); } public void setName(String n) { name = n; } public void setPrice(double p) { price = p; } public void setNumberOnHand(int n) { numberOnHand = n; } public void setVendor(Vendor v) { vendor = v; } public String getName() { return name; } public double getPrice() { return price; } public int getNumberOnHand() { return numberOnHand; } public Vendor getVendor() { return vendor; } public String toString() { return "Plant Name: " + name + "\n" + "Price: " + price + "\n" + "Number on hand: " + numberOnHand + "\n" + vendor + "\n"; } }//class ///////////////////////////////////NEW FILE//////////////////////////////// public class Staff extends Customer { private String department; public Staff(String n, String i, String l, String d) { super(n, i, l); setDepartment(d); } public void setDepartment(String d) { department = d; } public String getDepartment() { return department; } public String toString() { return super.toString() + "Department: " + department + "\n"; } }//class ///////////////////////////////////NEW FILE//////////////////////////////// public class Student extends Customer { private String major; public Student(String n, String i, String l, String m) { super(n, i, l); setMajor(m); } public void setMajor(String m) { major = m; } public String getMajor() { return major; } public String toString() { return super.toString() + "Major: " + major + "\n"; } }//class ///////////////////////////////////NEW FILE//////////////////////////////// import java.util.*; import java.io.*; public class TestPat implements PatsUserInterface { private PatsPottedPlants pats; private Customers customers; private Vendors vendors; private Inventory inventory; private Orders orders; private BufferedReader keyIn; public TestPat() throws Exception { keyIn = new BufferedReader( new InputStreamReader(System.in),1); pats = new PatsPottedPlants(); customers = pats.getCustomers(); vendors = pats.getVendors(); inventory = pats.getInventory(); orders = pats.getOrders(); String choice = "1"; while (!choice.equals("5")) { System.out.println("Pats Main Menu \n\n\t1. Add a Customer\n" + "\t2. Add a Vendor \n" + "\t3. Add a Plant \n" + "\t4. Enter an Order \n" + "\t5. Quit \n\n" + "Enter the number of your choice: "); choice = keyIn.readLine(); switch (choice.charAt(0)) { case '1': addCustomer(); break; case '2': addVendor(); break; case '3': addPlant(); break; case '4': enterOrder(); break; case '5': break; default: System.out.println("Please enter a valid choice!"); break; }//switch }//while }//constructor public void orderDone(Order order) { System.out.println("\n*********** Order Delivered ******************"); System.out.println(order); } public void backorder(Order order) { System.out.println("\n************** Order Backordered *****************"); System.out.println(order.getPlant().getName()); } public void addCustomer() throws Exception { String response = "Y"; while (response.equalsIgnoreCase("y")) { System.out.println("\n******************** New Customer ******************"); String id = null; Customer cust = null; do { System.out.print("Enter the new customer ID: "); id = keyIn.readLine(); cust = customers.getCustomer(id); if (!(cust == null)) { System.out.println("Customer ID alrady in use! Reenter\n"); } }while (!(cust == null)); System.out.print("Enter the customer name: "); String name = keyIn.readLine(); System.out.print("Enter the customer location: "); String location = keyIn.readLine(); System.out.print("Enter the S for Student or E for employee (staff): "); String type = keyIn.readLine(); if (type.equalsIgnoreCase("S")) { System.out.print("Enter the Student Major Field code: "); } else { System.out.print("Enter the employee department code: "); } String mOrd = keyIn.readLine(); customers.addCustomer(type, name, id, location, mOrd); System.out.print("\nDo you want to add another customer? (Y or N) "); response = keyIn.readLine(); }//while }//addCustomer public void addVendor() throws Exception { String response = "Y"; while (response.equalsIgnoreCase("y")) { System.out.println("\n******************** New Vendor ******************"); String name = null; Vendor vend = null; do { System.out.print("Enter the new vendor name: "); name = keyIn.readLine(); vend = vendors.getVendor(name); if (!(vend == null)) { System.out.println("Vendor name alrady in use! Reenter\n"); } }while (!(vend == null)); System.out.print("Enter the vendor address: "); String address = keyIn.readLine(); System.out.print("Enter the vendor average delivery time: "); int delivery = Integer.parseInt(keyIn.readLine()); vendors.addVendor(name, address,delivery); System.out.print("\nDo you want to add another vendor? (Y or N) "); response = keyIn.readLine(); }//while }//addVendor public void addPlant() throws Exception { String response = "Y"; while (response.equalsIgnoreCase("y")) { System.out.println("\n******************** New Plant ******************"); String name = null; Plant plant = null; Vendor vend = null; do { System.out.print("Enter the new plant name: "); name = keyIn.readLine(); plant = inventory.getPlant(name); if (!(plant == null)) { System.out.println("Plant name alrady in use! Reenter\n"); } }while (!(plant == null)); String vendorName = null; do { System.out.print("Enter Vendor Name: "); vendorName = keyIn.readLine(); vend = vendors.getVendor(vendorName); if (vend == null) { System.out.println("Vendor Name Invalid! Please reenter\n"); } }while (vend == null); System.out.print("Enter the price for the plant: "); double price = Double.parseDouble(keyIn.readLine()); System.out.print("Enter the number on hand: "); int onHand = Integer.parseInt(keyIn.readLine()); inventory.addPlant(name, price, onHand, vendorName); System.out.print("\nDo you want to add another plant? (Y or N) "); response = keyIn.readLine(); }//while }//addPlant public void enterOrder() throws Exception { String response = "Y"; while (response.equalsIgnoreCase("Y")) { System.out.println("*************** New Order ******************\n"); String id = null; String plantName = null; Customer cust = null; do { System.out.print("Enter the Customer ID: "); id = keyIn.readLine(); cust = customers.getCustomer(id); if (cust == null) { System.out.println("Customer ID Invalid! Please reenter\n"); } }while (cust == null); Plant plant = null; do { System.out.print("Enter Plant Name: "); plantName = keyIn.readLine(); plant = inventory.getPlant(plantName); if (plant == null) { System.out.println("Plant Name Invalid! Please reenter\n"); } }while (plant == null); orders.addOrder(id, plantName, this); System.out.print("\nDo you want place another order? (Y or N) "); response = keyIn.readLine(); }//while // System.out.println("\n" + orders); }//enterOrder public static void main(String[] args) { try { new TestPat(); } catch (Exception e) { System.out.println("Catastrophic data error!"); System.out.println(e.getMessage()); e.printStackTrace(); System.exit(1); } } }//class ///////////////////////////////////NEW FILE//////////////////////////////// public class Vendor { private String name; private String address; private int averageDelivery; public Vendor(String n, String a, int ad) { setName(n); setAddress(a); setAverageDelivery(ad); } public void setName(String n) { name = n; } public void setAddress(String a) { address = a; } public void setAverageDelivery(int ad) { averageDelivery = ad; } public String getName() { return name; } public String getAddress() { return address; } public int getAverageDelivery() { return averageDelivery; } public String toString() { return "Vendor name: " + name + "\n" + "Address: " + address + "\n" + "Average Delivery in days: " + averageDelivery + "\n"; } }//class ///////////////////////////////////NEW FILE//////////////////////////////// import java.util.*; import java.sql.*; public class Vendors { private Vector vendors;; public Vendors() throws Exception { vendors = new Vector(); PatsDatabase pdb = new PatsDatabase(); ResultSet rs = pdb.getData("vendor"); while (rs.next()) { String name = rs.getString(1); String location = rs.getString(2); int days = Integer.parseInt(rs.getString(3)); Vendor vend = new Vendor(name,location,days); vendors.addElement(vend); } pdb.close(); } public Vendor getVendor(String name) { for (int i = 0; i < vendors.size(); i++) { Vendor temp = (Vendor)vendors.elementAt(i); if (name.equals(temp.getName())) { return temp; } } return null; } public String toString() { String out = ""; for (int i = 0; i < vendors.size(); i++) { out = out + "\n" + vendors.elementAt(i); } return out; } public void addVendor(String name, String location, int days) throws Exception { vendors.addElement(new Vendor(name,location,days)); PatsDatabase pdb = new PatsDatabase(); pdb.addVendor(name, location, days); pdb.close(); } }//class