Advertisement
Guest User

Untitled

a guest
May 27th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1.  
  2. import java.util.InputMismatchException;
  3. import java.util.Scanner;
  4.  
  5. public class H6_main {
  6.  
  7. public static void main(String [] args){
  8.  
  9. ProductRepository sailio = new Hirsipuu();
  10. int valinta = 0;
  11. double hinta = 0;
  12. String nimi;
  13. String arvaus;
  14. String syote;
  15.  
  16. while(valinta < 10){
  17. Scanner sc = new Scanner(System.in);
  18. valinta = valinta + 1
  19. System.out.println("Arvaa kirjain :");
  20. arvaus = sc.nextLine();
  21.  
  22. }
  23.  
  24.  
  25. }
  26.  
  27.  
  28.  
  29.  
  30.  
  31. public static Product lueProduct(){
  32. Scanner sc = new Scanner(System.in);
  33. String nimi;
  34.  
  35. System.out.print("Anna sana->");
  36. nimi = sc.nextLine();
  37.  
  38.  
  39. return new Product( nimi );
  40.  
  41. }
  42.  
  43. }
  44. }
  45.  
  46. ------------------------------------------------
  47. import java.util.*;
  48. import java.io.*;
  49.  
  50. class Hirsipuu implements Serializable{
  51.  
  52. private Vector <Product> sailio;
  53.  
  54. public Hirsipuu(){
  55. sailio = new Vector<>(3,1);
  56. }
  57.  
  58. public void addProduct( Product prod ){
  59. sailio.addElement( prod );
  60. }
  61.  
  62. public Product removeLast(){
  63.  
  64. if(!sailio.isEmpty()){
  65. Product dProduct;
  66. dProduct = sailio.lastElement();
  67. int indeksi= sailio.lastIndexOf( dProduct );
  68. sailio.removeElementAt( indeksi );
  69. return dProduct;
  70. }
  71. return null;
  72.  
  73. }
  74.  
  75. public void tulosta(){
  76.  
  77. Iterator<Product> iter = sailio.iterator();
  78.  
  79. while( iter.hasNext()){
  80. System.out.println(iter.next());
  81. }
  82.  
  83. }
  84. public boolean tallennaTiedostoon( String tiedostonNimi ){
  85.  
  86. try (ObjectOutputStream oOut = new ObjectOutputStream( new FileOutputStream( tiedostonNimi ))){
  87.  
  88. Iterator<Product> iter = sailio.iterator();
  89.  
  90. while( iter.hasNext()){
  91. oOut.writeObject(iter.next());
  92. }
  93.  
  94. return true;
  95.  
  96. }catch( IOException ioe ){
  97. return false;
  98. }
  99.  
  100.  
  101.  
  102. }
  103.  
  104. public boolean lueTiedostosta( String tiedostonNimi ){
  105.  
  106. try (ObjectInputStream oIn = new ObjectInputStream( new FileInputStream( tiedostonNimi ))){
  107.  
  108. Product p =(Product)oIn.readObject();
  109. while ( p != null ){
  110. sailio.addElement( p );
  111. p =(Product)oIn.readObject();
  112. }
  113.  
  114.  
  115. return true;
  116.  
  117. }catch( IOException | ClassNotFoundException e ){
  118. return false;
  119. }
  120.  
  121. }
  122. }
  123.  
  124. --------------------------------------------------
  125.  
  126. import java.io.*;
  127.  
  128. public class Sanalista implements Serializable{
  129.  
  130. private String name;
  131. private double price;
  132. private int tuoteTunniste;
  133.  
  134.  
  135. public Product(final String name_par, final double price_par){
  136. this.name = name_par;
  137. this.price = price_par;
  138. this.tuoteTunniste = this.hashCode();
  139. System.out.println("Luotiin uusi olio: " + name + " hinta:" + price);
  140. }
  141.  
  142. public void setName( final String name_par){
  143. this.name = name_par;
  144. }
  145.  
  146. public String getName(){
  147. return this.name;
  148. }
  149.  
  150. public void setPrice(final double price_par){
  151. this.price = price_par;
  152. }
  153.  
  154. public double getPrice(){
  155. return this.price;
  156. }
  157.  
  158. public String toString(){
  159. return "Tuotekoodi: "+ tuoteTunniste +"Tuote: " + this.name + " hinta: " + this.price;
  160. }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement