ITI0011-2015:harjutus 06

Allikas: Kursused
Redaktsioon seisuga 7. märts 2015, kell 01:03 kasutajalt Ago (arutelu | kaastöö) (Uus lehekülg: '== Mall == <source lang="java"> import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * Caching to file. * You have a file of weigh...')
(erin) ←Vanem redaktsioon | Viimane redaktsiooni (erin) | Uuem redaktsioon→ (erin)
Mine navigeerimisribale Mine otsikasti

Mall

<source lang="java"> import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader;

/**

* Caching to file.
* You have a file of weights. Every person has a weight
* in kilograms.
* All the names are unique (there are no duplicate names).
* A name is connected to the weight. Example of file:

*

 * mati 100
 * kati 70
 * 
* 
* This means that mati has a weight of 100 and 
* kati has a weight of 70. If there are names which
* have the same weight, those should be grouped together
* in one line:

*

 * mati juri 100
 * kati kerli 70
 * 
* 
* The general format of the file is:

*

 * name1 [name2] [name3] weight1
 * name4 [name5] [name6] weight2
 * 
* 
* Every line has at least one name and weight.
* A line can consist of additional names
* (all names come before weight). Names consist
* only of characters (not numbers, spaces, commas etc.).
* 
* @author Ago
*
*/

public class EX06 {

/** * The main part. * In this exercise, the main code is provided. * You can modify this code as this method is not tested. * * @param args Command line arguments * @throws IOException Exception which can happen during input reading */ public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader( new InputStreamReader(System.in));

String filename = "weights.txt"; while (true) { System.out.println("Name:"); String name = reader.readLine(); if (name == null || name.equals("")) { writeWeights(filename); System.out.println("exiting"); break; } if (name.contains(" ")) { System.out.println("no spaces allowed!"); continue; } Integer weight = getWeight(name); if (weight == null) { System.out.println("Person does not exist in DB."); while (true) { System.out.println("Weight:"); String weightLine = reader.readLine(); if (weightLine != null) { try { weight = Integer.parseInt(weightLine); if (weight > 10 && weight < 200) { setWeight(name, weight); break; } } catch (NumberFormatException e) {

} }

}

} } reader.close(); }

/** * Sets the weight of the person with the given name. * The weight should be stored in a class variable. * This method should not try write anything to the file. * @param name The name of the person * @param weight The weight of the person */ public static void setWeight(String name, Integer weight) { // TODO: you should store name and weight somehow // tip: HashMap is probably a good idea }

/** * Opens a file with the given name and reads all * the data from it. * @param filename */ public static void readWeights(String filename) { String fileContents = ""; // TODO: read file contents


parseWeights(fileContents); }

/** * Parses names and weights from the given string. * This method is usually called by readWeights(String).

*

* This method should not read anything from the file. * All the information needed for this method is provided * as a String argument. *

* The information should be stored in a class variable, * which might be for example HashMap. * * @param contents File contents to parse */ public static void parseWeights(String contents) { // TODO: implement // here you have to split the contents into // names and weights and store them in a // class variable for later use. } /** * Writes weight information from a class variable * into file. * @param filename The filename to be written. * Notice! the file will be overwriten. */ public static void writeWeights(String filename) { // TODO: implement // open the file // write information into file // close the file } /** * Given the person name, the method should return the person's weight. * If the weight does not exist (in a class variable, which also means * that it does not exist in file), NULL is returned. * @param name Person name * @return Weight if the person name exists in the cache (file), * NULL otherwise. */ public static Integer getWeight(String name) { // name exists? // if yes, return the weight // if no, return null return null; } } </source>