System.out.println("Video Title?");
Scanner sc2 = new Scanner(System.in);
vdTitle = sc2.next();
System.out.println("Quantity?");
Scanner sc3 = new Scanner(System.in);
vdQty = sc3.nextInt();
System.out.println("vdPrice?");
Scanner sc4 = new Scanner(System.in);
vdPrice = sc4.nextDouble();
// Displaying the previously entered information
System.out.println("\n" +"Your Order details below...");
System.out.println("Video ID: "+ vdID);
System.out.println("Video Title: "+ vdTitle);
System.out.println("Video Price: "+ vdPrice);
System.out.println("Quantity: "+ vdQty);
//Subtotal is calculated and displayed
double vdSubtotal = vdPrice * vdQty;
System.out.println("Subtotal: " + vdSubtotal);
// Discount is calculated depending on the quantity
if (vdQty >= 15)
discount = disFor15Plus * vdSubtotal;
else
if (vdQty >= 10)
discount = disFor10To14 * vdSubtotal;
else
if (vdQty >= 5)
discount = disFor5To9 * vdSubtotal;
else
discount = 0;
//Discount is displayed
System.out.println("Discount: -" + discount);
// New subtotal is calculated after discount is subtracted
// Discounted Subtotal is Displayed
double vdSubtotal2 = vdSubtotal - discount;
System.out.println("Discounted Subtotal:" + vdSubtotal2);
// VAT is calculated and added to new subtotal
double VAT = vdSubtotal2 * vatRate;
double vdTotal = vdSubtotal2 + VAT;
//Final Total is displayed
System.out.println("Total inc VAT: " + vdTotal);
}
}
TESTING
Syntax Errors
After compiling and running my first attempt from the command prompt I encounter the following error.
The error message points me to the direction of line no. 55 and says there is a semi colon expected. Looking at the code, I can see at the end of the ‘if’ statement there is semicolon left out.
Logical Errors
As can be seen, there were no logical errors in this version.
VERSION 2
The second version shall have the ability to process multiple video titles in one transaction and also have some basic GUI features.
REQUIREMENTS
Input: String vdID, vdTitle, ask, indata
int vdQty,
double vdPrice,
final double vatRate = .175, disFor15Plus = 0.25,
disFor10To14 = 0.17, disFor5To9 = 0.12
Output: String vdID, vdTitle,
int vdQty, itemCount, vdQtyAll,
double vdPrice, vdSubtotal, vdSubtotalAll, dSubtotal, vdTotal, discount, VAT
Process: vdSubtotal = vdQty*vdPrice
vdQtyAll +=vdQty
vdSubtotalAll+=vdSubtotal
discount=vdsubtotalAll*disFor…
dSubtotal=vdSubtotalAll-discount
VAT=dSubtotal*vatRate
Total=dSubtotal+VAT
DATA TABLE
DESIGN PSEUDOCODE
Declare vat and discount rates constants
Prompt for video ID, Title, Price and Quantity
Read inputted values
vdSubtotal = vdPrice*vdQty
Add vdQty to vdQtyAll
Add vdSubtotal to vdSubtotalAll
Display string asking if to continue
while ask = 'y' or 'Y'
repeat all above
while ask = 'n' or 'N'
display vdQtyAll
display vdSubtotalAll
if vdQtyAll >= 15
discount = disFor15Plus * vdSubtotalAll
if vdQtyAll >= 10
discount = disFor10To14 * vdSubtotalAll
if vdQtyAll >= 5
discount = disFor5To9 * vdSubtotalAll
else
discount = 0
display dSubtotal
display vdtotal
IMPLEMENTATION (v. 2)
import java.util.Scanner;
import javax.swing.JOptionPane;
public class VideoSales2
{
public static void main (String[] args)
{
//Declaring Variables needed in the program and added values to the constants
String ask="";
double vdSubtotalAll=0;
double vdQtyAll =0;
final double vatRate = 0.175;
String vdID, vdTitle, indata;
int vdQty;
double vdPrice, discount;
final double disFor15Plus = 0.25;
final double disFor10To14 = 0.17;
final double disFor5To9 = 0.12;
// TODO code application logic here
do
{
// GUI - ask for input from the user regarding video ID, Title, Quantity, Price
vdID=JOptionPane.showInputDialog("Video ID?");
vdTitle=JOptionPane.showInputDialog("Video Title?");
indata= JOptionPane.showInputDialog("Quantity?");
//Convert indata string to integer
vdQty= Integer.parseInt(indata);
indata= JOptionPane.showInputDialog("Video Price?");
//Convert indata string to integer
vdPrice= Double.parseDouble(indata);
//Display input to user
System.out.println("\n"+"\n"+vdID);
System.out.println(vdTitle);
System.out.println("quantity= "+vdQty);
System.out.println("price = "+vdPrice+"\n"+"\n");
//Subtotal is calculated and displayed
double vdSubtotal = vdPrice * vdQty;
//Current video quantity and subtotal is added to all previous
vdQtyAll+=vdQty;
vdSubtotalAll+=vdSubtotal;
//setting variable ‘ask’ to the answer of a question
//variable needs to be inside of loop for the do while statement to work
ask=JOptionPane.showInputDialog("Repeat? y/n?");
}
//Depending on the answer the TODO logic should repeat
while(!(ask.equalsIgnoreCase("n")));
//Once loop is broken the whole video quantity and subtotal is displayed
System.out.println("your total video quantity is: " +vdQtyAll);
System.out.println("Your subtotal is: " +vdSubtotalAll+"\n");
// Discount is calculated depending on the quantity then displayed
if (vdQtyAll >= 15)
discount = disFor15Plus * vdSubtotalAll;
else
if (vdQtyAll >= 10)
discount = disFor10To14 * vdSubtotalAll;
else
if (vdQtyAll >= 5)
discount = disFor5To9 * vdSubtotalAll;
else
discount = 0;
System.out.println("discount = " + discount);
//Concluding calculations are made and displayed
double dSubtotal = vdSubtotalAll - discount;
System.out.println("discounted subtotal: " +dSubtotal+"\n");
double VAT = dSubtotal * vatRate;
System.out.println("VAT = " + VAT);
double vdTotal = dSubtotal + VAT;
System.out.println("Final total inc VAT ="+ vdTotal);
}
}
TESTING
Syntax Errors
When attempting to build the program, the Netbeans IDE discovered two errors. The first error message points to line 97 and indicates a closing bracket is needed.
In the code I can select specific brackets and the IDE will highlight it’s pairing bracket. I do this and find that line 97 does not pair with the opening bracket after main; it instead pairs with the opening bracket to the ‘do while’ statement.
do
{
// GUI - ask for input from the user regarding video ID, Title, Quantity, Price
vdID=JOptionPane.showInputDialog("Video ID?");
vdTitle=JOptionPane.showInputDialog("Video Title?");
indata= JOptionPane.showInputDialog("Quantity?");
//Convert indata string to integer
vdQty= Integer.parseInt(indata);
indata= JOptionPane.showInputDialog("Video Price?");
//Convert indata string to integer
vdPrice= Double.parseDouble(indata);
//Display input to user
System.out.println("\n"+"\n"+vdID);
System.out.println(vdTitle);
System.out.println("quantity= "+vdQty);
System.out.println("price = "+vdPrice+"\n"+"\n");
//Subtotal is calculated and displayed
double vdSubtotal = vdPrice * vdQty;
//Current video quantity and subtotal is added to all previous
vdQtyAll+=vdQty;
vdSubtotalAll+=vdSubtotal;
//setting variable ‘ask’ to the answer of a question
//variable needs to be inside of loop for the do while statement to work
ask=JOptionPane.showInputDialog("Repeat? y/n?");
//Depending on the answer the TODO logic should repeat
while(!(ask.equalsIgnoreCase("n")));
I have clearly forgotten to close off the ‘do’ logic. Fixing this will probably solve the following error on line 100, because that is also a closing bracket. After adding this, I attempt to build the program again.
Unfortunately more errors are shown. These error messages point to two variables which it says may have not been initialized. More research into this shows that these variables have not been assigned an initial value so making a calculation from them is impossible. After assigning the value of ‘0’ to these variables, the programs compiles free of any errors.
Logical Errors – Order 1
The above test log shows no errors for the overall total of the multiple items in the order 1. No discount was subtracted because the overall quantity was below five.
Logical Errors – Order 2 (first attempt)
The overall total should be the overall price of the 8 DVDs minus the discount which would give us ‘113.4848’, then add the VAT which equals 133.34. However the result is well below that. The data I input may be incorrect, but if, on the second run the same problem arises, then there are a number of additional possibilities:-
- One of the constants have the incorrect figure assigned to them
- One of the logical operators are incorrect
- The calculations have been placed somewhere incorrect in the program
- An incorrect variable name used in a calculation
After running the program again and re-entering the data, I come to the same result. The IDE did not show us any error messages when compiling, so there must be a problem with the logic.
I first check the declared values of the constants, they seem to be fine. After isolating some parts of the program by commenting them out and compiling, I run the program to find all the calculations are correct. I therefore check the final total code which I had commented out for this purpose. I find the logical error to be contained in this line; the VAT had been subtracted rather than added.
Logical Errors – Order 2 (second attempt)
VERSION 3
The third and final version shall provide connectivity to a database, storing all the transactions. The order information shall be displayed in a GUI in the relevant numeric format.
REQUIREMENTS
Input: String vdID, vdTitle, ask, indata
int vdQty,
double vdPrice,
final double vatRate = .175, disFor15Plus = 0.25,
disFor10To14 = 0.17, disFor5To9 = 0.12
Output: String vdID, vdTitle,
int vdQty, itemCount, vdQtyAll,
double vdPrice, vdSubtotal, vdSubtotalAll, dSubtotal, vdTotal, discount, VAT
Process: vdSubtotal = vdQty*vdPrice
vdQtyAll +=vdQty
vdSubtotalAll+=vdSubtotal
discount=vdsubtotalAll*disFor…
dSubtotal=vdSubtotalAll-discount
VAT=dSubtotal*vatRate
Total=dSubtotal+VAT
DATA TABLE
DESIGN PSEUDOCODE
Declare vat and discount rates constants
Prompt for video ID, Title, Price and Quantity
Read inputted values
vdSubtotal = vdPrice*vdQty
Add vdQty to vdQtyAll
Add vdSubtotal to vdSubtotalAll
Display string asking if to continue
while ask = 'y' or 'Y'
repeat all above
while ask = 'n' or 'N'
display vdQtyAll
display vdSubtotalAll
if vdQtyAll >= 15
discount = disFor15Plus * vdSubtotalAll
if vdQtyAll >= 10
discount = disFor10To14 * vdSubtotalAll
if vdQtyAll >= 5
discount = disFor5To9 * vdSubtotalAll
else
discount = 0
store vdID, vdTitle, vdQty, vdPrice in database
display dSubtotal
display vdtotal
FINAL IMPLEMENTATION (v. 3)
package videostore;
/**
*
* @author Kevin Abrahams
*/
import java.sql.*;
import java.text.NumberFormat;
import javax.swing.JOptionPane;
public class Main
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
Connection connection;
Statement statement;
String ask="";
double vdSubtotalAll=0;
double vdQtyAll =0;
final double vatRate = 0.175;
String vdID, vdTitle, indata;
int vdQty;
double vdPrice, discount;
final double disFor15Plus = 0.25;
final double disFor10To14 = 0.17;
final double disFor5To9 = 0.12;
//Displays currency sign
NumberFormat money = NumberFormat.getCurrencyInstance();
// TODO code application logic here
do
{
//Prompting for video detail variables
// GUI - ask for input from the user regarding video ID, Title, Quantity, Price
vdID=JOptionPane.showInputDialog("Video ID?");
vdTitle=JOptionPane.showInputDialog("Video Title?");
indata= JOptionPane.showInputDialog("Quantity?");
//Convert indata string to integer
vdQty= Integer.parseInt(indata);
indata= JOptionPane.showInputDialog("Video Price?");
//Convert indata string to integer
vdPrice= Double.parseDouble(indata);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection= DriverManager.getConnection("jdbc:odbc:VideoSales","","");
statement = connection.createStatement();
statement.executeUpdate("INSERT INTO tblVideoSales (VideoID,VideoTitle,Quantity,VideoPrice) VALUES ('"+ vdID+"','"+ vdTitle+"','"+ vdQty+"','"+vdPrice+"')");
connection.close();
}
catch (Exception e)
{
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
System.out.println("\n"+"\n"+vdID);
System.out.println(vdTitle);
System.out.println("quantity= "+vdQty);
System.out.println("price = "+vdPrice+"\n"+"\n");
double vdSubtotal = vdPrice * vdQty;
vdQtyAll+=vdQty;
vdSubtotalAll+=vdSubtotal;
ask=JOptionPane.showInputDialog("Repeat? y/n?");
}
while(!(ask.equalsIgnoreCase("n")));
// Discount is calculated depending on the quantity then displayed
if (vdQtyAll >= 15)
discount = disFor15Plus * vdSubtotalAll;
else
if (vdQtyAll >= 10)
discount = disFor10To14 * vdSubtotalAll;
else
if (vdQtyAll >= 5)
discount = disFor5To9 * vdSubtotalAll;
else
discount = 0;
//Concluding calculations are made
double dSubtotal = vdSubtotalAll - discount;
double VAT = dSubtotal * vatRate;
double vdTotal = dSubtotal + VAT;
//Concluding calculations displayed using GUI output message dialog box
JOptionPane.showMessageDialog(null,"The total video quantity is: " +money.format(vdQtyAll)+"\n"+"The subtotal is: "+money.format(vdSubtotalAll)+"\n"+"The discount is: " + money.format(discount)+"\n"+"The discounted subtotal is: "+money.format(dSubtotal)+"\n"+"The VAT is: " + money.format(VAT)+"\n"+"\n"+"The final total (inc VAT) is: "+ money.format(vdTotal));
}
}
TESTING
Database connectivity
In the control panel of windows it is necessary to go into the administrative tools and add a new data source (ODBC) in order for the program to have database connectivity.
After creating the driver for database connectivity, I compiled the program and it was error-free. However when running it, the following message appeared at the start of the data input process.
I found that data source name declared in the program was different to the actual data source just made. I changed the code as follows…
This worked and the following data successfully entered the relevant table in the database.
EVALUATION
I am satisfied that my application fulfils the basic user requirements; however it does leave room for improvement and further development. For example, it is able to process multiple video titles but I would like to, in the future, make the process of entering the video information easier by creating a better designed GUI.
The calculations all eventually passed the rigorous testing stage but the storing of these calculations rather than just the user input is another improvement that could be made. All the data together could then be displayed and even printed onto an itemised receipt.
I have left the program in a position in which the database functionality can be expanded on, to connect to a network. The program could even respond to scanned barcodes in relation to records held in the database.
CONCLUSION
The program is structured well with comments on all the major aspects, and I do believe that the above User Documentation is easy to understand and laid out in a good manner. The User Guide has screenshots to make the learning process easier.
This project was done under minimal supervision, and actually, I surprised myself in learning some of the key programming concepts by having to learn from and fix my own mistakes.
BIBLIOGRAPHY
BOOKS
-
Deitel H. & P. (2001) Java How To Program 4th Edition, Prentice Hall
-
McGrath, M. (2003) Java in easy steps, Computer Step
-
Knott, G. & Waites, N. (2002) BTEC Nationals for IT Practitioners, Durham: BC Publications
-
Parsons, J. J. & Oja, D. (2006) Computer Concepts, Sixth Edition, US: Course Technology Inc.
-
Long, L. & N. (2005) Computers: Information Technology in Perspective 12th Edition, NJ: Pearson Education
WEBSITES
-
[Accessed 01 May 2008]
-
[Accessed 03 May 2008]
-
[Accessed 15 June 2008]
-
[Accessed 16 June 2008]
SOFTWARE
- Microsoft® Student 2008 [DVD]. Redmond, WA: Microsoft Corporation, 2007