Project Philosophy

READY

Dining Project

Date: Jan 2, 2019
by Nicholas Duchon
Outline:

General Objectives

Here are some notes about the projects, the particular features of object-oriented design and object-oriented programming (OOD/OOP) the we want to cover in this class and some of the features of Java to help support that style of programming. We also want to explore the Java GUI system a little, with particular emphasis on viewing the data structures and effective ways to display the running of multiple threads competing for resources.

The particular scenarios selected for each semester ask you to implement as many of these objectives as possible in some compelling way. We are always open to additions and suggestions.

General objectives for each project:

Introduction - the Dining Project series

For this set of projects, you will simulate some of the aspects of a number of restaurants.

Here are the classes and their basic instance variables you should define:

  • DiningSolution extends JFrame
    • variables used by the GUI interface
    • world: World
  • Item
    • index: int
    • parent: int
    • name: String
  • World extends Item
    • countries: ArrayList <Country>
  • Tool extends Item
    • cost: double
    • type: String
    • used: Eat
  • Eat extends Item
    • calories: int
    • diner: Diner
    • eatTools: ArrayList <Tool>
    • reqs: ArrayList <String>
    • rest: Restaurant
    • time: int
    • jpb: JProgressBar
    • variable associated with
      controlling this thread
      and the associated GUI display
  • Diner extends Item
    • age: double
    • busyFlag: boolean
    • eats: ArrayList <Eat>
    • height: double
    • weight: double
  • Country extends Item
    • area: int
    • population: int
    • rests: ArrayList <Restaurant>
  • Restaurant extends Item
    • cash: double
    • diners: ArrayList <Diner>
    • menuSize: int
    • rating: int
    • tools: ArrayList <Tools>
  • TakeOut extends Restaurant
    • drivers: int
  • FoodTruck extends Restaurant
    • license: String
  • EatIn extends Restaurant
    • seatCount: int
  • Popular extends EatIn
    • salads: int
    • softs: int
    • wings: int
  • FastFood extends EatIn
    • burgers: int
    • fries: int
  • Elegant extends EatIn
    • appets: int
    • deserts: int
    • entrees: int
    • soups: int
  • Cafe extends EatIn
    • coffees: int
    • teas: int
  • Bar extends EatIn
    • beers: int
    • cognacs: int
    • whiskeys: int

Eventually, in Projects 3 and 4, you will be asked to show the progress of the jobs using JProgressBar's.

Here's a very quick overview of the projects:

  1. Read a data file, create the internal data structure, create a GUI to display the structure, and let the user search the structure.
  2. Sort the structure and use hash maps to create the structure more efficiently.
  3. Create a thread for each job (eating a dish), a diner can only eat one dish at a time, and create a GUI to show the progress of each job; and display the data structure using a JTree.
  4. Simulate competing for resources (tools required for eating each dish).

Documentation

HINT: You should start working on a documentation file before you do anything else with these projects, and fill in items as you go along. Leaving the documentation until the project is finished is not a good idea for any number of reasons.

Also see: Grading below

The documentation should include the following (graded) elements:

CAUTION: the documentation file size should be reasonable, currently < 5 MBytes, mostly < 1 MB.

Project 1 - Also see Grading

The goals are:

  1. Create a GUI
  2. Let the user select a data file, using JFileChooser
  3. Read the data file, creating the specified internal data structure
    1. see the Introduction for the classes and variables of the structure
  4. Display the internal data structure in a nice format in the GUI
    1. use JScrollPane and JTextArea
  5. Display the results of a Search specified by the user
    1. JTextField to specify the search target
    2. Searching names for matching substrings
    3. Note that a search may return more than one item
    4. DO NOT create new data structures (beyond the specified internal data structure) to search
      you may create a structure of found items as a return value

Data file format:

Here are examples of the valid lines, without comments:

world           Mars                      10000     0
country         Colombia                  20861 10000       48653419        1038700
restaurant      Food_by_Louie             35385 20861    1    56        70936.70
takeout         Meals_from_Dick           45331 20861    4    16        66142.27     1
foodtruck       Porkers_by_Berry          32543 20861    3    58        96393.49     KFUQYH
eatin           House_of_Alec             42203 20861    4    86        41758.35    60
popular         Ribs_by_Eddy              33138 20861    4    75        52377.74   101     9    14     4
fastfood        Burgers_by_Tracy          49457 20861    2    65        30278.99    67     3     9
elegant         Chez_Jayne                34389 20861    3    74        57359.54    51   199     1     9    25     4
cafe            Cafe_Carl                 40248 20861    0    50        32922.50    22     4     5
bar             Bar_Mario                 32180 20861    4    70        72525.87    48   175     8    10
tool            Soup                      60239 45331   261.06 spoon
diner           Katherine                 56241 45331   39.46   82.90  312.51
eat             Saint_Albray              62456 56241   125      899 spoon

Here are the details of valid lines, with an example of each line, including comments about the data types and their meanings on each line:

// world name index parentIndex  
// <String> <String> <int> <int>
world           world                     10000     0

// country name index parentIndex population area
// <String> <String> <int> <int> <int> <int>
country         Colombia                  20861 10000       48653419        1038700

// restaurant name index parentIndex  rating menuSize cash
// <String> <String> <int> <int>  <int> <int> <double>
restaurant      Food_by_Louie             35385 20861    1    56        70936.70

// takeout name index parentIndex  rating menuSize cash drivers
// <String> <String> <int> <int>  <int> <int> <double> <int>
takeout         Meals_from_Dick           45331 20861    4    16        66142.27     1

// foodtruck name index parentIndex  rating menuSize cash license
// <String> <String> <int> <int>  <int> <int> <double> <String>
foodtruck       Porkers_by_Berry          32543 20861    3    58        96393.49     KFUQYH

// eatin name index parentIndex  rating menuSize cash seatCount
// <String> <String> <int> <int>  <int> <int> <double> <int>
eatin           House_of_Alec             42203 20861    4    86        41758.35    60

// popular name index parentIndex  rating menuSize cash seatCount softDrinks salads wings
// <String> <String> <int> <int>  <int> <int> <double> <int> <int> <int> <int>
popular         Ribs_by_Eddy              33138 20861    4    75        52377.74   101     9    14     4

// fastfood name index parentIndex  rating menuSize cash seatCount burgerStyles friesStyles
// <String> <String> <int> <int>  <int> <int> <double> <int> <int> int>
fastfood        Burgers_by_Tracy          49457 20861    2    65        30278.99    67     3     9

// elegant name index parentIndex  rating menuSize cash seatCount wineCount appetizerCount soupCount entreeCount desertCount
// <String> <String> <int> <int>  <int> <int> <double> <int> <int> <int> <int> <int> <int>
elegant         Chez_Jayne                34389 20861    3    74        57359.54    51   199     1     9    25     4

// cafe name index parentIndex  rating menuSize cash seatCount coffeeCount teaCount
// <String> <String> <int> <int>  <int> <int> <double> <int> <int> <int>
cafe            Cafe_Carl                 40248 20861    0    50        32922.50    22     4     5

// bar name index parentIndex  rating menuSize cash seatCount beerCount whiskeyCount cognacCount
// <String> <String> <int> <int>  <int> <int> <double> <int> <int> <int> <int>
bar             Bar_Mario                 32180 20861    4    70        72525.87    48   175     8    10

// tool name index parentIndex cost
// <String> <String> <int> <int> <double>
tool            Soup                      60239 45331   261.06 spoon

// diner name index parentIndex age height weight
// <String> <String> <int> <int> <double> <double> <double>
diner           Katherine                 56241 45331   39.46   82.90  312.51

// eat name index parentIndex time calories [requirements]*
// <String> <String> <int> <int> <int> <int> [<String>]*
eat             Saint_Albray              62456 56241   125      899 spoon
You may assume that the data file is correctly formatted and that the parent links exist and are encountered in the data file as item indices before they are referenced as parent links.

There will be a Java program (DiningCreator.jar) provided with this package that will generate data files with various characteristics with the correct format.
You should use this program to generate your own data files to test various aspects of your project programs.

