定義校園類 校園名稱,校園地址,校園學生數量
抽象兩個構造方法 初始資料
學生數量不允許為空
校園名稱不低于3個字符,不高于10個字符
輸出校園的基本資訊
完成以下四個功能
1.1 添加校園資訊
1.2 根據輸入的校園名稱, 移除對應校園資訊
1.3 統計所有校園的總人數
1.4 將所有校園資訊輸出成檔案
uj5u.com熱心網友回復:
package com.SchoolManageSystem;
public class School {
private String name;
private String address;
private int StudentNum;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getStudentNum() {
return StudentNum;
}
public void setStudentNum(int studentNum) {
StudentNum = studentNum;
}
public School(String name, String address, int studentNum) {
this.name = name;
this.address = address;
this.StudentNum = studentNum;
}
public School() {
}
}
package com.SchoolManageSystem;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class Manager {
public Map<String, School> school;
Scanner sc = new Scanner(System.in);
public Manager() {
school = new HashMap<String, School>();
}
/**
* 添加校園資訊
*/
public void put() {
int count;
System.out.println("您一共想添加幾所學校?請輸入整數:");
count = sc.nextInt();
for (int i = 0; i < count; i++) {
School s1 = new School();
boolean flag = true;
while (flag) {
System.out.println("請輸入第" + (i + 1) + "所學校的名稱:");
String name = sc.next();
School s2 = school.get(name);
if (s2 != null) {
System.out.println("該名稱的學校已添加,請重新輸入!");
continue;
}
if (name.length() >= 3 && name.length() <= 10) {
s1.setName(name);
flag = false;
} else {
System.out.println("校園名稱不能低于3個字符,不能高于10個字符,請重新輸入!");
}
}
System.out.println("請輸入第" + (i + 1) + "所學校的地址:");
String address = sc.next();
s1.setAddress(address);
flag = true;
while (flag) {
System.out.println("請輸入第" + (i + 1) + "所學校的學生數量:");
int num = sc.nextInt();
if (num > 0) {
s1.setStudentNum(num);
flag = false;
} else {
System.out.println("校園學生數量不允許為空,請重新輸入!");
}
}
school.put(s1.getName(), s1);
}
}
/**
* 根據輸入的校園名稱移除對應的校園資訊
*/
public void remove() {
boolean flag = true;
while (flag) {
System.out.println("請輸入要洗掉的學校的名稱:");
String name = sc.next();
School s1 = school.get(name);
if (s1 != null) {
school.remove(name);
System.out.println("洗掉成功!");
flag = false;
} else {
System.out.println("您要洗掉的學校不存在,請重新輸入!");
}
}
}
/**
* 統計所有校園的總人數
*/
public int total() {
int sum = 0;
Set<String> KeySet = school.keySet();
for (String string : KeySet) {
School s1 = school.get(string);
if (s1 != null) {
sum += s1.getStudentNum();
}
}
return sum;
}
/**
* 遍歷學校資訊Map,并轉換成字串回傳
*
* @return學校資訊
*/
public String message() {
StringBuilder strb = new StringBuilder();
Set<String> KeySet = school.keySet();
for (String string : KeySet) {
School s1 = school.get(string);
if (s1 != null) {
strb.append("校園名稱:" + s1.getName() + " " + "校園地址:" + s1.getAddress() + " " + "校園學生數量:"
+ s1.getStudentNum() + "\n");
}
}
String context = strb.toString();
return context;
}
/**
* 將所有校園資訊輸出成檔案
*/
public void file() {
//輸出檔案所在路勁,可以自己修改
File txt = new File("E:/SchoolMessage.txt");
if (!txt.exists()) {
try {
txt.createNewFile();
} catch (IOException e) {
// TODO 自動生成的 catch 塊
e.printStackTrace();
}
}
byte bytes[] = new byte[512];
bytes = message().getBytes();
int b = bytes.length; // 是位元組的長度,不是字串的長度
FileOutputStream fos;
try {
fos = new FileOutputStream(txt);
fos.write(bytes, 0, b);
fos.close();
} catch (FileNotFoundException e) {
// TODO 自動生成的 catch 塊
e.printStackTrace();
} catch (IOException e) {
// TODO 自動生成的 catch 塊
e.printStackTrace();
}
}
}
package com.SchoolManageSystem;
public class Test {
public static void main(String[] args) {
// TODO 自動生成的方法存根
Manager m1 = new Manager();
m1.put();
m1.remove();
System.out.println("所有校園的總人數為:" + m1.total());
m1.file();
}
}

看到你這個60分還挺高的,我給你寫了一個。
我自己跑了一遍,你要求的4個功能都行。希望看到了,早點采納結貼喲
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/124626.html
標籤:Java相關
