Structure of simple POS system
Hi All,
I have been studying Java for some time and decided to finally try and make an application. I am going to try and build a basic cash register (Point of Sale) system that connects to a MySQL database which contains the products information (ID, name and price).
I have a basic idea on how I wish to set this up and just want to check that I'm correct before going ahead.
1) I intend to make the MySQL queries using JDBC.
2) I am going to use SWING to develop the GUI. The idea is the person at the cash register enters the product ID into a text box and it add's it to the sale. The GUI will then display a list of current products and a subtotal. When the user then finishes the sale, they press a button and the details are logged in a database.
The two main things I want to confirm are:
3) I was going to store the products on the current sale in a 2D array. Is this the best way of doing this?
4) In the GUI I was going to display the list of products using JTable. I dont want the user on the cash till to be able to edit the contents of the table, its simply to display what has been rung through. Again is this the best way to display this information?
Thanks for your help,
Joseph.
Re: Structure of simple POS system
Quote:
store the products on the current sale in a 2D array
No. It will be better if you make a class to hold the data. Parallel/2D arrays can be an update problem.
A class will get you lots more flexibility. One is the typing of the class object when you call a method to work on the container with the sales data.
Re: Structure of simple POS system
Thanks for the suggestion. How would I store the data in a class? Using variables within it? Or by creating a new object for each item and adding each item into an ArrayList?
Thanks,
Re: Structure of simple POS system
Quote:
creating a new object for each item and adding each item into an ArrayList
I thought there were 2 items. I'm confused why you mentioned the 2 dim array.
What data type is an "item"?
Re: Structure of simple POS system
Sorry, I didn't explain myself properly.
There can be an unlimited amount of items. Each item would contain: Item ID (int) / Item name (String) / Item price (double).
My initial thoughts would be to use a 2 dim array, so that each item would have a row, with 3 column to represent the above three items.
Re: Structure of simple POS system
With a class, you'd put those three items: ID, name and price in one object.
You could have a constructor that takes all three to build an object.
Quote:
unlimited amount of items
Then you'd want to put the objects in a container like an ArrayList instead of an array.
Re: Structure of simple POS system
Ok brilliant. Thanks for the advice.
With regards to displaying the items in the GUI, I simply want a list of all the items which will get longer as each item is added to the sale. Would using the JTable to display each 'item' object be the best way?
Re: Structure of simple POS system
Sorry, I don't do much GUI with JTables.
Re: Structure of simple POS system
Quote:
Originally Posted by
jrflynn
With regards to displaying the items in the GUI, I simply want a list of all the items which will get longer as each item is added to the sale. Would using the JTable to display each 'item' object be the best way?
Yes, a JTable would be a good solution. Each row would be an item and the columns would be ID, name, and price. See How To Use Tables.
Re: Structure of simple POS system
Thanks so much for your help. Just want to check that I understand you correctly and have implemented this properly. I have made the following code which works fine. Are there any improvements to my code you would suggest or am I now going down the right track?
order.java
PHP Code:
import java.util.ArrayList;
public class order {
String orderStatus;
double subtotal;
ArrayList<product> products = new ArrayList<product>();
public order() {
orderStatus="local";
}
public void addProduct(int SKU, double price, String name) {
products.add(new product(SKU, price, name));
subtotal=subtotal+price;
}
public void removeProduct(product productToRemove) {
subtotal=subtotal-productToRemove.getPrice();
products.remove(productToRemove);
}
}
product.java
PHP Code:
public class product {
int SKU;
double price;
String name;
public product(int SKU, double price, String name) {
this.SKU=SKU;
this.price=price;
this.name=name;
}
public double getPrice() {
return price;
}
}
Thanks for your help.
Re: Structure of simple POS system
One suggestion: Java naming conventions: Leading letter of name is uppercase for classes and lowercase for variables.
Re: Structure of simple POS system
Quote:
Originally Posted by
jrflynn
Sorry, I didn't explain myself properly.
There can be an unlimited amount of items. Each item would contain: Item ID (int) / Item name (String) / Item price (double).
My initial thoughts would be to use a 2 dim array, so that each item would have a row, with 3 column to represent the above three items.
Why would you use collections? The data shall be expired once the program stops running, no?
Choosing which container to use depends on what you will use them for. If you are gonna access your elements in a random way, you could use an ArrayList but it's slower to insert and remove elements in the middle of the list in which LinkedList is faster. If you want to have unique elements use Sets then. I hope this helps.
Re: Structure of simple POS system
Quote:
Originally Posted by
jrflynn
... Are there any improvements to my code you would suggest or am I now going down the right track?
Other than the Java Naming Conventions previously mentioned, it's good practice to make your member variables private, particularly if you provide getters for them - class data should not be open to modification by other classes without the owning class's knowledge/consent. BTW your Product class has a getter for price, but not for name or SKU...
Your Order class is always created with implicit 'local' status - I expected to see another constructor that could set the status explicitly.
When you add one numeric variable to another, you can use the short-form '+=' operator, which looks a little tidier (the same goes for subtraction with '-='):
Code java:
subtotal += price; // add price to subtotal
The removeProduct method takes a Product argument and tries to remove it. This seems reasonable, although it ought to check that Product is actually present before trying to remove it. The addProduct method takes three arguments and creates a Product object from them. This seems unnecessary and means that the Order class has to know about the contents of a Product and how to create one. I don't think an Order needs to know, and shouldn't know what's inside the items it holds. As far as the Order is concerned, a Product is just a box from the warehouse. As it is, if you decide to change the Product class, you'll have to change the Order class too. I recommend just passing a Product to the addProduct method, so it matches the removeProduct method. The Product can be created elsewhere. Typically there would be a complete list of Products held somewhere and one of those would be selected and added to the order.
There is also a potential problem with the removeProduct method if you're going to create multiple Product objects with the same contents (e.g. for different orders). By default, the ArrayList.remove(Object) method tries to remove the exact object that is passed to it. It looks in the list for the identical object instance, so will only find it if it was obtained from the product list in that specific Order originally. If you wanted to remove a Product with the same contents that you'd got from another Order (or created from scratch), it wouldn't be found, because it would be a different object instance. What you probably want here is for it to remove an object with the same contents, because two Product objects with the same contents surely represent the same product. This is the crucial difference between object identity and object equality. To make sure that the ArrayList will find a Product object with the same contents, you need to give the Product class an equals(..) method, that will return 'true' if the contents of the Product passed in are equal to those of the Product testing it.
In the real world, each Product would have a unique identifier code, and this would be used to find it in collections. In this scenario, the Product objects would be stored in a Map of some kind, with the unique Product code as a key.
Re: Structure of simple POS system
POS is a one type of hardware operating system they provide the system of external touch terminal. This system provide many product list by rating and give point of hardware equipment and supplies. Point of sale system is a one type of information system it is used to gathering and applying the information in the market. Each pos system is very different to the other system it have also huge productivity of your business.