Sample input file:

** File: ../Data/010a.txt
** Data file for Dining projects
** Date: Fri Oct 12 22:43:03 EDT 2018
** Random: false
** Sequence restraurant types: false
** parameters:
**  countries     restaurants       tools_per_rest      diners_per_rest     dishes_per_diner       tools_per_dish
**      1               1                    1                    1                    1                    1



// world name index parentIndex 
// <String> <String> <int> <int>
world           world                     10000     0

// country name index parentIndex population area
// <String> <String> <int> <int> <int> <int>
country         Czech_Republic            32245 10000       10610947          77247

// takeout name index parentIndex  rating menuSize cash drivers
// <String> <String> <int> <int>  <int> <int> <double> <int>
takeout         Meals_from_Anna           48409 32245    3    19        97520.01    15

// tool name index parentIndex cost
// <String> <String> <int> <int> <double>
tool            Decorating                51489 48409   272.15 knife

// diner name index parentIndex age height weight
// <String> <String> <int> <int> <double> <double> <double>
diner           Karl                      49894 48409   23.97   67.70  301.43

// eat name index parentIndex time calories [requirements]*
// <String> <String> <int> <int> <int> <int> [<String>]*
eat             Abbaye_de_Belloc          72821 49894   142      516 knife

Sample output as plain text - which should be displayed in a JTextArea on a JScrollPane in the BorderLayout.CENTER area of a JFrame.
This version takes the lines starting with ** as comments that are also shown in the output.

** File: ../Data/010a.txt
** Data file for Dining projects
** Date: Fri Oct 12 22:43:03 EDT 2018
** Random: false
** Sequence restraurant types: false
** parameters:
**  countries     restaurants       tools_per_rest      diners_per_rest     dishes_per_diner       tools_per_dish
**      1               1                    1                    1                    1                    1


world                        
   Czech_Republic                  10610947      77247
      Meals_from_Anna                 3   19 $ 97,520.01 drivers: 15
                     -- Tools:
         Decorating                     cost: $    272.15 type: knife
                     -- Diners:
         Karl                           age:      23.97 height:      67.70 weight:     301.43
                        -- Dishes:
            Abbaye_de_Belloc               time:   142 calories:    516 knife

Suggestions:

Methods that should be implemented.

Each class should have an appropriate toString method. Here are examples of two such methods:

Each class should have an appropriate Scanner constructor, allowing the class to take advantage of super constructors, so any particular constructor will focus only on the additional elements of interest to that particular class. As an example, here's one way to implement the Elegant constructor:

In the World class, we want to read the text file line by line.
Here are some methods and code fragments that you should find helpful:

You will probably find the comments in the following helpful, they are mostly about similar projects and general issues in Java relevant to our programs:

Project 2 - Also see Grading

Extend Project 1 to use advanced data structures and support sorting on various keys.

Elaboration:

  1. Required data structure - the data structure specified in Project 1:
    1. World has Countries
    2. Country has Restaurants
    3. Restaurant has Diners and Tools
    4. Diner has Eats
    5. Eat has Requirements
  2. Use the HashMap class to support efficient linking of the classes used in Project 1.
    1. The instances of the hash map class should be local to the readFile (Scanner) method.
    2. The HashMap instances should be passed as explicit parameters to other methods used when reading the data file.
      1. For example, the body of the methods like the following should be replaced to effectively use an <Integer, Restaurant> hash map, the surrounding code needs to support this structure:
        Restaurant getRestaurantByIndex (int x, java.util.HashMap <Integer, Restaurant> hmr) {
              return hmr.get(x);
           } // end
        getRestaurantByIndex
      2. Since the body of this method has become trivial, perhaps the call to this method can be simply replaced by the get method of the HashMap.
      3. Your code should be sure to handle a null return from this call gracefully.
    3. The HashMap instances should be released (go out of scope, hence available for garbage collection) when the readFile method returns.
    4. Comments: The idea here, besides getting some experience with an interesting JDK Collections class, is to change the operation of searching for an item with a particular index from an O(N) operation, that is searching through the entire data structure to see if the code can find the parent index parameter, to an O(1) operation, a hash map lookup. Of course, this isn't so very interesting in such a small program, but consider what might happen with hundreds of countries, thousands of restaurants, and perhaps millions of diners and dishes (Eat instances).
    5. Comments: Also, after the readFile operation, the indices are no longer interesting, and could be completely eliminated from the program. In this program, removing the index references could be accomplished by removing those variables from the parent class, Thing.
  3. Implement comparators to support sorting:
  4. Extend the GUI from Project 1 to allow the user to:
  5. Again, the GUI elements should be distinct from the other classes in the program.

