# java作業-15號 ###### tags: `JAVA` ----------- No.4 增量(燒腦?)版 ----------- 追加功能: 1. 提供scanner使用 2. 判斷是否為主管(輸入y或n),是才提供bonus作輸入 3. 檢查全輸入是否符合型態,不合則提供重新輸入 main() ``` package no4; public class TestNo4 { public static void main(String[] args) { Employees emp = new Employees(); emp.process(); } } ``` Employees class ``` package no4; import java.util.InputMismatchException; import java.util.Scanner; public class Employees { private int empno; private String name; private long salary; public void process() { System.out.println("此員工是否為主管? (y/n)"); while(true) { Scanner sca = new Scanner(System.in); String temp =sca.next(); if(temp.equals("y")) { Employees manager = new Manager(); manager.set(); manager.desc(); break; }else if(temp.equals("n")) { set(); desc(); break; }else { System.out.println("請重新輸入 (y/n)"); continue; } } } public void set() { System.out.println("請輸入員工編號 : "); while(true) { try { Scanner sca = new Scanner(System.in); this.empno = sca.nextInt(); if(this.empno <= 0) { System.out.println("員工編號不應小於或等於零,請重新輸入!"); continue; } break; } catch (InputMismatchException e) { System.out.println("員工編號為整數數字,請重新輸入!"); } } System.out.println("請輸入員工姓名 : "); Scanner sca = new Scanner(System.in); this.name = sca.nextLine(); System.out.println("請輸入員工薪水 : "); while(true) { try { Scanner sca1 = new Scanner(System.in); this.salary = sca1.nextLong(); if(this.salary < 0) { System.out.println("薪水為正整數,請重新輸入!"); continue; } break; } catch (InputMismatchException e) { System.out.println("薪水為整數數字,請重新輸入!"); } } } public void desc() { System.out.println(); System.out.println("員工編號 : "+getEmpno()); System.out.println("員工姓名 : "+getName()); System.out.println("員工薪水 : "+getSalary()+" 元"); } public int getEmpno() { return this.empno; } public String getName() { return this.name; } public long getSalary() { return this.salary; } } ``` Manager class ``` package no4; import java.util.InputMismatchException; import java.util.Scanner; public class Manager extends Employees{ private long salary; private long bonus; public void set() { super.set(); System.out.println("請輸入分紅 : "); while(true) { try { Scanner sca = new Scanner(System.in); this.bonus = sca.nextInt(); if(this.bonus < 0) { System.out.println("分紅為正整數,請重新輸入!"); continue; } break; } catch (InputMismatchException e) { System.out.println("分紅為整數數字,請重新輸入!"); } } } public void desc() { super.desc(); System.out.println("-------------------------"); System.out.println("主管分紅 : "+getBonus()+" 元"); } public long getSalary() { salary = super.getSalary() + bonus; return salary; } public long getBonus() { return bonus; } } ``` ---------- No.5 ---------- 只貼main(),其他class和題目一模一樣 ``` package no5; import java.util.ArrayList; import java.util.List; public class ShoppingCart { public static void main(String[] args) { List<Product> shoppingList = new ArrayList<Product>(); Product product1 = new Notebook("Asus" , 30000); Product product2 = new Notebook("Acer" , 20000); Product product3 = new Food("cookie" , 200); shoppingList.add(product1); shoppingList.add(product2); shoppingList.add(product3); int sum = 0; for(Product product : shoppingList) { sum = sum + product.getprice(); } System.out.println("購物車內總金額 : "+sum+" 元"); int sum1 = 0; for(Product product : shoppingList) { if(product instanceof Notebook) { sum1 = sum1 + product.getprice(); } } System.out.println("購物車內筆電總金額 : "+sum1+" 元"); } } ``` (不要問我剩兩題在哪。。。時間都花在第一題了XD)
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up