Issue
I am doing a Java project - Budget manager, which shows how I spend my money.
For that purpose, I used Hashmap
to store the type of purchase with parameters of String(enum), and Arraylist<Product>
. Product is a new class where name and price are to be passed.
Map<String, Arraylist<Product>> mapProducts = new HashMap<>();
Now I just can't figure out how to pass that array to Map.
switch (choice) {
case 1:
mapProducts.put(Product.Type.FOOD.name(),//add array);
break;
case 2:
mapProducts.put(Product.Type.CLOTHES.name(), );
break;
case 3:
mapProducts.put(Product.Type.ENTERTAINMENT.name(), );
break;
case 4:
mapProducts.put(Product.Type.OTHER.name(), );
break;
}
import java.util.*;
public class Account {
private ArrayList<String> listOfPurchases;
private ArrayList<Double> itemPrice;
private ArrayList<Product> products;
private double balance;
private Scanner input = new Scanner(System.in);
private Map<String, ArrayList<Product>> mapProducts = new HashMap<>();
public Account() {
this.listOfPurchases = new ArrayList<>();
this.itemPrice = new ArrayList<>();
this.balance = 0.00d;
}
public void mainMenuPrint() {
System.out.println(
"Choose your action:\n" +
"1) Add income\n" +
"2) Add purchase\n" +
"3) Show list of purchases\n" +
"4) Balance\n" +
"0) Exit");
}
public void startMenu() {
Scanner input = new Scanner(System.in);
boolean isOn = true;
while (isOn) {
mainMenuPrint();
int choice = input.nextInt();
switch (choice) {
case 0:
System.out.println("Bye!");
isOn = false;
break;
case 1:
addIncome();
break;
case 2:
addPurchase();
break;
case 3:
showPurchases();
break;
case 4:
showBalance();
break;
}
}
}
public void addIncome() {
double addMoney = 0.00d;
System.out.println("Enter income:");
addMoney = input.nextDouble();
setBalance(addMoney);
System.out.println("Income was added!");
System.out.println("");
}
public void addPurchase() {
System.out.println(
"Choose the type of purchase\n" +
"1) Food\n" +
"2) Clothes\n" +
"3) Entertainment\n" +
"4) Other\n" +
"5) Back\n");
int choice = input.nextInt();
double price = 0.0d;
if (choice == 5) {
startMenu();
} else {
System.out.println("Enter purchase name:");
String item = input.next();
setListOfPurchases(item);
System.out.println("Enter its price:");
try {
price = input.nextDouble();
setItemPrice(price);
balanceSubtractFromPurchasePrice(price);
} catch (
InputMismatchException e) {
System.out.println("Wrong input");
}
switch (choice) {
case 1:
mapProducts.put(Product.Type.FOOD.name(),//add array);
break;
case 2:
mapProducts.put(Product.Type.CLOTHES.name(), );
break;
case 3:
mapProducts.put(Product.Type.ENTERTAINMENT.name(), );
break;
case 4:
mapProducts.put(Product.Type.OTHER.name(), );
break;
}
System.out.println("");
}
}
public void showPurchases() {
double sum = 0.0d;
if (mapProducts.size() == 0) {
System.out.println("The purchase list is empty");
} else {
System.out.println(
"Choose the type of purchase\n" +
"1) Food\n" +
"2) Clothes\n" +
"3) Entertainment\n" +
"4) Other\n" +
"5) All\n" +
"6) Back\n");
int choice = input.nextInt();
for (Map.Entry<String, ArrayList<Product>> entry : mapProducts.entrySet()) {
System.out.println(entry.getKey());
System.out.println(mapProducts.get("FOOD").get(0));
}
for (int i = 0; i < listOfPurchases.size(); i++) {
sum += itemPrice.get(i);
}
System.out.println("Total sum: $" + sum);
}
System.out.println("");
}
public void showBalance() {
System.out.printf("Balance: $%.2f", getBalance());
System.out.println("");
System.out.println("");
}
public void setBalance(double balanceAmount) {
this.balance += balanceAmount;
}
public void balanceSubtractFromPurchasePrice(double purchasePrice) {
this.balance -= purchasePrice;
}
public double getBalance() {
if (balance < 0) {
System.out.println("Balance is negative ");
}
return this.balance;
}
public void setListOfPurchases(String item) {
this.listOfPurchases.add(item);
}
public ArrayList<String> getListOfPurchases() {
return listOfPurchases;
}
public ArrayList<Double> getItemPrice() {
return itemPrice;
}
public void setItemPrice(double itemPrice) {
this.itemPrice.add(itemPrice);
}
}
public class Product {
enum Type {
FOOD,
CLOTHES,
ENTERTAINMENT,
OTHER;
}
private String name;
private double price;
private Scanner input = new Scanner(System.in);
public Product(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
}
Thanks for the help!
Solution
Instead of saving name and price in two separate list just create a product and place it directly into the map using computeIfAbsent
.
System.out.println("Enter purchase name:");
String item = input.next();
System.out.println("Enter its price:");
try {
price = input.nextDouble();
balanceSubtractFromPurchasePrice(price);
// Lazy replacement for complicated switch case.
if(choice >= 1 && choice <= 4) {
Product.Type productType = Product.Type.values()[choice - 1];
Product product = new Product(item, price);
List<Product> products = mapProducts.computeIfAbsent(productType, k -> new ArrayList<>());
products.add(product);
} else {
System.out.println("Wrong input");
}
} catch (InputMismatchException e) {
System.out.println("Wrong input");
}
System.out.println("");
}
Answered By - magicmn
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.