-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a23871c
Showing
7 changed files
with
766 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import java.util.*; | ||
import java.util.ArrayList; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
class BlockContainer <T extends Comparable<T>>{ | ||
private List<TreeSet<T>> container; | ||
private int n; | ||
|
||
public BlockContainer(int n) { | ||
this.n = n; | ||
container = new ArrayList<TreeSet<T>>(); | ||
|
||
} | ||
|
||
public void add(T a){ | ||
if(container.isEmpty()) | ||
{ | ||
TreeSet<T> newSet = new TreeSet<>(); | ||
newSet.add(a); | ||
container.add(newSet); | ||
}else if(container.get(container.size()-1).size()<n){ | ||
container.get(container.size()-1).add(a); | ||
}else{ | ||
TreeSet<T> newSet = new TreeSet<>(); | ||
newSet.add(a); | ||
container.add(newSet); | ||
} | ||
} | ||
|
||
public boolean remove(T a){ | ||
|
||
if(container.get(container.size()-1).size()==1) | ||
{ | ||
container.remove(container.size()-1); | ||
} | ||
return container.get(container.size()-1).remove(a); | ||
} | ||
|
||
public void sort(){ | ||
List<T> elementi = container.stream().flatMap(Collection::stream).collect(Collectors.toList()); | ||
elementi.sort(Comparable::compareTo); | ||
container.clear(); | ||
for(T t : elementi) | ||
{ | ||
this.add(t); | ||
} | ||
|
||
} | ||
// @Override | ||
// public String toString() { | ||
// return blocks.stream().map(block -> "[" + | ||
// block.stream().map(Object::toString).collect(Collectors.joining(", ")) | ||
// + "]").collect(Collectors.joining(",")); | ||
// } | ||
|
||
public String toString(){ | ||
return container.stream().map(block-> "[" + | ||
block.stream().map(Objects::toString).collect(Collectors.joining(", ")) | ||
+"]").collect(Collectors.joining(",")); | ||
} | ||
|
||
|
||
|
||
} | ||
|
||
|
||
|
||
public class BlockContainerTest { | ||
public static void main(String[] args) { | ||
Scanner scanner = new Scanner(System.in); | ||
int n = scanner.nextInt(); | ||
int size = scanner.nextInt(); | ||
BlockContainer<Integer> integerBC = new BlockContainer<Integer>(size); | ||
scanner.nextLine(); | ||
Integer lastInteger = null; | ||
for(int i = 0; i < n; ++i) { | ||
int element = scanner.nextInt(); | ||
lastInteger = element; | ||
integerBC.add(element); | ||
} | ||
System.out.println("+++++ Integer Block Container +++++"); | ||
System.out.println(integerBC); | ||
System.out.println("+++++ Removing element +++++"); | ||
integerBC.remove(lastInteger); | ||
System.out.println("+++++ Sorting container +++++"); | ||
integerBC.sort(); | ||
System.out.println(integerBC); | ||
BlockContainer<String> stringBC = new BlockContainer<String>(size); | ||
String lastString = null; | ||
for(int i = 0; i < n; ++i) { | ||
String element = scanner.next(); | ||
lastString = element; | ||
stringBC.add(element); | ||
} | ||
System.out.println("+++++ String Block Container +++++"); | ||
System.out.println(stringBC); | ||
System.out.println("+++++ Removing element +++++"); | ||
stringBC.remove(lastString); | ||
System.out.println("+++++ Sorting container +++++"); | ||
stringBC.sort(); | ||
System.out.println(stringBC); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import java.util.Comparator; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import java.util.*; | ||
|
||
public class PhoneBookTest { | ||
|
||
public static void main(String[] args) { | ||
PhoneBook phoneBook = new PhoneBook(); | ||
Scanner scanner = new Scanner(System.in); | ||
int n = scanner.nextInt(); | ||
scanner.nextLine(); | ||
for (int i = 0; i < n; ++i) { | ||
String line = scanner.nextLine(); | ||
String[] parts = line.split(":"); | ||
try { | ||
phoneBook.addContact(parts[0], parts[1]); | ||
} catch (DuplicateNumberException e) { | ||
System.out.println(e.getMessage()); | ||
} | ||
} | ||
while (scanner.hasNextLine()) { | ||
String line = scanner.nextLine(); | ||
System.out.println(line); | ||
String[] parts = line.split(":"); | ||
if (parts[0].equals("NUM")) { | ||
phoneBook.contactsByNumber(parts[1]); | ||
} else { | ||
phoneBook.contactsByName(parts[1]); | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
|
||
|
||
class Contact{ | ||
private String name; | ||
private String number; | ||
|
||
public Contact(String name, String number) { | ||
this.name = name; | ||
this.number = number; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getNumber() { | ||
return number; | ||
} | ||
public String toString(){ | ||
return String.format("%s %s",name,number); | ||
} | ||
|
||
} | ||
|
||
|
||
class PhoneBook{ | ||
Map<String,Contact> contacts; | ||
|
||
public PhoneBook() { | ||
this.contacts = new HashMap<String,Contact>(); | ||
} | ||
|
||
public void addContact(String name, String number) throws DuplicateNumberException { | ||
if(contacts.containsKey(number)) throw new DuplicateNumberException(number); | ||
else contacts.putIfAbsent(number,new Contact(name, number)); | ||
} | ||
|
||
public void contactsByNumber(String number){ | ||
List<Contact> list = contacts.values().stream().filter(x->x.getNumber().contains(number)) | ||
.sorted(Comparator.comparing(Contact::getName).thenComparing(Contact::getNumber)) | ||
.collect(Collectors.toList()); | ||
if(list.isEmpty()) | ||
{ | ||
System.out.println("NOT FOUND"); | ||
}else{ | ||
list.stream().forEach(System.out::println); | ||
} | ||
} | ||
|
||
public void contactsByName(String name){ | ||
List<Contact> list = contacts.values().stream().filter(x->x.getName().equals(name)) | ||
.sorted(Comparator.comparing(Contact::getName).thenComparing(Contact::getNumber)) | ||
.collect(Collectors.toList()); | ||
if(list.isEmpty()) | ||
{ | ||
System.out.println("NOT FOUND"); | ||
}else{ | ||
list.stream().forEach(System.out::println); | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
} | ||
class DuplicateNumberException extends Exception{ | ||
public DuplicateNumberException(String num) { | ||
super(num + "is duplicate"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
class Proizvod{ | ||
private String name; | ||
private float cena; | ||
private float popust; | ||
private float p; | ||
|
||
public Proizvod(float popust, float cena) { | ||
this.popust = popust; | ||
this.cena = cena; | ||
this.p = (Math.abs(cena-popust)/cena)*100; | ||
|
||
} | ||
|
||
|
||
public float getP() { | ||
return p; | ||
} | ||
|
||
|
||
public Proizvod(String name, int popust, int cena) { | ||
this.name = name; | ||
this.popust = popust; | ||
this.cena = cena; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public float getCena() { | ||
return cena; | ||
} | ||
|
||
public float getPopust() { | ||
return popust; | ||
} | ||
} | ||
|
||
class Store{ | ||
String name; | ||
List<Proizvod> ceni; | ||
|
||
public Store(String name, List<Proizvod> ceni) { | ||
this.name = name; | ||
this.ceni = ceni; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public List<Proizvod> getCeni() { | ||
return ceni; | ||
} | ||
|
||
public double detAverageDiscount(){ | ||
double sum=0.0; | ||
for(int i=0;i<ceni.size();i++) | ||
{ | ||
sum+=((ceni.get(i).getCena()-ceni.get(i).getPopust())*100)/ceni.get(i).getCena(); | ||
} | ||
return sum/ceni.size(); | ||
} | ||
|
||
|
||
public int getTotalDiscount(){ | ||
return (int) ceni.stream().mapToDouble(s->s.getCena()-s.getPopust()).sum(); | ||
} | ||
|
||
public String toString(){ | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append(getName()); | ||
sb.append('\n' + "Average discount: "+ detAverageDiscount()); | ||
sb.append('\n' + "Total discount: " + getTotalDiscount() + '\n'); | ||
|
||
List<Proizvod> list = new ArrayList<Proizvod>(); | ||
for(int i=0;i<ceni.size();i++) | ||
{ | ||
int p=0; | ||
list.add(new Proizvod(ceni.get(i).getPopust(),ceni.get(i).getCena())); | ||
} | ||
|
||
list= list.stream().sorted(Comparator.comparing(Proizvod::getP).reversed().thenComparing(Proizvod::getName)).collect(Collectors.toList()); | ||
for(int i=0;i<list.size();i++) | ||
{ | ||
sb.append(list.get(i).getP()+" "); | ||
sb.append(list.get(i).getPopust()+" "+ list.get(i).getCena() + "\n"); | ||
} | ||
sb.deleteCharAt(sb.length()-1); | ||
return sb.toString(); | ||
} | ||
} | ||
|
||
|
||
|
||
class Discounts{ | ||
|
||
private Map<String,Store> map; | ||
|
||
public Discounts() { | ||
this.map = new HashMap<String, Store>(); | ||
} | ||
|
||
|
||
|
||
|
||
public int readStores(InputStream inputStream) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); | ||
String s; | ||
String [] pom; | ||
|
||
while((s=br.readLine())!=null) | ||
{ | ||
pom=s.split("\\s+"); | ||
String name = pom[0]; | ||
List<Proizvod> CENI = new ArrayList<>(); | ||
for(int i=1;i<pom.length;i++) | ||
{ | ||
String [] delovi = pom[i].split(":"); | ||
int cena =Integer.parseInt(delovi[1]) ; | ||
int popust = Integer.parseInt(delovi[0]); | ||
CENI.add(new Proizvod(popust,cena)); | ||
} | ||
map.putIfAbsent(name,new Store(name,CENI)); | ||
} | ||
return map.size(); | ||
} | ||
|
||
|
||
|
||
public List<Store> byAverageDiscount(){ | ||
List<Store> list = new ArrayList<>(); | ||
list =map.values().stream().sorted(Comparator.comparing(Store::detAverageDiscount).reversed().thenComparing(Store::getName)) | ||
.limit(3).collect(Collectors.toList()); | ||
return list; | ||
} | ||
|
||
public List<Store> byTotalDiscount(){ | ||
List<Store> list = map.values().stream().sorted(Comparator.comparing(Store::detAverageDiscount).reversed().thenComparing(Store::getName)) | ||
.limit(3).collect(Collectors.toList()); | ||
return list; | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
} | ||
|
||
|
||
public class DiscountsTest { | ||
public static void main(String[] args) throws Exception{ | ||
Discounts discounts = new Discounts(); | ||
int stores = discounts.readStores(System.in); | ||
System.out.println("Stores read: " + stores); | ||
System.out.println("=== By average discount ==="); | ||
discounts.byAverageDiscount().forEach(System.out::println); | ||
System.out.println("=== By total discount ==="); | ||
discounts.byTotalDiscount().forEach(System.out::println); | ||
} | ||
} | ||
|
Oops, something went wrong.