0% found this document useful (0 votes)
2 views

Lab 5

LAB 05

Uploaded by

mahamimrann25
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab 5

LAB 05

Uploaded by

mahamimrann25
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

NAME: Maham Imran

ROLL NO: 2020F-BSE-111.

Lab # 5

INTERMEDIATE JAVASCRIPT

OBJECTIVE: To get familiar and become more knowledgeable in implementing real world JS
use cases.

LAB TASK

TASK 01: Classes and Inheritance:


a. Create a base class called Student and export it
i. Add id, name, date of birth properties
ii. Add enroll method
b. Create two child classes called RegularStudent and ExecutiveStudent and inherit them
from Student base class
i. Add attendLab method in RegularStudent class
ii. Add attendTheory method in ExecutiveStudent class
c. Create a separate module and import RegularStudent and ExecutiveStudent classes.
Validate using console.log that the properties from base classes have been inherited into
child classes along with own properties.

SOURCE CODE:
JS CODE
class Student {
constructor(id,name,dob) {
this.id=id;
this.name=name;
this.dob=dob;
}
enroll() {
console.log('${this.name} has been enrolled successfully!');
}
}
// MAHAM IMRAN (2020F-BSE-111)
import {Student} from './student.js;'
class RegularStudent extends Student {
constructor(id,name,dob) {
super(id,name,dob);
}
attendLab() {
console.log('${this.name} is attending lab.');
}
}

Page | 1
NAME: Maham Imran
ROLL NO: 2020F-BSE-111.

export {Exceutivestudent};
//MAHAM IMRAN (2020F-BSE-111)
class ExecutiveStudent extends Student {
constructor(id,name,dob) {
super(id,name,dob);
}
attendTheory() {
console.log('${this.name} is attending theory.');
}};
module.exports= {Student, RegularStudent, ExecutiveStudent};

const { Student, RegularStudent, ExecutiveStudent} = require('./student');


