Solved programming problem

Create an Address Book application, where a user can create new record, update record, delete record

Source code

Main Method:

 

package address;

import java.util.Scanner;

/**

 *

 * @author Sabit

 */

public class AddressTest 

{

    public static void main(String[] args)

    {    

        Scanner S = new Scanner(System.in);

         String name;

         int p,h,n;

         

         ///first input

         System.out.printf("Name: ");

         name = S.nextLine();

         System.out.printf("House Number: "); 

         h= S.nextInt();

         System.out.printf("Phone Number: ");

         p = S.nextInt();

        

        Address A = new Address(name,h,p); ///initial object create

        

        n=1;

        while(n!=0)

        {  

            System.out.println("Enter 1 for Create new
2 for update
3 for delete
4 for Display
 0 to Exit");

            n = S.nextInt();

            

            if(n==1)

            {

                A.createNew();

            }

            else if(n==2)

            {

                A.updateRecord();

            }

            else if(n==3)

            {

                A.deleteRecord();

            }

            else if(n==4)

            {

                A.display();

            }

                    

        }

              

    }

}

 

Address Class:

 

package address;

import java.util.Scanner;

 

/**

 *

 * @author Sabit

 */

public class Address {

 

    private String name;

    private int house_number;

    private int phone;

    Scanner S = new Scanner(System.in);

 

    public Address(String name, int house, int phone) {

        this.name = name;

        this.house_number = house;

        this.phone = phone;

    }

 

    public void createNew() {

        System.out.printf("Name: ");

        name = S.nextLine();

        System.out.printf("House Number: ");

        house_number = S.nextInt();

        System.out.printf("Phone Number: ");

        phone = S.nextInt();

    }

 

    public void updateRecord() {

        int a;

 

        System.out.println("Press 1 to Update name
Press 2 to Update House Number
Press 3 to Update Phone Number");

 

        a = S.nextInt();

 

        if (a == 1) {

            Scanner SS = new Scanner(System.in); ///create another Scanner object 

            System.out.printf("Name: ");

            name = SS.nextLine();

        }

        if (a == 2) {

            System.out.printf("House Number: ");

            house_number = S.nextInt();

        }

        if (a == 3) {

            System.out.printf("Phone Number: ");

            phone = S.nextInt();

        }

    }

 

    public void deleteRecord() {

        this.name = "";

        this.house_number = 0;

        this.phone = 0;

 

    }

 

    public void display() {

        System.out.println("Name: " + name);

        System.out.println("House Number: " + house_number);

        System.out.println("Phone Number: " + phone);

    }

}
Join the discussion

Add your comment