Skip to content

Commit

Permalink
checkout ok
Browse files Browse the repository at this point in the history
  • Loading branch information
saribricka committed Jan 16, 2022
1 parent 334a3b2 commit 6985137
Show file tree
Hide file tree
Showing 23 changed files with 76 additions and 27 deletions.
10 changes: 9 additions & 1 deletion Item.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,12 @@



4;vino rosso;50;8.6;null;null;beverage

2;cipolla;50;0.4;null;null;vegetables
3;penna;100;0.5;null;null;diary

1;tofu;18;3.2;null;null;bio

4;vino rosso;46;8.6;null;null;beverage

5;mayo veg;73;3.2;null;null;sauce
2 changes: 2 additions & 0 deletions Shop.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
2022-01-15 22:29:56 11 simonetta cornacchia 74.7
2022-01-16 13:20:51 11 simonetta cornacchia 58.2
2022-01-16 13:30:09 1 penny briccoli 63.2
Binary file modified bin/main/java/controller/InvoiceController.class
Binary file not shown.
Binary file modified bin/main/java/controller/InvoiceControllerImpl.class
Binary file not shown.
Binary file modified bin/main/java/controller/ItemController.class
Binary file not shown.
Binary file modified bin/main/java/controller/ItemControllerImpl.class
Binary file not shown.
Binary file modified bin/main/java/model/file/FileItemImpl.class
Binary file not shown.
Binary file modified bin/main/java/model/file/FileShopImpl.class
Binary file not shown.
Binary file modified bin/main/java/model/file/FileUserImpl.class
Binary file not shown.
Binary file modified bin/main/java/view/EditItemView$3.class
Binary file not shown.
Binary file modified bin/main/java/view/InvoiceView$7.class
Binary file not shown.
Binary file modified bin/main/java/view/InvoiceView$8.class
Binary file not shown.
Binary file modified bin/main/java/view/InvoiceView.class
Binary file not shown.
2 changes: 2 additions & 0 deletions src/main/java/controller/InvoiceController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ public interface InvoiceController {
boolean addToCart(String cartLine);

boolean newInvoice();

boolean checkout();
}
18 changes: 18 additions & 0 deletions src/main/java/controller/InvoiceControllerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ public boolean addToCart(String cartLine) {
public boolean newInvoice() {
return fileInvoice.emptyFile();
}

@Override
public boolean checkout() {
try {
Set<String> cartLines = fileInvoice.fileReader();
for(String line : cartLines) {
String[] data = line.split("\t");
String id = data[0];
int qty = Integer.parseInt(data[2]);
itemController.recalculateQuantity(id, qty);
}
newInvoice();
return true;
} catch (NumberFormatException e) {
e.printStackTrace();
return false;
}
}


}
2 changes: 1 addition & 1 deletion src/main/java/controller/ItemController.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public interface ItemController {
* @param nSold
* @return the new quantity.
*/
int recalculateQuantity(String barcode, int nSold);
void recalculateQuantity(String barcode, int nSold);

/**
* Show all the stock.
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/controller/ItemControllerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,13 @@ public Set<Integer> fromOneToQuantity(Item item) {
}

@Override
public int recalculateQuantity(String barcode, int nSold) {
// TODO Auto-generated method stub
return 0;
public void recalculateQuantity(String barcode, int nSold) {
Item i = searchItem(barcode);
int oldQty = i.getQuantity();
int newQty = oldQty - nSold;

Item itemToUpdate = new ItemImpl(i.getBarcode(), i.getName(), newQty, i.getUnitPrice(), null, null, i.getCategory());
updateItem(itemToUpdate);
}

@Override
Expand Down
11 changes: 0 additions & 11 deletions src/main/java/controller/ShopControllerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,4 @@ public Set<String> showPayments() {
Set<String> payments = fileShop.fileReader();
return payments;
}

// @Override
// public Set<String> searchByUser(String target){
// return null;
// }
//
//
// @Override
// public Set<String> searchByDate(String target){
// return null;
// }
}
2 changes: 1 addition & 1 deletion src/main/java/model/file/FileItemImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public boolean deleteLine(String target) {
try {
File file = createFile();
List<String> out = Files.lines(file.toPath())
.filter(line -> !line.contains(target.toLowerCase()))
.filter(line -> !line.split(";")[0].contains(target.toLowerCase()))
.collect(Collectors.toList());

Files.write(file.toPath(), out, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
Expand Down
37 changes: 31 additions & 6 deletions src/main/java/model/file/FileShopImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,36 @@

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;

public class FileShopImpl implements FileStrategy {

private static final String SHOP_FILE = "Shop.txt";
private Set<String> list = new TreeSet<>();

private File createFile() {
try {
File myObj = new File(SHOP_FILE);
myObj.createNewFile();
return myObj;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

@Override
public Set<String> fileReader() {
try(BufferedReader reader = new BufferedReader(new FileReader(SHOP_FILE))) {
Expand Down Expand Up @@ -51,7 +67,9 @@ public String searchInFile(String target) {
try {
Set<String> lines = fileReader();
for(String line : lines) {
if(line.contains(target.toLowerCase())) {
String[] data = line.split("\t");
String barcode = data[0];
if(barcode.contains(target.toLowerCase())) {
return line;
}
}
Expand All @@ -63,12 +81,19 @@ public String searchInFile(String target) {
}

@Override
/**
* Disabled in shop payments.
*/
public boolean deleteLine(String target) {
// TODO Auto-generated method stub
return false;
try {
File file = createFile();
List<String> out = Files.lines(file.toPath())
.filter(line -> !line.split("\t")[0].contains(target.toLowerCase()))
.collect(Collectors.toList());

Files.write(file.toPath(), out, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/model/file/FileUserImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public boolean deleteLine(String target) {
try {
File file = createFile();
List<String> out = Files.lines(file.toPath())
.filter(line -> !line.contains(target.toLowerCase()))
.filter(line -> !line.split(";")[0].contains(target.toLowerCase()))
.collect(Collectors.toList());

Files.write(file.toPath(), out, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/view/EditItemView.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public void actionPerformed(ActionEvent e) {
String strP = textField_Price.getText();
price = (!strP.isEmpty()) ? Double.parseDouble(strP) : 0.0;

ItemImpl itemToAdd = new ItemImpl(barcode, name, quantity, price, null, null, category);
Item itemToAdd = new ItemImpl(barcode, name, quantity, price, null, null, category);
controller.updateItem(itemToAdd);

comboBox_ItemId.setSelectedIndex(0);
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/view/InvoiceView.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,10 @@ public void actionPerformed(ActionEvent arg0) {
if(!strTot.isBlank()) {
Double tot = Double.parseDouble(strTot);
int userId = Integer.parseInt(String.valueOf(comboBox_CustomerId.getSelectedItem()));
if(shopController.addPayment(userId, tot)) {
boolean payCheck = shopController.addPayment(userId, tot);
boolean opCheck = invoiceController.checkout();
if(payCheck && opCheck) {
JOptionPane.showMessageDialog(null, "Operation ended successfully!");
invoiceController.newInvoice();
comboBox_CustomerId.setSelectedIndex(0);
comboBox_Quantity.setSelectedIndex(0);
comboBox_ItemId.setSelectedIndex(0);
Expand Down

0 comments on commit 6985137

Please sign in to comment.