Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.InputMismatchException;
- import java.util.Scanner;
- public class H6_main {
- public static void main(String [] args){
- ProductRepository sailio = new Hirsipuu();
- int valinta = 0;
- double hinta = 0;
- String nimi;
- String arvaus;
- String syote;
- while(valinta < 10){
- Scanner sc = new Scanner(System.in);
- valinta = valinta + 1
- System.out.println("Arvaa kirjain :");
- arvaus = sc.nextLine();
- }
- }
- public static Product lueProduct(){
- Scanner sc = new Scanner(System.in);
- String nimi;
- System.out.print("Anna sana->");
- nimi = sc.nextLine();
- return new Product( nimi );
- }
- }
- }
- ------------------------------------------------
- import java.util.*;
- import java.io.*;
- class Hirsipuu implements Serializable{
- private Vector <Product> sailio;
- public Hirsipuu(){
- sailio = new Vector<>(3,1);
- }
- public void addProduct( Product prod ){
- sailio.addElement( prod );
- }
- public Product removeLast(){
- if(!sailio.isEmpty()){
- Product dProduct;
- dProduct = sailio.lastElement();
- int indeksi= sailio.lastIndexOf( dProduct );
- sailio.removeElementAt( indeksi );
- return dProduct;
- }
- return null;
- }
- public void tulosta(){
- Iterator<Product> iter = sailio.iterator();
- while( iter.hasNext()){
- System.out.println(iter.next());
- }
- }
- public boolean tallennaTiedostoon( String tiedostonNimi ){
- try (ObjectOutputStream oOut = new ObjectOutputStream( new FileOutputStream( tiedostonNimi ))){
- Iterator<Product> iter = sailio.iterator();
- while( iter.hasNext()){
- oOut.writeObject(iter.next());
- }
- return true;
- }catch( IOException ioe ){
- return false;
- }
- }
- public boolean lueTiedostosta( String tiedostonNimi ){
- try (ObjectInputStream oIn = new ObjectInputStream( new FileInputStream( tiedostonNimi ))){
- Product p =(Product)oIn.readObject();
- while ( p != null ){
- sailio.addElement( p );
- p =(Product)oIn.readObject();
- }
- return true;
- }catch( IOException | ClassNotFoundException e ){
- return false;
- }
- }
- }
- --------------------------------------------------
- import java.io.*;
- public class Sanalista implements Serializable{
- private String name;
- private double price;
- private int tuoteTunniste;
- public Product(final String name_par, final double price_par){
- this.name = name_par;
- this.price = price_par;
- this.tuoteTunniste = this.hashCode();
- System.out.println("Luotiin uusi olio: " + name + " hinta:" + price);
- }
- public void setName( final String name_par){
- this.name = name_par;
- }
- public String getName(){
- return this.name;
- }
- public void setPrice(final double price_par){
- this.price = price_par;
- }
- public double getPrice(){
- return this.price;
- }
- public String toString(){
- return "Tuotekoodi: "+ tuoteTunniste +"Tuote: " + this.name + " hinta: " + this.price;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement