Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package projectgrandmother;
- public class Product {
- private String name;
- private int amount;
- private float price;
- private float value;
- public Product(String name, int amount, float price) {
- this.name = name;
- this.amount = amount;
- this.price = price;
- }
- public Product() {
- }
- public float getValue() {
- return amount * price;
- }
- public void setValue(float value){
- this.value = value;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAmount() {
- return amount;
- }
- public void setAmount(int amount) {
- this.amount = amount;
- }
- public float getPrice() {
- return price;
- }
- public void setPrice(float price) {
- this.price = price;
- }
- @Override
- public String toString() {
- return "Product{" +
- "name='" + name + '\'' +
- ", amount=" + amount +
- ", price=" + price +
- ", value=" + value +
- '}';
- }
- }
- /////////////////////////////
- import java.util.ArrayList;
- import java.util.List;
- public class ListGrandpa {
- private String listString;
- private List<Product> products;
- public ListGrandpa(String listString) {
- this.listString = listString;
- products = new ArrayList<>();
- splitList();
- }
- public void splitList() {
- String[] list = listString.split("/");
- for(String s : list) {
- String[] properties = s.split(":");
- Product product = new Product();
- product.setName(properties[0]);
- product.setAmount(Integer.valueOf(properties[1]));
- product.setPrice(Float.valueOf(properties[2]));
- float value = product.getPrice() * product.getAmount();
- product.setValue(value);
- if(isExist(product.getName())){
- System.out.println("Produkt: " + product.getName() + " już był kupiony");
- }else{
- products.add(product);
- }
- }
- }
- public Product maxAmount() {
- int max = 0;
- Product product = null;
- for(Product p : products) {
- if(p.getAmount() > max) {
- max = p.getAmount();
- product = p;
- }
- }
- return product;
- }
- public boolean isExist(String name){
- for(Product p : products){
- if(p.getName().equals(name)) {
- return true;
- }
- }
- return false;
- }
- public float valueOfShopping(){
- float value = 0;
- for(Product p : products){
- value += p.getValue();
- }
- return value;
- }
- public List<Product> getProducts() {
- return products;
- }
- public void setProducts(List<Product> products) {
- this.products = products;
- }
- @Override
- public String toString() {
- return "ListGrandpa{" +
- "products=" + products +
- '}';
- }
- }
- ///////////////////////////
- public class Main {
- public static void main(String[] args) {
- try {
- StringBuilder shoppingList = new StringBuilder();
- FileInputStream fileInputStream = new FileInputStream("C:\\Bootcamp\\babcia.txt");
- int read;
- while ((read = fileInputStream.read()) != -1) {
- shoppingList.append((char) read);
- }
- ListGrandpa listGrandpa = new ListGrandpa(shoppingList.toString());
- System.out.println("Najwięcej kupionych " + listGrandpa.maxAmount().getName() + " wartość to: " + listGrandpa.maxAmount().getAmount());
- System.out.println("Wartość zakupów" + listGrandpa.valueOfShopping());
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement