0

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");
}


     }
     }
  • I think it's time to learn about Maps... https://docs.oracle.com/javase/tutorial/collections/interfaces/map.html – Jaroslaw Pawlak Mar 19 '18 at 17:38
  • [Reading a .txt file using Scanner class](https://stackoverflow.com/questions/13185727/reading-a-txt-file-using-scanner-class-in-java) – user7 Mar 19 '18 at 17:40
  • I'm really missing any kind of attempt at actually reading the data from a file. Where is that in your code, what issue are you having trouble with specifically? – markspace Mar 19 '18 at 17:50
  • There is a lot of issues with your code, but some are really easy to fix and will probably change considerably your result on this assignment. First make sure to have correct indentation, second meaningful variable naming (not flag), and third don't catch Exception, only catch the exception that you need to catch (here NumberFormatException), fourth use methods, do not put everything in the same place. If you fix all those issues (which shouldn't take long) it will help a lot. – Nyamiou The Galeanthrope Mar 19 '18 at 18:11

1 Answers1

0

So, first instead of using multiple arrays of primitives, use one java.util.List of Java objects. You can have a Java class called Product with the fields name and price and getters and setters and then create one Product instance for each of your products and put them in the list. It's doesn't seems like much now, but you'll see this will make making changes to your code much more easier. Like this:

public class Product {
    private String name;
    private Integer price;

    public Product(String name, Integer price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {return name;}
    public Integer getPrice() {return price;}
    public void setName(String name) {this.name = name;}
    public void setPrice(Integer price) {this.price = price;}
}

Now on the big part, as I understand you have to load the product list from a file, it's suggest using a properties files to do that, it's a file with on each line a key, the equals sign and a value, and there is already utilities on Java to parse those files, java.util.Properties. There are not exactly designed for this kind of job, but if you are short on time and you don't know how to create a parser yourself, this might be your best solution. You can do something like:

Properties prop = new Properties();
try (InputStream stream = new FileInputStream("path/filename")) {
  prop.load(stream);
} finally {
  stream.close();
}
List<Product> products = new ArrayList<>();
for(Object key : prop.keySet()) {
    String name = (String) key;
    Integer price = Integer.valueOf((String) prop.getProperty(key));
    products.add(new Product(name, price));
}