Mam do zrobienia apke, która będzie pobierać z poliku dane nt firm i faktur do opłacenia. Następnie ma być możliwość opłacenia wybranej fv. Opłaceniem fv ma być zmiana pola "status" z 'false' na 'true';
Proszę o rzucenie okiem na moją pracę. Proszę nie wchodzić w szczegóły implementacji poszczególnych metod. Na razie chodzi mi o sprwadzenie, przede wszystkim podziału na klasy, metody, relacje między klasami, wykorzystanie kompozycji itp. Oczywiście jak już to dopracuje to chętnie dowiem się czegoś nt pozostałych elementów.
public class Factur {
private int facturNumber;
BigDecimal amountToBePaid;
private String title;
private Boolean status;
public Factur(int facturNumber, BigDecimal amountToBePaid, String title, Boolean status) {
this.facturNumber = facturNumber;
this.amountToBePaid = amountToBePaid;
this.title = title;
this.status = status;
}
public int getFacturNumber() {
return facturNumber;
}
public Boolean getStatus() {
return status;
}
public void setStatus(Boolean status) {
this.status = status;
}
@Override
public String toString() {
return "Factur{" +
"facturNumber=" + facturNumber +
", price=" + amountToBePaid +
", title='" + title + '\'' +
", status='" + status + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Factur factur = (Factur) o;
return facturNumber == factur.facturNumber &&
Objects.equals(amountToBePaid, factur.amountToBePaid) &&
Objects.equals(title, factur.title) &&
Objects.equals(status, factur.status);
}
@Override
public int hashCode() {
return Objects.hash(facturNumber, amountToBePaid, title, status);
}
}
public class Company {
private String companyName;
private List<Factur> factursForPay;
public Company(String companyName, List<Factur> factursForPay) {
this.companyName = companyName;
this.factursForPay = factursForPay;
}
public String getCompanyName() {
return companyName;
}
public List<Factur> getFactursForPay() {
return factursForPay;
}
@Override
public String toString() {
return "Company{" +
"companyName='" + companyName + '\n' +
" factursForPay=" + factursForPay + '\n';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Company company = (Company) o;
return Objects.equals(companyName, company.companyName);
}
@Override
public int hashCode() {
return Objects.hash(companyName);
}
}
public class Payments {
private static List<Company> companyList = getCompanyFromDatabase("faktury");
public Payments() {
}
private static List<Company> getCompanyFromDatabase(String filename) {
List<Company> companyList = new ArrayList<>();
List<String> newCompany = new ArrayList<>();
Boolean elExist = false;
try (
FileReader fR = new FileReader(filename);
Scanner sc = new Scanner(fR);) {
while (sc.hasNextLine()) {
newCompany = Arrays.asList(sc.nextLine().split(";"));
elExist = false;
for (Company elOfCompanyList : companyList) {
if (elOfCompanyList.getCompanyName().equals(newCompany.get(0)))
elExist = true;
}
if (!elExist) {
companyList.add(new Company(newCompany.get(0), getFacturList(filename, newCompany.get(0))));
}
}
} catch (Exception e) {
throw new IllegalArgumentException("FileReader err");
}
//System.out.println("From Factursinformationclass getcompany method: " + companyList);
return List.copyOf(companyList);
}
private static List<Factur> getFacturList(String fileName, String companyName) {
List<Factur> facturList = new ArrayList<>();
String factur;
try (
FileReader fRr = new FileReader(fileName);
Scanner scc = new Scanner(fRr);) {
while (scc.hasNextLine()) {
factur = scc.nextLine();
if (factur.matches(".{0,}" + companyName + ".{0,}")) {
facturList.add(Validation.createFactur(factur));
}
}
} catch (Exception e) {
throw new IllegalArgumentException("FileReader err");
}
return facturList;
}
public static List<Company> getCompanyList() {
return companyList;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Payments payments = (Payments) o;
return Objects.equals(companyList, payments.companyList);
}
@Override
public int hashCode() {
return Objects.hash(companyList);
}
@Override
public String toString() {
return "Payments{" +
"companyList=" + companyList +
'}';
}
}
public class Validation {
static Factur createFactur(String facturInformation){
List<String> fvInfo = Arrays.asList(facturInformation.split(";"));
if(!fvInfo.get(1).matches("[0-9]{5}")){
throw new IllegalArgumentException("Factur nr is wrong");
}
return new Factur(Integer.valueOf(fvInfo.get(1)),
new BigDecimal(fvInfo.get(2)),
fvInfo.get(3),
Boolean.valueOf(fvInfo.get(4)));
}
}
class FactursManager {
private static Map<Factur, Boolean> facturStatus = new HashMap<>();
private static void refreshFacturInfo(List<Company> companyList) {
//-------------Tutaj dodałbym zapis do pliku nowych info o statusie
//poprzez czytanie starego pliku, aktualizacja statusu i zapis do listy.
//Następnie lista do pliku;
for (Company elOfCompanyList : companyList) {
for (Factur elOfFacturList : elOfCompanyList.getFactursForPay()) {
facturStatus.put(elOfFacturList, elOfFacturList.getStatus());
}
}
System.out.println("Recently payment's status");
for (Map.Entry<Factur, Boolean> elOfMap : facturStatus.entrySet()) {
System.out.println(elOfMap);
}
}
static void makePeyment() {
List<Company> companyList = Payments.getCompanyList();
System.out.println("Company List: " + companyList);
int choose = Integer.MIN_VALUE;
do {
choose = UserDataService.getInt("1 --> Pay || anyButton --> Exit");
if(choose == 1) {
int fvNr = UserDataService.getInt("Factur number for pay: ");
for (Company elOfCompanyList : companyList) {
for (Factur elOfFvList : elOfCompanyList.getFactursForPay()) {
if (elOfFvList.getFacturNumber() == fvNr) {
transferMoney(fvNr, companyList);
}
}
}
refreshFacturInfo(companyList);
}
} while (!(choose == 2));
}
private static void transferMoney(int fvNumber, List<Company> companyList) {
for (Company elOfCompanyList : companyList) {
for (Factur elOfFacturList : elOfCompanyList.getFactursForPay()) {
if (elOfFacturList.getFacturNumber() == fvNumber) {
elOfFacturList.setStatus(true);
System.out.println("transfer complet");
}
}
}
}
}
public class Main {
public static void main(String[] args) {
FactursManager.makePeyment();
}
}