Java_上机5_6

项目下载

Java上机5

package cn.edu.hdu.account.core

Account

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package cn.edu.hdu.account.core;

import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

public class Account implements AccountFeature{
private double balance;
private ArrayList<Record> records;
Scanner input = new Scanner(System.in);

@Override
public void showMainMenu() {//显示主菜单
System.out.println("------------家用收支记账软件-----------");
System.out.println("1. 收支明细");
System.out.println("2. 登记收入");
System.out.println("3. 登记支出");
System.out.println("4. 退出");
System.out.println("请选择(1~4)");

}

@Override
public void showDetailInfo() {//显示账单信息
System.out.println("--------------当前收支明细记录------------------------------------");
System.out.printf("%-10s\t%-10s\t%-10s\t%-10s\t%-10s%n", "收支", "账户金额", "收支金额", "说明", "时间");
//循环遍历账本records中的每个记录,一次输出其变量信息
//输出格式"%-10s\t%-10s\t%-10s\t%-10s\t%-10s%n",依次输出收支类型/账户当时余额/单笔记录金额、备注、时间
Record record = new Record();
for (int i = 0; i < records.size(); i++) {
record = records.get(i);
System.out.printf("%-10s\t%-15s\t%-15s\t%-10s\t%-10s%n",record.getRecordType(),record.getCurBalance(),record.getAmountOfMoney(),record.getRemark(),record.getDate());
}
}

@Override
public void editIncomeInfo() {//登记收入信息
//编辑收入信息,收入信息包括:记录类型、当前余额、单笔金额、备注、时间
//分别获得这些信息,然后调用setRecord函数,将这些信息写入记录
//当前余额的计算通过updateBalance函数完成
System.out.println("本次收入金额");
Double money = input.nextDouble();
System.out.println("本次收入说明");
String remark = input.next();
String type = "收入";
updateBalance("+",money);
setRecord(type,this.balance,money,remark,new Date());
}

@Override
public void editSpendInfo() {//登记支出信
//编辑支出信息,收入信息包括:记录类型、当前余额、单笔记录金额、备注、时间
//分别获得这些信息,然后调用setRecord函数,将这些信息写入记录
//当前余额的计算通过updateBalance函数完成
System.out.println("本次支出金额");
Double money = input.nextDouble();
System.out.println("本次支出说明");
String remark = input.next();
String type = "支出";
updateBalance("-",money);
setRecord(type,this.balance,money,remark,new Date());
}

@Override
public void exitAccount() {//退出记账器
System.exit(0);
}

public void init(){//初始化账本
this.balance = 0.0;
this.records = new ArrayList<Record>();
while(true){
//在一个循环中不断显示主菜单showMainMenu,并获取用户输入gerUserInput
//捕获在getUserInput中可能出现的输入异常,进行异常处理(这里的异常处理就是提示用户信息输错了,请重新输入
showMainMenu();
try{
getUserInput();
} catch (IllegalInputException e) {
System.out.println();
}

}
}

private void getUserInput() throws IllegalInputException {//处理逻辑
int choice = input.nextInt();//获取用户的输入
switch (choice){
case 1: {
//显示账本详情
showDetailInfo();
break;
}
case 2: {
//编辑收入信息
editIncomeInfo();
break;
}
case 3: {
//编辑支出信息
editSpendInfo();
break;
}
case 4:{
//退出账本系统
exitAccount();
break;
}
default:{
//即输入异常,则抛出IllegalInputException异常
throw new IllegalInputException(choice);
}
}
}

private void setRecord(String type, double balance, double money, String remark, Date date){//维护账单
//新建一个Record对象,将参数中的信息置入这俄格Record对象
//并将该Record对象作为记录加入账本的records列表中
Record record = new Record();
record.setRecordType(type);
record.setCurBalance(balance);
record.setAmountOfMoney(money);
record.setRemark(remark);
record.setDate(date);
records.add(record);
}

private void updateBalance(String op, double money){//维护余额
//根据参数op所指的操作(+或—),完成余额balance的更新
switch (op){
case "+": {
this.balance+= money;
break;
}
case "-": {
this.balance-= money;
break;
}
}
}
}

AccountFeature

1
2
3
4
5
6
7
8
9
package cn.edu.hdu.account.core;

public interface AccountFeature {
public void showMainMenu();
public void showDetailInfo();
public void editIncomeInfo();
public void editSpendInfo();
public void exitAccount();
}

IllegalInputException

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package cn.edu.hdu.account.core;

