[go: up one dir, main page]

0% found this document useful (0 votes)
22 views2 pages

List Exercitii

The document is a Java program that allows users to input four purchases, removes duplicates, and displays the updated list. It also provides functionality to delete a specified product and shows products starting with the letter 'M'. Additionally, it lists purchases whose next item starts with 'M'.

Uploaded by

Eduard Manea
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views2 pages

List Exercitii

The document is a Java program that allows users to input four purchases, removes duplicates, and displays the updated list. It also provides functionality to delete a specified product and shows products starting with the letter 'M'. Additionally, it lists purchases whose next item starts with 'M'.

Uploaded by

Eduard Manea
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

package advanced_features.collections.

exercise2;
import java.util.*;
public class ListCollectionsPurchases {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Purchase1: ");
String purchase1 = scan.nextLine();
System.out.print("Purchase2: ");
String purchase2 = scan.nextLine();
System.out.print("Purchase3: ");
String purchase3 = scan.nextLine();
System.out.print("Purchase4: ");
String purchase4 = scan.nextLine();
List<String> purchase = new ArrayList<>();
purchase.add(purchase1);
purchase.add(purchase2);
purchase.add(purchase3);
purchase.add(purchase4);
System.out.println("PRODUSELE DUPLICATE SUNT: ");
List<String> newPurchase = removeDuplicates(purchase);
System.out.println("COSUL DVS CONTINE: ");
for (String shopping : newPurchase) {
System.out.print(shopping + " ");
}
System.out.println();
// List<String> duplicatePurchase = new ArrayList<>(new
LinkedHashSet<>(purchase));
// for (String cosCumparaturi : duplicatePurchase) {
// System.out.println(cosCumparaturi);
// }
//stergere produs din cos
System.out.print("STERGETI PRODUSUL DORIT: ");
String text = scan.nextLine();
if (text.equals(purchase1)) {
newPurchase.remove(purchase1);
} else if (text.equals(purchase2)) {
newPurchase.remove(purchase2);
} else if (text.equals(purchase3)) {
newPurchase.remove(purchase3);
} else if (text.equals(purchase4)) {
newPurchase.remove(purchase4);
}
System.out.println("COSUL DVS A FOST ACTUALIZAT: ");
for (String afterDelete : newPurchase) {
System.out.println(afterDelete + " ");
}
// afisare produse care ibncep cu M
System.out.println("Produsele care incep cu M: ");
for (int i = 0; i < newPurchase.size(); i++) {
String[] element = newPurchase.toArray(new String[0]);
if (element[i].contains("M")) {
System.out.println(element[i]);
}
}
//d)* View only purchases whose next product on the list starts with „m"
( e.g . eggs ,
//if milk was next on the list)
System.out.println("Only purchases whose next product: ");
for (int i = 0; i < newPurchase.size(); i++){
String[] element = newPurchase.toArray(new String[0]);
for (int j = i+1; j < newPurchase.size(); j++){
if (element[j].contains("M")){
System.out.println(element[i]);
break;
}
}
}
}
// Function to remove duplicates from List
public static <T> List<T> removeDuplicates(List<T> list) {
// Create a new ArrayList
List<T> newList = new ArrayList<T>();
// Traverse through the first list
for (T element : list) {
// If this element is not present in newList
// then add it
if (!newList.contains(element)) {
newList.add(element);
} else
System.out.println(element + " ");
}
// return the new list
return newList;
}
}

You might also like