Unit 2 Assignment FullStack
Unit 2 Assignment FullStack
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: