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

code2pdf_6741f6dfb17a2

coding

Uploaded by

umernisar053
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)
15 views

code2pdf_6741f6dfb17a2

coding

Uploaded by

umernisar053
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/ 16

// class Simple{

// protected double num1;


// protected double num2;

// Simple(){

// }

// Simple(double num1 , double num2){


// this.num1 = num1;
// this.num2 = num2;

// }
// void setNum1(double num1){
// this.num1 = num1;
// }

// void setNum2(double num2){


// this.num2=num2;
// }

// double getNum1(){
// return num1;
// }

// double getNum2(){
// return num2;
// }

// void add(){
// System.out.println("Number1 + Number2 :"+ (num1+num2));
// }

// void mul(){
// System.out.println("Number1 * Number2 :"+ num1*num2);
// }

// void div(){
// System.out.println("Number1 / Number2 :"+ num1/num2);
// }

// void sub(){
// System.out.println("Number1 - Number2 :"+ (num1-num2));
// }

// }

// class VerifiedSimple extends Simple{

// VerifiedSimple(){
// super();
// }

// VerifiedSimple(double num1 , double num2){


// super(num1 , num2);
// }

// void add(){

// if(super.num1 > 0 && super.num2>0){

// super.add();

// }else{
// System.out.println("Both numbers shoulb be greater than zero.");
// }

// }

// void sub(){

// if(super.num1 > 0 && super.num2>0){

// super.sub();

// }else{
// System.out.println("Both numbers shoulb be greater than zero.");
// }

// }

// void mul(){

// if(super.num1 > 0 && super.num2>0){

// super.mul();

// }else{
// System.out.println("Both numbers shoulb be greater than zero.");
// }

// }

// void div(){

// if(super.num1 > 0 && super.num2>0){

// super.div();

// }else{
// System.out.println("Both numbers shoulb be greater than zero.");
// }

// }

// }

// public class Exception{


// public static void main(String[] args) {

// Simple s1 = new VerifiedSimple(10.1,19);

// s1.add();

// s1.sub();

// s1.mul();

// s1.div();

// Simple s2 = new VerifiedSimple();

// s2.setNum1(10);
// s2.setNum2(10);

// s2.add();
// s2.mul();
// s2.div();
// s2.sub();

// }
// }

// abstract class Demo{

// abstract void m1();


// abstract void m2();
// abstract void m3();

// }

// abstract class One extends Demo{

// void m1(){
// System.out.println("M1");
// }

// }
// class Two extends One{

// void m2(){
// System.out.println("M2");
// }

// void m3(){
// System.out.println("M3");
// }
// }

// class Main{
// public static void main(String[] args) {
// Two t1 = new Two();

// t1.m1();
// t1.m2();
// t1.m3();
// }
// }

// abstract class Shapes {


// protected int noOfLines;
// protected String penColor;
// protected String fillColor;

// Shapes() {
// this.noOfLines = 0;
// this.penColor = "Black";
// this.fillColor = "White";
// }

// Shapes(int noOfLines, String penColor, String fillColor) {


// this.noOfLines = noOfLines;
// this.penColor = penColor;
// this.fillColor = fillColor;
// }

// public int getNoOfLines() {


// return noOfLines;
// }

// public void setNoOfLines(int noOfLines) {


// this.noOfLines = noOfLines;
// }

// public String getPenColor() {


// return penColor;
// }

// public void setPenColor(String penColor) {


// this.penColor = penColor;
// }
// public String getFillColor() {
// return fillColor;
// }

// public void setFillColor(String fillColor) {


// this.fillColor = fillColor;
// }

// public abstract void draw();


// }

// class Rectangle extends Shapes{

// Rectangle(){
// super();
// }

// Rectangle(int noOfLines , String penColor , String fillColor){


// super(noOfLines, penColor, fillColor);
// }

// public void draw(){


// System.out.println("No of lines :" + noOfLines);
// System.out.println("Pen Color :" + penColor);
// System.out.println("Fill Color :" + fillColor);
// }

// }

// class Square extends Shapes{

// Square(){
// super();
// }

// Square(int noOfLines , String penColor , String fillColor){


// super(noOfLines, penColor, fillColor);
// }

// public void draw(){


// System.out.println("No of lines :" + noOfLines);
// System.out.println("Pen Color :" + penColor);
// System.out.println("Fill Color :" + fillColor);
// }

// }

// class Circle extends Shapes{

// Circle(){
// super();
// }

// Circle(int noOfLines , String penColor , String fillColor){


// super(noOfLines, penColor, fillColor);
// }

// public void draw(){


// System.out.println("No of lines :" + noOfLines);
// System.out.println("Pen Color :" + penColor);
// System.out.println("Fill Color :" + fillColor);
// }

// }

// class Main{
// public static void main(String[] args) {

// Shapes s1 = new Circle(1, "Red", "Red");


// s1.draw();

// Shapes s2 = new Square(4, "Blue", "Blue");


// s2.draw();

// Shapes s3 = new Rectangle();

// s3.setNoOfLines(10);
// s3.setFillColor("Orange");
// s3.setPenColor("Orange");

// s3.draw();

// }
// }

// class Shape{

// void draw(){
// System.out.println("I am in Shape");
// }
// }

// class Circle extends Shape{

// void draw(){
// System.out.println("I am in circle");
// }

// void circle(){
// System.out.println("Circle");
// }
// }

// class Square extends Shape{

// void draw(){
// System.out.println("I am in Square");
// }

// void square(){
// System.out.println("Square");
// }
// }

// class Rectangle extends Shape{

// void draw(){
// System.out.println("I am in Rectangle");
// }

// void rectangle(){
// System.out.println("Rectangle");
// }
// }

// class Main{
// public static void main(String[] args) {

// Shape s1 = new Circle();


// s1.draw();

// Shape s2 = new Rectangle();


// s2.draw();

// Shape s3 = new Square();


// s3.draw();

// Shape[] shapes = {s1,s2,s3};

// for(int i=0; i<shapes.length;i++){


// if(shapes[i] instanceof Circle){
// ((Circle)shapes[i]).circle();
// }else if(shapes[i] instanceof Square){
// ((Square)shapes[i]).square();
// }else if(shapes[i] instanceof Rectangle){
// ((Rectangle)shapes[i]).rectangle();
// }
// }

// }
// }

// class OverFlow {
// public static void test(int i) {

// if (i == 0)
// return;
// else {
// System.out.println(i);
// test(i++);
// }
// }
// }

// class Exception {
// public static void main(String[] args) {

// OverFlow.test(5);

// }
// }

// class Exception{
// static int divideByZero(int a, int b) {
// int i = a / b;
// return i;
// }

// static int computeDivision(int a, int b) {


// int res = 0;
// try {
// res = divideByZero(a, b);
// }
// catch (ArithmeticException ex) {
// System.out.println("ArithmeticException occurred: " + ex.getMessage());
// }
// return res;
// }

// public static void main(String[] args) {

// System.out.println(computeDivision(10, 0));

// }
// }

// class throwException {

// static void fun() throws NullPointerException{


// try {
// throw new NullPointerException("Null Exception");
// } catch (NullPointerException e) {
// System.out.println("Caiught in throwException");
// System.out.println(e.getMessage());
// throw e;

// }

// }
// }

// class Exception {
// public static void main(String[] args) {

// try{
// throwException.fun();
// }catch(NullPointerException e){
// System.out.println("Caught in main");
// System.out.println(e.getMessage());
// }

// }
// }

// All Important Exceptions

// class Exception{
// public static void main(String[] args) {

// int a = 10 ;
// int b = 0 ;

// try{
// int c = a/b;
// System.out.println(c);
// }catch(ArithmeticException e){
// System.out.println(e.getMessage());
// }

// String s = null;

// try{
// System.out.println(s.length());

// }catch(NullPointerException e){
// System.out.println(e.getMessage());

// }

// String c = "abc";

// try{

// int d = Integer.parseInt(c);
// System.out.println(d);

// }catch(NumberFormatException e){
// System.out.println(e.getMessage());
// }

// int[] array = new int[10];

// try{
// array[11] = 12;

// }catch(ArrayIndexOutOfBoundsException e){
// System.out.println(e.getMessage());
// }

// }
// }

//User Defined Exceptions

// class MyException extends Throwable{

// private String str1;

// MyException(String str1){

// this.str1= str1;

// }

// public String toString(){


// return "My Exception Occured :" + str1;
// }
// }

// class Exception{
// public static void main(String[] args) {

// try{
// System.out.println("In try block.");
// throw new MyException("My Exception.");
// }catch(MyException exp){
// System.out.println("In catch block.");
// System.out.println(exp);
// }
// }
// }
//One Practice Program

// Step 1: Account interface


// interface Account {
// void deposit(double amount) throws InvalidAmountException;
// void withdraw(double amount) throws InsufficientFundsException, InvalidAmountException;
// double getBalance();
// }

// // Step 2: Custom Exception Classes


// class InsufficientFundsException extends Exception {
// public InsufficientFundsException(String message) {
// super(message);
// }
// }

// class InvalidAmountException extends Exception {


// public InvalidAmountException(String message) {
// super(message);
// }
// }

// // Step 3: BankAccount class implementing Account interface


// class BankAccount implements Account {
// private double balance;

// // Constructor to initialize balance


// public BankAccount(double initialBalance) {
// this.balance = initialBalance;
// }

// // Deposit method with exception handling


// @Override
// public void deposit(double amount) throws InvalidAmountException {
// if (amount <= 0) {
// throw new InvalidAmountException("Deposit amount must be positive.");
// }
// balance += amount;
// System.out.println("Deposited: $" + amount);
// }

// // Withdraw method with exception handling


// @Override
// public void withdraw(double amount) throws InsufficientFundsException, InvalidAmountException {
// if (amount <= 0) {
// throw new InvalidAmountException("Withdrawal amount must be positive.");
// }
// if (amount > balance) {
// throw new InsufficientFundsException("Insufficient funds. Your balance is $" + balance);
// }
// balance -= amount;
// System.out.println("Withdrew: $" + amount);
// }

// // Get current balance


// @Override
// public double getBalance() {
// return balance;
// }
// }

// // Step 4: Test Class


// public class TestBankAccount {
// public static void main(String[] args) {
// BankAccount account = new BankAccount(500.00);

// try {
// System.out.println("Initial balance: $" + account.getBalance());

// // Test valid deposit


// account.deposit(200.00);
// System.out.println("Balance after deposit: $" + account.getBalance());

// // Test invalid deposit (negative amount)


// account.deposit(-50.00); // This should throw InvalidAmountException

// } catch (InvalidAmountException e) {
// System.out.println("Error: " + e.getMessage());
// }

// try {
// // Test valid withdrawal
// account.withdraw(100.00);
// System.out.println("Balance after withdrawal: $" + account.getBalance());

// // Test invalid withdrawal (more than balance)


// account.withdraw(700.00); // This should throw InsufficientFundsException

// } catch (InsufficientFundsException e) {
// System.out.println("Error: " + e.getMessage());
// } catch (InvalidAmountException e) {
// System.out.println("Error: " + e.getMessage());
// }

// try {
// // Test invalid withdrawal (negative amount)
// account.withdraw(-50.00); // This should throw InvalidAmountException

// } catch (InsufficientFundsException e) {
// System.out.println("Error: " + e.getMessage());
// } catch (InvalidAmountException e) {
// System.out.println("Error: " + e.getMessage());
// }
// }

// }

// CompareAble and Cloneable Interface Practice

// class Circle implements Comparable<Circle>{


// private int radius;

// Circle(){

// }

// @Override
// public int compareTo(Circle c){

// if(this.radius> c.radius){
// return 1;
// }
// else if(this.radius < c.radius){
// return -1;
// }else{
// return 0;
// }

// }

// Circle(int radius ){
// this.radius = radius;
// }

// void setRadius(int radius){


// this.radius = radius;
// }

// int getRadius(){
// retur radius;
// }

// int calculateArea(){
// return 3.14*radius*radius;

// }

// }

// class Main{
// public static void main(String[] args) {

// Circle c1 = new Circle(10);

// Circle c2 = new Circle(12);

// System.out.println(c1.compareTo(c2));

// }
// }

//Cloneable

// class Circle implements Cloneable {

// int radius;

// Circle() {
// }

// Circle(int radius) {
// this.radius = radius;
// }

// void display() {
// System.out.println("Radius is: " + radius);
// }

// @Override
// protected Object clone() throws CloneNotSupportedException {
// return super.clone();
// }
// }

// public class Main {


// public static void main(String[] args) {
// Circle c1 = new Circle(10);

// try {
// Circle c2 = (Circle) c1.clone();
// c2.display();
// } catch (CloneNotSupportedException e) {
// e.printStackTrace();
// }
// }
// }

// CloneAble Pratice

// class Circle implements Cloneable{


// int radius;
// Circle(){

// }
// Circle(int radius){
// this.radius = radius;
// }

// void display(){
// System.out.println("Radius is :" + radius);

// }

// @Override
// protected Object clone() throws CloneNotSupportedException{
// return super.clone();
// }

// }
// public class Main {

// public static void main(String[] args) {

// Circle c1 = new Circle(10);

// try{
// Circle c2 = (Circle) c1.clone();
// c2.display();
// }catch(CloneNotSupportedException e){
// System.out.println(e.getMessage());
// }

// }
// }

// import java.util.ArrayList;

// class Main{
// public static void main(String[] args) {

// ArrayList<Integer> arrayList = new ArrayList<>();

// arrayList.add(0);
// arrayList.add(1);
// arrayList.add(2);
// arrayList.add(3);
// arrayList.add(5);

// System.out.println(arrayList);

// arrayList.remove(4);

// System.out.println(arrayList);

// arrayList.set(0, 5);

// System.out.println(arrayList);

// System.out.println(arrayList.size());

// System.out.println(arrayList.get(0));

// for(int i=0; i<arrayList.size() ;i++){


// System.out.println(arrayList.get(i));
// }

// }

// }

// Exception Handling Practice

// Custom Exceptions
// class CheckedException extends Exception{
// CheckedException(){

// }
// CheckedException(String message){
// super(message);
// }
// }

// class InsufficientFundsException extends CheckedException {


// public InsufficientFundsException() {
// super();
// }

// public InsufficientFundsException(String message) {


// super(message);
// }
// }

// class MinimumBalanceException extends CheckedException {


// public MinimumBalanceException() {
// super();
// }

// public MinimumBalanceException(String message) {


// super(message);
// }
// }
// class OverdraftLimitExceedException extends CheckedException {
// public OverdraftLimitExceedException() {
// super();
// }

// public OverdraftLimitExceedException(String message) {


// super(message);
// }
// }

// class BankAccount {
// protected double balance;

// public BankAccount() {}

// public BankAccount(double balance) {


// this.balance = balance;
// }

// public void setBalance(double balance) {


// this.balance = balance;
// }

// public double getBalance() {


// return this.balance;
// }

// public void withdraw(double amount) throws CheckedException {


// if (amount > balance) {
// throw new InsufficientFundsException("Insufficient funds for the withdrawal.");
// }
// System.out.println("Bank Account");
// System.out.println("Bank Account Balance :" + balance);
// balance -= amount;
// System.out.println("Successfully withdrew: " + amount);
// System.out.println("Remaining balance: " + balance);
// }
// }

// class SavingAccount extends BankAccount {


// private double minimumBalance;

// public SavingAccount() {
// super();
// }

// public SavingAccount(double balance, double minimumBalance) {


// super(balance);
// this.minimumBalance = minimumBalance;
// }

// public void setMinimumBalance(double minimumBalance) {


// this.minimumBalance = minimumBalance;
// }

// public double getMinimumBalance() {


// return minimumBalance;
// }

// @Override
// public void withdraw(double amount) throws CheckedException {
// if (super.getBalance() - amount < minimumBalance) {
// throw new MinimumBalanceException("Cannot withdraw below the minimum balance.");
// }

// super.withdraw(amount);
// }
// }

// class CurrentAccount extends BankAccount {


// private double overdraftLimit;

// public CurrentAccount() {
// super();
// }

// public CurrentAccount(double balance, double overdraftLimit) {


// super(balance);
// this.overdraftLimit = overdraftLimit;
// }

// public void setOverdraftLimit(double overdraftLimit) {


// this.overdraftLimit = overdraftLimit;
// }

// public double getOverdraftLimit() {


// return overdraftLimit;
// }

// @Override
// public void withdraw(double amount) throws CheckedException {
// if (super.getBalance() - amount < -overdraftLimit) {
// throw new OverdraftLimitExceedException("Cannot withdraw beyond the overdraft limit.");
// }

// System.out.println("Current Account");
// System.out.println("Current Account Balance :" + super.balance);
// super.withdraw(amount);
// }
// }
// public class Main {
// public static void main(String[] args) {
// BankAccount b1 = new BankAccount(100000.0);
// try {
// b1.withdraw(3400);
// } catch (InsufficientFundsException e) {
// System.out.println(e.getMessage());
// }catch (CheckedException e) {
// System.out.println(e.getMessage());
// }

// SavingAccount s1 = new SavingAccount(1000, 200);


// try {
// s1.withdraw(900);
// }catch(MinimumBalanceException e){
// System.out.println(e.getMessage());
// }
// catch (CheckedException e) {
// System.out.println(e.getMessage());
// }

// CurrentAccount a1 = new CurrentAccount(2000, 1000);


// try {
// a1.withdraw(2000);
// } catch (OverdraftLimitExceedException e) {
// System.out.println(e.getMessage());
// }catch(CheckedException e){
// System.out.println(e.getMessage());
// }
// }
// }

// Another Exception Handling Question


// class CheckedExceptions extends Exception{
// CheckedExceptions(){
// super();
// }
// CheckedExceptions(String message){
// super(message);
// }
// }

// class InvalidSalaryException extends CheckedExceptions{


// InvalidSalaryException(){
// super();
// }
// InvalidSalaryException(String messasge){
// super(messasge);
// }
// }

// class InvalidTeamSizeException extends CheckedExceptions{


// InvalidTeamSizeException(){
// super();
// }
// InvalidTeamSizeException(String messasge){
// super(messasge);
// }
// }

// class InvalidCodeCountException extends CheckedExceptions{


// InvalidCodeCountException(){
// super();
// }
// InvalidCodeCountException(String messasge){
// super(messasge);
// }
// }

// class Employee {
// protected int id;
// protected String name;
// protected double salary;

// Employee() {}

// Employee(int id, String name, double salary) throws CheckedExceptions{


// this.id = id;
// this.name = name;
// if (salary <= 0) {
// throw new InvalidSalaryException("Salary must be greater than zero.");
// }
// this.salary = salary;
// }

// public int getId() {


// return id;
// }

// public void setId(int id) {


// this.id = id;
// }

// public String getName() {


// return name;
// }

// public void setName(String name) {


// this.name = name;
// }
// public double getSalary() {
// return salary;
// }

// public void setSalary(double salary) throws CheckedExceptions{


// if (salary <= 0) {
// throw new InvalidSalaryException("Salary must be greater than zero.");
// }
// this.salary = salary;
// }

// double calculateBonus(){
// return (0.1)*salary;
// }

// void printDetails(){
// System.out.println("ID :" + id + ", Name :"+ name + ", Salary :"+ salary);
// System.out.println("Bonus :" +this.calculateBonus());
// }
// }

// class Manager extends Employee{

// private int teamSize;

// Manager(){
// super();
// }

// Manager(int id, String name, double salary, int teamSize) throws CheckedExceptions{
// super(id, name, salary);
// if(teamSize<0){
// throw new InvalidTeamSizeException("Invalid team size.");
// }
// this.teamSize = teamSize;
// }

// void setTeamSize(int teamSize) throws CheckedExceptions{


// if(teamSize<0){
// throw new InvalidTeamSizeException("Invalid team size.");
// }
// this.teamSize = teamSize;
// }

// int getTeamSize(){

// return teamSize;
// }

// public double calculateBonus(){


// return (0.20)+ teamSize *100;
// }
// void printDetails(){
// super.printDetails();
// }
// }

// class Developer extends Employee{


// private int linesOfCode;

// Developer(){
// super();
// }

// Developer(int id, String name, double salary, int linesOfCode) throws CheckedExceptions{
// super(id, name, salary);
// if(linesOfCode<0){
// throw new InvalidCodeCountException("Invalid Code lines.");
// }
// this.linesOfCode = linesOfCode;
// }

// void setLinesOfCode(int linesOfCode) throws CheckedExceptions{


// if(linesOfCode<0){
// throw new InvalidCodeCountException("Invalid Code lines.");
// }
// this.linesOfCode = linesOfCode;

// }

// int getLinesOfCode(){
// return linesOfCode;
// }

// public double calculateBonus(){


// return (0.15)+ linesOfCode * 0.5;
// }

// void printDetails(){
// super.printDetails();
// }
// }
// public class Main{
// public static void main(String[] args) {

// try{
// Employee e1 = new Employee(411989, "umer", 10000);
// System.out.println("Employee Details");
// e1.printDetails();
// }catch(InvalidSalaryException e){
// System.out.println(e.getMessage());
// }catch(CheckedExceptions e){
// System.out.println(e.getMessage());
// }

// try{
// Manager m1 = new Manager(512345, "Talha", 20000, 12);
// System.out.println("Manager Details");
// m1.printDetails();
// }catch(InvalidTeamSizeException e){
// System.out.println(e.getMessage());
// }catch(CheckedExceptions e){
// System.out.println(e.getMessage());
// }

// try{
// Developer d1 = new Developer(512345, "Talha", 25000, 1600);
// System.out.println("Developer Details");
// d1.printDetails();
// }catch(InvalidCodeCountException e){
// System.out.println(e.getMessage());
// }catch(CheckedExceptions e){
// System.out.println(e.getMessage());
// }

// }
// }

// polymorphism and abstraction combined question

// abstract class PaymentMethod{


// protected double amount;

// PaymentMethod(){

// }
// PaymentMethod(double amount){
// this.amount = amount;
// }

// abstract void processPayment();

// String getPaymentDetails(){
// return "Amount :" + amount;
// }
// }

// class CreditCard extends PaymentMethod{


// private String creditCardNumber;
// private String expiryDate;

// CreditCard(){

// super();

// }
// CreditCard(double amount,String creditCardNumber, String expiryDate){
// super(amount);
// this.creditCardNumber = creditCardNumber;
// this.expiryDate = expiryDate;
// }

// void processPayment(){
// System.out.println("Payment through credit card.");
// }

// String getPaymentDetails(){
// System.out.println("Credit Card");
// return super.getPaymentDetails() + ", Credit card number :" + creditCardNumber + ", Expiry date :"+ expiryDate;
// }

// }

// class PayPal extends PaymentMethod{


// private String email;

// PayPal(){

// super();

// }
// PayPal(double amount, String email){
// super(amount);
// this.email = email;

// }

// void processPayment(){
// System.out.println("Payment through paypal.");
// }

// String getPaymentDetails(){
// System.out.println("PayPal");
// return super.getPaymentDetails() + ", Email :" + email;

// }

// }

// class BankTransfer extends PaymentMethod{


// private String bankAccountNumber;
// private String bankName;
// BankTransfer(){
// super();
// }

// BankTransfer(double amount , String bankAccountNumber , String bankName){


// super(amount);
// this.bankAccountNumber = bankAccountNumber;
// this.bankName = bankName;
// }

// void processPayment(){
// System.out.println("Payment through bank transfer.");
// }

// String getPaymentDetails(){
// System.out.println("Bank Transfer");
// return super.getPaymentDetails() + ", Bank account number :" + bankAccountNumber + ", Bank name : "+ bankName;

// }

// }

// class Main{
// public static void main(String[] args) {

// PaymentMethod p1 = new CreditCard(20000, "908990", "22-10-2021");

// PaymentMethod p2 = new PayPal(12000 , "[email protected]");

// PaymentMethod p3 = new BankTransfer(100000, "512345", "Meezan Bank");

// PaymentMethod[] payments = {p1, p2, p3};

// for(int i=0 ;i<payments.length ;i++){


// System.out.println(payments[i].getPaymentDetails());
// payments[i].processPayment();
// }

// }
// }

//Student List Gui

// import javax.swing.*;
// import java.awt.*;
// import java.awt.event.*;
// import java.util.ArrayList;

// class Student {
// private String st_name;
// private int st_id;

// Student(String name, int id) {


// st_name = name;
// st_id = id;
// }

// String getName() {
// return st_name;
// }

// int getId() {
// return st_id;
// }

// @Override
// public String toString() {
// return "Name: " + st_name + ", ID: " + st_id;
// }
// }

// public class StudentManagerGUI {


// private JFrame frame;
// private JTextField nameField, idField, removeField;
// private JTextArea studentListArea;
// private ArrayList<Student> studentList;

// public StudentManagerGUI() {
// studentList = new ArrayList<>();
// initUI();
// }

// private void initUI() {


// frame = new JFrame("Student Manager");
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// frame.setSize(400, 500);
// frame.setLayout(new BorderLayout());

// // Top panel for adding students


// JPanel addPanel = new JPanel(new GridLayout(3, 2, 5, 5));
// addPanel.setBorder(BorderFactory.createTitledBorder("Add Student"));
// addPanel.add(new JLabel("Name:"));
// nameField = new JTextField();
// addPanel.add(nameField);
// addPanel.add(new JLabel("ID:"));
// idField = new JTextField();
// addPanel.add(idField);

// JButton addButton = new JButton("Add Student");


// addPanel.add(addButton);
// // Middle panel for displaying the student list
// JPanel displayPanel = new JPanel(new BorderLayout());
// displayPanel.setBorder(BorderFactory.createTitledBorder("Student List"));
// studentListArea = new JTextArea(10, 30);
// studentListArea.setEditable(false);
// JScrollPane scrollPane = new JScrollPane(studentListArea);
// displayPanel.add(scrollPane);

// // Bottom panel for removing students


// JPanel removePanel = new JPanel(new GridLayout(2, 2, 5, 5));
// removePanel.setBorder(BorderFactory.createTitledBorder("Remove Student"));
// removePanel.add(new JLabel("Name:"));
// removeField = new JTextField();
// removePanel.add(removeField);

// JButton removeButton = new JButton("Remove Student");


// removePanel.add(removeButton);

// // Add all panels to the main frame


// frame.add(addPanel, BorderLayout.NORTH);
// frame.add(displayPanel, BorderLayout.CENTER);
// frame.add(removePanel, BorderLayout.SOUTH);

// // Add student button functionality


// addButton.addActionListener(e -> {
// String name = nameField.getText().trim();
// String idText = idField.getText().trim();

// if (name.isEmpty() || idText.isEmpty()) {
// JOptionPane.showMessageDialog(frame, "Please fill in both fields.", "Error", JOptionPane.ERROR_MESSAGE);
// return;
// }

// try {
// int id = Integer.parseInt(idText);
// if (id < 0) {
// throw new NumberFormatException("ID must be positive.");
// }
// studentList.add(new Student(name, id));
// nameField.setText("");
// idField.setText("");
// updateStudentList();
// } catch (NumberFormatException ex) {
// JOptionPane.showMessageDialog(frame, "ID must be a valid positive number.", "Error", JOptionPane.ERROR_MESSAGE);
// }
// });

// // Remove student button functionality


// removeButton.addActionListener(e -> {
// String nameToRemove = removeField.getText().trim();
// if (nameToRemove.isEmpty()) {
// JOptionPane.showMessageDialog(frame, "Please enter the name of the student to remove.", "Error", JOptionPane.ERROR_MESSAGE);
// return;
// }

// int index = findStudentIndex(nameToRemove);


// if (index != -1) {
// studentList.remove(index);
// removeField.setText("");
// JOptionPane.showMessageDialog(frame, "Student removed successfully.", "Success", JOptionPane.INFORMATION_MESSAGE);
// updateStudentList();
// } else {
// JOptionPane.showMessageDialog(frame, "No student found with the name \"" + nameToRemove + "\".", "Error", JOptionPane.ERROR_MESSAGE);
// }
// });

// frame.setVisible(true);
// }

// private int findStudentIndex(String targetName) {


// for (int i = 0; i < studentList.size(); i++) {
// if (studentList.get(i).getName().equalsIgnoreCase(targetName)) {
// return i;
// }
// }
// return -1;
// }

// private void updateStudentList() {


// studentListArea.setText("");
// for (Student student : studentList) {
// studentListArea.append(student.toString() + "\n");
// }
// }

// public static void main(String[] args) {


