I have to do an assignment for school that is basically a small online shop with 10 items.
The way I've done it is with having 2 premade arrays that include the price, and product names. Then I made an array for storing user inputs and an array for storing the multiplied result of input by price. The program works, however the problem is we needed to have a separate file that will contain the item and price list and then call it by using a separate method.
I've been trying to figure out a way to do it but it's a bit above my head to be honest and I'm running out of time. If anyone can advise me how I could separate my functions and product/price lists using methods it would be much appreciated.
This is my program
import java.util.Scanner;
public class dunnes{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
String product[]={"Coat", "Jeans", "Shirt", "Shoes", "Skirt", "Dress", "Socks","Scarf", "Handbag","Vest"};
double price[]={99.99, 53.59, 9.99, 29.99, 24.99, 83.16, 5.99, 10.95, 23.99, 18.99};
int howMany[]=new int[10];
double multiplied[]=new double[10];
int i =0;
boolean err = false;
boolean flag = true;
for(i = 0; i<price.length; i++){
System.out.print(+i+1+ " Please enter how many ");
System.out.print(product[i]+ " you would like to buy (");
System.out.print(price[i]+ ") euro each \n");
do{
err = false;
try{
howMany[i] = Integer.parseInt(in.next());
}catch (Exception e){
err = true;
System.out.print("Incorrect input. Must be whole numbers. \n");
System.out.print(+i+1+ " Please enter how many ");
System.out.print(product[i]+ " you would like to buy (");
System.out.print(price[i]+ ") euro each \n");
}
}while(err);
}
for(i = 0; i<price.length; i++){
multiplied[i]=howMany[i]*price[i];
multiplied[i] = Math.round(multiplied[i] * 100.0) / 100.0;
}
System.out.print("\nUnsorted total bill:\n\n");
for(i = 0; i<price.length; i++){
System.out.print(product[i]+"\t\t");
System.out.print(""+multiplied[i]+"\n");}
while(flag){
flag=false;
for(i=0;i<multiplied.length-1;i++){
if(multiplied[i]>multiplied[i+1]){
double temp = multiplied[i];
multiplied[i] = multiplied[i+1];
multiplied[i+1]= temp;
String temp2=product[i];
product[i]=product[i+1];
product[i+1]=temp2;
flag = true;
}
}
}
System.out.print("\nSorted total bill:\n\n");
for(i = 0; i<price.length; i++){
System.out.print(product[i]+"\t\t");
System.out.print(""+multiplied[i]+"\n");
}
}
}