-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainList.java
More file actions
158 lines (143 loc) · 4.83 KB
/
MainList.java
File metadata and controls
158 lines (143 loc) · 4.83 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// class ItemList {
// int itemNumber;
// String name;
// float price;
// int quantity;
// public ItemList(int itemNumber,String nm,float pr,int qty){
// this.itemNumber=itemNumber;
// this.name=nm;
// this.price=pr;
// this.quantity=qty;
// }
// public float getStockValue(){
// return price*quantity;
// }
// @Override
// public String toString(){
// return "Item Number: "+itemNumber+"\nName: "+name+"\nPrice: "+price+"\nQuantity: "+quantity;
// }
// }
// class itemStock{
// }
// public class itemList{
// public static void main(String[] args) {
// }
// }
import java.util.Scanner;
class ItemList {
int itemNumber;
String name;
float price;
int quantity;
public ItemList(int itemNumber, String name, float price, int quantity) {
this.itemNumber = itemNumber;
this.name = name;
this.price = price;
this.quantity = quantity;
}
public float getStockValue() {
return price * quantity;
}
// @Override
// public String toString() {
// return "ItemNumber: " + itemNumber + ", Name: " + name + ", Price: " + price + ", Quantity: " + quantity;
// }
}
class ItemStock {
ItemList[] items;
int itemCount;
public ItemStock(int size) {
items = new ItemList[size];
itemCount = 0;
}
public void addItem(ItemList item) {
if (itemCount < items.length) {
items[itemCount++] = item;
} else {
System.out.println("Stock is full. Cannot add more items.");
}
}
public void deleteItem(int itemNumber) {
for (int i = 0; i < itemCount; i++) {
if (items[i].itemNumber == itemNumber) {
for (int j = i; j < itemCount - 1; j++) {
items[j] = items[j + 1];
}
items[--itemCount] = null;
return;
}
}
System.out.println("Item not found.");
}
public float calculateStockValue() {
float totalValue = 0;
for (int i = 0; i < itemCount; i++) {
totalValue += items[i].getStockValue();
}
return totalValue;
}
public void displayItemsDescendingOrder() {
for (int i = 0; i < itemCount - 1; i++) {
for (int j = 0; j < itemCount - i - 1; j++) {
if (items[j].price < items[j + 1].price) {
ItemList temp = items[j];
items[j] = items[j + 1];
items[j + 1] = temp;
}
}
}
for (int i = 0; i < itemCount; i++) {
System.out.println(items[i]);
}
}
}
public class MainList {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of items: ");
int n = scanner.nextInt();
ItemStock itemStock = new ItemStock(n);
int choice;
do {
System.out.println("Menu:");
System.out.println("1. Add Item");
System.out.println("2. Delete Item");
System.out.println("3. Calculate Stock Value");
System.out.println("4. Display Items in Descending Order of Price");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter item number: ");
int itemNumber = scanner.nextInt();
scanner.nextLine(); // consume newline
System.out.print("Enter item name: ");
String name = scanner.nextLine();
System.out.print("Enter item price: ");
float price = scanner.nextFloat();
System.out.print("Enter item quantity: ");
int quantity = scanner.nextInt();
itemStock.addItem(new ItemList(itemNumber, name, price, quantity));
break;
case 2:
System.out.print("Enter item number to delete: ");
int deleteItemNumber = scanner.nextInt();
itemStock.deleteItem(deleteItemNumber);
break;
case 3:
System.out.println("Total Stock Value: " + itemStock.calculateStockValue());
break;
case 4:
itemStock.displayItemsDescendingOrder();
break;
case 5:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 5);
scanner.close();
}
}