public class IllegalInputException extends Exception{
//这个变量用来记录用户输入的参数异常,比如输入5那么input就是5
private int input;
//构造函数,用异常输入来创建异常类
public IllegalInputException(int input){
this.input = input;
System.out.println("请输入1~4的整数");
}
public void InputMismatchException(String m){
System.out.println("请输入1~4的整数");
}
//用于获取该异常参数
public int getInput(){
return this.input;
}
}

Record

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package cn.edu.hdu.account.core;

import java.util.Date;

public class Record {
private String recordType; //记录类型
private double curBalance; //当前余额
private double amountOfMoney; //记录金额
private String remark; //记录备注
private Date date; //记录时间

public String getRecordType() {
return recordType;
}

public void setRecordType(String recordType) {
this.recordType = recordType;
}

public double getCurBalance() {
return curBalance;
}

public void setCurBalance(double curBalance) {
this.curBalance = curBalance;
}

public double getAmountOfMoney() {
return amountOfMoney;
}

public void setAmountOfMoney(double amountOfMoney) {
this.amountOfMoney = amountOfMoney;
}

public String getRemark() {
return remark;
}

public void setRemark(String remark) {
this.remark = remark;
}

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}
}

package cn.edu.hdu.account.main

MainProcess

1
2
3
4
5
6
7
8
9
10
package cn.edu.hdu.account.main;

import cn.edu.hdu.account.core.Account;

public class MainProcess {
public static void main(String[] args) {
Account account = new Account();
account.init();
}
}

Java上机6

package cn.edu.hdu.rent.core

Automobile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package cn.edu.hdu.rent.core;

public class Automobile {
private String brand; //品牌
private double price; //价格

public String getBrand() {
return brand;
}

public void setBrand(String brand) {
this.brand = brand;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}
}

AutomobileManager

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package cn.edu.hdu.rent.core;

import cn.edu.hdu.account.core.IllegalInputException;
import cn.edu.hdu.rent.util.Management;
import cn.edu.hdu.rent.util.RentSysConst;

import java.util.Iterator;
import java.util.Map;

public class AutomobileManager implements Management {

@Override
public void add(int type) throws IllegalInputException {
//添加汽车
String brand;
double price;
switch (type){
case 1:{
System.out.println("请输入品牌");
brand = RentSysConst.INPUT.next();
System.out.println("请输入佣金");
price = RentSysConst.INPUT.nextDouble();
System.out.println("请输入载人量:");
double peopleCapacity = RentSysConst.INPUT.nextDouble();
Car car=new Car();
car.setBrand(brand);
car.setPeopleCapacity(peopleCapacity);
car.setPrice(price);
RentSysConst.AUTO_LIST.put(RentSysConst.AUTO_ID+1,car);
RentSysConst.AUTO_ID++;
break;
}
case 2:{
System.out.println("请输入品牌");
brand = RentSysConst.INPUT.next();
System.out.println("请输入佣金");
price = RentSysConst.INPUT.nextDouble();
System.out.println("请输入载货量:");
double cargoCapacity = RentSysConst.INPUT.nextDouble();
Truck truck = new Truck();
truck.setCargoCapacity(cargoCapacity);
truck.setBrand(brand);
truck.setPrice(price);
RentSysConst.AUTO_LIST.put(RentSysConst.AUTO_ID+1,truck);
RentSysConst.AUTO_ID++;
break;
}
case 3:{
System.out.println("请输入品牌");
brand = RentSysConst.INPUT.next();
System.out.println("请输入佣金");
price = RentSysConst.INPUT.nextDouble();
System.out.println("请输入载人量:");
double peopleCapacity = RentSysConst.INPUT.nextDouble();
System.out.println("请输入载货量:");
double cargoCapacity = RentSysConst.INPUT.nextDouble();
PickUp pickUp = new PickUp();
pickUp.setPeopleCapacity(peopleCapacity);
pickUp.setCargoCapacity(cargoCapacity);
pickUp.setBrand(brand);
pickUp.setPrice(price);
RentSysConst.AUTO_LIST.put(RentSysConst.AUTO_ID+1,pickUp);
RentSysConst.AUTO_ID++;
break;
}
default:{
throw new IllegalInputException(type);
}

}


}

@Override
public void delete(int ID) {
RentSysConst.AUTO_LIST.remove(ID);
}

@Override
public void update(int ID) {
}

@Override
public void show(){
//显示已经汽车列表
Iterator<Map.Entry<Integer, Automobile>> iterator = RentSysConst.AUTO_LIST.entrySet().iterator();
System.out.printf("%-10s\t%-10s\t%-10s\t%-10s\t%-10s%n","序号","汽车名称","租金","载客量","载货量");
while (iterator.hasNext()){
Map.Entry<Integer, Automobile> entry = iterator.next();
if(entry.getValue() instanceof Car){
Car car = (Car)entry.getValue();
System.out.printf("%-10s\t%-15s\t%-10s\t%-15s\t%-10s%n",entry.getKey(),car.getBrand(),car.getPrice(),car.getPeopleCapacity(),"0.0");
}
else if(entry.getValue() instanceof Truck){
Truck truck = (Truck) entry.getValue();
System.out.printf("%-10s\t%-15s\t%-10s\t%-15s\t%-10s%n",entry.getKey(),truck.getBrand(),truck.getPrice(),"0.0",truck.getCargoCapacity());
}
else if(entry.getValue() instanceof PickUp){
PickUp pickUp = (PickUp) entry.getValue();
System.out.printf("%-10s\t%-15s\t%-10s\t%-15s\t%-10s%n",entry.getKey(),pickUp.getBrand(),pickUp.getPrice(),pickUp.getPeopleCapacity(),pickUp.getCargoCapacity());
}


}
}

@Override
public int exit() {
//退出函数
return 0;
}

public void init(){
int flag = 1;
while (flag == 1){
showMenu();
try{
flag = getUserInput();
}catch (IllegalInputException e){
System.out.println("输入了无效信息,请重新输入");
}
}
}; //初始化管理系统

public void showMenu(){
System.out.println("=========汽车管理系统=========");
System.out.println("1.增加汽车");
System.out.println("2.删除汽车");
System.out.println("3.显示汽车");
System.out.println("4.退出系统");

}; //显示管理系统界面

private int getUserInput() throws IllegalInputException {

int choice = RentSysConst.INPUT.nextInt();
int type = 0;
switch (choice){
case 1:{
System.out.println("请输入汽车类型(1-汽车,2-卡车,3-皮卡):");
type = RentSysConst.INPUT.nextInt();
add(type);
break;
}
case 2:{
System.out.println("请输入要删除的汽车ID");
type = RentSysConst.INPUT.nextInt();
delete(type);
break;
}
case 3:{
show();
break;
}
case 4:{
return exit();
}
default:{
throw new IllegalInputException(choice);
}
}
return 1;
}; //处理逻辑
}

Car

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package cn.edu.hdu.rent.core;

import cn.edu.hdu.rent.util.CanCarryPeople;

public class Car extends Automobile implements CanCarryPeople {
private double peopleCapacity;

@Override
public void setPeopleCapacity(double capacity) {
this.peopleCapacity = capacity;
}

@Override
public double getPeopleCapacity() {
return this.peopleCapacity;
}
}

IllegalInputeException

1
2
3
4
5
6
7
8
9
10
11
package cn.edu.hdu.rent.core;

public class IllegalInputException extends Exception{
private int input;
public IllegalInputException(int input){
this.input = input;
}
public int getInput(){
return this.input;
}
}

PickUp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package cn.edu.hdu.rent.core;

import cn.edu.hdu.rent.util.CanCarryCargo;
import cn.edu.hdu.rent.util.CanCarryPeople;

public class PickUp extends Automobile implements CanCarryPeople, CanCarryCargo {
private double peopleCapacity;
private double cargoCapacity;

@Override
public void setPeopleCapacity(double capacity) {
this.peopleCapacity = capacity;
}

@Override
public double getPeopleCapacity() {
return this.peopleCapacity;
}

@Override
public void setCargoCapacity(double capacity) {
this.cargoCapacity = capacity;
}

@Override
public double getCargoCapacity() {
return this.cargoCapacity;
}
}

RentSystem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package cn.edu.hdu.rent.core;

