Lab 4 - Development, Phase I
The development environment is up and running. During the previous homework we went through the software versioning, building scripts, several library components and created your first Java project. We will continue to extend created project in Eclipse with a special attention on one of the main development tools - debugger.
POS System
For this homework we prepared a base code that is available here. Download it, extract and copy all contents into your Java project created during the previous homework.
During the previous homework you also developed two classes: Intro and IntroUI.
You need to extend the Intro class using the following code:
public class Intro { private static final Logger log = Logger.getLogger(Intro.class); private static final String MODE = "console"; public static void main(String[] args) { final SalesDomainController domainController = new SalesDomainControllerImpl(); if (args.length == 1 && args[0].equals(MODE)) { log.debug("Mode: " + MODE); ConsoleUI cui = new ConsoleUI(domainController); cui.run(); } else { IntroUI introUI = new IntroUI(); introUI.setVisible(true); introUI.setAlwaysOnTop(true); final SalesSystemUI ui = new SalesSystemUI(domainController); ui.setVisible(true); introUI.setAlwaysOnTop(false); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } introUI.setVisible(false); } }
In order to run your application in console you need to create a new target runc:
<target name="runc" depends="build" description="Run the application (console)"> <java classname="ee.ut.math.tvt.[team].Intro" classpathref="test.classpath" fork="yes"> <arg value="console" /> </java> </target>
Task 1. Build Script
Extend your existing build script by adding two additional targets:
- git_commit - on execution it should ask for a commit comment, make a commit to the local repository and push changes to the GitHub server
In other words this target should perform three operations:
git add -A
(this command selects all new/changed files/folders for commit)git commit -m 'Your comment for commit here'
git push
Due to many emails, here is a tip describing the way to include the username and password to the push action! When you do the git push
you can also specify the remote repository URL ( git push https://myurl.git
). This URL can also include inline username and password. This will prevent the dynamic interaction of the push action (will not ask for the password).
The general form of such URL is the following:
https://USERNAME:PASSWORD@github.com/blabla/blablabla.git
Where USERNAME is the short account name you can see on the right top corner of your GitHub page. You can build that URL dynamically in the git_commit task using your repository URL and by asking for a username and password from the user (ANT input task).
- git_tag - on execution it should ask for a tag name and comment. This target should create the corresponding tag and synchronize it with the GitHub.
It equivalents to running two commands in console:
git tag -a homework_n -m "Homework n"
(where n is your homework number)git push --tags
Task 2. Extending the POS System
- You need to change your POS GUI, instead of writing the product ID (Bar Code) you should create a dropdown menu, where you can select the products by name. Everything else should be the same - that means selection of the product triggers automatic filling of the fields "Bar code" and "Price" with the corresponding product data. (Hint: check the JComboBox component).
- Extending the Shopping Cart
- Add an additional column "Sum" to the "Shopping Cart" table, where the total sum for the product will be calculated. (Price * Amount).
- Prohibit adding the product to the shopping cart once the warehouse has no more items. The user must be notified about it with a warning message.
- Paying
- When user confirms his order, there should appear an additional screen that:
- will show the total sum of the order.
- will have a field to enter the payment amount.
- will show the change amount (tagastusraha).
- will have the buttons to accept or cancel the payment. If the payment is accepted then order should be accepted and saved. If the payment is canceled, then the screen should be closed/hided and the shopping cart should restore the state when it was left.
- When user confirms his order, there should appear an additional screen that:
- Saving the order and order history
- Extend the application with the history table (on history tab) for the accepted orders. You should add JTable to the History tab. This table should display accepted orders with following information:
- Date of the order.
- Time of the order.
- Total order price.
- Left mouse click on any of the accepted orders should open a table with a list of all ordered elements (product name, amount, unit price, total price) basically the same what you see in the shopping cart table.
- Do not forget to decrease the amount of goods in the warehouse if the order was accepted.
- Extend the application with the history table (on history tab) for the accepted orders. You should add JTable to the History tab. This table should display accepted orders with following information:
- Warehouse
- Extend the warehouse tab with the functionality to add new products. Users should be able to set all the product attributes.
- Create a new tag with name homework_4 where you commit the final solution, comment it with "Homework 4". As a result your solution on the GitHub should have the link:
- https://github.com/[account name]/[repo name]/tree/homework_4
In all tasks you also need to think about exceptions, your POS application should not show or accept incorrect data (includes rounding).
Task 3. Eclipse debugger
In general, debugger is a software that allows to see how the code is executed line-by-line with an ability to pause the execution on an arbitrary line. Debuggers are mostly used to analyze the algorithms and to find bugs.
Java Virtual Machine (JVM) allows to attach different debuggers via a specific protocol. During this homework you will use the most common one - Eclipse IDE built-in debugger. In order to activate debugging in the JVM you need to run your application with a specific flag. We will add this flag in our ANT script build.xml file. After the change, the run target will look like this:
<java classname="ee.ut.math.tvt.[team].Intro" classpathref="run.classpath" fork="yes"> <jvmarg value="-Xdebug" /> <jvmarg value="-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=1044" /> </java>
After the flag is set (when executed, must print in console something similar to "Listening for transport dt_socket at address: 1044"), you can attach the Eclipse built-in debugger. Choose: Run --> Debug configurations .., there Remote Java Application and fill in the correct data (host = localhost, port = 1044 or other, depends on what you used in the build.xml). When data is set, press "Apply" and then "Debug". Preferably, also open the debug perspective (Window --> Open Perspective --> Debug). If everything is OK, you will see that debugger is connected to the JVM. You will also see different active threads in the JVM.
The work with the debugger usually starts with setting the breakpoints on the code lines of interest. After breakpoints are set, the application is intentionally directed to trigger them. When JVM reaches the line with a breakpoint, it pauses the execution and allows developer to inspect and change the state.
Get familiar and practice the debugging process and answer the following questions:
Some of them can be answered also without the use of a debugger. However, try to think how the use of the debugger can help you in all the cases. Use the initial code given in the beginning of this homework (plus your homework 3 results). Extended code can give a bit different results that can be interpreted as false without an extensive check.
- Name at least one method, where a breakpoint will stop the application execution right after pressing the "New purchase" button.
- Name a method, where a breakpoint will stop the application execution right after pressing the "Confirm" button (during confirming the order).
- List the subclasses of the java.awt.Container class, which regularly call doLayout() method (if you randomly use the application, Hint: put the breakpoint to the method Container#doLayout())
- How many times the constructor new String() (without argument) in the class java.lang.String is used after pressing the New purchase button.
- How many times the constructor new JPanel() (without argument) in the class javax.swing.JPanel is used after pressing the New purchase button.
You should deliver your task 3 results using the GitHub wiki.