Project 3 - Also see Grading

Implement threads and a GUI interface using advanced Java Swing classes.

The project will be graded according the criteria for the final project - see below.

Elaboration:

  1. Required data the data structure specified in Project 1:
    1. World has Countries
    2. Country has Restaurants
    3. Restaurant has Diners and Tools
    4. Diner has Eats
    5. Eat has Requirements
  2. Extend Project 2 to use the Swing class JTree effectively to display the contents of the data file.
  3. Impossible jobs? None of the jobs in Project 3 should be impossible, but some will be in Project 4.
  4. Threads:
  5. Display: use either a JPanel or a row in a JTable (MUCH more challenging) to display and control the job thread.
    1. Exactly what your display looks like is up to you as long as at least the required information and controls are shown for each job.
  6. As before, the GUI elements should be distinct (as appropriate) from the other classes in the program.
  7. See the code at the end of this posting for some suggestions.

Suggestions for Project 3 Eat class.

Also, some of the code at the following sites might give you some ideas about how to proceed with this project:

// j:<index>:<name>:<creature index>:<time>[:<required artifact type>:<number>]*
class Job extends CaveElement implements Runnable {
  JPanel parent;
  Creature worker = null;
  int jobIndex;
  long jobTime;
  String jobName = "";
  JProgressBar pm = new JProgressBar ();
  boolean goFlag = true, noKillFlag = true;
  JButton jbGo   = new JButton ("Stop");
  JButton jbKill = new JButton ("Cancel");
  Status status = Status.SUSPENDED;
 
  enum Status {IMPOSSIBLE, RUNNING, SUSPENDED, WAITING, DONE};

  public Job (HashMap <Integer, CaveElement> hmElements, JPanel cv, Scanner sc) {
    parent = cv;
    sc.next (); // dump first field, j
    jobIndex = sc.nextInt ();
    jobName = sc.next ();
    int target = sc.nextInt ();
    worker = (Creature) (hmElements.get (target));
    jobTime = sc.nextInt ();
    pm = new JProgressBar ();
    pm.setStringPainted (true);
    parent.add (pm);
    parent.add (new JLabel (worker.name, SwingConstants.CENTER));
    parent.add (new JLabel (jobName    , SwingConstants.CENTER));
    
    parent.add (jbGo);
    parent.add (jbKill);
    
    jbGo.addActionListener (e -> toggleGoFlag ());

    jbKill.addActionListener e -> setKillFlag ());
   
    new Thread (this).start();
  } // end constructor

