Code about leather company it expected of you create an application for company that will allow them to view the list of lounge suite, update the price of a lounge suite , as well finding the cheapest 3 seater lounge suite
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String[] names = {"Modern", "Classic", "Triangle", "Transformer"};
double[] prices = {1799, 1500, 2000, 2499};
Scanner in = new Scanner(System.in);
while (true) {
System.out.println("\n1. View List\n2. Update price\n3. Show 3 the cheapest\n0. Exit");
switch (in.nextLine()) {
case "1":
for (int i = 0; i < names.length; i++) {
System.out.println(i + 1 + ". " + names[i] + " $" + prices[i]);
}
break;
case "2":
System.out.println("Which one (1-" + prices.length + "):");
int index = Integer.parseInt(in.nextLine());
System.out.println("New price:");
double newPrice = Double.parseDouble(in.nextLine());
prices[index - 1] = newPrice;
break;
case "3":
int size = 3;
int[] min = new int[Math.min(prices.length, size)];
for (int i = 0; i < prices.length; i++) {
if (i < size) {
min[i] = i;
} else {
for (int j = 0; j < min.length; j++) {
if (prices[min[j]] > prices[i]) {
boolean highest = true;
for (int k = 0; k < min.length; k++) {
if (k != j && prices[min[j]] < prices[min[k]]) {
highest = false;
break;
}
}
if (highest) {
min[j] = i;
break;
}
}
}
}
}
for (int i = 0; i < min.length - 1; i++) {
int minI = i;
for (int j = i + 1; j < min.length; j++) {
if (prices[min[minI]] > prices[min[j]]) {
minI = j;
}
}
int tmp = min[minI];
min[minI] = min[i];
min[i] = tmp;
}
for (int i = 0; i < min.length; i++) {
System.out.println(i + 1 + ". " + names[min[i]] + " $" + prices[min[i]]);
}
break;
case "0":
System.exit(0);
}
}
}
}
Comments
Leave a comment