Skip to content

Instantly share code, notes, and snippets.

@Cvetomird91
Created February 25, 2020 18:41
Show Gist options
  • Save Cvetomird91/32265b44e07670218eb1d972a5073e15 to your computer and use it in GitHub Desktop.
Save Cvetomird91/32265b44e07670218eb1d972a5073e15 to your computer and use it in GitHub Desktop.
package org.pu.javapp.model;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.io.File;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Client {
private String names;
private LocalDate registrationDate;
private Integer numberOfPurchases;
private Double totalPurchasedSum;
private Integer rating;
public Client() {}
public Client(String names, LocalDate registrationDate, Integer numberOfPurchases, Double totalPurchasedSum, Integer rating) {
this.names = names;
this.registrationDate = registrationDate;
this.numberOfPurchases = numberOfPurchases;
this.totalPurchasedSum = totalPurchasedSum;
this.rating = rating;
}
public Client(String names, String registrationDate, Integer numberOfPurchases, Double totalPurchasedSum, Integer rating) {
DateTimeFormatter fomatter = DateTimeFormatter.ofPattern("dd.M.yyyy", Locale.ENGLISH);
this.names = names;
this.registrationDate = LocalDate.parse(registrationDate, fomatter);
this.numberOfPurchases = numberOfPurchases;
this.totalPurchasedSum = totalPurchasedSum;
this.rating = rating;
}
public String getNames() {
return names;
}
public void setNames(String names) {
this.names = names;
}
public void setRegistrationDate(LocalDate registrationDate) {
this.registrationDate = registrationDate;
}
public Integer getNumberOfPurchases() {
return numberOfPurchases;
}
public void setNumberOfPurchases(Integer numberOfPurchases) {
this.numberOfPurchases = numberOfPurchases;
}
public Double getTotalPurchasedSum() {
return totalPurchasedSum;
}
public void setTotalPurchasedSum(Double totalPurchasedSum) {
this.totalPurchasedSum = totalPurchasedSum;
}
public Integer getRating() {
return rating;
}
private void calculateRating() {
if (numberOfPurchases == 0 || numberOfPurchases < 0) {
throw new IllegalStateException("Number of purchases not set for client");
}
if (numberOfPurchases >= 1 && numberOfPurchases < 100)
rating = 1;
if (numberOfPurchases >= 100 && numberOfPurchases < 299)
rating = 2;
if (numberOfPurchases >= 300 && numberOfPurchases < 499)
rating = 3;
if (numberOfPurchases >= 500 && numberOfPurchases < 999)
rating = 4;
if (numberOfPurchases >= 1000 && numberOfPurchases < 9999)
rating = 5;
}
public void persistClientData(String path) throws IOException {
File file = new File(path);
if (!file.exists()) {
file.createNewFile();
}
BufferedWriter writer = new BufferedWriter(new FileWriter(path, true));
persistClientData(writer);
}
private void persistClientData(BufferedWriter writer) throws IOException {
StringBuilder clientCsvDataBuilder = new StringBuilder();
calculateRating();
clientCsvDataBuilder.append(names+","+registrationDate.format(DateTimeFormatter.ofPattern("dd.M.yyyy", Locale.ENGLISH))+
","+numberOfPurchases+","+totalPurchasedSum+"," + getRating() +"\n");
writer.write(clientCsvDataBuilder.toString());
writer.close();
}
public String getYear() {
return Integer.toString(registrationDate.getYear());
}
@Override
public String toString() {
return names + ", " + numberOfPurchases + ", " + String.format("%.2f",totalPurchasedSum) + ", " + registrationDate + ", " + Stream.generate(() -> "*").limit(getRating()).collect(Collectors.joining());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment