Skip to content
Snippets Groups Projects
Commit 6637f1b7 authored by Grethe Sandstrak's avatar Grethe Sandstrak
Browse files

legge til filene til workshoppen

parents
Branches master
No related tags found
No related merge requests found
Showing
with 1139 additions and 0 deletions
class By implements java.io.Serializable{
private String navn;
private String ordfrer;
private int antallInnbyggere;
private int maksAntallInnbyggere = 1000;
public By(String navn, String ordfrer, int antallInnbyggere, int maksAntallInnbyggere) throws Exception{
this.navn = navn;
this.ordfrer = ordfrer;
if (antallInnbyggere <= maksAntallInnbyggere){
this.antallInnbyggere = antallInnbyggere;
this.maksAntallInnbyggere = maksAntallInnbyggere;
} else throw new Exception("Byen kan ikke ha flere innbyggere enn det er plass til!");
}
public By (){}
public String getNavn(){ return navn; }
public void setNavn(String newValue) { navn = newValue; }
public String getOrdfrer(){ return ordfrer; }
public void setOrdfrer(String newValue) { ordfrer = newValue; }
public int getAntallInnbyggere(){return antallInnbyggere;}
public void setAntallInnbyggere(int newValue){ maksAntallInnbyggere = newValue;}
public int getMaksAntallInnbyggere(){ return maksAntallInnbyggere;}
public void setMaksAntallInnbyggere(int newValue){maksAntallInnbyggere = newValue;}
public String toString() {
return navn + " " + ordfrer + " " + antallInnbyggere + " " + maksAntallInnbyggere;
}
public boolean equals(Object obj) {
if (this==obj) return true;
if (obj instanceof By) {
By by = (By) obj;
if (by.getNavn().equals(navn)) return true;
}
return false;
}
public int getLedigKapasitet(){return maksAntallInnbyggere - antallInnbyggere;}
public boolean ledig(){ return (antallInnbyggere <= maksAntallInnbyggere);}
}
\ No newline at end of file
class Land implements java.io.Serializable{
private String navn;
private String statsoverhode;
private By[] byer; // hovedstad indeks 0
private int antallbyer = 0;
public Land(String navn, String statsoverhode, int maksAntallByer) {
this.navn = navn;
this.statsoverhode = statsoverhode;
if (maksAntallByer > 0) {
byer = new By[maksAntallByer];
antallbyer = 0;
}
}
public Land(String navn, String statsoverhode, By[] byer) throws Exception{
this.navn = navn;
this.statsoverhode = statsoverhode;
/* komposisjon */
if (byer != null) {
this.byer = new By[byer.length];
for (int i = 0; i < byer.length; i++) {
this.byer[i] = new By(byer[i].getNavn(), byer[i].getOrdfrer(), byer[i].getAntallInnbyggere(), byer[i].getMaksAntallInnbyggere());
}
}
}
public String getNavn() { return navn; }
public void setNavn(String newValue) {navn = newValue;}
public String getStatsoverhode() { return statsoverhode; }
public void setStatsoverhode(String newValue) {statsoverhode = newValue; }
public String toString() {
String res = navn + ", Statsoverhode: " + statsoverhode + "\n Byer:\n---------------------\n";
for (By b : byer) {
if (b != null) res += b + "\n";
}
return res;
}
private boolean regFr(By by){
if (by != null) {
// sjekk at by ikke er registrert fra fr
for (int i=0; i<antallbyer; i++) {
if (byer[i]!=null && byer[i].equals(by)) {
return true; // by er allerede registrert
}
}
}
return false; // ikke registrert fr
}
public boolean regBy(By by) throws Exception {
if (by != null) {
if (regFr(by)) return false; // by er allerede registrert
if (antallbyer < byer.length) {
byer[antallbyer] = new By(by.getNavn(), by.getOrdfrer(), by.getAntallInnbyggere(), by.getMaksAntallInnbyggere());
return false;
}
}
return true;
}
public int getAntallInnbyggere() {
int antall = 0;
for (int i=0; i<antallbyer; i++) {
antall += byer[i].getAntallInnbyggere();
}
return antall;
}
public By[] getByerMedPlass(int antall) throws Exception{
By[] tmp = new By[byer.length];
int j= 0;
for(int i=0; i<antallbyer; i++){
if(byer[i].getLedigKapasitet()<= antall){
tmp[j] = new By(byer[i].getOrdfrer(), byer[i].getNavn(), byer[i].getAntallInnbyggere(), byer[i].getMaksAntallInnbyggere());
i++;
}
}
if (j==byer.length) return tmp;
else{
By[] returTab = new By[j];
for(int i=0; i<j; i++){
returTab[i] =new By(tmp[i].getNavn(), tmp[i].getOrdfrer(), tmp[i].getAntallInnbyggere(), tmp[i].getMaksAntallInnbyggere());
}
return returTab;
}
}
private String[] getOrdfrere(){
String[] ordfrere = new String[antallbyer];
for(int i=0; i<antallbyer; i++){
ordfrere[i] = byer[i].getOrdfrer();
}
return ordfrere;
}
public String[] getSortertOrdfrerListe() {
String[] ordfrere = getOrdfrere();
for(int start=0; start<ordfrere.length; start++){
int hittilMinst=start;
for(int j=start; j<ordfrere.length; j++){
if(ordfrere[j].compareTo(ordfrere[hittilMinst]) <= 0) hittilMinst=j;
}
// bytt plass start og hittilminst
String tmp = ordfrere[start];
ordfrere[start] = ordfrere[hittilMinst];
ordfrere[hittilMinst] = tmp;
}
return ordfrere;
}
private boolean gyldigIndeks(int indeks){
if (indeks >=0 && indeks<=antallbyer) return true;
else return false;
}
public By getBy(int indeks){
if (gyldigIndeks(indeks)) return byer[indeks];
else return null;
}
}
\ No newline at end of file
import static javax.swing.JOptionPane.*;
import java.io.*;
class Land_Testklient {
public static Land lesFraFil(String filnavn) throws Exception {
try{
FileInputStream innstrom = new FileInputStream(filnavn);
ObjectInputStream inn = new ObjectInputStream(innstrom);
Land land = (Land)inn.readObject(); // kaster eofexception ved tom fil, FileNotFound
inn.close();
return land;
}catch(FileNotFoundException e){
System.out.println("fil ikke funnet!");
}catch(EOFException e){
System.out.println("fil funnet, men tom!");
}
return null;
}
public static boolean skrivTilFil(Land l, String filnavn){
boolean ok = false;
if (l != null){
try{
FileOutputStream utstrom = new FileOutputStream(filnavn); // true = append
ObjectOutputStream ut = new ObjectOutputStream(utstrom);
ut.writeObject(l);
ut.close();
ok = true;
}catch(FileNotFoundException e){
System.out.println("fil ikke funnet!");
}catch(Exception e){
System.out.println("Noe gikk galt med skriving til fil!");
}
}
return ok;
}
public static void main(String[] args) {
String[] muligheter = {"Nytt land", "Registrer by", "List ordfrere", "List alle byer", "Avslutt"};
final int NYTT_LAND = 0;
final int REG_BY = 1;
final int LIST_ORDFRERE = 2;
final int LIST_ALLE_BYER = 3;
final int AVSLUTT = 4;
int valg = showOptionDialog(null, "Velg operasjon", "Eksamen vr 2016", YES_NO_OPTION, INFORMATION_MESSAGE, null, muligheter, muligheter[0]);
Land land = null;
final String filnavn = "landdata.ser";
// les eventuelle data fra fil
try{
land = lesFraFil(filnavn);
if (land == null){ // legger inn noen testdata, her kunne en valgt ta inn brukerinput istd
land = new Land("Norge", "Kong Harald V", 10);
land.regBy(new By("Trondheim", "Otervik, Rita", 170000, 200000));
land.regBy(new By("Oslo", "Borgen, Marianne", 650000, 700000));
land.regBy(new By("Stjrdal", "Vigdenes, Ivar", 23308, 23400));
land.regBy(new By("Verdal", "Iversen, Bjrn", 14855, 15000));
}
}catch(Exception e){
System.out.println("noe gikk galt ved lesing fra fil" + e.toString());
}
while (valg != AVSLUTT){
switch (valg){
case NYTT_LAND:
String navn = showInputDialog("Navn: ");
String statsoverhode = showInputDialog("Statsoverhode: ");
int antallByer = Integer.parseInt(showInputDialog("Maks antallbyer: "));
int sikker = showConfirmDialog(null, "Sikker? - Du vil slette alle tidligere registrerte data");
if (sikker == YES_OPTION) land = new Land(navn, statsoverhode, antallByer);
else showMessageDialog(null, "Avbryter");
break;
case REG_BY:
if (land != null){
String bynavn = showInputDialog("Navn: ");
String ordfrer = showInputDialog("Ordfrer(Etternavn, Fornavn): ");
int antallInnbyggere = Integer.parseInt(showInputDialog("Antall innbyggere: " ));
int maksAntallInnbyggere = Integer.parseInt(showInputDialog("Maks antall innbyggere: " ));
try{
boolean ok = land.regBy(new By(bynavn, ordfrer, antallInnbyggere, maksAntallInnbyggere));
if (ok) showMessageDialog(null, "By registrert");
else showMessageDialog(null, "By er registrert fra fr/ ikke plass til by/ feil inndata");
} catch(Exception e){
showMessageDialog(null, "Antall innbyggere kan ikke vre hyere enn maksAntallInnbyggere" + e.toString());
}
} else showMessageDialog(null, "Du m registrere ett land frst");
break;
case LIST_ORDFRERE:
if (land != null){
String[] oListe = land.getSortertOrdfrerListe();
if (oListe != null){
String res = "";
for (String s: oListe){
res += s + "\n";
}
showMessageDialog(null, "Ordfrere:\n----------------\n" + res);
}
} else showMessageDialog(null, "Du m registrere ett land frst");
break;
case LIST_ALLE_BYER:
if (land != null){
showMessageDialog(null, land);
} else showMessageDialog(null, "Du m registrere ett land frst");
default: break;
}
valg = showOptionDialog(null,"Velg operasjon","Eksamen vr 2016", DEFAULT_OPTION, PLAIN_MESSAGE, null, muligheter, muligheter[0]);
}
// skriv til fil ved avslutt
try{
if (land != null) skrivTilFil(land, filnavn);
System.out.println("Skriv til fil; " + land);
}catch(Exception e) {
System.out.println("Noe gikk galt ved skriving til fil. " + e.toString());
}
}
}
\ No newline at end of file
class By implements java.io.Serializable{
private String navn;
private String ordfrer;
private int antallInnbyggere;
private int maksAntallInnbyggere = 1000;
public By(String navn, String ordfrer, int antallInnbyggere, int maksAntallInnbyggere) throws Exception{
this.navn = navn;
this.ordfrer = ordfrer;
if (antallInnbyggere <= maksAntallInnbyggere){
this.antallInnbyggere = antallInnbyggere;
this.maksAntallInnbyggere = maksAntallInnbyggere;
} else throw new Exception("Byen kan ikke ha flere innbyggere enn det er plass til!");
}
public By (){}
public String getNavn(){ return navn; }
public void setNavn(String newValue) { navn = newValue; }
public String getOrdfrer(){ return ordfrer; }
public void setOrdfrer(String newValue) { ordfrer = newValue; }
public int getAntallInnbyggere(){return antallInnbyggere;}
public void setAntallInnbyggere(int newValue){ antallInnbyggere = newValue;}
public int getMaksAntallInnbyggere(){ return maksAntallInnbyggere;}
public void setMaksAntallInnbyggere(int newValue){maksAntallInnbyggere = newValue;}
public String toString() {
return navn + " " + ordfrer + " " + antallInnbyggere + " " + maksAntallInnbyggere;
}
public boolean equals(Object obj) {
if (this==obj) return true;
if (obj instanceof By) {
By by = (By) obj;
if (by.getNavn().equals(navn)) return true;
}
return false;
}
public int getLedigKapasitet(){return maksAntallInnbyggere - antallInnbyggere;}
public boolean ledig(){ return (antallInnbyggere <= maksAntallInnbyggere);}
}
\ No newline at end of file
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.*;
public class Land_Fasit_Test{
private static Land instance;
@BeforeClass
public static void setUpClass() throws Exception {
}
@AfterClass
public static void tearDownClass() throws Exception {
}
@Before
public void setUp()throws Exception {
//legge inn kjent data i tabellene
instance = new Land("Norge", "Kong Harald V", 4); // plass til kun en by
instance.regBy(new By("By1", "Ordfrer1, en", 10, 12));
instance.regBy(new By("By2", "Ordfrer2, to", 10, 100));
instance.regBy(new By("By3", "Ordfrer3, tre", 10, 200));
}
@After
public void tearDown() {
// tilbakestill
}
/* Test av metoden getBy */
@Test
public void testGetBy() throws Exception{
System.out.println("testGetBy()");
By result = instance.getBy(0);
By expResult = new By("By1", "Ordfrer1, en", 10, 12);
assertEquals(expResult, result);
}
/* Test av metoden getByerMedPlass */
@Test
public void testGetByerMedPlass() throws Exception{
System.out.println("testGetByerMedPlass()");
int antNyeInnbyggere = 80;
By[] expResult = {new By("By2", "Ordfrer2, to", 10, 100), new By("By3", "Ordfrer3, tre", 10, 200)};
By[] result = instance.getByerMedPlass(antNyeInnbyggere);
assertArrayEquals(expResult, result);
}
/* Test av metoden Land.regBy */
@Test (expected=Exception.class)// kan ikke opprette en by med for mange innbyggere
public void testregByForMangeInnbyggere() throws Exception{
System.out.println("testregByForMangeInnbyggere()");
// skal kaste unntak fordi byen har for mange innbyggere, unntaket kastes fra konstruktr i klassen By
instance.regBy(new By("TulleBy", "Hansen, Hege", 100, 10));
}
@Test // Normalsituasjon, reg en ok by
public void testregByOk() throws Exception{
System.out.println("testregByOk()");
// registrere en by - normalsituasjon skal vre ok
boolean result = instance.regBy(new By("Trondheim", "Otervik, Rita", 170000, 200000));
boolean expResult = true;
assertEquals(expResult, result);
}
@Test // kan ikke opprette samme by to ganger
public void testregByDuplikat() throws Exception{
System.out.println("testregByDuplikat()");
// registrere en by - normalsituasjon skal vre ok
instance.regBy(new By("Trondheim", "Otervik, Rita", 170000, 200000));
// registrere samme by to ganger - forventes feile
boolean result = instance.regBy(new By("Trondheim", "Otervik, Rita", 170000, 200000));
boolean expResult = false;
assertEquals(expResult, result);
}
@Test // kan ikke opprette flere byer enn det er plass til
public void testregByIkkePlass() throws Exception{
System.out.println("testregByIkkePlass()");
// registrere en by - normalsituasjon skal vre ok
instance.regBy(new By("Trondheim", "Otervik, Rita", 170000, 200000));
// registrere en by mer enn det er plass til, forventes feile
boolean result = instance.regBy(new By("Stjrdal", "Vigdenes, Ivar", 23308, 23400));
boolean expResult = false;
assertEquals(expResult, result);
}
public static void main(String args[]) {
org.junit.runner.JUnitCore.main(Land_Fasit_Test.class.getName()); // tas med dersom textpad ikke er konfigurert
}
}
\ No newline at end of file
class Land implements java.io.Serializable{
private String navn;
private String statsoverhode;
private By[] byer; // hovedstad indeks 0
private int antallbyer = 0;
public Land(String navn, String statsoverhode, int maksAntallByer) {
this.navn = navn;
this.statsoverhode = statsoverhode;
if (maksAntallByer > 0) {
byer = new By[maksAntallByer];
antallbyer = 0;
}
}
public Land(String navn, String statsoverhode, By[] byer) throws Exception{
this.navn = navn;
this.statsoverhode = statsoverhode;
/* komposisjon */
if (byer != null) {
this.byer = new By[byer.length];
for (int i = 0; i < byer.length; i++) {
this.byer[i] = new By(byer[i].getNavn(), byer[i].getOrdfrer(), byer[i].getAntallInnbyggere(), byer[i].getMaksAntallInnbyggere());
}
}
}
public String getNavn() { return navn; }
public void setNavn(String newValue) {navn = newValue;}
public String getStatsoverhode() { return statsoverhode; }
public void setStatsoverhode(String newValue) {statsoverhode = newValue; }
public String toString() {
String res = navn + ", Statsoverhode: " + statsoverhode + "\n Byer:\n---------------------\n";
for (By b : byer) {
if (b != null) res += b + "\n";
}
return res;
}
private boolean regFr(By by){
if (by != null) {
// sjekk at by ikke er registrert fra fr
for (int i=0; i<antallbyer; i++) {
if (byer[i]!=null && byer[i].equals(by)) {
return true; // by er allerede registrert
}
}
}
return false; // ikke registrert fr
}
public boolean regBy(By by) throws Exception {
if (by != null) {
if (regFr(by)) return false; // by er allerede registrert
if (antallbyer < byer.length) {
byer[antallbyer] = new By(by.getNavn(), by.getOrdfrer(), by.getAntallInnbyggere(), by.getMaksAntallInnbyggere());
antallbyer++;
return true;
}
}
return false;
}
public int getAntallInnbyggere() {
int antall = 0;
for (int i=0; i<antallbyer; i++) {
antall += byer[i].getAntallInnbyggere();
}
return antall;
}
public By[] getByerMedPlass(int antall) throws Exception{
By[] tmp = new By[byer.length];
int j= 0;
for(int i=0; i<antallbyer; i++){
if(byer[i].getLedigKapasitet()>= antall){
tmp[j] = new By(byer[i].getNavn(), byer[i].getOrdfrer(), byer[i].getAntallInnbyggere(), byer[i].getMaksAntallInnbyggere());
j++;
}
}
if (j==byer.length) return tmp;
else{
By[] returTab = new By[j];
for(int i=0; i<j; i++){
returTab[i] =new By(tmp[i].getNavn(), tmp[i].getOrdfrer(), tmp[i].getAntallInnbyggere(), tmp[i].getMaksAntallInnbyggere());
}
return returTab;
}
}
private String[] getOrdfrere(){
String[] ordfrere = new String[antallbyer];
for(int i=0; i<antallbyer; i++){
ordfrere[i] = byer[i].getOrdfrer();
}
return ordfrere;
}
public String[] getSortertOrdfrerListe() {
String[] ordfrere = getOrdfrere();
for(int start=0; start<ordfrere.length; start++){
int hittilMinst=start;
for(int j=start; j<ordfrere.length; j++){
if(ordfrere[j].compareTo(ordfrere[hittilMinst]) <= 0) hittilMinst=j;
}
// bytt plass start og hittilminst
String tmp = ordfrere[start];
ordfrere[start] = ordfrere[hittilMinst];
ordfrere[hittilMinst] = tmp;
}
return ordfrere;
}
private boolean gyldigIndeks(int indeks){
if (indeks >=0 && indeks<=antallbyer) return true;
else return false;
}
public By getBy(int indeks){
if (gyldigIndeks(indeks)) return byer[indeks];
else return null;
}
}
\ No newline at end of file
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author grethsan
*/
import java.util.*;
class AlfabetSpill {
public static void main(String[] args) {
try{
Kortstokk kortstokken = new Kortstokk();
Spill spillet = new Spill(new Spiller("Grethe"), new Spiller("Nils"));
String resultat = spillet.spillMangeOmganger(13);
System.out.println(resultat + "\n" + spillet.avsluttSpill());
} catch(Exception e){
System.out.println(e);
}
}
} // AlfabetSpill
/* Kjøring av programmet
Grethe trakk K. Nils trakk Y. De byttet ikke. Grethe har 1 p. og Nils har 0 p.
Grethe trakk M. Nils trakk R. De byttet ikke. Grethe har 2 p. og Nils har 0 p.
Grethe trakk V. Nils trakk U. De byttet. Grethe har 3 p. og Nils har 0 p.
Grethe trakk G. Nils trakk Q. De byttet ikke. Grethe har 4 p. og Nils har 0 p.
Grethe vant :)
*/
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author grethsan
*/
import java.util.*;
public class Kortstokk {
private Random tallTrekker = new Random();
public static final int forsteBokstav = (int) 'A';
public static final int sisteBokstav = (int) 'Z';
public int antallBokstaver = sisteBokstav - forsteBokstav+1; // For å få med Z..(+1)
ArrayList<Character> kortstokk = new ArrayList<Character>();
public Kortstokk(){
for (int i=forsteBokstav; i<=sisteBokstav; i++) {
kortstokk.add(new Character((char)i));
}
}
public ArrayList<Character> getKortstokk(){
return kortstokk;
}
public int getAntallBokstaver(){
return antallBokstaver;
}
public char getKort() throws Exception{
if (antallBokstaver > 0){
int tall = tallTrekker.nextInt(antallBokstaver);
char bokstav = kortstokk.get(tall);
kortstokk.remove(tall); // fjerner kort etterhvert som de brukes/ trekkes
antallBokstaver--;
return bokstav;
} else throw new Exception("Ikke flere kort igjen"); // else sier dette er litt ufornuftig
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author grethsan
*/
class Spill {
private Spiller spiller1;
private Spiller spiller2;
private Kortstokk kortstokken;
public Spill(Spiller spiller1, Spiller spiller2) {
this.spiller1 = spiller1;
this.spiller2 = spiller2;
kortstokken = new Kortstokk();
}
public String spillEnOmgang() throws Exception {
String resultat = "";
// Trekk et kort for hver av spillerne fra kortstokken
spiller1.setBokstav(kortstokken.getKort());
spiller2.setBokstav(kortstokken.getKort());
resultat += spiller1.getNavn() + " trakk " + spiller1.getBokstav() + ". ";
resultat += spiller2.getNavn() + " trakk " + spiller2.getBokstav() + ". ";
if (vilBeggeBytte(spiller1.getBokstav(), spiller2.getBokstav())){
bytteKort();
resultat += "De byttet. ";
} else resultat += "De byttet ikke. ";
beregnPoengForEnOmgang();
// resultat += spiller1.getNavn() + " har " + spiller1.getAntallPoeng() + " p. og "
// + spiller2.getNavn() + " har " + spiller2.getAntallPoeng() + " p.";
resultat += spiller1 + " og " + spiller2;
return resultat;
}
private boolean vilBeggeBytte(char a, char b) {
return a>'M' && b>'M';
}
// bytt kort med hverandre dersom begge har høyere en M.
public boolean bytteKort() {
char hjelp = spiller1.getBokstav();
spiller1.setBokstav(spiller2.getBokstav());
spiller2.setBokstav(hjelp);
return true;
}
public void beregnPoengForEnOmgang() {
if (spiller1.getBokstav() < spiller2.getBokstav()) spiller1.økAntallPoeng();
else if (spiller1.getBokstav() > spiller2.getBokstav()) spiller2.økAntallPoeng();
}
public String spillMangeOmganger(int antallOmganger) throws Exception{
if (antallOmganger * 2 > kortstokken.getAntallBokstaver()) throw new Exception("Det er ikke nok kort til så mange omganger");
String resultat = "";
for (int i = 0; i < antallOmganger; i++) {
resultat += '\n' + spillEnOmgang();
}
return resultat;
}
public String avsluttSpill(){
if(spiller1.getAntallPoeng()>spiller2.getAntallPoeng()) return spiller1.getNavn() + " vant:)";
else if (spiller2.getAntallPoeng() >spiller1.getAntallPoeng()) return spiller2.getNavn() + " vant:)";
else return "Det ble uavhgjort";
}
public String toString() {
return spiller1.toString() + '\n' + spiller2.toString();
}
}
\ No newline at end of file
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author grethsan
*/
class Spiller {
private String navn;
private char bokstav;
private int antallPoeng;
public Spiller(String startNavn) {
navn = startNavn;
antallPoeng = 0;
}
public String getNavn() {
return navn;
}
public char getBokstav() {
return bokstav;
}
public int getAntallPoeng() {
return antallPoeng;
}
public boolean setBokstav(char nyBokstav) {
if (nyBokstav >= Kortstokk.forsteBokstav && nyBokstav <= Kortstokk.sisteBokstav) {
bokstav = nyBokstav;
return true;
} else return false;
}
public void økAntallPoeng() {
antallPoeng++;
}
public String toString() {
return navn + " har bokstav: " + bokstav + " og antall Poeng: " + antallPoeng;
}
}
\ No newline at end of file
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.ArrayList;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* @author grethsan
*/
public class KortstokkTest {
private Kortstokk instance;
private static final int forsteBokstav = 'A';
private static final int sisteBokstav = 'Z';
public KortstokkTest() {
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
//lag ny kortstokken før hver test
instance = new Kortstokk();
}
@After
public void tearDown() {
}
@Test
public void testGetKortstokk(){ // test om kortstokken inneholder alle bokstavene i alfabetet, og ingen duplikater
System.out.println("testGetKortstokk()");
ArrayList<Character> result = instance.getKortstokk();
ArrayList<Character> expResult = new ArrayList<Character>();
for(int i=forsteBokstav; i<=sisteBokstav; i++){
expResult.add(new Character((char)i));
}
assertEquals(expResult, result);
}
@Test
public void testGetAntallBokstaver(){
System.out.println("testGetAntallBokstaver()");
int result = instance.getAntallBokstaver();
int expResult = sisteBokstav - forsteBokstav + 1;
assertEquals(expResult, result);
}
@Test
public void testGetKort_normalsituasjon() throws Exception{ // tester at etter ett tall er trukket så er arraylisten med kort minket med 1
System.out.println("testGetKort_normalsituasjon()");
// trekker to kort
char bokstav = instance.getKort();
bokstav = instance.getKort();
int result = instance.getAntallBokstaver();
int expResult = sisteBokstav - forsteBokstav-1; //trekker fra 1 for å få med Z..
assertEquals(expResult, result);
}
@Test (expected=Exception.class)// tester at det kastes ett unntak når kortstokken er tom
public void testGetKort_tomkortstokk() throws Exception{
System.out.println("testGetKort_tomkortstokk()");
int antallBokstaver = instance.getAntallBokstaver();
for (int i=forsteBokstav; i<=sisteBokstav;i++){
char bokstav = instance.getKort();
}
char bokstav = instance.getKort(); // skal kaste unntak, kortstokken er nå tom
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.containsString;
/**
*
* @author grethsan
*/
public class SpillTest {
public SpillTest() {
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
/**
* Test of spillEnOmgang method, of class Spill.
*/
@Test
public void testSpillEnOmgang() throws Exception {
System.out.println("Spill: spillEnOmgang");
Spiller spiller1 = new Spiller("Grethe");
Spiller spiller2 = new Spiller("Nils");
Spill instance = new Spill(spiller1, spiller2);
String result = instance.spillEnOmgang();
String expResult = "";
boolean spiller1VilBytte = spiller1.getBokstav() > 'M';
boolean spiller2VilBytte = spiller2.getBokstav() > 'M';
boolean byttet = spiller1VilBytte && spiller2VilBytte;
String byttetLest = "De byttet ikke. ";
if (byttet){
byttetLest = "De byttet. ";
expResult += spiller1.getNavn() + " trakk " + spiller2.getBokstav() + ". ";
expResult += spiller2.getNavn() + " trakk " + spiller1.getBokstav() + ". ";
}
else{
expResult += spiller1.getNavn() + " trakk " + spiller1.getBokstav() + ". ";
expResult += spiller2.getNavn() + " trakk " + spiller2.getBokstav() + ". ";
}
expResult += byttetLest;
expResult += spiller1 + " og " + spiller2;
assertEquals(expResult, result);
}
/**
* Test of bytteKort method, of class Spill.
*/
@Test
public void testBytteKort() throws Exception {
System.out.println("Spill: bytteKort");
Spiller spiller1 = new Spiller("Grethe");
Spiller spiller2 = new Spiller("Nils");
Spill instance = new Spill(spiller1, spiller2);
instance.spillEnOmgang();
char spiller1Bokstav = spiller1.getBokstav();
char spiller2Bokstav = spiller2.getBokstav();
instance.bytteKort();
boolean expResult = true;
boolean result = spiller1.getBokstav()==spiller2Bokstav && spiller2.getBokstav() == spiller1Bokstav;
assertEquals(expResult, result);
}
/**
* Test of beregnPoengForEnOmgang method, of class Spill.
*/
@Test
public void testBeregnPoeng() throws Exception {
System.out.println("Spill: beregnPoengForEnOmgang");
Spiller spiller1 = new Spiller("Grethe");
Spiller spiller2 = new Spiller("Nils");
Spill instance = new Spill(spiller1, spiller2);
instance.spillEnOmgang();
int spiller1Poeng = 0;
int spiller2Poeng = 0;
if (spiller1.getBokstav() < spiller2.getBokstav()) spiller1Poeng++;
else if (spiller1.getBokstav() > spiller2.getBokstav()) spiller2Poeng++;
boolean expResult = true;
boolean result = spiller1.getAntallPoeng()==spiller1Poeng && spiller2.getAntallPoeng()==spiller2Poeng;
assertEquals(expResult, result);
}
/**
* Test of spillMangeOmganger method, of class Spill.
*/
@Test
public void testSpillMangeOmganger() throws Exception {
System.out.println("Spill: spillMangeOmganger");
int antallOmganger = 1;
Spiller spiller1 = new Spiller("Grethe");
Spiller spiller2 = new Spiller("Nils");
Spill instance = new Spill(spiller1, spiller2);
String expResult = "\n"; // mellom hver omgang skal det skrives et linjeskift..
String result = instance.spillMangeOmganger(antallOmganger);
Assert.assertThat(result, containsString(expResult));
}
@Test (expected=Exception.class)
public void testSpillMangeOmganger_exeption() throws Exception {
System.out.println("Spill: spillMangeOmganger");
Spiller spiller1 = new Spiller("Grethe");
Spiller spiller2 = new Spiller("Nils");
Spill instance = new Spill(spiller1, spiller2);
int antallOmganger = 14; // Blir en omgang for mye -
instance.spillMangeOmganger(antallOmganger);
}
/**
* Test of avsluttSpill method, of class Spill.
*/
@Test
public void testAvsluttSpill() throws Exception{
System.out.println("Spill: avsluttSpill");
Spiller spiller1 = new Spiller("Grethe");
Spiller spiller2 = new Spiller("Nils");
Spill instance = new Spill(spiller1, spiller2);
instance.spillEnOmgang();
String expResult= "Det ble uavgjort";
if (spiller1.getAntallPoeng()>spiller2.getAntallPoeng()) expResult = spiller1.getNavn() + " vant:)";
else if (spiller2.getAntallPoeng()>spiller1.getAntallPoeng()) expResult = spiller2.getNavn() + " vant:)";
String result = instance.avsluttSpill();
assertEquals(expResult, result);
}
/**
* Test of toString method, of class Spill.
*/
@Test
public void testToString() throws Exception{
System.out.println("Spill: toString");
Spiller spiller1 = new Spiller("Grethe");
Spiller spiller2 = new Spiller("Nils");
Spill instance = new Spill(spiller1, spiller2);
instance.spillEnOmgang();
String expResult = spiller1 + "\n" + spiller2;
String result = instance.toString();
assertEquals(expResult, result);
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* @author grethsan
*/
public class SpillerTest {
public SpillerTest() {
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
/**
* Test of getNavn method, of class Spiller.
*/
@Test
public void testGetNavn() {
System.out.println("Spiller: getNavn");
Spiller instance = new Spiller("Grethe");
String expResult = "Grethe";
String result = instance.getNavn();
assertEquals(expResult, result);
}
/**
* Test of getBokstav method, of class Spiller.
*/
@Test
public void testGetBokstav() {
System.out.println("Spiller: getBokstav");
Spiller instance = new Spiller("Grethe");
instance.setBokstav('A');
char expResult = 'A';
char result = instance.getBokstav();
assertEquals(expResult, result);
}
/**
* Test of getAntallPoeng method, of class Spiller.
*/
@Test
public void testGetAntallPoeng() {
System.out.println("Spiller: getAntallPoeng");
Spiller instance = new Spiller("Grethe");
int expResult = 0;
int result = instance.getAntallPoeng();
assertEquals(expResult, result);
}
/**
* Test of setBokstav method, of class Spiller.
*/
@Test
public void testSetBokstav() {
System.out.println("Spiller: setBokstav");
char nyBokstav = 'B';
Spiller instance = new Spiller("Grethe");
instance.setBokstav(nyBokstav);
char expResult = 'B';
char result = instance.getBokstav();
assertEquals(expResult, result);
}
/**
* Test of økAntallPoeng method, of class Spiller.
*/
@Test
public void testØkAntallPoeng() {
System.out.println("Spiller: okAntallPoeng");
Spiller instance = new Spiller("Grethe");
instance.økAntallPoeng();
instance.økAntallPoeng();
instance.økAntallPoeng();
instance.økAntallPoeng();
int expResult = 4;
int result = instance.getAntallPoeng();
assertEquals(expResult, result);
}
/**
* Test of toString method, of class Spiller.
*/
@Test
public void testToString() {
System.out.println("Spiller: toString");
Spiller instance = new Spiller("Grethe");
instance.setBokstav('A');
String expResult = "Grethe har bokstav: A og antall Poeng: 0";
String result = instance.toString();
assertEquals(expResult, result);
}
}
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment