Sie sind auf Seite 1von 8

*Main

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class Main {

Scanner scan = new Scanner(System.in);


ArrayList<Mouse> mouseList = new ArrayList<>();
Random rand = new Random();

private int random(int min, int max) {


int bound = max - min + 1;
int numeric = rand.nextInt(bound);
return numeric;
}

private String generateCode(String type) {

String code;
if (type.equals("Standard"))
code = "SM-";
else
code = "GM-";

for (int i = 0; i < 3; i++) {


code += random(0, 9);
}
return code;
}

private int searchCode(String code) {


for (int i = 0; i < mouseList.size(); i++) {
String mouseCode = mouseList.get(i).getCode();
if(mouseCode.equals(code)) return i;
}

return -1;
}

private void removeAll() {


while(!mouseList.isEmpty()){
mouseList.remove(0);
}
}

public Main() {
// TODO Auto-generated constructor stub
int choose = 0;
do {
System.out.println(" Mouse");
System.out.println(" 1. Add Items");
System.out.println(" 2. View Items");
System.out.println(" 3. Update Items");
System.out.println(" 4. Delete Items");
System.out.println(" 5. Exit");
do {
try {
choose = scan.nextInt();
} catch (Exception e) {
// TODO: handle exception
choose = 0;
System.out.println("Please input numeric only!");
}
scan.nextLine();
}while(choose <1 || choose >5);
switch (choose) {
case 1:
addMouse();
break;
case 2:
viewMouse();
break;
case 3:
updateMouse();
break;
case 4:
deleteMouse();
break;
case 5:
System.out.println(" Thank You");
removeAll();
break;
}
scan.nextLine();
}while(choose != 5);
}

private void deleteMouse() {


if(mouseList.size()!= 0) {
String code;
int index = 0;
viewMouse();
do {
System.out.println("Input Code: ");
code = scan.nextLine();
index = searchCode(code);
}while(index == -1);

mouseList.remove(index);
System.out.println("Berhasil dihapus");
}else {
System.out.println("No Mouse");
}

}
private void updateMouse() {
// TODO Auto-generated method stub
if(mouseList.size()!=0) {
String codeToSearch;
int index = 0;

viewMouse();
do {
System.out.println("Input Code: ");
codeToSearch = scan.nextLine();
index = searchCode(codeToSearch);
}while(index == -1);

String oldType = "";


String oldCode = mouseList.get(index).getCode();
if(mouseList.get(index) instanceof MouseGaming) {
oldType = "Gaming";
}else if(mouseList.get(index) instanceof Mouse) {
oldType = "Standard";
}

String code,merk,factory,type,color;
int basePrice;
do {
System.out.println("Input merk[2 words]");
merk = scan.nextLine();
}while(isMerkError(merk));
do {
System.out.println("Input factory[starts with Pt.]");
factory = scan.nextLine();
}while (isFactoryError(factory));
do {
System.out.print(" Input Type [Standard|Gaming]: ");
type = scan.nextLine();
} while (isTypeError(type));
do {
System.out.print(" Input base price [50000 - 100000]: ");
try {
basePrice = scan.nextInt();
} catch (Exception e) {
basePrice = 0;
System.out.println("Input numeric only");
}
scan.nextLine();
}while(isPriceError(basePrice));
if(oldType.equals(type)) {
code = oldCode;
}else {
code = generateCode(type);
}
if(type.equals("Gaming")){
do {
System.out.println("Input Color [Red|Blue|RGB|: ");
color = scan.nextLine();
}while(isColorErorr(color));
Mouse newGaming = new MouseGaming(code, merk, factory,
basePrice, color);
mouseList.set(index, newGaming);
}else {
Mouse newStandard = (new
Mouse(code,merk,factory,basePrice));
mouseList.set(index, newStandard);
}
System.out.println("Mouse berhasi diupdate");
}
else {
System.out.println("Tidak ada Mouse");
}
}

private void viewMouse() {


// TODO Auto-generated method stub
if(mouseList.size()!=0) {
System.out.printf(" | %6s | %20s | %20s | %5s | %10s
|\n","Code","Merk","Factory","Color","Price");
for(Mouse curr: mouseList) {
curr.printInformation();
}
}else {
System.out.println("No Mouse");
}
}

private void addMouse() {


// TODO Auto-generated method stub
String code,merk,factory,type,color;
int basePrice;
do {
System.out.println("Input merk[2 words]");
merk = scan.nextLine();
}while(isMerkError(merk));
do {
System.out.println("Input factory[starts with Pt.]");
factory = scan.nextLine();
}while (isFactoryError(factory));
do {
System.out.print(" Input Type [Standard|Gaming]: ");
type = scan.nextLine();
} while (isTypeError(type));
do {
System.out.print(" Input base price [50000 - 100000]: ");
try {
basePrice = scan.nextInt();
} catch (Exception e) {
basePrice = 0;
System.out.println("Input numeric only");
}
scan.nextLine();
}while(isPriceError(basePrice));

code = generateCode(type);

if(type.equals("Gaming")){
do {
System.out.println("Input Color [Red|Blue|RGB|: ");
color = scan.nextLine();
}while(isColorErorr(color));
mouseList.add(new MouseGaming(code, merk, factory, basePrice,
color));
}else {
mouseList.add(new Mouse(code,merk,factory,basePrice));
}
System.out.println("Mouse berhasi diinput");
}

//Validasi

private boolean isFactoryError(String factory) {


if(!factory.startsWith("Pt. ")) return true;
if(factory.length() >20) return true;

return false;
}

private boolean isMerkError(String merk) {


if(merk.startsWith(" ")) return true;
if(merk.endsWith(" ")) return true;

String trimmed = merk.trim();


String[] splitted = trimmed.split(" ");
if(splitted.length !=2) return true;

return false;
}

private boolean isColorErorr(String color) {


if(color.equals("Red")) return false;
if(color.equals("Blue")) return false;
if(color.equals("RGB")) return false;

return true;
}

private boolean isPriceError(int basePrice) {


if(basePrice < 50000 || basePrice > 100000) return true;
return false;
}
private boolean isTypeError(String type) {
if(type.equals("Standard")) return false;
if(type.equals("Gaming")) return false;

return true;
}

public static void main(String[] args) {


// TODO Auto-generated method stub
new Main();
}

*Mouse

public class Mouse {


protected String code;
protected String merk;
protected String factory;
protected int basePrice;

public Mouse(String code, String merk, String factory, int basePrice) {


this.code = code;
this.merk = merk;
this.factory = factory;
this.basePrice = basePrice;
}

public String getCode() {


return code;
}

public void setCode(String code) {


this.code = code;
}

public String getMerk() {


return merk;
}

public void setMerk(String merk) {


this.merk = merk;
}

public String getFactory() {


return factory;
}
public void setFactory(String factory) {
this.factory = factory;
}

public int getBasePrice() {


return basePrice;
}

public void setBasePrice(int basePrice) {


this.basePrice = basePrice;
}

public int getCalculatedPrice(){


return this.basePrice;
}

public void printInformation() {


System.out.printf(" | %6s | %20s | %20s | %5s | %10d
|\n",this.code,this.merk,this.factory,"",getCalculatedPrice());
}

*MouseGaming

public class MouseGaming extends Mouse {


private String color;

public MouseGaming(String code, String merk, String factory, int


basePrice,String color) {
super(code, merk, factory, basePrice);
this.color = color;
}
@Override
public int getCalculatedPrice() {
int percent = 0;
if(this.color.equals("Red")) {
percent = 5;
}
else if(this.color.equals("Blue")) {
percent = 10;
}
else if(this.color.equals("RGB")) {
percent = 20;
}
return this.basePrice + percent*basePrice/100;
}
@Override
public void printInformation() {
System.out.printf(" | %6s | %20s | %20s | %5s | %10d
|\n",this.code,this.merk,this.factory,this.color,getCalculatedPrice());
}

Das könnte Ihnen auch gefallen