// SwingUtilities.invokeLater(StudentManagerGUI::new);
// }
// }

//Array List Practice

// import java.util.*;

// class Student {
// private String name;
// private int id;

// Student() {}
// Student(String name, int id) {
// this.name = name;
// this.id = id;
// }

// void display() {
// System.out.println("Name: " + name);
// System.out.println("ID: " + id);
// }

// String getName() {
// return name;
// }

// int getId() {
// return id;
// }

// public boolean equals(Object obj) {


// if (this == obj) {
// return true;
// }
// if (obj == null || getClass() != obj.getClass()) {
// return false;
// }
// Student student = (Student) obj;
// return id == student.id && name.equalsIgnoreCase(student.name);
// }
// }

// class Main {
// public static Student createStudent(Scanner scanner, int count) {
// int id;
// String name;

// System.out.print("Enter Student " + count + " Name: ");


// name = scanner.nextLine();
// while (name.isEmpty()) {
// System.out.println("Name cannot be empty. Please provide a valid name.");
// System.out.print("Enter Student " + count + " Name: ");
// name = scanner.nextLine();
// }

// System.out.print("Enter Student " + count + " ID: ");


// id = scanner.nextInt();
// while (id < 0) {
// System.out.println("ID must be a positive number. Please provide a valid ID.");
// System.out.print("Enter Student " + count + " ID: ");
// id = scanner.nextInt();
// }
// scanner.nextLine();

// return new Student(name, id);


// }

// public static void displayStudents(List<Student> students) {


// for (int i = 0; i < students.size(); i++) {
// System.out.println("Student " + (i + 1) + ":");
// students.get(i).display();
// }
// }

// public static void removeStudent(Scanner scanner, List<Student> students) {


// System.out.print("Enter the name of the student to remove: ");
// String name = scanner.nextLine();

// System.out.print("Enter the ID of the student to remove: ");


// int id = scanner.nextInt();
// scanner.nextLine();

// Student targetStudent = new Student(name, id);

// if (students.remove(targetStudent)) {
// System.out.println("Successfully removed the student.");
// if (!students.isEmpty()) {
// System.out.println("Updated Student List:");
// displayStudents(students);
// } else {
// System.out.println("The student list is now empty.");
// System.out.print("Would you like to add new students (yes or no)? ");
// String response = scanner.nextLine();
// if (response.equalsIgnoreCase("yes")) {
// int count = 1;
// do {
// students.add(createStudent(scanner, count));
// System.out.print("Add another student (yes/no)? ");
// response = scanner.nextLine();
// count++;
// } while (response.equalsIgnoreCase("yes"));
// } else {
// System.out.println("Operation complete.");
// }
// }
// } else {
// System.out.println("No matching student found.");
// }
// }

// public static void main(String[] args) {


// List<Student> students = new ArrayList<>();
// Scanner scanner = new Scanner(System.in);
// String response;
// int count = 1;

// do {
// students.add(createStudent(scanner, count));
// System.out.print("Would you like to add another student (yes or no)? ");
// response = scanner.nextLine();
// count++;
// } while (response.equalsIgnoreCase("yes"));

// System.out.println("\nAll Registered Students:");


// displayStudents(students);

// System.out.print("\nWould you like to perform further operations (yes or no)? ");


// String proceed = scanner.nextLine();

// if (proceed.equalsIgnoreCase("yes")) {
// String action;
// String exitChoice;
// do {
// System.out.println("Available Actions:");
// System.out.println("1. Enter 'remove' to delete a student");
// System.out.println("2. Enter 'display' to view all students");

// action = scanner.nextLine();

// if (action.equalsIgnoreCase("remove")) {
// if(!students.isEmpty()){

// removeStudent(scanner, students);

// }else{
// System.out.println("No Students to display");
// }
// } else if (action.equalsIgnoreCase("display")) {
// if(!students.isEmpty()){

// displayStudents(students);

// }else{
// System.out.println("No Students to display");
// }

// } else {
// System.out.println("Invalid input. Please try again.");
// }

// System.out.print("Would you like to exit (yes or no)? ");


// exitChoice = scanner.nextLine();
// } while (exitChoice.equalsIgnoreCase("no"));
// }

// scanner.close();
// }
// }

You might also like