Skip to content

Instantly share code, notes, and snippets.

@chanwit
Created December 21, 2014 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chanwit/6c987286f28ec5ebbaa1 to your computer and use it in GitHub Desktop.
Save chanwit/6c987286f28ec5ebbaa1 to your computer and use it in GitHub Desktop.
เฉลย OOT 57 Lab 5
package oot.lab5.group1.jacket;
public class Cloth {
private String name;
private Pocket leftPocket;
private Pocket rightPocket;
public void setLeftPocket(Pocket value) {
this.leftPocket = value;
}
public void setRightPocket(Pocket value) {
this.rightPocket = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Pocket getLeftPocket() {
return leftPocket;
}
public Pocket getRightPocket() {
return rightPocket;
}
@Override
public String toString() {
return String.format("Jacket \"%s\", Arm Color[L: %s R: %s]",
this.name,
this.leftPocket.getColor(),
this.rightPocket.getColor());
}
}
package oot.lab5.group1;
import java.util.Scanner;
import oot.lab5.group1.jacket.Cloth;
import oot.lab5.group1.jacket.Pocket;
public class No1 {
public static void main(String[] args) {
Cloth cloth = new Cloth();
cloth.setName("CPE");
Pocket p1 = new Pocket();
p1.setColor("GREEN");
p1.setSize(10);
cloth.setLeftPocket(p1);
Pocket p2 = new Pocket();
p2.setColor("BLACK");
p2.setSize(20);
cloth.setRightPocket(p2);
printCloth(cloth);
Scanner sc = new Scanner(System.in);
while (true) {
System.out.print("Choose pocket L or R (0 to exit): ");
char ch = sc.next("[LR0]").charAt(0);
if (ch == '0') {
break;
}
if (ch == 'L') {
System.out.printf("LEFT color is: %s\n", p1.getColor());
System.out.printf("LEFT size is: %d\n", p1.getSize());
System.out.print("Enter new color: ");
String color = sc.next();
p1.setColor(color);
System.out.print("Enter new size: ");
int size = sc.nextInt();
p1.setSize(size);
} else if (ch == 'R') {
System.out.printf("RIGHT color is: %s\n", p2.getColor());
System.out.printf("RIGHT size is: %d\n", p2.getSize());
System.out.print("Enter new color: ");
String color = sc.next();
p2.setColor(color);
System.out.print("Enter new size: ");
int size = sc.nextInt();
p2.setSize(size);
}
printCloth(cloth);
}
}
private static void printCloth(Cloth cloth) {
System.out.println("==");
System.out.printf("Cloth \"%s\", ", cloth.getName());
Pocket l = cloth.getLeftPocket();
Pocket r = cloth.getRightPocket();
System.out.printf("Pocket [L: %s/%d, ", l.getColor(), l.getSize());
System.out.printf(" R: %s/%d]", r.getColor(), r.getSize());
System.out.println();
System.out.println("==");
}
}
package oot.lab5.group1.jacket;
public class Pocket {
private String color;
private int size;
public void setColor(String color) {
this.color = color;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public String getColor() {
return this.color;
}
}
package oot.lab5.group2.bike;
public class Handle {
private int length;
private String color;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getLength() {
return this.length;
}
public void setLength(int length) {
this.length = length;
}
}
package oot.lab5.group2.bike;
public class MotorCycle {
private int engineSize;
private Handle leftHandle;
private Handle rightHandle;
public int getEngineSize() {
return engineSize;
}
public void setEngineSize(int value) {
this.engineSize = value;
}
public void setRightHandle(Handle value) {
this.rightHandle = value;
}
public void setLeftHandle(Handle value) {
this.leftHandle = value;
}
public Handle getRightHandle() {
return this.rightHandle;
}
public Handle getLeftHandle() {
return this.leftHandle;
}
}
package oot.lab5.group2;
import java.util.Scanner;
import oot.lab5.group2.bike.MotorCycle;
import oot.lab5.group2.bike.Handle;
public class No1 {
public static void main(String[] args) {
MotorCycle mc = new MotorCycle();
mc.setEngineSize(200);
Handle h1 = new Handle();
h1.setColor("GREY");
h1.setLength(25);
mc.setLeftHandle(h1);
Handle h2 = new Handle();
h2.setColor("BLACK");
h2.setLength(30);
mc.setRightHandle(h2);
printMotorCycle(mc);
Scanner scan = new Scanner(System.in);
while(true) {
System.out.print("Choose Handle,\n");
System.out.print(" 1. Left\n");
System.out.print(" 2. Right\n");
System.out.print("(0 to exit): ");
int ch = scan.nextInt();
if(ch == 0) {
break;
}
if (ch == 1) {
System.out.printf("Left Handle length is: %d cm\n", h1.getLength());
System.out.printf("Left Handle color is: %s \n", h1.getColor());
} else if(ch == 2) {
System.out.printf("Right Handle length is: %d cm\n", h2.getLength());
System.out.printf("Right Handle color is: %s \n", h2.getColor());
}
System.out.print("Enter new handle length: ");
int size = scan.nextInt();
System.out.print("Enter new handle color: ");
String color = scan.next();
if (ch == 1) {
h1.setLength(size);
h1.setColor(color);
} else if(ch == 2) {
h2.setLength(size);
h2.setColor(color);
}
printMotorCycle(mc);
}
}
public static void printMotorCycle(MotorCycle mc) {
System.out.printf("****************************************\n");
System.out.printf("Motorcycle: %d cc ", mc.getEngineSize());
Handle l = mc.getLeftHandle();
Handle r = mc.getRightHandle();
System.out.printf("[L: %d cm / %s] ", l.getLength(), l.getColor());
System.out.printf("[R: %d cm / %s]\n", r.getLength(), r.getColor());
System.out.printf("****************************************\n");
}
}
package oot.lab5.group34;
import java.util.Scanner;
import oot.lab5.group34.device.Phone;
import oot.lab5.group34.device.Simcard;
public class No1 {
public static void main(String[] args) {
Phone phone = new Phone();
phone.setPrice(25000);
Simcard s1 = new Simcard();
s1.setNumber("0910000000");
s1.setProvider("TRUE");
phone.setFirst(s1);
Simcard s2 = new Simcard();
s2.setNumber("0810000000");
s2.setProvider("DTAC");
phone.setSecond(s2);
printPhone(phone);
Scanner scan = new Scanner(System.in);
while(true) {
System.out.print("Choose Sim: ");
System.out.print(" F. First Sim /");
System.out.print(" S. Second Sim ");
System.out.print("(0 to exit): ");
char ch = scan.next("[FS0]").charAt(0);
if(ch == '0') {
break;
}
if (ch == 'F') {
System.out.printf("First SIM: %s\n", s1.getProvider());
System.out.printf("First SIM No: %s\n", s1.getNumber());
} else if(ch == 'S') {
System.out.printf("Second SIM: %s\n", s2.getProvider());
System.out.printf("Second SIM No: %s\n", s2.getNumber());
}
System.out.print("Enter new provider: ");
String provider = scan.next();
System.out.print("Enter new number: ");
String no = scan.next();
if (ch == 'F') {
s1.setProvider(provider);
s1.setNumber(no);
} else if(ch == 'S') {
s2.setProvider(provider);
s2.setNumber(no);
}
printPhone(phone);
}
}
private static void printPhone(Phone phone) {
System.out.printf("###\n");
System.out.printf("Price: %d.- ", phone.getPrice());
Simcard f = phone.getFirst();
Simcard s = phone.getSecond();
System.out.printf("[1st: %s @ %s] ", f.getNumber(), f.getProvider());
System.out.printf("[2nd: %s @ %s]\n", s.getNumber(), s.getProvider());
System.out.printf("###\n");
}
}
package oot.lab5.group34.device;
public class Phone {
private Simcard first;
private Simcard second;
private int price;
public Simcard getFirst() {
return first;
}
public void setFirst(Simcard first) {
this.first = first;
}
public Simcard getSecond() {
return second;
}
public void setSecond(Simcard second) {
this.second = second;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
package oot.lab5.group34.device;
public class Simcard {
private String number;
private String provider;
public void setProvider(String provider) {
this.provider = provider;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getProvider() {
return provider;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment