Public class Product { private String name; private String creator


Download 116.03 Kb.
bet1/2
Sana29.11.2020
Hajmi116.03 Kb.
#155707
  1   2
Bog'liq
Mahsulot(1)


Mahsulot klasi

public class Product {
private String name;
private String creator;
private int cost;
private String time;
public Product(String name, String creator, int cost, String time) {
this.name = name;
this.creator = creator;
this.cost = cost;
this.time = time; }
public String getName()

{return name; }


public int getCost() {return cost; }
public String getTime() {return time;}
public String getCreator() {return creator;}
@Override
public String toString() {
return name+" "+creator+" tomonidan ishlab chiqarilgan, "+
"narxi = "+cost+" so'm, yaroqlilik muddati : "+time; }}

import java.util.*;

public class Main {
private static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
LinkedHashSet
set = new LinkedHashSet
();
set.add(new Product("Galaxy S10", "Samsung", 2000000, "2030.08.30"));
set.add(new Product("iPhone X", "Apple", 1500000, "2025.05.12"));
set.add(new Product("LG K8", "Life's Good", 1750000, "2023.09.15"));
set.add(new Product("Redmi 5", "Xiaomi", 1800000, "2027.01.19"));
for (Product p : set){ System.out.println(p); }
menu(set); }
private static void menu(LinkedHashSet
s){
while (true){
System.out.println("\nKerakli bo'limni tanlang :");
System.out.println("1. Qidirish");
System.out.println("2. Saralash");
System.out.println("3. O'chirish");
System.out.println("4. Chiqish");
int k = in.nextInt();
switch (k){
case 1: qidir(s); break;
case 2: saralash(s); break;
case 3: ochir(s); break;
case 4: System.exit(0); break;
default:System.exit(-1);
}
}
}

private static void qidir(LinkedHashSet
set){
while (true){
System.out.println("\nQaysi bo'lim bo'yicha qidirilsin :");
System.out.println("1. Nomi");
System.out.println("2. Ishlab chiqaruvchi");
System.out.println("3. Yaroqlilik muddati");
System.out.println("4. Asosiy menyuga qaytish");
int t = in.nextInt();
in.nextLine();
switch (t){
case 1:{
String s = in.nextLine();
for (Product p : set){
if (p.getName().equalsIgnoreCase(s)){
System.out.println(p);
break; } } break;}

case 2:{
String s = in.nextLine();
for (Product p : set){
if (p.getCreator().equalsIgnoreCase(s)){
System.out.println(p);
break; } } break;}
case 3:{
String s = in.nextLine();
for (Product p : set){
if (p.getTime().equalsIgnoreCase(s)){
System.out.println(p);
break; } } break;}
case 4:{
menu(set);
System.exit(0);}
default: System.exit(-2); } } }
private static void saralash(LinkedHashSet
list){
int k;
ArrayList
arr = new ArrayList<>(list);
Comparator narxi = Comparator.comparing(Product::getCost);
Comparator nomi = Comparator.comparing(Product::getName);
Comparator ishchiq = Comparator.comparing(Product::getCreator);
while (true) {
System.out.println("\nQaysi maydon bo'yicha saralansin :");
System.out.println("1. Nomi");
System.out.println("2. Ishlab chiqaruvchi");
System.out.println("3. Narxi");
System.out.println("4. Asosiy menyuga qaytish");
k = in.nextInt();

switch (k){
case 1:{
Collections.sort(arr, nomi);
System.out.println("Nomi bo'yicha saralangan ro'yxat :");
break;}
case 2:{
Collections.sort(arr, ishchiq);
System.out.println("Nomi bo'yicha saralangan ro'yxat :");
break;}
case 3:{
Collections.sort(arr, narxi);
System.out.println("Nomi bo'yicha saralangan ro'yxat :");
break;}
case 4:{
menu(list);
System.exit(0);}
default: System.exit(-3); }
for (Product x : arr){
System.out.println(x); }} }
private static void ochir(LinkedHashSet
p){
System.out.print("\nBugungi sanani kiriting : ");
LinkedList
pp = new LinkedList<>(p);
in.nextLine();
String s = in.nextLine();
for (int i = 0; i < p.size(); i++) {
if (pp.get(i).getTime().compareTo(s) <= 0){
pp.set(i, null); }}
System.out.println("Muddati o'tmagan mahsulotlar :");
for (Product ppp : pp){
if (ppp != null){
System.out.println(ppp);
}
}
}
}


Download 116.03 Kb.

Do'stlaringiz bilan baham:
  1   2




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling