java - Read data from a text file and create an object -


i need help: i'm making supermarket simulation on java, i've got 1 problem, have text file (stock.txt) have supermarket stock on example:

  • 0-bakery-chocolate cake-$12.5-250
  • 1-meat-premium steak-$2.6-120
  • 2-seafood-tuna - $1.2-14
  • ...

where first number "id" product, next department product belongs, third name of product, next thing price, , last number how pieces of product stock has. have class:

public class product {     protected string name;     protected double price;     protected string department;     protected int id;     protected int stock; } 

so, need read each line text file , create product, i.e. first line make this:

product product1 = new product(0,"bakery","chocolate cake", 12.5, 250);      

and add array

product[0] = product1; 

for things in text file, then, when running simulation each costumer buy random quantity of random products in stock, stock number decrease. finally, when simulation ends, program must write in same text file, modify quantity of each product.

the thing maybe it's easy have no idea of how this, because reading , writing file in java has been real problem me since started programming in java (i'm beginner). have ideas of using bufferedreader , stringtokenizer classes reading , creating object problems, can't figure out how it, , have no idea of how must overwritting problem. i'd appreciate help!

oh! way, need use arrays, using arraylist or other structure it's not choice :(

this job scanner read in data. far not being able use collections arraylist you'll have dynamically reallocate array yourself.

try following:

public static void main(string[] args) throws filenotfoundexception {     scanner input = new scanner(new file("stock.txt"));     input.usedelimiter("-|\n");      product[] products = new product[0];     while(input.hasnext()) {         int id = input.nextint();         string department = input.next();         string name = input.next();         double price = double.valueof(input.next().substring(1));         int stock = input.nextint();          product newproduct = new product(name, price, department, id, stock);         products = addproduct(products, newproduct);     }      (product product : products) {         system.out.println(product);     } }  private static product[] addproduct(product[] products, product producttoadd) {     product[] newproducts = new product[products.length + 1];     system.arraycopy(products, 0, newproducts, 0, products.length);     newproducts[newproducts.length - 1] = producttoadd;      return newproducts; }  public static class product {     protected string name;     protected double price;     protected string department;     protected int id;     protected int stock;      private static numberformat formatter = new decimalformat("#0.00");      public product(string n, double p, string d, int i, int s) {         name = n;         price = p;         department = d;         id = i;         stock = s;     }      @override     public string tostring() {         return string.format("id: %d\r\ndepartment: %s\r\nname: %s\r\nprice: %s\r\nstock: %d\r\n",                  id, department, name, formatter.format(price), stock);     } } 

results:

id: 0 department: bakery name: chocolate cake price: 12.50 stock: 250  id: 1 department: meat name: premium steak price: 2.60 stock: 120  id: 2 department: seafood name: tuna price: 1.20 stock: 14 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -