0% found this document useful (0 votes)
2 views7 pages

Unit 2 Assignment FullStack

The document outlines a mini project assignment for a full stack development course, which includes creating a Customer class with validation and a TestCustomer class for user input. It also describes a MyThread class that extends the Thread class and a ThreadDemo class to demonstrate multithreading behavior. The provided code snippets implement these functionalities in Java, handling exceptions and thread execution.

Uploaded by

abcalok56
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Unit 2 Assignment FullStack

The document outlines a mini project assignment for a full stack development course, which includes creating a Customer class with validation and a TestCustomer class for user input. It also describes a MyThread class that extends the Thread class and a ThreadDemo class to demonstrate multithreading behavior. The provided code snippets implement these functionalities in Java, handling exceptions and thread execution.

Uploaded by

abcalok56
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Unit 2 Assignment

Name : Verma Alokkumar Amardayal


Course : MCA
Roll no : 202410116100239
Subject : Mini Project-2(Full Stack Development)
Section :D
Q1)Create a class Customer having following members:private String custNoprivate String
custNameprivate String category Parameterized constructor to initialize all instance
variablesGetter methods for all instance variables Perform following validations in the
constructor- custNo must start with ‘C’ or ‘c’- custName must be atleast of 4 characters-
category must be either ‘Platinum’, ‘Gold’ or ‘Silver ‘ When any of these validations fail, then
raise a user defined exception InvalidInputException Create a class TestCustomer having main
method. Ask user to enter customer details. Create an object of Customer and perform
validations. Print details of customer.

Code:
Customer.java
package javalab;
public class Customer {
private String custNo;
private String custName;
private String category;
public Customer(String custNo, String custName, String category) throws InvalidInputException
{
if (!custNo.startsWith("C") && !custNo.startsWith("c")) {
throw new InvalidInputException("Customer number must start with 'C' or 'c'.");
}
if (custName.length() < 4) {
throw new InvalidInputException("Customer name must be at least 4 characters long.");
}
if (!category.equals("Platinum") && !category.equals("Gold") && !category.equals("Silver")) {
throw new InvalidInputException("Category must be either 'Platinum', 'Gold', or 'Silver'.");
}
this.custNo = custNo;
this.custName = custName;
this.category = category;
}
public String getCustNo() {
return custNo;
}
public String getCustName() {
return custName;
}
public String getCategory() {
return category;
}
}
class InvalidInputException extends Exception {
public InvalidInputException(String message) {
super(message);
}
}

TestCusatomer.java
package javalab;
import java.util.Scanner;
public class TestCustomer {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
System.out.print("Enter customer number: ");
String custNo = scanner.nextLine();
System.out.print("Enter customer name: ");
String custName = scanner.nextLine();
System.out.print("Enter customer category (Platinum/Gold/Silver): ");
String category = scanner.nextLine();
try {
Customer customer = new Customer(custNo, custName, category);
System.out.println("Customer Details:");
System.out.println("Customer Number: " + customer.getCustNo());
System.out.println("Customer Name: " + customer.getCustName());
System.out.println("Customer Category: " + customer.getCategory());
} catch (InvalidInputException e) {
System.out.println("Error: " + e.getMessage());
}
scanner.close();
}
}

Output:
Q2) Create a class MyThread derived from Thread class and override the run method. Create a
class ThreadDemo having main method. Create 2 objects of MyThread class and observe the
behavior of threads.
Code:

MyThread.java
package javalab;
public class MyThread extends Thread{
private String threadName;
public MyThread(String threadName) {
this.threadName=threadName;
}
@Override
public void run() {
for(int i=1;i<=5;i++) {
System.out.println(threadName+" - Count: "+i);
try {
Thread.sleep(500);
}catch(InterruptedException e) {
System.out.println(threadName+" interrupted.");
}
}
System.out.println(threadName+ " finished.");
}
}
ThreadDemo.java

package javalab;
public class ThreadDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyThread thread1=new MyThread("Thread 1");
MyThread thread2=new MyThread("Thread 2");
thread1.start();
thread2.start();
try {
thread1.join();
thread2.join();
}catch(InterruptedException e){
System.out.println("Main thread interupted.");
}
System.out.println("Main thread finished.");
}
}

Output:

You might also like