-
Notifications
You must be signed in to change notification settings - Fork 627
/
Copy pathstockPortfolioTracker.java
92 lines (80 loc) · 3.03 KB
/
stockPortfolioTracker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Stock {
private String symbol;
private String name;
private double price;
private int quantity;
public Stock(String symbol, String name, double price, int quantity) {
this.symbol = symbol;
this.name = name;
this.price = price;
this.quantity = quantity;
}
public double getValue() {
return price * quantity;
}
public String getSymbol() {
return symbol;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public int getQuantity() {
return quantity;
}
}
public class StockPortfolioTracker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Stock> portfolio = new ArrayList<>();
while (true) {
System.out.println("Stock Portfolio Tracker Menu:");
System.out.println("1. Add Stock");
System.out.println("2. View Portfolio");
System.out.println("3. Exit");
System.out.print("Select an option (1/2/3): ");
int option = scanner.nextInt();
switch (option) {
case 1:
scanner.nextLine();
System.out.print("Enter stock symbol: ");
String symbol = scanner.nextLine();
System.out.print("Enter stock name: ");
String name = scanner.nextLine();
System.out.print("Enter stock price: $");
double price = scanner.nextDouble();
System.out.print("Enter quantity: ");
int quantity = scanner.nextInt();
portfolio.add(new Stock(symbol, name, price, quantity));
System.out.println("Stock added successfully.");
break;
case 2:
if (portfolio.isEmpty()) {
System.out.println("Your portfolio is empty.");
} else {
System.out.println("Stock Portfolio:");
for (Stock stock : portfolio) {
System.out.println("Symbol: " + stock.getSymbol());
System.out.println("Name: " + stock.getName());
System.out.println("Price: $" + stock.getPrice());
System.out.println("Quantity: " + stock.getQuantity());
System.out.println("Value: $" + stock.getValue());
System.out.println();
}
}
break;
case 3:
System.out.println("Exiting the Stock Portfolio Tracker.");
scanner.close();
System.exit(0);
default:
System.out.println("Invalid option. Please select 1, 2, or 3.");
}
}
}
}