How do I get my code to recognize the duplicates? Math 130 Java Programming Here is an
Question:
How do I get my code to recognize the duplicates?
Math 130 Java Programming
Here is an input file:
8
Albert Zoe
8581234567
44
Beth Yup
8582345678
46
Cat Xiver
6193456789
20
Dan Woow
8584567891
2
Esther Uno
8584567890
16
Frankie Uion
4294567123
16
Gab Xvy
6184567234
16
Henrik Que
7604567345
16
input file 2:
8
Albert Zoe
8581234567
44
Albert Zoe
8581234567
44
Cat Xiver
6193456789
20
Dan Woow
8584567891
2
Esther Uno
8584567890
16
Frankie Uion
4294567123
10
Gab Xvy
6184567234
12
Henrik Que
7604567345
11
output file 1:
Contacts (8)
===================
Name: Albert Zoe Phone: 8581234567 Age: 44
Name: Beth Yup Phone: 8582345678 Age: 46
Name: Cat Xiver Phone: 6193456789 Age: 20
Name: Dan Woow Phone: 8584567891 Age: 2
Name: Esther Uno Phone: 8584567890 Age: 16
Name: Frankie Uion Phone: 4294567123 Age: 16
Name: Gab Xvy Phone: 6184567234 Age: 16
Name: Henrik Que Phone: 7604567345 Age: 16
Total Friends Age:(44,46,20,2,16,16,16,16) Total: 176
Contacts (8)
===================
Name: Albert Zoe Phone: 8581234567 Age: 44
Name: Beth Yup Phone: 8582345678 Age: 46
Name: Cat Xiver Phone: 6193456789 Age: 20
Name: Dan Woow Phone: 8584567891 Age: 2
Name: Esther Uno Phone: 8584567890 Age: 16
Name: Frankie Uion Phone: 4294567123 Age: 16
Name: Gab Xvy Phone: 6184567234 Age: 16
Name: Henrik Que Phone: 7604567345 Age: 16
Total Friends Age:(44,46,20,2,16,16,16,16) Total: 176
Here in this output file there are duplicate contact detected.
How do I get my code to understand the duplicate detection?
output file 2:
Contacts (8)
===================
Name: Albert Zoe Phone: 8581234567 Age: 44
Name: Albert Zoe Phone: 8581234567 Age: 44
Name: Cat Xiver Phone: 6193456789 Age: 20
Name: Dan Woow Phone: 8584567891 Age: 2
Name: Esther Uno Phone: 8584567890 Age: 16
Name: Frankie Uion Phone: 4294567123 Age: 10
Name: Gab Xvy Phone: 6184567234 Age: 12
Name: Henrik Que Phone: 7604567345 Age: 11
Total Friends Age:(44,44,20,2,16,10,12,11) Total: 159
Duplicate contact detected: (0)Albert Zoe and (1)Albert Zoe
Contacts (8)
===================
Name: Albert Zoe Phone: 8581234567 Age: 44
Name: Albert Zoe(1) Phone: 8581234567(1) Age: 0
Name: Cat Xiver Phone: 6193456789 Age: 20
Name: Dan Woow Phone: 8584567891 Age: 2
Name: Esther Uno Phone: 8584567890 Age: 16
Name: Frankie Uion Phone: 4294567123 Age: 10
Name: Gab Xvy Phone: 6184567234 Age: 12
Name: Henrik Que Phone: 7604567345 Age: 11
Total Friends Age:(44,20,2,16,10,12,11,0) Total: 115
My code:
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
public class Assignment08
{
public static void main(String[] args) throws Exception
{
File inputData = new File("input02.txt");
Scanner input = new Scanner(inputData);
int totalNumber = input.nextInt();
Friend.friends = new Friend[totalNumber];
input.nextLine();
for (int i = 0; i < totalNumber; i++)
{
String name = input.nextLine();
String phone = input.nextLine();
String age = input.nextLine();
int ageNum = Integer.parseInt(age);
Friend.friends[i] = new Friend(name, phone, ageNum);
}
printContacts(Friend.friends);
Friend.showTotalFriendAge();
ManageDuplicate(Friend.friends);
printContacts(Friend.friends);
Friend.showTotalFriendAge();
}
public static void printContacts(Friend[] contacts)
{
System.out.println("Contacts (" + contacts.length + ")");
System.out.println("===================");
for (Friend friend : contacts)
{
System.out.println(friend);
}
System.out.println();
}
public static void ManageDuplicate(Friend[] contacts)
{
ArrayList duplicates = new ArrayList<>();
for (int i = 0; i < contacts.length - 1; i++)
{
for (int j = i + 1; j < contacts.length; j++)
{
if (contacts[i].equals(contacts[j]))
{
duplicates.add(contacts[i]);
duplicates.add(contacts[j]);
}
}
}
if (!duplicates.isEmpty())
{
System.out.println("Duplicate contact detected:");
for (int i = 0; i < duplicates.size(); i += 2)
{
System.out.println("(" + i + ")" + duplicates.get(i).getName() + " and (" + (i + 1) + ")"
+ duplicates.get(i + 1).getName());
}
System.out.println();
// Modify duplicate entries and set age to 0
for (Friend duplicate : duplicates)
{
duplicate.setName(duplicate.getName() + "(" + duplicates.indexOf(duplicate) + ")");
duplicate.setPhone(duplicate.getPhone() + "(" + duplicates.indexOf(duplicate) + ")");
duplicate.setAge(0);
}
}
}
}
public class Friend
{
private String name;
private String phone;
private int age;
public static Friend[] friends; //array
public String toString()
{
return String.format("%-20s%-20s%-10s","Name: " + name, "Phone: " + phone, "Age: "+ age);
}
public Friend(String name)
{
this.name = name;
}
public Friend(String name, String phone)
{
this.name = name;
this.phone = phone;
}
public Friend(String name, String phone, int age)
{
this.age = age;
this.name = name;
this.phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean equals(Friend dest)
{
if(this.name == dest.name && this.phone == dest.phone && this.age == dest.age)
{
return true;
}
else
{
return false;
}
}
public static void showTotalFriendAge()
{
int totalAge = 0;
System.out.print("Total Friends Age:(");
for(int i = 0; i < friends.length; i++)
{
if(i < friends.length - 1)
{
System.out.print(Friend.friends[i].age + ",");
}
else
{
System.out.print(Friend.friends[i].age + "");
}
totalAge += Friend.friends[i].age;
}
System.out.print(") Total: " + totalAge);
}
public static int getNumFriends()
{
return 0;
}
}
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill