Customer Address Using String Builder

 ADDRESS.JAVA:

public class Address {
    private String line1;
    private String line2;
    private String city;
    private String country;
    private int zipCode;
    

    public Address(String line1, String line2, String city, String country, int zipCode) {
		super();
		this.line1 = line1;
		this.line2 = line2;
		this.city = city;
		this.country = country;
		this.zipCode = zipCode;
	}

	public Address() {
		// TODO Auto-generated constructor stub
	}

	public String getLine1() {
        return line1;
    }

    public void setLine1(String line1) {
        this.line1 = line1;
    }

    public String getLine2() {
        return line2;
    }

    public void setLine2(String line2) {
        this.line2 = line2;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public int getZipCode() {
        return zipCode;
    }

    public void setZipCode(int zipCode) {
        this.zipCode = zipCode;
    }
    public String toString(){
      return "Address Details :\n"+getLine1()+","+"\n"+getLine2()+","+"\n"+getCity()+" - "+getZipCode()+"\n"+getCountry();
    }

}




MAIN.JAVA:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter Address Details :\n" +
                "Enter Line 1 :");
        StringBuilder l1  = new StringBuilder(sc.nextLine());

        System.out.println("Enter Line 2 :");
        StringBuilder l2  = new StringBuilder(sc.nextLine());
        System.out.println("Enter City :");
        StringBuilder city  = new StringBuilder(sc.nextLine());
     
        System.out.println("Enter Country :");
        StringBuilder cont  = new StringBuilder(sc.nextLine());
        System.out.println("Enter Zip Code :");
        StringBuilder zip  = new StringBuilder(sc.nextLine());
        Address ad = new Address();
        ad.setLine1(l1.toString());
        ad.setLine2(l2.toString());
        ad.setCity(city.toString());
        ad.setCountry(cont.toString());
        ad.setZipCode(Integer.parseInt((zip.toString())));



        System.out.println(ad);





    }
}

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.