import cn.edu.hdu.rent.util.RentSysConst;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class RentSystem {
private HashMap<Integer, HashMap<Automobile,Integer>> orders = new HashMap<Integer, HashMap<Automobile,Integer>>(); //汽车品牌
private int ID = 1; //订单ID

public void showMenu(){
System.out.println("=========你可以租车的类型及其价目表========");
Iterator<Map.Entry<Integer, Automobile>> iterator = RentSysConst.AUTO_LIST.entrySet().iterator();
System.out.printf("%-10s\t%-10s\t%-10s\t%-10s\t%-10s%n","序号","汽车名称","租金","载客量","载货量");
while (iterator.hasNext()){
Map.Entry<Integer, Automobile> entry = iterator.next();
if(entry.getValue() instanceof Car){
Car car = (Car)entry.getValue();
System.out.printf("%-10s\t%-15s\t%-10s\t%-15s\t%-10s%n",entry.getKey(),car.getBrand(),car.getPrice(),car.getPeopleCapacity(),"0.0");
}
else if(entry.getValue() instanceof Truck){
Truck truck = (Truck) entry.getValue();
System.out.printf("%-10s\t%-15s\t%-10s\t%-15s\t%-10s%n",entry.getKey(),truck.getBrand(),truck.getPrice(),"0.0",truck.getCargoCapacity());
}
else if(entry.getValue() instanceof PickUp){
PickUp pickUp = (PickUp) entry.getValue();
System.out.printf("%-10s\t%-15s\t%-10s\t%-15s\t%-10s%n",entry.getKey(),pickUp.getBrand(),pickUp.getPrice(),pickUp.getPeopleCapacity(),pickUp.getCargoCapacity());
}
}
} //显示租车系统界面

public void chooseAutomobiles(){
int id = 0;
int days = 0;
int flag = 1;

while (flag == 1) {
HashMap<Automobile,Integer> hashMap = new HashMap<Automobile,Integer>();
while (true) {
System.out.println("请输入要租的汽车ID(-1退出)");
id = RentSysConst.INPUT.nextInt();
if (id == -1) break;

System.out.println("请输入要租的天数:");
days = RentSysConst.INPUT.nextInt();
hashMap.put(RentSysConst.AUTO_LIST.get(id), days);

}
orders.put(ID, hashMap);
ID++;
flag = showOrder();
}
}//选择车辆

public int showOrder(){
int flag = 0;
System.out.println("你的订单");
Iterator<Map.Entry<Integer, HashMap<Automobile, Integer>>> iterator = orders.entrySet().iterator();
while (iterator.hasNext()) {
double peopleCapacity = 0;
double cargoCapacity = 0;
double price = 0;
Map.Entry<Integer, HashMap<Automobile, Integer>> entry = iterator.next();
System.out.println("订单" + entry.getKey() + ":");
Iterator<Map.Entry<Automobile, Integer>> entryIterator = entry.getValue().entrySet().iterator();
while (entryIterator.hasNext()) {
Map.Entry<Automobile, Integer> next = entryIterator.next();
System.out.println("<" + next.getKey().getBrand() + "," + next.getValue() + "天>");
price += next.getKey().getPrice();
if (next.getKey() instanceof Car) {
peopleCapacity += ((Car) next.getKey()).getPeopleCapacity();
} else if (next.getKey() instanceof PickUp) {
peopleCapacity += ((PickUp) next.getKey()).getPeopleCapacity();
}
if (next.getKey() instanceof Truck) {
cargoCapacity += ((Truck) next.getKey()).getCargoCapacity();

} else if (next.getKey() instanceof PickUp) {
cargoCapacity += ((PickUp) next.getKey()).getCargoCapacity();
}
}
System.out.println("总载客量" + peopleCapacity + "人");
System.out.println("总载货量" + cargoCapacity + "吨");
System.out.println("总租车价" + price + "元");
}
System.out.println("是否继续: Y or N ?");

String option = RentSysConst.INPUT.next();

if(option.equals("Y")){
flag = 1;

}
else if(option.equals("N")){
flag = 0;

}

return flag;

}//展示订单

public void init(){

showMenu();
chooseAutomobiles();

}//初始化租车系统

}

Truck

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package cn.edu.hdu.rent.core;


import cn.edu.hdu.rent.util.CanCarryCargo;

public class Truck extends Automobile implements CanCarryCargo {
private double cargoCapacity;

@Override
public void setCargoCapacity(double capacity) {
this.cargoCapacity = capacity;
}

@Override
public double getCargoCapacity() {
return this.cargoCapacity;
}
}

package cn.edu.hdu.rent.main

MainProcess

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package cn.edu.hdu.rent.main;



import cn.edu.hdu.rent.core.AutomobileManager;
import cn.edu.hdu.rent.core.IllegalInputException;
import cn.edu.hdu.rent.core.RentSystem;

import java.util.Scanner;

public class MainProcess {
public static void main(String[] args) throws IllegalInputException {
Scanner sc = new Scanner(System.in);
int choice;
while (true) {
System.out.println("请选择系统");
System.out.println("1.汽车管理系统");
System.out.println("2.租车系统");
System.out.println("3.退出");
System.out.println("请输入(1-3):");
choice = sc.nextInt();
switch (choice) {
case 1: {
AutomobileManager am = new AutomobileManager();
am.init();
break;
}
case 2: {
RentSystem rs = new RentSystem();
rs.init();
break;
}
case 3: {
System.exit(0);
break;
}
default: {
System.out.println("输入值无效,请重新输入");
}
}
}
}
}