forked from dejediah/java1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslay.java
More file actions
70 lines (60 loc) · 2.83 KB
/
slay.java
File metadata and controls
70 lines (60 loc) · 2.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
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package com.mycompany.asdfkljalksdfj;
/**
*
* @author armin
*/
public class slay {
//bubble sort algorithm
static void bubbleSort(int[] arr, String[] arr2) {
int n = arr.length;
int temp = 0;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(arr[j-1] > arr[j]){
//swaps string elements whenever a comparison is made
String temp2 = arr2[j-1];
arr2[j-1] = arr2[j];
arr2[j] = temp2;
//swaps integer elements whenever a comparison is made
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
}
public static void main(String[] args) {
//use *INT* arr[] for the product amount input
int arr[] ={3,60,35,2,45,320,5};
//use *STRING* arr2[] for the product name input
String arr2[] = {"apple", "orange", "banana", "lotion", "hairspray", "crackers", "oil"};
System.out.println("Unsorted Products");
for(int i=0; i < arr.length; i++){
System.out.print("Product name: " + arr2[i] + " | " + "Product amount: "+ arr[i] + "\n");
System.out.println("-----------");
}
System.out.println();
bubbleSort(arr, arr2);//sorting array elements using bubble sort
//add user input for ASCENDING, DESCENDING, and SEARCH
/*
Menu should look like:
Hello! What would you like to do with the inventory?
1. Search for specific product.
2. Sort inventory in ascending order.
3. Sort inventory in descending order.
*/
System.out.println("Sorted Products (Ascending)");
for(int i=0; i < arr.length; i++){
System.out.println("Product name: " + arr2[i] + " " + "\nProduct amount: "+ arr[i]);
System.out.println(" ");
}
System.out.println("Sorted Products (Descending)");
for(int i= (arr.length - 1); i > 0; i--){
System.out.println("Product name: " + arr2[i] + " " + "\nProduct amount: "+ arr[i]);
System.out.println(" ");
}
}
}