CB Work Home | Notes | CB MCQ Work | CB FRQ Work | CB Future Study Plan |
MCQ Work
MCQ Preperation
MCQ Work
- Key Topics to Focus on:
- ArrayLists
- Writing classes
- 2d Arrays
import java.util.*;
import java.util.Arrays;
class Item {
public String name;
public double price;
public Item(String name, double price) {
this.name = name;
this.price = price;
}
}
public class StoreInventorySystem {
private static final int ROWS = 2;
private static final int COLS = 2;
private static Item[][] shelves = new Item[ROWS][COLS];
private static ArrayList<Item> cart = new ArrayList<>();
StoreInventorySystem(){
setupStore();
displayShelves();
simulatePurchase();
displayCart();
}
private static void setupStore() {
shelves[0][0] = new Item("binary socks", 9.99);
shelves[0][1] = new Item("code code code computer", 599.99);
shelves[1][0] = new Item("code code code keyboard", 79.99);
shelves[1][1] = new Item("Slack Pro", 12.99);
}
private static void displayShelves() {
System.out.println("\nStore Shelves:");
String[][] display = new String[ROWS][COLS];
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
display[i][j] = shelves[i][j].name;
}
}
System.out.println(Arrays.deepToString(display));
}
private static void simulatePurchase() {
cart.add(shelves[0][0]); // binary socks
cart.add(shelves[1][1]); // Slack Pro
}
public static void displayCart() {
System.out.println("\nShopping Cart:");
double total = 0;
for (Item item : cart) {
System.out.println("- " + item.name + " ($" + item.price + ")");
total += item.price;
}
System.out.println("Total: "+ total);
}
}
StoreInventorySystem store= new StoreInventorySystem();
// store.displayCart();
Store Shelves:
[[binary socks, code code code computer], [code code code keyboard, Slack Pro]]
Shopping Cart:
- binary socks ($9.99)
- Slack Pro ($12.99)
Total: 22.98