const { createTable, insertData, selectAll, closeConnection} =
require('./database');
const { fetchAboutPage } = require('./http');
const student1 = new Student (1, 'MAHAM IMRAN', '2003-02-25');
console.log(student1);
student1.enroll();
const regularStudent1 = new regularStudent(2, 'ZAYN KHAN', '2000-08-07');
console.log(regularStudent1);
regularStudent1.attendLab();
//MAHAM IMRAN 2020F-SE-111
const executiveStudent1 = new ExecutiveStudent(3, 'Areesha Imran','2004-04-
17');
console.log(executiveStudent1);
executiveStudent1.attendTheory();
createTable();
insertData('students', {name: 'MAHAM IMRAN', dob: '2003-02-25'});
insertData('students', {name: 'ZAYN KHAN', dob: '2000-08-07'});
selectAll('students' , rows => console.log(rows));
closeConnection();
fetchAboutPage();

const axios = require('axios');


const fetchAboutPage = () => {
const url = 'https://www.ssuet.edu.pk/';
axios
.get(url)
.then(response => {
console.log(response.data);
})
.catch(error => {
console.log(err.message);
});
// Maham Imran 2020F-SE-111
};
Page | 2
NAME: Maham Imran
ROLL NO: 2020F-BSE-111.

module.exports = {fetchAboutPage};

const sqlite3=require ('sqlite3').verbose();


const DB_PATH='.students.db';
const db=new sqlite3.Database(DB_PATH, err => {
if (err) {
console.error(err.message);
}
console.log('Connected to the student database.');
});
// Maham Imran 2020F-SE-111
const createTable = () => {
const sql = `CREATE TABLE IF NOT EXISTS students (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT null,
dob TEXT NOT NULL
)`;
db.run(sql, err => {
if (err) {
console.error(err.message);
}
console.log('Table students created successfully!');
});
};
// Maham Imran 2020F-SE-111
const insertData = (table, data) => {
const placeholders = Objects.keys(data).map(()=> '?').join(', ');
const sql = `INSERT INTO ${table} (${Object.keys(data)}) VALUES
(${placeholders})`;
const values = Object.values(data);
db.run(sql, values, err => {
if (err) {
console.error(err.message);
}
console.log('Data inserted successfully!');
});
};
// Maham Imran 2020F-SE-111
const selectAll = (table, callback) => {
const sql = `SELECT * FROM &{table}`;
db.all(sql, [], (err,rows) => {
if (err) {
console.error(err.message);
}
callback(rows);
});
};
Page | 3
NAME: Maham Imran
ROLL NO: 2020F-BSE-111.

const closeConnection = () => {


db.close(err => {
if (err) {
console.error(err.message);
}
console.log('Closed the database connection.');
});
// Maham Imran 2020F-SE-111
};
module.exports= { createTable, insertData, selectAll, closeConnection };

OUTPUT:

Page | 4
NAME: Maham Imran
ROLL NO: 2020F-BSE-111.

HOME TASK

TASK 01: 1 Classes and Inheritance:


a. Create a class called Course and export it
i. Add id, name, credit hours properties
b. Create a class called University and export it
i. Add name, image properties
ii. Add a method callesetImage
c. Modify the Student class
i. Add a property called university of type University class
ii. Add a property called courses of an array of Course class
iii Add a method called addCourse accepting Course class object and set that onto courses
property
iv Add a method called belongsToUniversity accepting University class object and set that
onto university property

2. HTTP and Database:


a. Modify the University class’s setImage method
i. Fetch the SSUET logo from the SSUET website using axios GET method and set it to image
property
ii. Create a new instance and call setImage method on the instance and validate using console.log if
the image property has been set
b. Create DB, open connection and perform CRUD operations for University using
sqlite library
i.Create and open database
ii.Create University table using properties from University class created above
iii. Create two University instances iv. Call setImage method on both the
instances
iv. Insert both the instances using their name and image properties

SOURCE CODE:
class course{
constructor(id, name, creditHours){ this.id = id,
this.name = name, this.creditHours = creditHours
}
}
//MAHAM IMRAN 2020F-SE-111
export default Course

import Course from "./course.js"


import University from "./university.js"
class Student{
constructor(id, name, age){ this.id = id,
this.name = name, this.age = age, this.university = null,
this.courses = []
}
addCourse(course) {
Page | 5
NAME: Maham Imran
ROLL NO: 2020F-BSE-111.

if(course instanceof Course){ this.courses = courses.push(course)


}
//MAHAM IMRAN 2020F-SE-111
else{ Error("INVALID TYPE")}
}
belongsToUniversity (university) { if(university instanceof
University){
this.university = university
}
else{ Error("INVALID TYPE")}
}
}
export default Student

import { selectRecord, insertRecord } from "./controllers/university.js"


import connection from "./connectionToDB.js";
class University { constructor(name, imageURL)
{
this.name = name, this.image = imageURL
}
getImage() { return this.image }
async performStudentDBOperations() { connection()
insertRecord(this.name, this.image)
selectRecord(this.name) }
}
//MAHAM IMRAN 2020F-SE-111
export default University

import mongoose from "mongoose"; const connection = () => {


mongoose.connect("mongodb://127.0.0.1:27017/lab5")
.then(() => console.log("CONNECTED TO DATABASE"))
.catch((error) => console.log(error))
}
export default connection
//MAHAM IMRAN 2020F-SE-111

import mongoose from "mongoose";


const UniversitySchema = mongoose.Schema({ name: String,
imageURL: String,
})
//MAHAM IMRAN 2020F-SE-111
const UniversityData = mongoose.model("University", UniversitySchema)
export default UniversityData

import assert from 'assert';


import University from './university.js'; function testDescribe(){

Page | 6
NAME: Maham Imran
ROLL NO: 2020F-BSE-111.

const uni = new University('SSUET', './public/logo.jpg');


uni.performStudentDBOperations()
console.log(`Running Test`);
console.log('==================================');
assert.ok(uni.name, 'SSUET', 'TEST PASSED');
assert.ok(uni.image,'./public/logo.jpg', 'TEST PASSED');
}
//MAHAM IMRAN 2020F-SE-111
export default testDescribe

OUTPUT:

Page | 7

You might also like