Program 2 StringTokenizer

Write  a  Java  class  called Customer  to  store  their  name  and  date_of_birth.  The date_of_birth  format  should  be  dd/mm/yyyy.  Write  methods  to  read  customer data  as  <name,  dd/mm/yyyy>  and  display  as  <name,  dd,  mm,  yyyy>  using StringTokenizer class considering the delimiter character as “/”.

1 package program2.tokenizer;
2 
3 import java.util.StringTokenizer;
4 
5 public class Customer {
6         private String name;
7         private String dateOfBirth;
8         
9         public Customer(String name, String dateOfBirth) {
10                super();
11                this.name = name;
12                this.dateOfBirth = dateOfBirth;
13        }
14        
15        void formatCustomerDOB() {
16                
17                String dateOfBirth = "";
18                
19                StringTokenizer tokenizer = new StringTokenizer(this.dateOfBirth, "/");
20                
21                    while(tokenizer.hasMoreTokens())
22                    {
23                           dateOfBirth += tokenizer.nextToken() + ", ";
24                    }
25                   
26                    this.dateOfBirth = dateOfBirth.substring(0, dateOfBirth.lastIndexOf(","));
27
28        }
29
30        @Override
31        public String toString() {
32                return "Customer [name=" + name + ", dateOfBirth=" + dateOfBirth + "]";
33        }
34        
35}
36
37package program2.tokenizer;
38
39import java.util.Scanner;
40
41public class TokenizerDemo {
42
43        public static void main(String[] args) {
44                
45                Scanner s = new Scanner(System.in);
46                
47                System.out.println("Enter Customer Name: ");
48                String name = s.next();
49                
50                System.out.println("Enter Customer Date (dd/mm/yyyy)");
51                String dataOfBirth = s.next();
52                
53                Customer cust = new Customer(name, dataOfBirth);
54                
55                cust.formatCustomerDOB();
56                
57                System.out.println("\nFormatted Customer details: \n" + cust);
58
59        }
60
61}


OUTPUT :

Enter Customer Name:

Thomas

Enter Customer Date (dd/mm/yyyy)

10/10/1916

Formatted Customer details:

Customer [name=Thomas, dateOfBirth=10, 10, 1916]


No comments:

Post a Comment

Online Certifications

Python for Beginners Offered by: Christian Drumm, Stephan Jacobs Course dates: 2022-04-05 to 2022-06-01 Topics Python Fundamentals Lists and...