Property Test : Java Beans « Development Class « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Development Class » Java BeansScreenshots 
Property Test


/**
 @version 1.00 11 Mar 1997
 @author Cay Horstmann
 */

public class PropertyTest {
  public static void main(String[] args) {
    Employee harry = new Employee("Harry Hacker"35000);
    Employee carl = new Employee("Carl Cracker"75000);

    PropertyEditor editor = new PropertyEditor(new Property[] {
        harry.getSalaryProperty(), harry.getSalaryProperty(),
        carl.getSalaryProperty(), carl.getSeniorityProperty() });

    System.out.println("Before:");
    harry.print();
    carl.print();
    System.out.println("Edit properties:");
    editor.editProperties();
    System.out.println("After:");
    harry.print();
    carl.print();
  }
}

interface Property {
  public String get();

  public void set(String s);

  public String name();
}

class PropertyEditor {
  public PropertyEditor(Property[] p) {
    properties = p;
  }

  public void editProperties() {
    while (true) {
      for (int i = 0; i < properties.length; i++)
        System.out.println((i + 1":" + properties[i].name() "="
            + properties[i].get());
      //set new value for second
      String value = "10";
      properties[1].set(value);

    }
  }

  private Property[] properties;
}

class Employee {
  public Employee(String n, double s) {
    name = n;
    salary = s;
  }

  public void print() {
    System.out.println(name + " " + salary + " ");
  }

  public void raiseSalary(double byPercent) {
    salary *= + byPercent / 100;
  }

  private class SalaryProperty implements Property {
    public String name() {
      return name + "'s salary";
    }

    public String get() {
      return "" + salary;
    }

    public void set(String s) {
      if (isSet)
        return// can set once
      double sal = Double.parseDouble(s);
      if (sal > 0) {
        salary = sal;
        isSet = true;
      }
    }

    private boolean isSet = false;
  }

  public Property getSalaryProperty() {
    return new SalaryProperty();
  }

  private class SeniorityProperty implements Property {
    public String name() {
      return name + "'s years on the job";
    }

    public String get() {
      return "20";
    }

    public void set(String s) {
    // can't set seniority
  }

  public Property getSeniorityProperty() {
    return new SeniorityProperty();
  }

  private String name;

  private double salary;
}



           
       
Related examples in the same category
1. JavaBean: BeanContextSupportJavaBean: BeanContextSupport
2. JavaBean: Test program that adds 100 beans to a contextJavaBean: Test program that adds 100 beans to a context
3. JavaBean: how to use the instantiateChild() convenience method to create a beanJavaBean: how to use the instantiateChild() convenience method to create a bean
4. JavaBean: illustrate delivery of the BeanContextMembershipEventJavaBean: illustrate delivery of the BeanContextMembershipEvent
5. JavaBean: creates all of the objects, a tests the service capabilitiesJavaBean: creates all of the objects, a tests the service capabilities
6. Bean ContainerBean Container
7. PropertyTablePropertyTable
8. Introspecting a BeanIntrospecting a Bean
9. Saving and restoring the state of a pretend CAD systemSaving and restoring the state of a pretend CAD system
w__w__w__.j___a_va___2s___._c___o___m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.