Write, document (internally) and test a Java program to do the following: A nati
ID: 3777152 • Letter: W
Question
Write, document (internally) and test a Java program to do the following:
A national company records the following information about each of its customers:
-name
-address
-customer number (9 digits)
-date (year) when the customer last placed an order
a. Define a class Customer to represent this information.
b. Create a singly-linked list of Customer objects. The information for each customer is to be input. Output the contents of the list after it has been created.
I already have parts A & B done, so I am only looking for parts c, d, & e.
(I can attach my code so far if necessary)
c Write a method which, given a customer number, returns the customer information. Output the customer information after it is returned.
d. Output the name and address of each customer who placed an order last year (2015).
e. Delete from the list those Customer records for which an order has not been placed in the last five years (i.e., 2012-2016).
Explanation / Answer
// the text file
information.txt
koti hyderabad 998563279 2015
ramu banjarahills 256489752 2014
ramesh guntur 258914587 2011
------------
package com.jg.graphics;
public class Customer {
private String name;
private String address;
private int no;
private String date;
public Customer() {
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((address == null) ? 0 : address.hashCode());
result = prime * result + ((date == null) ? 0 : date.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + no;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Customer other = (Customer) obj;
if (address == null) {
if (other.address != null)
return false;
} else if (!address.equals(other.address))
return false;
if (date == null) {
if (other.date != null)
return false;
} else if (!date.equals(other.date))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (no != other.no)
return false;
return true;
}
@Override
public String toString() {
return "Customer [name=" + name + ", address=" + address + ", no=" + no
+ ", date=" + date + "]";
}
public Customer(String name, String address, int no, String date) {
this.name = name;
this.address = address;
this.no = no;
this.date = date;
}
}
------------------------
package com.jg.graphics;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Scanner;
public class Test {
static LinkedList<Customer> list = new LinkedList<Customer>();
static Scanner scanner = new Scanner(System.in);
static Customer customer = new Customer();
public static void main(String[] args) throws IOException {
String name = null;
String address = null;
int no = 0;
String date = null;
String[] words = null;
File file = new File("D:\information.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String line = null;
while ((line = br.readLine()) != null) {
// to dislay the every line in text file
// System.out.println(line);
words = line.split(" ");
for (int i = 0; i < words.length; i++) {
name = words[0];
address = words[1];
no = Integer.parseInt(words[2]);
date = words[3];
}
// convert the object
Customer customer = new Customer(name, address, no, date);
list.add(customer);
}
/*
* for(Customer customer:list) { System.out.println(customer); }
*/
/*
* System.out.println("enter the number"); int number=scanner.nextInt();
*
* getContact(number);
*
* System.out.println("enter the order placed in year"); int
* year=scanner.nextInt(); getYear(year);
*/
System.out
.println("enter the year to delete the record order not placed before 5 years to this year ");
int startyear = scanner.nextInt();
int beforeyeartodelete = startyear - 5;
doSearching(beforeyeartodelete);
}
private static void doSearching(int startyear) {
int count = 0;
for (Customer customer : list) {
if (Integer.parseInt(customer.getDate()) <= startyear) {
++count;
list.remove(customer);
System.out.println("the removing record is" + customer);
}
}
System.out.println("the no of records deleted is" + count);
}
private static void getYear(int year) {
int count = 0;
for (Customer customer : list) {
if (Integer.parseInt(customer.getDate()) == year) {
++count;
System.out.println(customer);
}
}
if (count == 0) {
System.out.println(" given year records are not found");
}
}
private static void getContact(int no) {
int count = 0;
for (Customer customer : list) {
if (customer.getNo() == no) {
++count;
System.out.println(customer);
}
}
if (count == 0) {
System.out.println("given no records are not found");
}
}
}
-----------
output
Customer [name=koti, address=hyderabad, no=998563279, date=2015]
Customer [name=ramu, address=banjarahills, no=256489752, date=2014]
Customer [name=ramesh, address=guntur, no=258914587, date=2011]
enter the number
998563279
Customer [name=koti, address=hyderabad, no=998563279, date=2015]
enter the order placed in year
2015
Customer [name=koti, address=hyderabad, no=998563279, date=2015]
enter the year to delete the record order not placed before 5 years to this year
2016
the removing record isCustomer [name=ramesh, address=guntur, no=258914587, date=2011]
the no of records deleted is1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.