//     JLabel jln = new JLabel (worker.name);
//       following shows how to align text relative to icon
//     jln.setHorizontalTextPosition (SwingConstants.CENTER);
//     jln.setHorizontalAlignment (SwingConstants.CENTER);
//     parent.jrun.add (jln);
 
  public void toggleGoFlag () {
    goFlag = !goFlag;
  } // end method toggleRunFlag
 
  public void setKillFlag () {
    noKillFlag = false;
    jbKill.setBackground (Color.red);
  } // end setKillFlag
 
  void showStatus (Status st) {
    status = st;
    switch (status) {
      case IMPOSSIBLE:
        jbGo.setBackground (Color.gray);
        jbGo.setText ("Impossible");
        break;
      case RUNNING:

        jbGo.setBackground (Color.green);
        jbGo.setText ("Running");
        break;
      case SUSPENDED:
        jbGo.setBackground (Color.yellow);
        jbGo.setText ("Suspended");
        break;
      case WAITING:
        jbGo.setBackground (Color.orange);
        jbGo.setText ("Waiting turn");
        break;
      case DONE:
        jbGo.setBackground (Color.red);
        jbGo.setText ("Done");
        break;
    } // end switch on status
  } // end showStatus

  public void run () {
    long time = System.currentTimeMillis();
    long startTime = time;
    long stopTime = time + 1000 * jobTime;
    double duration = stopTime - time;

    if (status == Status.IMPOSSIBLE) {
      showStatus (Status.IMPOSSIBLE);
      return;
    } // end if impossible
   
    synchronized (worker.party) { // party since looking forward to P4 requirements
      while (worker.busyFlag) {
        showStatus (Status.WAITING);
        try {
          worker.party.wait();
        }
        catch (InterruptedException e) {
        } // end try/catch block
      } // end while waiting for worker to be free
      worker.busyFlag = true;
    } // end sychronized on worker

    while (time < stopTime && noKillFlag) {
      try {
        Thread.sleep (100);
      } catch (InterruptedException e) {}
      if (goFlag) {
        showStatus (Status.RUNNING);
        time += 100;
        pm.setValue ((int)(((time - startTime) / duration) * 100));
      } else {
        showStatus (Status.SUSPENDED);
      } // end if stepping
    } // end runninig

    pm.setValue (100);
    showStatus (Status.DONE);
    synchronized (worker.party) {
      worker.busyFlag = false;
      worker.party.notifyAll ();
    }

  } // end method run - implements runnable
 
  public String toString () {
    String sr = String.format ("j:%7d:%15s:%7d:%5d", jobIndex, jobName, worker.index, jobTime);
    return sr;
  } //end method toString
 
} // end class Job

Project 4 - Also see Grading

Extend project 3 to include making jobs wait until tools with the resources required by the job are available at the restaurant.

Elaboration:

  1. Reading Job specifications from a data file and adding the required resources to each Job instance.
  2. Resource pools - Restaurant.ArrayList <Tool> list of tools at each restaurant, treated as resource pools, along with supporting assignment to jobs.
  3. Eat (job) threads - using the resource pools and supporting the concept of blocking until required resources are available before proceeding.
  4. Synchronization - Since the jobs now also require tools, and the tools are now held by restaurants, the synchronization should be on the Restaurant of the job, not the Diner as in Project 3.
  5. The Job threads should be efficient:
    1. If the diner is free and all the required tools are available, the job should start updating the JProgress bar, as in Project 3.
    2. Otherwise, the Job should not hold any resources if it cannot progress.
    3. Use synchronization to avoid race conditions.
    4. Each job  (Eat) thread should hold any required synchronization locks for a very short period.
    5. When a job is over, all the resources used by the job (the tools) should be released back to the restaurant.
    6. NOTE: If a job can never progress because the tools are not available at the restaurant, the program should report this and cancel the job.
  6. GUI showing:

Grading

Deliverables - for all projects:

  1. Java source code files
  2. Data files used to test your program
  3. Configuration files used
  4. A well-written document including the following sections:
    1. Design: including a UML class diagram showing the type of the class relationships
    2. User's Guide: description of how to set up and run your application
    3. Test Plan:
      1. which data file is to be used and expected results, and
      2. including test data and actual results,
      3. with screen snapshots of some of your test cases
    4. (optionally) Comments: design strengths and limitations, and suggestions for future improvement and alternative approaches
    5.  Lessons Learned
    6. Use one of the following formats: MS Word doc, docx, OpenOffice odt, pdf, rtf.

Your project is due by midnight, EST, on the day of the date posted in the class schedule.
We do not recommend staying up all night working on your project - it is so very easy to really mess up a project at the last minute by working when one was overly tired.

Your instructor's policy on late projects applies to this project.

Submitted projects that show evidence of plagiarism will be handled in accordance with UMUC Policy 150.25 — Academic Dishonesty and Plagiarism.

Format - Documentation format and length.

Coding hints:

Grading Table

Attributes Value
Project design 20 points
Project functionality 40 points
Test data 20 points
Approach documentation 15 points
Approach documentation
grammar and spelling
5 points
Total 100 points

Package


END.