这是包的名字
这里我用了while语句和switch-case语句来提高代码的互动性
package Phone;
import java.util.Scanner;
public class phonedemo {
public static void main(String[] args) {
//写入手机种类及配置
CellPhone p1 = new CellPhone("华为","6.58","5000","8+512");
CellPhone p2 = new CellPhone("Iphone","6.5","1000","4+256");
//利用while循环展示手机
while (true){
System.out.println("-----------欢迎进入您的手机小仓库-----------");
System.out.println("请选择你要查看的手机");
System.out.println("1.华为p40pro+");
System.out.println("2.Iphone11pro");
System.out.println("3.走出仓库");
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
//利用switch语句调用展示方法和进行功能操作的switch语句
switch (line) {
case "1":{
//展示方法调用
p1.Show();
System.out.println("请选择你要使用功能");
System.out.println("1.发短信");
System.out.println("2.打电话");
//功能的操作
String function = sc.nextLine();
switch (function){
case "1":{
//调用sendMessage方法
p1.sendMessage();
break;
}
case "2":{
//调用callPhone方法
p1.callPhone();
break;
}
default:
System.out.println("对不起您输入的信息有误,已退出手机使用");
break;
}
break;
}
case "2":{
//展示方法调用
p2.Show();
System.out.println("请选择你要使用功能");
System.out.println("1.发短信");
System.out.println("2.打电话");
String function = sc.nextLine();
switch (function){
case "1":{
//调用sendMessage方法
p2.sendMessage();
break;
}
case "2":{
//调用sendMessage方法
p2.callPhone();
break;
}
default:
System.out.println("您输入的信息有误,请重新输入");
break;
}
break;
}
case "3":{
//直接结束系统运行
System.out.println("已退出仓库");
System.exit(0);
}
default:
System.out.println("您输入的信息有误,请重新输入");
break;
}
}
}
}
这是手机的包
package Phone;
import java.util.Scanner;
public class CellPhone {
//定义手机属性
private String logo;
private String screen;
private String camera;
private String memory;
//无参方法
public CellPhone() {
}
//set get方法
public CellPhone(String logo, String screen,String camera,String memory) {
this.logo = logo;
this.screen = screen;
this.camera = camera;
this.memory = memory;
}
public void setlogo(String logo) {
this.logo = logo;
}
public String getlogo(){
return logo;
}
public void setscreen(String screen) {
this.screen = screen;
}
public String getscreen(){
return screen;
}
public void setcamera(String camera) {
this.camera = camera;
}
public String getcamera(){
return screen;
}
public void setmemory(String memory) {
this.memory = memory;
}
public String getmemory(){
return memory;
}
//show展示手机信息的方法
public void Show(){
System.out.println("这是一部"+memory+"G内存,"+camera+"万像素,"+screen+"英寸的"+logo+"牌手机");
}
//构建sendMessage方法,输入信息,输入发送失败的提示
public static void sendMessage() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要发送的信息");
String message = sc.nextLine();
System.out.println("发送成功!");
}
//构建callPhone方法,输入电话号,输入无法拨打的提示
public static void callPhone(){
Scanner sc = new Scanner(System.in);
System.out.println("请输入要拨打的电话");
String call = sc.nextLine();
System.out.println("对不起您的手机已欠费,无法拨打");
}
}
运行结果展示