2006

advertisement
PRT1221/PRT2021
PAGE 1 OF 6
QUESTION 1
[40]
Write a program that will accept a user’s password and validate it. If it is not valid, an error message
must be displayed and the user must be prompted to re-enter the password. If the password is valid, it
must be encrypted and written to an output file, Password.txt.
You may assume that the following code has already been written. DO NOT REWRITE!!
import java.io.*;
import java.util.*;
javax.swing.*;
public class Password
{
static PrintWriter passwordFile = null;
public static void main(String[] args) throws IOException
{
String password = “”;
boolean validLength, validLetters, validNumbers;
.
.
NOTE:.


Some of the variables are declared within the main method. Therefore, if necessary, these
variables may have to be passed as parameters to the various methods.
Do NOT make use of StringTokenizer to manipulate the password.
INPUT:

The user must be prompted to enter a password.

Make use of JOptionPane.
PROCESSING:



(1)
(10)
The password must be validated as follows:
o May not be less than 6 characters or more than 10 characters.
o Must contain at least 1 digit.
o Must contain at least 1 letter.
If the password does not meet all validation checks (i.e. fails any one), prompt the user to re-enter
his/her password.
Once the password has met all validation requirements and has been confirmed, this password
must be encrypted and written to an output file, called passwordFile (i.e. Password.txt).
PRT1221/PRT2021
PAGE 2 OF 6
You must ensure that you divide your program logic into the following methods which must be
invoked (called) from an appropriate place in the main method. You must decide on the input
parameters and return type for each of the methods.

Check password length
o
o
o



(Method name = checkNumber)
(7)
Determines whether the password contains at least 1 digit.
If the password does not contain a digit, return false.
If the password does contain at least 1 digit, then return true.
Contains a letter
o
o
o
(3)
Checks the length of the password, which may not be less than 6 characters or more than 10
characters.
If the length falls in the correct range, return true.
If the length does not fall within this range, return false.
Contains a number
o
o
o
(Method name = checkLength)
(Method name = checkLetter)
(7)
Determines whether the password contains at least 1 letter.
If the password does contain at least 1 letter, then return true.
If the password does not contain at least 1 letter, then return false.
Encrypt the password
(Method name = encryptPassword)
(7)
o This method will receive the validated password and encrypt it.
o Make use of the method, isDigit, to check if a character is a digit or not.
o The password is encrypted by replacing all digits by a corresponding special character as
follows:
DIGIT
SPECIAL
CHARACTER
o
o
0
?
1
#
2
|
3
@
4
&
5
%
6
$
7
=
8
!
9
+
The encrypted password must be written to an output file, passwordFile (i.e.
Password.txt).
For example: Original password ab7c4x2
Encrypted password
ab=c&x|
PRT1221/PRT2021

PAGE 3 OF 6
Checks if character a digit
o
o
(Method name = isDigit)
(5)
Receives a character and determines whether it is a digit or not.
Return true if it is a digit, otherwise return false.
QUESTION 2
[50]
Rest-A-While Vacation Property Sales needs a report summarizing the total sales and commission
earned by each salesperson. The General Manager of this company is looking for someone to write a
program to analyse the data and report the top salesperson for the year. You have been chosen to
write a program to help the General Manager.
The General Manager wants the output in the following format:
**************** REST-A-WHILE VACATION ******************
PROPERTY SALES
NAME
QUARTER 1 QUARTER 2 QUARTER 3 QUARTER 4
TOTAL
COMMISSION
SALES
Darwin
50 000
Fortune
Kravitz
0
0
150 000
200 000
12 000
0
30 000
0
50 000
80 000
4 000
20 000
235 000
40 000
0
295 000
20 650
.
.
TOP SALESPERSON: Kravitz
TOTAL SALES: R 295 000
From the sample output above, it is clear that the program must organise the Sales data by Quarters.
The program must also calculate the Total Sales and Commission of each salesperson. The winning
salesperson must also be reported at the end of the report.
NOTE:
 Quarter 1
 Quarter 2
 Quarter 3
 Quarter 4




January, February, March
April, May, June
July, August, September
October, November, December
PRT1221/PRT2021
PAGE 4 OF 6
You may assume that the following code has already been written. DO NOT REWRITE!!
import java.io.*;
import java.util.*;
public class SalesResults
{
final static int QUARTERS = 4;
static BufferedReader keyboard = new BufferedReader(
new InputStreamReader(System.in));
static BufferedReader personFile;
static BufferedReader salesFile;
static int noOfPersons;
static String[] salesperson;
static double[][] salesByQuarter;
public static void main (String[] args) throws IOException
{ .
.
.
INPUT FILES:

Salesperson Data
(Physical File name = personData.txt)
This file consists of the names of the salespersons who holiday vacation during the current year.
You may assume that the data in this file is already sorted alphabetically, and that there are no
duplicate names. Each record in the file consists of only one field, namely:
Salesperson Name

Sales Data
(Physical File name = salesData.txt)
This file consists of the sales made during the year. The data in this file is not sorted. Each record
(i.e. line) in the file consists of the following fields delimited (i.e. separated) by a comma:
Salesperson Name
(String)
Sale Date
(String in day/month/year format)
Sales Amount
(double)
For example, the data in this file could look like:
Darwin,15/02/2005,25000.00
Kravitz,20/01/2005,20000.00
Fortune,30/06/2005,30000.00
Kravitz,10/04/2005,235000.00
Darwin,09/03/2005,25000.00
Fortune,30/11/2005,50000.00
Darwin,30/11/2005,150000.00
Kravitz,15/08/2005,20000.00
Kravitz,20/09/2005,20000.00
PRT1221/PRT2021
PAGE 5 OF 6
.
.
Remember that file processing can cause FileNotFoundException and IOException
exceptions to occur. Ensure that your program caters for both these exceptions and any other
exceptions that may occur.
PROCESSING:
(10)

To find out how many Salespersons work for Rest-A-While Vacation Property Sales, the
user must be prompted.

Since the data types for the Saleperson’s name and the Total Sales are different, you must
make use of parallel arrays:
o A 1-dimensional array to hold the Salesperson names.
Use the name
salesperson for this array.
o A 2-dimensional array to hold the total sales for each Salesperson. Each row
contains the Total Sales for the 4 different Quarters for each Salesperson (i.e. row =
the Salesperson, column = a Quarter). Use the name salesByQuarter for this
array.
You must ensure that you divide your program logic into the following methods which must be
invoked (called) from an appropriate main method. You must decide on the parameters and
return type for each of the methods.

Load salespersons’ names
(Method name = loadNames)
Reads data from the input file, personData.txt, and fills the array, salesperson.
Use a try…catch block to handle the appropriate file exception.

Process sales data
(Method name = processSales)
(15)
Reads data from the input file, salesData.txt, and fills the array, salesByQuarter.
For each entry in the salesData.txt file:
o Tokenize the input line and get the Salesperson’s name, Sale date and Total sales
for this salesperson.
o Search the array salesperson to find the location (i.e. row number) for this
particular salesperson. The method findSalesperson will do this.
o Update the appropriate entry in the array salesByQuarter.
o Use a try…catch block to handle the appropriate file exception.
(4)
PRT1221/PRT2021
PAGE 6 OF 6

Find a salesperson
(Method name = findSalesperson)
(4)
Finds the row number in the array salesperson corresponding to a particular
Salesperson.

Print sales results
(Method name = printResults)
For each Salesperson display a line showing:
o his/her sales for each of the 4 quarters
o total sales for the year.
o the commission earned
Determine and display the name and total sales of the winning Salesperson.

Determine commission rate
(Method name = commissionRate)
Returns the commission rate based on the total sales amount.
Commission rates are determined as follows:
Sales Amount
Rate
0
- 50 000
.04
50 001 - 125 000
.05
125 001 - 200 000
.06
More than 200 000
.07
(14)
(3)
Download