assignment2-group2-section15

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Assignment

Course
BIS 20503 Item 2/
Code
Lab 7
FACULTY OF
COMPUTER SCIENCE
Course SOFTWARE Sem / 1/
AND INFORMATION
SECURITY Session 20242025
TECHNOLOGY

Title Secure Coding Principle: Input Validation


At the end of this lab, student should be able to:
i. Demonstrate input validation design through
Objectives flowchart
ii. Implement input validation principles through
programming
- Dhava Akbar Kusuma (JI240002)
- Achsani Faiz Paramartha (JI240003)
Name & Matric No. - Valfajar Prayoga Putra Husnan (JI240005)
- Chrisnanda Yunus Risqiandhika (JI240007)
- Solehudin Yusuf (JI240008)

Case scenario

You are involved in a UTHM New International Student Information System development
project. In the project development, your part is to design and write a code segment for the
Personal Information module, in which new students are required to enter their personal
information. As a part of security requirements, the code must apply input validation
elements.

Users (i.e. new international students) are required to enter the following information:
1. Name (must be string)
2. Date of birth (in format: dd/mm/yyyy)
3. Passport no (must be alphanumeric, start with letter and followed by digits, it must not
be more than 10 digits)
4. Home country (must be string)
5. Program code (users must enter: 1 for BIT, 2 for BIS, and 3 for BIP)

All information will be displayed after users finish entering the required information with
correct format. If there is/are any incorrect input, warning message will be displayed for
users to enter correct input. Example of output is presented in Figure 1.

Name: Ahmed Mostafa Abdullah


Date of birth: 03/09/2003
Passport number: CX2107080
Home country: Egypt
Program code: 2
Program Name: BIS

Figure 1: Example of Personal Information output

BIS 20503 Page 1


Questions

a) Illustrate the input validation flow using a flowchart (refer to slide no. 62 in
Chapter 4 notes).

b) Insert the source code for the Personal Information module. (You can choose to
copy & paste the code from programming software OR screenshot the code – as
long as it is clear and visible).
import java.util.*;

public class lab_7 {

public static String checkName(Scanner scan) {

BIS 20503 Page 2


while (true) {
System.out.print("Please enter your name here: ");
String name = scan.nextLine();
if (name.matches("^[a-zA-Z ]+$")) {
return name;
} else {
System.out.println("Error: Invalid input. Name must be a
string.");
}
}
};

public static String checkDate(Scanner scan) {


while (true) {
System.out.print("Please enter your date of birth (dd/mm/yyyy): ");
String dateOfBirth = scan.nextLine();

if (dateOfBirth.matches("^\\d{2}/\\d{2}/\\d{4}$")) {
return dateOfBirth;
} else {
System.out.println("Error; Invalid input. Please follow the format
dd/mm/yyyy.");
}
}
};

public static String checkPassport(Scanner scan) {


while (true) {
System.out.print("Enter your Passport number: ");
String passport = scan.nextLine();
if (passport.matches("^[a-zA-Z]\\d{9,}$")) {
return passport;
} else {
System.out.println(
"Error: Passport Number must start with letter, followed
by digits, and must be 10 characters. Please try again");
}
}
}

public static String checkCountry(Scanner scan) {


while (true) {
System.out.print("Enter your home country: ");
String country = scan.nextLine();
if (country.matches("[a-zA-z]+")) {
return country;
} else {
System.out.println("Error: Home Country must contain only letters.
Please try again");
}
}

BIS 20503 Page 3


}

public static String checkProgramCode(Scanner scan) {


while (true) {
System.out.print("Enter Program Code (1 for BIT, 2 for BIS, 3 for
BIP): ");
String code = scan.nextLine();
if (code.matches("[123]")) {
return code;
} else {
System.out.println("Error: Program Code doesn't exist");
}
}
}

public static void main(String[] args) {


Scanner scan = new Scanner(System.in);

System.out.println("Welcome to the UTHM new International Student


Information System");
System.out.println("Please enter the required information below.");

String name = checkName(scan);


String dateOfBirth = checkDate(scan);
String passportNo = checkPassport(scan);
String homeCountry = checkCountry(scan);
String programCode = checkProgramCode(scan);

String programName = "";


switch (programCode) {
case "1":
programName = "BIT";
break;
case "2":
programName = "BIS";
break;
case "3":
programName = "BIP";
break;
}

System.out.println("\nPersonal Information:");

System.out.println("\nName: " + name);


System.out.println("Date of Birth: " + dateOfBirth);
System.out.println("Passport Number: " + passportNo);
System.out.println("Home Country: " + homeCountry);
System.out.println("Program Code: " + programCode);
System.out.println("Program Name: " + programName);

BIS 20503 Page 4


}
c) Provide a screenshot of the wrong input format for Date of Birth.

d) Provide a screenshot of the input that exceeds 10 digits for Passport no.

e) Provide a screenshot of the wrong input that does not enter 1, 2, or 3 for Program
Code.

f) Provide a screenshot of the output with all correct inputs.

BIS 20503 Page 5


ASSIGNMENT 2 RUBRIC

Criteria Rating Marks

A. Practical (CLO2, P4)


• Related flowchart flow

• Validation for all required data types 

• Consistent with the input validation 


flowchart

• Appropriate error handling 

Total /15

BIS 20503 Page 6

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy